链接:https://www.nowcoder.com/acm/contest/105/G
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
这是一个加强版的斐波那契数列。
给定递推式
求F(n)的值,由于这个值可能太大,请对10
9+7取模。
输入描述:
第一行是一个整数T(1 ≤ T ≤ 1000),表示样例的个数。18
以后每个样例一行,是一个整数n(1 ≤ n ≤ 10
)。
输出描述:
每个样例输出一行,一个整数,表示F(n) mod 1000000007。
示例1
输入
4 1 2 3 100
输出
1 16 57 558616258
这题我必须要说句MMP才能开始讲,题目你给出T范围是1 <= T <= 1000,结果实际数据T,TMD超过了int,excuse me????真TM的坑,教会我做人了,找了2小时bug,一遍一遍排查哦。。。
知识储备:快速幂: https://www.cnblogs.com/linyujun/p/5194184.html
矩阵相乘运算:https://blog.csdn.net/wust_zzwh/article/details/52058209
不会不要紧,几句话就解释了,快速幂就是求 a ^ b次方(假装你不知道逆元),矩阵相乘:线性代数内容,c[i][j]为A的第i行与B的第j列对应乘积的和,即:
-------------------------------------------------------------正式开始讲题目-------------------------------------------------------------------------------------------------
注:本题一开始我想的是 2018年东北农业大学春季校赛 的wyh的数列那题,找循环节:https://www.nowcoder.com/acm/contest/93/K
但是发现找不到,事后才知道有矩阵快速幂这一说(别想着暴力,10^18次方你装不下的,还有T卡你呢!!!)所以,那就开始矩阵快速幂了:
步骤1> 先了解斐波拉契数列的基本矩阵相乘和快速幂和混合运用
Fi[i] = 1*Fi[i-1]+F*fi[i-2] Fi[i-1] = 1*F[i-1] + 0*f[i-2];即
我们这里不妨把[1 1 当成一个数,也就是求res ^ (n- 1),那么就是直接快速幂了,怎么转化成数?结构体里面写好了
1, 0]
观察上列公式,这里求出来的res ^ (n - 1)就是直接是答案了,因为F[n]是res[0][0] * F(1),而F(1)=1.F[0] = 0,则不考虑res[0][1],代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedB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,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX: 18 7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ... 19 .XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. ..... 20 JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... . 21 .Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr ....... 22 1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:....... 23 D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ... 24 :Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. ....... 25 sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. .......... 26 OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;.............. 27 .BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,............. 28 1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... ............. 29 ..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... .. 30 ,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........ 31 .:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,. 32 .;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,. 33 ,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. .. 34 rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2 35 ;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr 36 r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77; 37 .:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc 38 .:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c 39 .:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw 40 ;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2 41 J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU 42 JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J 43 U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1 44 2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ 45 a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U 46 5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5 47 U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2 48 s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS 49 s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZpgggav, ;UpEZaaaKHHUHPPaUJHGO 50 a;.775: .::rU. gR:,:. :;:BBBBQar . ,, ,.,7rggJwU6DDGMgOGgXc:, ,rPGOpKSXSXSKUKHU1UHgOEP 51 1i :SZJrLXpBBMRB: .v Ba,. :sv: . irsBUSpEEGPPpg65r:..,;sKZDZHSX5XaHU55K2wUGMRZ6Z6 52 :K :7asvwc2MgEQB, :gg.:iB7 .,,,:. ...,. . .. .:iM6GEpSSXZOEs:...;spZpKKXXSX5Xa5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r;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws 65 OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;:: 66 ::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r 67 :r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs 68 .:J: :; .,:K6BQS7:.,.,. 69 :r7Ji;r7;::;;. 70 . **/ 71 72 #include <bits/stdc++.h> 73 using namespace std; 74 75 #define FF(i, a, b) for(int i = a; i < b; i++) 76 #define RR(i, a, b) for(int i = a; i > b; i++) 77 #define ME(a, b) memset(a, b, sizeof(a)) 78 #define SC(x) scanf("%d", &x) 79 #define PR(x) printf("%d\n", x) 80 #define INF 0x3f3f3f3f 81 #define MAX 1001 82 #define MOD 1000000007 83 #define E 2.71828182845 84 #define PI 3.14159265358979 85 #define M 8 86 #define N 6 87 typedef long long LL; 88 typedef pair<int, int> Author; 89 vector<pair<string, int> > VP; 90 91 struct Matrix{ 92 LL a[2][2]; 93 Matrix(){ME(a, 0);} 94 Matrix operator * (const Matrix y){ 95 Matrix ans; 96 //¾ØÕóÏà³Ë 97 for(int i = 0; i <= 1; i++)for(int j = 0; j <= 1; j++)for(int k = 0; k <= 1; k++)ans.a[i][j] += a[i][k] * y.a[k][j]; 98 return ans; 99 } 100 void operator = (const Matrix b){ 101 for(int i = 0; i <= 1; i++)for(int j = 0; j <= 1; j++)a[i][j] = b.a[i][j]; 102 } 103 }; 104 int Solve(LL x){ 105 Matrix ans, trs; 106 ans.a[0][0] = ans.a[1][1] = 1; //µ¥Î»¾ØÕó 107 trs.a[0][0] = trs.a[1][0] = trs.a[0][1] = 1; //¹¹Ôì¾ØÕó 108 while(x){ 109 if(x & 1) ans = ans * trs; 110 trs = trs * trs; 111 x >>= 1; 112 } 113 return ans.a[0][0]; 114 } 115 116 int main(void){ 117 #ifdef LOCAL 118 freopen("in.txt", "r", stdin); 119 freopen("out.txt", "w", stdout); 120 #endif 121 ios::sync_with_stdio(false); cin.tie(0); 122 int n; 123 cin>>n; 124 125 cout<<Solve(n - 1)<<endl; //ÇóµÚF[n] 126 127 return EXIT_SUCCESS; 128 }
步骤2> 你了解了基本F[n] = F[n - 1] + F[n - 1]后,但是这题不一样呀,那怎么做?其实道理一样,只是重新构建res矩阵即可
上述代码中,ans = ans * trs,这里其实乘以单元矩阵就是其本身,就好像快速幂里面要把trs初始化为1一样,这里我们可以不需要乘单元矩阵了,因为trs需要改变成新的矩阵,而,ans也要改变,其余代码均不变!!!
我们继续来分析上上个图的公式n - 1可通过乘矩阵来求F[n],延伸一下
我要求本题的
F[n], F[n - 1], (i + 1) ^ 3, (i + 1) ^ 2, (i + 1), 1(即把i都增加1,和第一个公式一样) 所以就根据矩阵
f[i] = 1 1 1 1 1 1 f[i-1] 1
f[i-1] = 1 0 0 0 0 0 f[i-2] 0
(i+1)^3 = 0 0 1 3 3 1 i^3 8
(i+1)^2 = 0 0 0 1 2 1 * i^2 4
i + 1 = 0 0 0 0 1 1 i 2
1 = 0 0 0 0 0 1 1 1
想比这时你发现做矩阵快速幂的规律了,就是把所给的公式i + 1在右边列出,原本公式右边列出,再求矩阵即可,而这即为ans,那么trs呢?就是开头的[1, 0],再把每行都加起来(或者说是2 ^ n次)!
直接上代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 3 :;LaEaHKEEGpPXU7;, 4 .:75pKH11252U252XapZgRQgD6XJscLr;,. 5 :LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7. 6 .r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r . 7 ,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B 8 ,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc 9 rpQ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::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... . 21 .Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr ....... 22 1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:....... 23 D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ... 24 :Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. ....... 25 sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. .......... 26 OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;.............. 27 .BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,............. 28 1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... ............. 29 ..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... .. 30 ,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........ 31 .:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,. 32 .;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,. 33 ,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. .. 34 rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2 35 ;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr 36 r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77; 37 .:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc 38 .:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c 39 .:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw 40 ;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2 41 J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU 42 JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J 43 U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1 44 2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ 45 a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U 46 5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5 47 U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2 48 s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS 49 s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZpgggav, ;UpEZaaaKHHUHPPaUJHGO 50 a;.775: .::rU. gR:,:. :;:BBBBQar . ,, ,.,7rggJwU6DDGMgOGgXc:, ,rPGOpKSXSXSKUKHU1UHgOEP 51 1i :SZJrLXpBBMRB: .v Ba,. :sv: . irsBUSpEEGPPpg65r:..,;sKZDZHSX5XaHU55K2wUGMRZ6Z6 52 :K :7asvwc2MgEQB, :gg.:iB7 .,,,:. ...,. . .. .:iM6GEpSSXZOEs:...;spZpKKXXSX5Xa5Ja5wwKGQDEpp6OZ 53 w; ,LXLr77sLwL5aR. gRgQgaQBi ,vJwvi,. .;;:;7: . . ,... rRDRJUSXpgas:,.:;U66SaHa2X5aUa552X12SgQgpZpEpGOD 54 H: ;XJ;77rv7rJUaQ;rgaPgXXHBB; . . .ri:;; ,: . :6QZEKHXpXXL:,::r1PKXaa2XSS5a5S5UJ22aOBgEpOppZOOgG 55 iwsJsr7rsLrrLcJ2gQGXHwa1SUHBB: . ... . .., : 1BRKpOEGp2Jr;::rsXP1Uaa2U5a5aUa5SsSpgQRZGZEEZZOOgDg 56 ,cs5aXaP552ssLwRSHXaSS5asXBB. . . . LBBgGgZHsr;vrr;rcXpP5UUS5X5S2XS5SS1URBggpEZgDDGOGggRD 57 ,;irrs2KgQRPJJJSUXKXSHJpBM . . . ZBBOUsr;:. ,,,rHgZPUaSaUSUSSXaUPXsSBB6ZZp6EEGOOGDEggO 58 ;BBQQZaSJ5J15SJDQ7 .iEBL:. . .,:,:;SRQGZXPXXSHaS2UUSUSswQB6OZgG6pZZGODEOGDE 59 JgUri1aGEpEpXSS5LBQ, .rLap2Jr ;712OO6ZggRDZXHXpKK2211LLLc7wgBa2PODRGE6OZgDgGDGD 60 7QB; ,Jc76DaZXOZgDPEBBX, :c1HEZ1c;:,.,:;cHDgQQggMZgGOZO6OEGpXsLLsJXHPOBBBc;vss5EMggZDODOgED6 61 LZJ::iwrrr72EPgXU5OBBBBBBBp7: .i5P6wL;, .:LS6DMgMgZKgEOPOGGDDGEXULvLSOBBBBBQBgDQDJ5rrr7JOQQDDZGEgDD 62 wp rR5, .HQRX7r72RQBBBBBQBMEK6Uc7sc7cHOU:, .:LHgOQg6ZOPZpZGGEDERZPwcr7LKDDaJr:. rBX;;:r:ir1DBQMgRgDp 63 7D,sw: , Z. .LgBBQBBOsJaQBBBBBBXrr. .:rwZBBQgROgDgDOERRMgROOScrLsPP5r, 7Qs,::::::rUgRgZOG6 64 ,Q57:r ,: ,r U: .;; .vL7L;r:UG7.:sr;JSgQBgDEZXXUSXpHa21svr7r7s2v:. iQK...:,:::;LLJJws 65 OBHs;. :;;,v H: :6X; rpL;7pSrrr7r;::,....,,:;iirrvvvr, :BBPs;;:,,,,::;:: 66 ::rPDEL:..7:c;7r pK .: :KL.:r:.. ..:icsS5sr:,. JgGgBQDOSJss77r 67 :r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs 68 .:J: :; .,:K6BQS7:.,.,. 69 :r7Ji;r7;::;;. 70 . **/ 71 72 #include <bits/stdc++.h> 73 using namespace std; 74 75 #define FF(i, a, b) for(int i = a; i < b; i++) 76 #define RR(i, a, b) for(int i = a; i > b; i++) 77 #define ME(a, b) memset(a, b, sizeof(a)) 78 #define SC(x) scanf("%d", &x) 79 #define PR(x) printf("%d\n", x) 80 #define INF 0x3f3f3f3f 81 #define MOD 1000000007 82 #define MAX 1001 83 #define E 2.71828182845 84 #define PI 3.14159265358979 85 #define M 8 86 #define N 6 87 typedef long long LL; 88 typedef pair<int, int> Author; 89 vector<pair<string, int> > VP; 90 91 struct Matrix{ 92 LL a[N][N]; 93 Matrix(){ME(a, 0);} 94 Matrix operator * (const Matrix& y){ 95 Matrix ans; 96 //矩阵相乘 97 for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)for(int k = 0; k < N; k++)ans.a[i][j] = (ans.a[i][j] + a[i][k] * y.a[k][j]) % MOD; 98 return ans; 99 } 100 void operator = (const Matrix b){for(int i = 0; i < N; i++)for(int j = 0; j < N; j++)a[i][j] = b.a[i][j];} 101 }; 102 103 LL Solve(Matrix ans, LL x){ 104 Matrix trs; 105 //只是为了初始化,结构体内数组无法直接初始化 106 for(int i = 0; i < N; i++) trs.a[i][i] = 1; //构造矩阵 107 while(x){ 108 if(x & 1) trs = trs * ans; 109 x >>= 1; 110 ans = ans * ans; 111 } 112 return (trs.a[0][0] + trs.a[0][2] * 8 + trs.a[0][3] * 4 + trs.a[0][4] * 2 + trs.a[0][5]) % MOD; 113 } 114 115 int main(void){ 116 #ifdef HYQ 117 freopen("in.txt", "r", stdin); 118 freopen("out.txt", "w", stdout); 119 #endif 120 ios::sync_with_stdio(false); cin.tie(0); 121 LL T, LL, n, ans; Matrix base; 122 int mm[6][6] = {{1,1,1,1,1,1}, 123 {1,0,0,0,0,0}, 124 {0,0,1,3,3,1}, 125 {0,0,0,1,2,1}, 126 {0,0,0,0,1,1}, 127 {0,0,0,0,0,1}}; 128 cin>>T; 129 for(int i = 0;i < N; i++)for(int j = 0;j < N; j++) base.a[i][j] = mm[i][j]; 130 131 while(T--){ 132 cin>>n; 133 cout<<Solve(base, n - 1)<<endl; //求第F[n] 134 } 135 return EXIT_SUCCESS; 136 }
总结:从入门到崩溃,回想起来才发现没什么,之前一直没发现矩阵相乘可以运用到编程中,这次终于运用到了,加油吧,Acmer们.