POJ - 3070 Fibonacci (矩阵快速幂)

推出构造矩阵是 {1,1,1,0}

剩下就好写了,矩阵相乘就行了

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long LL;
const int maxn=1e5+5;
const int maxm=5e5+5;
const int inf=0x3f3f3f3f;
const int mod =10000;
int n;

void matrix(int (&a)[2][2],int b[2][2])
{
	int res[2][2]={0};
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++)
			for(int k=0;k<2;k++)
				res[i][j]=(res[i][j]+a[i][k]*b[k][j])%mod;
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++)
			a[i][j]=res[i][j];
}
int main(int argc, char const *argv[])
{
	while(scanf("%d",&n)!=EOF&&n!=-1)
	{
		int base[2][2]={1,1,1,0},ans[2][2]={1,0,0,1};
		while(n)
	    {
		    if(n&1) matrix(ans,base);
		    matrix(base,base);
		    n>>=1;
	    }
		printf("%d\n", ans[0][1]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wzazzy/article/details/88210285