K - 纪念SlingShot FZU - 1683-----------------------------------数论(矩阵快速幂)

在这里插入图片描述
在这里插入图片描述

解析:
Fn = [ f n f n − 2 f n − 3 S n ] \begin{bmatrix}fn & fn-2 & fn-3 & Sn\end{bmatrix} [fnfn2fn3Sn]
Fn+1 = [ f n + 1 f n f n − 1 S n + 1 ] \begin{bmatrix}fn+1 & fn & fn-1 & Sn+1\end{bmatrix} [fn+1fnfn1Sn+1]

因为Sn+1 =Sn +Fn

[ f n f n − 2 f n − 3 S n ] \begin{bmatrix}fn & fn-2 & fn-3 & Sn\end{bmatrix} [fnfn2fn3Sn] * [ 3 1 0 3 2 0 1 2 7 0 0 7 0 0 0 1 ] \begin{bmatrix}3 & 1& 0 & 3\\2 & 0 &1 & 2\\7 & 0 & 0 &7\\0 &0 & 0 & 1\end{bmatrix} 3270100001003271

= [ f n + 1 f n f n − 1 S n + 1 ] \begin{bmatrix}fn+1 & fn & fn-1 & Sn+1\end{bmatrix} [fn+1fnfn1Sn+1]

最后等价于
[ 3 2 7 9 ] \begin{bmatrix}3 &2& 7 & 9\end{bmatrix} [3279] * [ 3 1 0 3 2 0 1 2 7 0 0 7 0 0 0 1 ] \begin{bmatrix}3 & 1& 0 & 3\\2 & 0 &1 & 2\\7 & 0 & 0 &7\\0 &0 & 0 & 1\end{bmatrix} 3270100001003271 (n-2) = [ f n + 1 f n f n − 1 S n + 1 ] \begin{bmatrix}fn+1 & fn & fn-1 & Sn+1\end{bmatrix} [fn+1fnfn1Sn+1]

#include<iostream>
#include<cstring>
using namespace std;
const int mod=2009;
struct lxw
{
	int res[4][4];
}node,base;
lxw multi(lxw a,lxw b)
{
	lxw tmp;
	memset(tmp.res,0,sizeof(tmp.res));
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			for(int k=0;k<4;k++)
				tmp.res[i][j]+=(a.res[i][k]%mod*b.res[k][j]%mod)%mod;
		return tmp;
				
}
int main()
{
	int t;
	cin>>t;
	for(int i=0;i<t;i++)
	{
		int x;
		cin>>x;
		if(x==0)
		{
			printf("Case %d: 1\n",i+1);
			continue;
		} 	
		else if(x==1) 
		{
			printf("Case %d: 4\n",i+1);
			continue;
		}
		else if(x==2)
		{
			printf("Case %d: 9\n",i+1);
			continue;
		} 
		memset(node.res,0,sizeof(node.res));
		memset(base.res,0,sizeof(base.res));
		node.res[0][0]=5;node.res[0][1]=3;node.res[0][2]=1;node.res[0][3]=9;
		base.res[0][0]=3;base.res[0][1]=1;base.res[0][3]=3;
		base.res[1][2]=base.res[3][3]=1;
		base.res[1][0]=base.res[1][3]=2;
		base.res[2][0]=base.res[2][3]=7;
		int n=x-2;
		while(n)
		{
			if(n&1)
				node=multi(node,base);
			base=multi(base,base);
			n>>=1;
		}
		printf("Case %d: %d\n",i+1,(node.res[0][3]%mod));
	}
		return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值