Fibonacci of Fibonacci
题目大意:求第n项斐波那契数的斐波那契,即FFn mod 20160519
先找Fn mod 20160519的循环节,啊这个可以用板子求,然后用矩阵快速幂求一下就好了
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 typedef long long ll; 5 const int p = 20160519; 6 const ll mode = 26880696;//用板子求的,有求斐波那契循环节的题目 7 using namespace std; 8 ll mod; 9 struct mat { 10 ll a[3][3]; 11 mat() { 12 memset(a, 0, sizeof(a)); 13 } 14 mat operator *(const mat &o) const { 15 mat t; 16 for(int i = 1; i <= 2; i++) { 17 for(int j = 1; j <= 2; j++) 18 for(int k = 1; k <= 2; k++) { 19 t.a[i][j] = (t.a[i][j] + a[i][k]*o.a[k][j])%mod; 20 } 21 } 22 return t; 23 } 24 } a, b; 25 ll slove(ll n) { 26 a.a[1][1] = a.a[2][2] = 1, a.a[1][2] = a.a[2][1] = 0; 27 b.a[1][1] = b.a[1][2] = b.a[2][1] = 1, b.a[2][2] = 0; 28 if(n == 0) return 0; 29 else { 30 n--; 31 while(n > 0) { 32 if(n&1) { 33 a = b*a; 34 n--; 35 } 36 n >>= 1; 37 b = b*b; 38 } 39 return a.a[1][1]; 40 } 41 } 42 43 int main() { 44 int t; 45 ll n; 46 scanf("%d", &t); 47 while(t--) { 48 scanf("%lld", &n); 49 mod = mode; 50 ll ans = slove(n); 51 mod = p; 52 ans = slove(ans); 53 printf("%lld ", ans); 54 } 55 return 0; 56 }