Problem Description
Bob's school has a big playground, boys and girls always play games here after school.
To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.
Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.
He has infinite carpets with sizes of 1×2 and 2×1, and the size of the playground is 4×n.
Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?
Input
There are no more than 5000 test cases.
Each test case only contains one positive integer n in a line.
1≤n≤1018
Output
For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
Bob's school has a big playground, boys and girls always play games here after school.
To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.
Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.
He has infinite carpets with sizes of 1×2 and 2×1, and the size of the playground is 4×n.
Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?
Input
There are no more than 5000 test cases.
Each test case only contains one positive integer n in a line.
1≤n≤1018
Output
For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
5
没有想到有一样的题:poj 3420。
大神的解说:http://blog.csdn.net/a664607530/article/details/77619554
然后就是自己有点难想通的地方是:
a[n] = a[n - 1] + b[n - 1] + c[n - 1] + d[n - 1]
b[n] = a[n - 1]//这里不是 b[n] = a[n-1]+b[n-1]的原因就是 b[n]表示的是放完n行后n+1行的状态,b[n-1]已经算放完n行了所以不能再放 ,从而不能推出b[n].所以b[n-1]的状态只能推到a[n]
c[n] = a[n - 1] + e[n - 1]
d[n] = a[n - 1] * 2 + d[n - 1]
e[n] = c[n - 1]
自己的代码:
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec>mat;
const ll Mod = 1000000007;
ll n;
mat mul(mat &A,mat &B)
{
mat C(A.size(),vec(B[0].size()));
for(int i=0,l=A.size(); i<l; i++)
{
for(int k=0,l1=B.size(); k<l1; k++)
{
for(int j=0,l2=B[0].size(); j<l2; j++)
{
C[i][j] = (C[i][j] + A[i][k]*B[k][j])%Mod;
}
}
}
return C;
}
mat matpow(mat A,ll n)
{
mat B(A.size(),vec(A.size()));
for(int i=0,l=A.size(); i<l; i++)
{
B[i][i] = 1;
}
while(n>0)
{
if(n&1) B=mul(B,A);
A = mul(A,A);
n>>=1;
}
return B;
}
void solve()
{
mat A(5,vec(5));
mat C(5,vec(1));
for(int i=0;i<5;i++)
for(int j=0;j<5;j++) A[i][j] = 0,C[i][0]=0;
A[0][0] = A[0][1] = A[0][2] = A[0][3] = 1;
A[1][0] = 1, A[2][0] = 1, A[2][4] = 1, A[3][0] = 2, A[3][3] = 1, A[4][2] = 1;
A = matpow(A,n), C[0][0] = 1;
C = mul(A,C);
printf("%I64d\n",C[0][0]%Mod);
}
int main()
{
while(~scanf("%I64d",&n))
{
solve();
}
return 0;
}