Title Link: http://hihocoder.com/problemset/problem/1143
This recursion is still classic, and the result is the Fibonacci sequence. f (i) = f (i-1) + f (i-2). The data range is too large and should be accelerated with a fast power.
1 /*2 ━━━━━┒ギリギリ♂eye! 3 ┓┏┓┏┓┃キリキリ♂mind! 4 ┛┗┛┗┛┃\0/5 ┓┏┓┏┓┃/6 ┛┗┛┗┛┃ノ)7 ┓┏┓┏┓┃8 ┛┗┛┗┛┃9 ┓┏┓┏┓┃Ten ┛┗┛┗┛┃ One ┓┏┓┏┓┃ A ┛┗┛┗┛┃ - ┓┏┓┏┓┃ - ┃┃┃┃┃┃ the ┻┻┻┻┻┻ - */ -#include <algorithm> -#include <iostream> +#include <iomanip> -#include <cstring> +#include <climits> A#include <complex> at#include <fstream> -#include <cassert> -#include <cstdio> -#include <bitset> -#include <vector> -#include <deque> in#include <queue> -#include <stack> to#include <ctime> +#include <Set> -#include <map> the#include <cmath> * using namespacestd; $ #defineFr FirstPanax Notoginseng #defineSC Second - #defineCL Clear the #defineBUG puts ("Here!!!") + #defineW (a) while (a--) A #definePB (a) push_back (a) the #defineRint (a) scanf ("%d", &a) + #defineRll (a) scanf ("%lld", &a) - #defineRs (a) scanf ("%s", a) $ #defineCIN (a) CIN >> a $ #defineFRead () freopen ("in", "R", stdin) - #defineFWrite () freopen ("Out", "w", stdout) - #defineRep (i, Len) for (int i = 0; i < (len); i++) the #defineFor (I, A, Len) for (int i = (a); I < (len); i++) - #defineCls (a) memset ((a), 0, sizeof (a))Wuyi #defineCLR (A, X) memset ((a), (x), sizeof (a)) the #defineFull (a) memset ((a), 0x7f7f7f, sizeof (a)) - #defineLRT RT << 1 Wu #defineRRT RT << 1 | 1 - #definePi 3.14159265359 About #defineRT return $ #defineLowbit (x) x & (-X) - #defineOnenum (x) __builtin_popcount (x) -typedefLong LongLL; -typedefLong DoubleLD; Atypedef unsignedLong LongULL; +typedef pair<int,int>PII; thetypedef pair<string,int>psi; -typedef PAIR<LL, Ll>PLL; $typedef map<string,int>MSI; thetypedef vector<int>VI; thetypedef vector<ll>VL; thetypedef vector<vl>VVL; thetypedef vector<BOOL>vb; - in Const intMoD =19999997; the Const intMAXN =5; the LL N; About thetypedefstructMatrix { the LL M[MAXN][MAXN]; the intR; + intC; - Matrix () { ther = c =0;BayiMemset (M,0,sizeof(m)); the } the } Matrix; - -Matrix Mul (Matrix M1, matrix m2,intMoD) { theMatrix ans =Matrix (); theANS.R =M1.R; theANS.C =m2.c; the for(inti =1; I <= M1.R; i++) { - for(intj =1; J <= M2.R; J + +) { the for(intK =1; K <= m2.c; k++) { the if(M2.m[j][k] = =0)Continue; theAns.m[i][k] = ((Ans.m[i][k] + m1.m[i][j] * m2.m[j][k]% MoD)%MoD;94 } the } the } the returnans;98 } About -Matrix Quickmul (Matrix M,intNintMoD) {101Matrix ans =Matrix ();102 for(inti =1; I <= M.R; i++) {103Ans.m[i][i] =1;104 } theANS.R =M.R;106ANS.C =m.c;107 while(n) {108 if(N &1) {109Ans =Mul (M, ans, MoD); the }111m =Mul (M, M, MoD); theN >>=1;113 } the returnans; the } the 117 intMain () {118 //FRead ();119 while(Cin >>N) { - Matrix p, q;121P.R = P.C =2;122p.m[1][1] =1; p.m[1][2] =1;123p.m[2][1] =1; p.m[2][2] =0;124Q.R =2; Q.C =1; the if(N <=2) {126cout << N <<Endl;127 Continue; - }129Q = Quickmul (p, n1, MoD); thecout << (q.m[1][1] + q.m[1][2])% mod <<Endl;131 } theRt0;133}
[HIHO1143] Domino Overlay problem • One (matrix fast power, recursion)