H. Coloring Game (南昌网络赛)

David has a white board with 2 \times N2×N grids.He decides to paint some grids black with his brush.He always starts at the top left corner and ends at the bottom right corner, where grids should be black ultimately.

Each time he can move his brush up(), down(), left(), right(), left up(), left down(), right up(), right down () to the next grid.

For a grid visited before,the color is still black. Otherwise it changes from white to black.

David wants you to compute the number of different color schemes for a given board. Two color schemes are considered different if and only if the color of at least one corresponding position is different.

Input

One line including an integer n(0<n \le 10^9)n(0<n≤109)

Output

One line including an integer, which represent the answer \bmod 1000000007mod1000000007

样例输入1

2

样例输出1

4

样例解释1

样例输入2

3

样例输出2

12

样例解释2

 题目链接:https://nanti.jisuanke.com/t/38227

代码:

做的时候想了各种做法,然后发现就是一道找规律的题。

用2*4举例,x1和yn肯定是要走的,是黑色,则y1可以走可以不走,yn也一样,则有2*2种选择。则除两边外的中间的2至n-1行中,每一列有三种情况,上黑下白,下白上黑,或者上黑下黑。所以2至n-1行有3^(n-2)种情况。一个2*n的共有2*2*3^(n-2)种情况。然后注意数据范围,要用快速幂计算。

#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL fun(LL x,LL n)
{
    LL res=1;
    while(n>0)
    {
        if(n & 1)
            res=(res*x)%1000000007;
        x=(x*x)%1000000007;
        n>>= 1;
    }
    return res;
}
int main()
{
    long long n;
    scanf("%lld",&n);
    if(n==1)
    {
        printf("1\n");
        return 0;
    }
    long long sum=fun(3,(n-2))*4;
    printf("%lld\n",sum%1000000007);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shinian_acmer/article/details/89429705