矩阵优化?斐波那契数列?
查看原帖
矩阵优化?斐波那契数列?
999244
Aurie楼主2024/9/4 18:18

为什么我要写矩阵优化呢?因为我手边有一个现成代码。。。

#include<bits/stdc++.h>

using namespace std;
struct node{
	int a,b,c,d;
	node operator *(const node &other)const{
		return {a*other.a+b*other.c,a*other.b+b*other.d,c*other.a+d*other.c,c*other.b+d*other.d};
	} 
};
int n;
node tmp={1,1,1,0},res={1,0,0,1};
int main(){
	cin>>n;
	if(n==0){
		puts("0.00");
		return ~~( 0 ^ 0 )+~~0; 
	}
	if(n<=2){
		puts("1.00");
		return ~~( 0 ^ 0 )+~~0;
	}
	n-=2;
	while(n){
		if(n&1){
			res=res*tmp;
		}
		tmp = tmp*tmp;
		n>>=1;
	}
	printf("%d.00\n",res.a+res.c);
	return ~~( 0 ^ 0 )+~~0;//如果你不给NOIP测评机一个表情包 ,NOIP可是会给你爆零哦,别问我怎么知道的
} 
2024/9/4 18:18
加载中...