/* Matrix Quick Power Template Fibonacci */#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int MAXN = 2;const int mod = 10000;//matrix struct matrix{int A[MAXN][MAXN]; void init () {//initialized to the unit matrix memset (A, 0, sizeof (a)); for (int i=0;i<maxn;++i) {A[i][i] = 1; }}};//matrix multiplication in matrix Mul (matrix A, matrix B) {matrix ans; for (int i=0;i<maxn;++i) {for (int j=0;j<maxn;++j) {Ans.a[i][j] = 0; for (int k=0;k<maxn;++k) {Ans.a[i][j] + = a.a[i][k] * B.a[k][j]; ANS.A[I][J]%= mod; }}} return ans;} Matrix fast-Powers matrix Qpow (matrix A, int n) {matrix ans; Ans.init (); while (n) {if (n&1) ans = mul (ans, a); A = Mul (A, a); n/= 2; } return ans; void output (Matrix a) {for (int. i=0;i<maxn;++i) {for (int j=0;j<maxn;++j) {cout << a.a[i][j ] << ""; } cout << Endl; }}inT Main () {Matrix A; A.a[0][0] = 1; A.A[0][1] = 1; A.a[1][0] = 1; A.A[1][1] = 0; Matrix ans = qpow (A, 10); cout << "F" = [f (2), F (1)] = [1, 1] * a^10 = "<< ans.a[0][0] + ans.a[1][0]; return 0;}
Recursive SequenceTime
limit:1000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i6 4u HDU 5950description Farmer John likes to the play mathematics games with his N cows. Recently, they is attracted by recursive sequences. In each turn, the cows would stand with a line, while John writes, the 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 i 4 ">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 righ T.
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 < 2 Mrow class="mjx-texatom-ord"> "> 2< Span id= "mjxc-node-12" class= "mjx-mn" >31 As described above.
Output
For each test case, output the number of the n-th cow. This number might is very large, so you need to output it modulo 2147493647.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const Long LongMoD =2147493647;Const intMAXN =8;structprog{Long LongA[MAXN][MAXN];}; Prog S,b;prog matrixmul (Prog A,prog B)//matrix multiplication{prog C; for(intI=1; i<maxn; ++i) for(intj=1; j<maxn; ++j) {C.a[i][j]=0; for(intk=1; k<maxn; k++) C.a[i][j]+ = (A.a[i][k]*b.a[k][j])%MoD; C.A[I][J]%=MoD; } returnC;} Prog Mul (Prog s,intk) {prog ans; for(intI=1; i<maxn; ++i) for(intj=1; j<maxn; ++j) Ans.a[i][j]= (i==j)?1:0; while(k) {if(k&1) ans=Matrixmul (ans,s); K>>=1; S=Matrixmul (s,s); } returnans;}intMain () {intn,t,a,b; for(SCANF ("%d", &t); t--;) {scanf (" %d%d%d",&n,&a,&b); if(n==1) {printf ("%lld\n", a%MoD); Continue; } if(n==2) {printf ("%lld\n", b%MoD); Continue; } if(n==3) {printf ("%lld\n",(Bayi+2*A%MOD+B%MOD)%MoD); Continue; } N-=2; for(intI=1; i<=7; ++i) for(intj=1; j<=7; ++j) S.a[i][j]=0, B.a[i][j]=0; for(intI=1; i<=5; i++) s.a[i][1]=1; for(intI=2; i<=5; i++) s.a[i][2]=i-1; s.a[3][3]=1; s.a[4][3]=3; s.a[5][3]=6; s.a[4][4]=1; s.a[5][4]=4; s.a[5][5]=1; s.a[6][5]=1; s.a[6][6]=1; s.a[7][6]=1; s.a[6][7]=2; b.a[1][1]=1; b.a[2][1]=3; b.a[3][1]=9; b.a[4][1]= -; b.a[5][1]=Bayi; b.a[6][1]=b; b.a[7][1]=A; S=Mul (s,n); S=Matrixmul (S,B); printf ("%lld\n", s.a[6][1]%MoD); } return 0;}
View Code
Sample Input
23 1 24) 1 10
Sample Output
85369
Hint
The third number is 85 = 2*1 33 ^ 4. In the second case, the third number is 93 = 2*1 Ten 1*10 ten 3^4 and the fourth number is 369 = 2 * 1034 ^ 4.
Matrix Quick Power Template