hdu 3519 Lucky Coins Sequence

//参考文献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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值