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
The input data contain the only even integer n (2 ≤ n ≤ 106).spa
Output the only number — the amount of Peter's routes modulo 1000000009.rest
2
10
4
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,不然爲0。
-
若是跑到了這裏,設下凸的個數爲$n$,它的方案數爲$g_{n}$。(注意,如下討論的方案數是指在保證路徑合法的狀況下)那麼考慮點A到點C的方案數,顯然爲2。
$A\rightarrow B,A\rightarrow C\rightarrow B, A\rightarrow C \rightarrow D \rightarrow B$
而後考慮點C到點B的方案數和點D到點B的方案數,顯然它們是1,至於點C到點D呢,根據定義可得它是$g_{n - 1}$。
而後想一想,點A到點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 }