九度OJ 1081 清华09机试题之递推数列

//九度OJ 1081 清华09机试题之递推数列
//http://ac.jobdu.com/problem.php?pid=1081
//利用二分求幂法和蛮生疏的函数递归调用编写的矩阵的大数幂。
//相信N年后的我再回来看看这稚气未脱的刚入道时自己写的代码,会心一笑吧哈哈
#include <stdio.h>
#define N 10000
typedef struct M{
	long long h[2][2];
}M;
M c,e;//e在main函数里面被设置成了单位矩阵。
long long a,b,s;
M copy(M a,M b)
{
	a=b;
	return a;
}
M mul(M a,M b)
{
	M tempm;
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++)
		{
			tempm.h[i][j]=0;
			for(int k=0;k<2;k++)tempm.h[i][j]+=(a.h[i][k])*(b.h[k][j]);
			tempm.h[i][j]%=N;
		}
	return tempm;
}
M fang(int k,M linshi)
{
	if(k==0)return e;
	if(k==1)return c;
	linshi=fang(k>>1,linshi);
	linshi=mul(linshi,linshi);
	if(k&1)linshi=mul(linshi,c);
	return linshi;
}
int main()
{
	int k;
	c.h[1][0]=1;c.h[1][1]=0;
	e.h[0][0]=e.h[1][1]=1;
	e.h[1][0]=e.h[0][1]=0;
	while(~scanf("%lld %lld %d %d %d",&a,&b,&c.h[0][0],&c.h[0][1],&k))
	{
		M res=c;
		if(k==0){printf("%lld\n",a%N);continue;}
		if(k==1){printf("%lld\n",b%N);continue;}
		res=fang(k-1,res);
		printf("%lld\n",(res.h[0][0]*b+res.h[0][1]*a)%N);
	}
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值