Triangles

看了一天卡特兰数...我要自闭了QAQ

(如果不写一篇明天就会全部忘记QuQ

(要不感觉对不起看了一天别人的题解qwqqqq

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 n n levels, where n n n is an even number.

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

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.

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

Input

The input data contain the only even integer n n n ( 2<=n<=106 2<=n<=10^{6} 2<=n<=106 ).

Output

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

n=2 时,一共有 10 种方案,其中 5 种是这样,另外的是顺时针顺序地走

5E-triangles

Solution:首先第 1 层是最特殊的,因为这里没有灰色的三角形,然后注意到灰色的部分把整个金字塔分成了左部和右部

5E-triangles-level1

路径现在可以分成三个部分

  • 在第一层移动,这一共 10 种(见样例)
  • 进入左部或右部后直接回到 H
  • 进入左部(右部)后经过 E 点进入右部(左部)

对于第二种情况,在第一层的部分走法可以是(只有逆时针部分,实际上有 8 种)

对于第三种情况,在第一层(原来图中的两层现在算一层,也就是下面的 n 是题目中 n 的 12 )的部分走法有 2 种,左部到右部和右部到左部

现在要统计的就是在左部和右部内有多少种方案,因为左部右部对称,所以答案是一样的,我们来考虑左部

Sn 表示在有 n 层的金字塔中,走左部的方案数,因为走进左部后每一层都会遇到凹进去的部分,这部分只要枚举一下走了多远可以统计出第 n 层一共有

这么多的情况 

因此

答案是

(没错我借鉴了老师的ppt

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm> 
const long long mod = 1000000009;
using namespace std;
int main()
{
    int n;
    scanf("%d", &n);
    long long pw = 2, g = 4, s = 0;
    while(n -= 2)
    {
        pw = (pw << 1) % mod;
        g = g * (pw - 3) % mod;
        s = (s + g) % mod;
    }
    int ans = (2 * s * s + 8 * s + 10) % mod;
    if(ans < 0) ans += mod;
    printf("%d\n", ans);
    return 0;
}

好难啊。。。。

舍管阿姨真烦....连作业都不让写.....什么时候才能回去啊....

猜你喜欢

转载自www.cnblogs.com/Grigory/p/10360121.html