[poj3070]Fibonacci

先附题

点击打开链接

算法:

        矩阵乘法快速幂

AC程序:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int tmp=10000;
class WZY{
	public:
	inline int read(){
		char c=getchar();int num=0;int f=1;
		while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
		while(c>='0'&&c<='9'){num=num*10+(c-'0');c=getchar();}
		return num*f;
	}
	inline void qwq(int x){
		if(x>9)qwq(x/10);
		putchar(x%10+'0');
	}
	inline void write(int x){
		if(x<0){x=-x;putchar('-');}
		qwq(x);putchar('\n');
	}
}wzy;
int nop[3][3];
int temp[3][3];
int fo[3][3];
void matrix_scz(int a[][3],int b[][3]){
	for(int i=1;i<=2;i++){
		for(int j=1;j<=2;j++){
			fo[i][j]=0;
			for(int k=1;k<=2;k++){
				fo[i][j]+=(a[i][k]*b[k][j])%tmp;
				fo[i][j]%=tmp;
			}
		}
	}
	for(int i=1;i<=2;i++){
		for(int j=1;j<=2;j++)b[i][j]=fo[i][j];
	}
	return;
}
int main(){
	int n=wzy.read();
	while(n!=-1){
		nop[1][1]=1;nop[1][2]=1;nop[2][1]=1;nop[2][2]=0;temp[1][1]=1;temp[2][1]=0;
		if(n==0){wzy.write(0);n=wzy.read();continue;}
		if(n<=2){wzy.write(1);n=wzy.read();continue;}
		while(n){
			if(n&1)matrix_scz(nop,temp);
			matrix_scz(nop,nop);
			n>>=1;
		}
		wzy.write(temp[2][1]%tmp);
		n=wzy.read();
	}
	return 0;
}
这个真不是笔者在夏令营学的。。。不过也可以算作被碾压记的第二神作(我好菜啊~~~~)

猜你喜欢

转载自blog.csdn.net/bill_benation/article/details/80918331