//参考文献http://blog.sina.com.cn/s/blog_83ccc39d0101jv6d.html
//代码参考http://blog.csdn.net/acm_davidcn/article/details/5819193
//矩阵快速幂
#include<iostream>
#define MOD 10007
using namespace std;
int f[4][4], res[4][4];
void A(int a[4][4], int b[4][4])
{
int t[4][4];
memset(t, 0, sizeof(t));
int i, j, k;
for( i=1; i < 4; i++ )
for( j=1; j < 4; j++ )
for( k=1; k < 4; k++ )
t[i][j] += a[i][k] * b[k][j] % MOD;
for( i=1; i < 4; i++ )
for( j=1; j < 4; j++ )
a[i][j] = t[i][j];
}
int main()
{
int n;
int a[5] = {0, 0, 0, 2, 6};
while(scanf("%d", &n) != EOF )
{
if(n <= 4) { printf("%d\n", a[n]); continue;}
memset(f, 0, sizeof(f));
memset(res, 0, sizeof(res));
f[1][1] = f[1][2] = f[1][3] = f[2][1] = 1;
f[3][3] = 2;
res[1][1] = res[2][2] = res[3][3] = 1;
n -= 4;
while(n > 0)
{
if(n % 2 == 0)
n /=2, A(f, f);
else
n--, A(res, f);
}
printf("%d\n", (res[1][1]*6 + res[1][2]*2 + res[1][3]*8) % MOD);
}
return 0;
}
hdu 3519 Lucky Coins Sequence
最新推荐文章于 2017-07-11 20:16:54 发布