#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL __int64
const int mod=1e4+7;
const int maxn=4;
struct matrix{
int f[maxn][maxn];
};
matrix mul(matrix a,matrix b)
{
matrix c;
int i,j,k;
memset(c.f,0,sizeof(c.f));
for(k=0;k<maxn;k++)
{
for(i=0;i<maxn;i++)
{
if(!a.f[i][k])continue;
for(j=0;j<maxn;j++)
{
if(!b.f[k][j])continue;
c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j])%mod;
}
}
}
return c;
}
matrix pow_mod(matrix a,int b)
{
matrix s;
memset(s.f,0,sizeof(s.f));
for(int i=0;i<maxn;i++)
s.f[i][i]=1;
while(b)
{
if(b&1)
s=mul(s,a);
a=mul(a,a);
b=b>>1;
}
return s;
}
int pows(int a,int b)
{
int s=1;
while(b)
{
if(b&1)
s=(s*a)%mod;
a=(a*a)%mod;
b=b>>1;
}
return s;
}
int main()
{
int n;
while(cin>>n)
{
matrix e;
memset(e.f,0,sizeof(e.f));
e.f[0][1]=e.f[0][2]=e.f[1][0]=e.f[1][3]=e.f[2][1]=e.f[3][0]=1;
e=pow_mod(e,n-1);
int ans=0;
ans=(e.f[0][0]+e.f[1][0]+e.f[0][1]+e.f[1][1]+e.f[0][2]+e.f[1][2]+e.f[0][3]+e.f[1][3])%mod;
ans=(pows(2,n)-ans+mod)%mod;
cout<<ans<<endl;
}
return 0;
}
/*
令f[i][j]为长度为i的,末尾是j的不符合条件的个数。(j=1,0,11,00),动态规划后得到dp公式,n太大,需要矩阵优化
可得到矩阵:
|f[1][1] f[1][0] f[1][11] f[1][00]|*|0 1 1 0|^(n-1)=|f[n][1] f[n][0] f[n][11] f[n][00]|
|1 0 0 1|
|0 1 0 0|
|1 0 0 0|
ans=2^n-(f[n][1]+f[n][0]+f[n][11]+f[n][00]);
其中f[1][1]=f[1][0]=1;f[1][11]=f[1][00]=0;
*/
hdu 3519 Lucky Coins Sequence dp+矩阵连乘优化
最新推荐文章于 2020-03-19 15:45:30 发布