2018年湘潭大学程序设计竞赛 G- 又见斐波那契

时间:2023-03-10 08:05:18
2018年湘潭大学程序设计竞赛 G-	又见斐波那契

推一推矩阵直接快速幂。

 #include<bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
using namespace std; const int N=1e5+;
const int M=1e5+;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+; LL n;
struct Matrix {
LL a[][];
Matrix() {memset(a, , sizeof(a));}
void init() {
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
a[i][j] = (i == j);
} Matrix operator * (const Matrix &B) const {
Matrix C;
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
for(int k = ; k < ; k++) {
C.a[i][j] = (C.a[i][j] + a[i][k] * B.a[k][j]) % mod;
}
}
}
return C;
} Matrix operator ^ (const LL &p) const {
Matrix A = *this, res;
res.init();
LL t = p;
while(t) {
if(t & ) res = res * A;
A = A * A; t >>= ;
}
return res;
}
}A; int main() { int a[][] = {
{, , , , , },
{, , , , , },
{, , , , , },
{, , , , , },
{, , , , , },
{, , , , , }
}; for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
A.a[i][j] = a[i][j]; int T; scanf("%d", &T);
while(T--) {
scanf("%lld", &n);
if(n == ) {
puts("");
continue;
}
Matrix B = A ^ (n - );
LL ans = B.a[][] + B.a[][] * + B.a[][] * + B.a[][] * + B.a[][];
ans %= mod;
printf("%lld\n", ans);
}
return ;
}
/*
*/