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×21×2 and 2×12×1, and the size of the playground is 4×n4×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≤10181≤n≤1018
Output
For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
5
一看就知道是个规律题,数学上来先打表.但是打表也不太好打…还是太菜了.
暴力打表:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxx=1e3+100;
bool vis[maxx][maxx];
int n;
int ans;
void dfs(int sum)
{
if(sum==4*n)
{
ans++;
return ;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<4;j++)
{
if(vis[i][j]==0)
{
if(vis[i][j+1]==0&&j+1<4)
{
vis[i][j]=1;
vis[i][j+1]=1;
dfs(sum+2);
vis[i][j]=0;
vis[i][j+1]=0;
}
if(vis[i+1][j]==0&&i+1<n)
{
vis[i][j]=1;
vis[i+1][j]=1;
dfs(sum+2);
vis[i][j]=0;
vis[i+1][j]=0;
}
return ;
}
}
}
}
int main()
{
while(cin>>n)
{
memset(vis,0,sizeof(vis));
ans=0;
dfs(0);
cout<<ans<<endl;
}
return 0;
}
//f[n]=-f[n-4]+f[n-3]+5f[n-2]+f[n-1]
打表之后的规律
1e18的数据量,矩阵快速幂…
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define mod 1000000007
using namespace std;
const int maxx=5;
ll a[maxx][maxx];
ll ans[maxx][maxx];
ll n,m;
void init()
{
a[1][1]=1;a[1][2]=5;a[1][3]=1;a[1][4]=-1;
a[2][1]=1;a[2][2]=0;a[2][3]=0;a[2][4]=0;
a[3][1]=0;a[3][2]=1;a[3][3]=0;a[3][4]=0;
a[4][1]=0;a[4][2]=0;a[4][3]=1;a[4][4]=0;
}
void multy(ll b[5][5],ll c[5][5])
{
int tep[5][5];
memset(tep,0,sizeof(tep));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
tep[i][j]=(tep[i][j]+(b[i][k]*c[k][j])%mod)%mod;
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
b[i][j]=tep[i][j]%mod;
}
}
}
void qsm(ll y)
{
memset(ans,0,sizeof(ans));
for(int i=1;i<=4;i++) ans[i][i]=1;
while(y)
{
if(y&1)
multy(ans,a);
y/=2;
multy(a,a);
}
}
int main()
{
while(scanf("%lld",&m)!=EOF)
{
if(m==1) cout<<1<<endl;
else if(m==2) cout<<5<<endl;
else if(m==3) cout<<11<<endl;
else if(m==4) cout<<36<<endl;
else
{
init();
n=4;
m-=4;
qsm(m);
ll sum=0;
sum+=ans[1][1]*36%mod;
sum=(sum%mod+mod)%mod;
sum+=ans[1][2]*11%mod;
sum=(sum%mod+mod)%mod;
sum+=ans[1][3]*5%mod;
sum=(sum%mod+mod)%mod;
sum+=ans[1][4]*1%mod;
sum=(sum%mod+mod)%mod;
printf("%lld\n",sum%mod);
}
}
}
努力加油a啊,(o)/~