题干:
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
题目大意:
本身是2*N的白色格子,你从左上角开始走到右下角,每次可以往八个方向走,问你可以踩出多少种本质不同的图案来。
解题报告
不难发现如果要走到后面,那么中间的任何一列都要被踩到。所以对于每一列,可以踩上 下 上下 这三种选择,在加上首尾两列的四种选择,所以答案就是 4*3^(n-2)。
AC代码:
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
const ll mod = 1000000007;
ll n;
ll qpow(ll a,ll k) {
ll res = 1;
while(k) {
if(k&1) res =(res*a)%mod;
a=(a*a)%mod;k>>=1;
}
return res%mod;
}
int main()
{
cin>>n;
if(n == 1) printf("1\n");
else if(n == 2) printf("4\n");
else {
ll ans = qpow(3,n-2);
ans = ans*4;
printf("%lld\n",ans%mod);
}
return 0 ;
}