Question: Chinese is not spoken.
Solution: Rapid power summation of matrices. We can know that we can use a matrix to quickly calculate its F [N]. Obviously, every extra f [N] will be added to our sum, however, because we are a fast power and cannot find all f [N], we can only add a one-dimensional matrix to maintain and.
Intermediate Matrix
3 2 7 0
1 0 0 0
0 1 0 0
1 0 0 1 (and)
Initial Matrix
5
3
1
4 (sum of the first two items)
Solution code:
1 // file name: temp. CPP 2 // Author: darkdream 3 // created time: wednesday, September 17, 2014, 45 seconds, 4 5 # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # include <cmath> 21 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 # define ll long 25 # define M 2009 26 using namespace STD; 27 int N; 28 struct matrix 29 {30 int mat [4] [4]; 31 void clear () 32 {33 memset (MAT, 0, sizeof (MAT )); 34} 35 void output () 36 {37 for (INT I = 0; I <n; I ++) 38 {39 for (Int J = 0; j <N; j ++) 40 printf ("% d", mat [I] [J]); 41 printf ("\ n"); 42} 43} 44 void Init () 45 {46 clear (); 47 N = 4; 48 mat [0] [0] = 3; 49 mat [0] [1] = 2; 50 mat [0] [2] = 7; 51 mat [1] [0] = 1; 52 mat [2] [1] = 1; 53 mat [3] [0] = 1; 54 mat [3] [3] = 1; 55} 56 Matrix Operator * (const Matrix & B) const 57 {58 matrix ret; 59 ret. clear (); 60 for (INT I = 0; I <n; I ++) 61 for (Int J = 0; j <n; j ++) 62 {63 for (int K = 0; k <n; k ++) 64 {65 ret. mat [I] [J] = (Ret. mat [I] [J] + mat [I] [k] * B. mat [k] [J]) % m; // column J, column 66} 67} 68 return ret; 69} 70}; 71 matrix POW (matrix, ll t) 72 {73 matrix ret; 74 ret. clear (); 75 for (INT I = 0; I <n; I ++) 76 ret. mat [I] [I] = 1; 77 matrix TMP = A; 78 while (t) 79 {80 If (T & 1) ret = RET * TMP; 81 TMP = TMP * TMP; 82 t> = 1; 83} 84 return ret; 85} 86 int main () {87 int L; 88 int CA = 0; 89 int t; 90 scanf ("% d", & T); 91 while (t --) 92 {93 ca ++; 94 scanf ("% d ", & L); 95 int TT [] = {4, 1, 3, 5}; 96 If (L <= 2) 97 {98 If (L = 0) 99 printf ("case % d: 1 \ n", CA); 100 if (L = 1) 101 printf ("case % d: 4 \ n", CA ); 102 If (L = 2) 103 printf ("case % d: 9 \ n", CA); 104 continue; 105} 106 matrix A; 107. init (); 108 A = POW (A, L-1); 109 //. output (); 110 int ans = 0; 111 for (INT I = 0; I <n; I ++) 112 {113 ans = (ANS +. mat [3] [I] * TT [3-I]) % m; 114} 115 printf ("case % d: % d \ n", CA, ANS ); 116} 117 return 0; 118}
View code
Fzu 1683 commemorating the slingshot matrix power