矩阵快速幂 求斐波那契第N项

时间:2023-03-10 02:23:35
矩阵快速幂 求斐波那契第N项
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std; const int M = 1e9+; struct Matrix {
long long a[][];
Matrix() {
memset(a, , sizeof(a));
}
Matrix operator * (const Matrix y) {
Matrix ans;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
for(int k = ; k <= ; k++)
ans.a[i][j] += a[i][k]*y.a[k][j];
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
ans.a[i][j] %= M;
return ans;
}
void operator = (const Matrix b) {
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
a[i][j] = b.a[i][j];
}
}; int solve(long long x) {
Matrix ans, trs;
ans.a[][] = ans.a[][] = ;
trs.a[][] = trs.a[][] = trs.a[][] = ;
while(x) {
if(x&)
ans = ans*trs;
trs = trs*trs;
x >>= ;
}
return ans.a[][];
} int main() {
int n;
scanf("%d", &n);
cout << solve(n-) << endl;
return ;
}

POJ 3070