Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30 Accepted Submission(s): 20
Problem Description
Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4
. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.
Input
The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
Output
For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
Sample Input
2
3 1 2
4 1 10
Sample Output
85
369
Hint
In the first case, the third number is 85 = 2*1十2十3^4.
In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.Source
题意:f[i]=2*f[i-2]+f[i-1]+i^4
题解:[f[i],f[i-1],i^4,i^3,i^2,i,1]
1 1 0 0 0 0 0
2 0 0 0 0 0 0
1 0 1 0 0 0 0
4 0 4 1 0 0 0
6 0 6 3 1 0 0
4 0 4 3 2 1 0
1 0 1 1 1 1 1
构造矩阵 矩阵快速幂
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 #include<bits/stdc++.h> 8 #include<map> 9 #include<set> 10 #include<cmath> 11 #include<queue> 12 #include<bitset> 13 #include<math.h> 14 #include<vector> 15 #include<string> 16 #include<stdio.h> 17 #include<cstring> 18 #include<iostream> 19 #include<algorithm> 20 #pragma comment(linker, "/STACK:102400000,102400000") 21 using namespace std; 22 #define A first 23 #define B second 24 const int mod=1000000007; 25 const int MOD1=1000000007; 26 const int MOD2=1000000009; 27 const double EPS=0.00000001; 28 typedef __int64 ll; 29 const ll MOD=1000000007; 30 const int INF=1000000010; 31 const ll MAX=1ll<<55; 32 const double eps=1e-8; 33 const double inf=~0u>>1; 34 const double pi=acos(-1.0); 35 typedef double db; 36 typedef unsigned int uint; 37 typedef unsigned long long ull; 38 const ll k=2147493647; 39 struct matrix 40 { 41 ll m[10][10]; 42 } ans,exm; 43 struct matrix matrix_mulit1(struct matrix aa,struct matrix bb) 44 { 45 struct matrix there; 46 for(int i=0;i<7;i++) 47 { 48 for(int j=0;j<7;j++) 49 { 50 there.m[i][j]=0; 51 for(int u=0;u<7;u++) 52 there.m[i][j]=(there.m[i][j]+aa.m[i][u]*bb.m[u][j]%k)%k; 53 } 54 } 55 return there; 56 } 57 struct matrix matrix_mulit2(struct matrix aa,struct matrix bb) 58 { 59 struct matrix there; 60 for(int j=0;j<7;j++) 61 { 62 there.m[0][j]=0; 63 for(int u=0;u<7;u++) 64 there.m[0][j]=(there.m[0][j]+aa.m[0][u]*bb.m[u][j]%k)%k; 65 } 66 return there; 67 } 68 ll matrix_quick(ll aa,ll bb,ll gg) 69 { 70 exm.m[0][0]=1;exm.m[0][1]=1;exm.m[0][2]=0;exm.m[0][3]=0;exm.m[0][4]=0;exm.m[0][5]=0;exm.m[0][6]=0; 71 exm.m[1][0]=2;exm.m[1][1]=0;exm.m[1][2]=0;exm.m[1][3]=0;exm.m[1][4]=0;exm.m[1][5]=0;exm.m[1][6]=0; 72 exm.m[2][0]=1;exm.m[2][1]=0;exm.m[2][2]=1;exm.m[2][3]=0;exm.m[2][4]=0;exm.m[2][5]=0;exm.m[2][6]=0; 73 exm.m[3][0]=4;exm.m[3][1]=0;exm.m[3][2]=4;exm.m[3][3]=1;exm.m[3][4]=0;exm.m[3][5]=0;exm.m[3][6]=0; 74 exm.m[4][0]=6;exm.m[4][1]=0;exm.m[4][2]=6;exm.m[4][3]=3;exm.m[4][4]=1;exm.m[4][5]=0;exm.m[4][6]=0; 75 exm.m[5][0]=4;exm.m[5][1]=0;exm.m[5][2]=4;exm.m[5][3]=3;exm.m[5][4]=2;exm.m[5][5]=1;exm.m[5][6]=0; 76 exm.m[6][0]=1;exm.m[6][1]=0;exm.m[6][2]=1;exm.m[6][3]=1;exm.m[6][4]=1;exm.m[6][5]=1;exm.m[6][6]=1; 77 ans.m[0][0]=bb;ans.m[0][1]=aa;ans.m[0][2]=16;ans.m[0][3]=8;ans.m[0][4]=4;ans.m[0][5]=2;ans.m[0][6]=1; 78 gg-=2; 79 while(gg) 80 { 81 if(gg&1) 82 ans=matrix_mulit2(ans,exm); 83 exm=matrix_mulit1(exm,exm); 84 gg >>= 1; 85 } 86 return ans.m[0][0]; 87 } 88 int t; 89 ll n,a,b; 90 int main() 91 { 92 scanf("%d",&t); 93 for(int i=1;i<=t;i++) 94 { 95 scanf("%I64d %I64d %I64d",&n,&a,&b); 96 if(n==1) 97 printf("%I64d ",a); 98 else if(n==2) 99 printf("%I64d ",b); 100 else 101 printf("%I64d ",(matrix_quick(a, b, n)+k)%k); 102 } 103 return 0; 104 }