Codeforces 15E Triangles - 组合数学

Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number.c++

In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better).ide

After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics:动画

  • it starts and ends in the same place — the granny's house;
  • the route goes along the forest paths only (these are the segments marked black in the picture);
  • the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway);
  • the route cannot cross itself;
  • there shouldn't be any part of dense forest within the part marked out by this route;

You should find the amount of such suitable oriented routes modulo 1000000009.ui

The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example.this

Input

The input data contain the only even integer n (2 ≤ n ≤ 106).spa

Output

Output the only number — the amount of Peter's routes modulo 1000000009.rest

Examples
input
2
output
10
input
4
output
74

题目大意code

  首先,请仔细看图,找出图中的规律。blog

  给定这个图的大小$n$,问有多少条有向路径知足:ci

  • 路径长度为正
  • 路径是沿着图中的边走的
  • 路径不能自交
  • 路径从H点开始,在H点结束
  • 路径不能将黑色的格子圈住

  答案模$10^{9}+9$。不是1e9 + 7。表示由于模数打错一发wa

   经过手动画图,能够发现,路径主要有2种 


  • 这样的有向路径总共只有2条
  • 这样能够说是先在一侧乱窜,而后到点P,而后再到另外一侧,最后在边上回到点H。

  因为图是对称的,统计的是有向路径,因此只须要经过计算在一侧,点H到点P的合法的无向路径的数量而后再进行简单的运算就可以算出答案。

  经过观察,能够发现,一旦从$l_{1}$上离开,就在到P点前不可能再回到$l_{1}$上。同时它也不可能继续向下走。

  因此考虑从$l_{1}$上的某一点,向右或者向右下走一步而后到达点P 的方案数。

  离开$l_{1}$后,就会沿大体向上的方向走,那么考虑在某个位置走到它的右上的方案数。

  这个稍微分类讨论一下

  1. 若是在这种地方,若是能够向右上走1步,那么方案数为1,不然为0。

  2. 若是跑到了这里,设下凸的个数为$n$,它的方案数为$g_{n}$。(注意,如下讨论的方案数是指在保证路径合法的状况下)那么考虑点A到点C的方案数,显然为2。
    而后考虑点C到点B的方案数和点D到点B的方案数,显然它们是1,至于点C到点D呢,根据定义可得它是$g_{n - 1}$。
    而后想一想,点A到点B的路径有哪几种?

    $A\rightarrow B,A\rightarrow C\rightarrow B, A\rightarrow C \rightarrow D \rightarrow B$

    因而不难根据加法原理获得

    $g_{n} = 2g_{n - 1} + 3$

  然而有什么用呢?考虑刚离开$l_{1}$后到达的第一个点,那么它到点P的方案数就能够经过乘法原理计算。

  紧接着就能够计算出在离开$l_{1}$上每一个点后到达点P的方案数。而后把它们加起来就可以获得总的方案数,

  假如成功算出了这样的总方案数为$s$,那么如何根据它来求答案呢?

  容易根据加法和乘法原理获得:

$ans = 2s^{2} + 2$

  说的很长,其实代码很短

Code

 1 /**  2  * Codeforces  3  * Problem#15E  4  * Accepted  5  * Time: 60ms  6  * Memory: 2024k  7  */
 8 #include <bits/stdc++.h>
 9 using namespace std; 10 
11 const int M = 1e9 + 9; 12 
13 int n; 14 
15 inline void init() { 16     scanf("%d", &n); 17 } 18 
19 int sum = 1; 20 inline void solve() { 21     for (int i = 3, last = -1, P = 1; i <= n; i++) { 22         if (i & 1) { 23             last = (2 * last + 3) % M; 24             P = (P * 1ll * last) % M; 25  } 26         sum = (sum + P) % M; 27  } 28     sum = (sum << 1) % M; 29     sum = (sum * 1ll * sum) % M; 30     sum = ((sum + 1) << 1) % M; 31     printf("%d", sum); 32 } 33 
34 int main() { 35  init(); 36  solve(); 37     return 0; 38 }
相关文章
相关标签/搜索