焦作网络赛L-Poor God Water矩阵快速幂
Posted wyboooo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了焦作网络赛L-Poor God Water矩阵快速幂相关的知识,希望对你有一定的参考价值。
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous.
Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 continuous hours when he eats only one kind of food, he will be unhappy. Besides, if there are 33 continuous hours when he eats all kinds of those, with chocolate at the middle hour, it will be dangerous. Moreover, if there are 33 continuous hours when he eats meat or fish at the middle hour, with chocolate at other two hours, it will also be dangerous.
Now, you are the doctor. Can you find out how many different kinds of diet that can make God Water happy and safe during NN hours? Two kinds of diet are considered the same if they share the same kind of food at the same hour. The answer may be very large, so you only need to give out the answer module 10000000071000000007.
Input
The fist line puts an integer TT that shows the number of test cases. (T le 1000T≤1000)
Each of the next TT lines contains an integer NN that shows the number of hours. (1 le N le 10^{10}1≤N≤1010)
Output
For each test case, output a single line containing the answer.
样例输入
3 3 4 15
样例输出
20 46 435170
题目来源
就是告诉你任意三个可以填的情况 推n个的时候有多少种情况
像这种前一种情况可以推后一种情况的 而且n还这么大的 可以考虑矩阵快速幂
建一个9*9的矩阵 每一种表示一个2位可填的状态 行表示前两个 列表示这前两个可推出的后两个
比如MCC是一种可行的 就在MC这行对应CC这列写1
快速幂可得所有情况
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<stack> 6 #include<queue> 7 #include<map> 8 #include<vector> 9 #include<cmath> 10 #include<cstring> 11 #include<set> 12 #include<stack> 13 //#include<bits/stdc++.h> 14 #define inf 0x7f7f7f7f7f7f7f7f 15 using namespace std; 16 typedef long long LL; 17 18 const LL mod = 1e9 + 7; 19 int t; 20 LL n; 21 22 struct Matrix { 23 LL a[10][10] = {{0}}; 24 Matrix operator * (const Matrix &b)const { 25 Matrix ret; 26 for (int i = 1; i <= 9; i++) { 27 for (int j = 1; j <= 9; j++) { 28 ret.a[i][j] = 0; 29 for (int k = 0; k <= 9; k++) { 30 ret.a[i][j] += a[i][k] * b.a[k][j]; 31 ret.a[i][j] %= mod; 32 } 33 } 34 } 35 return ret; 36 } 37 }; 38 39 Matrix ksm(Matrix a, LL x) 40 { 41 Matrix ret, k; 42 k = a; 43 ret = a; 44 x--; 45 while (x) { 46 if (x & 1) { 47 ret = ret * k; 48 } 49 x >>= 1; 50 k = k * k; 51 } 52 return ret; 53 } 54 55 int main() 56 { 57 cin >> t; 58 while (t--) { 59 cin >> n; 60 if (n == 1) { 61 cout << 3 << endl; 62 } 63 else if (n == 2) { 64 cout << 9 << endl; 65 } 66 else { 67 Matrix m; 68 m.a[1][4] = m.a[1][5] = 1; 69 m.a[2][6] = m.a[2][7] = 1; 70 m.a[3][8] = m.a[3][9] = 1; 71 m.a[4][2] = m.a[4][7] = 1; 72 m.a[5][3] = m.a[5][9] = 1; 73 m.a[6][1] = m.a[6][5] = 1; 74 m.a[7][3] = m.a[7][8] = m.a[7][9] = 1; 75 m.a[8][1] = m.a[8][5] = 1; 76 m.a[9][2] = m.a[9][6] = m.a[9][7] = 1; 77 m = ksm(m, n - 2); 78 LL f[10] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; 79 LL ans[10] = { 0 }; 80 for (int i = 1; i <= 9; i++) { 81 for (int j = 1; j <= 9; j++) { 82 ans[i] += m.a[i][j] * f[j]; 83 ans[i] %= mod; 84 } 85 } 86 87 LL res = 0; 88 for (int i = 1; i <= 9; i++) { 89 res = (res + ans[i]) % mod; 90 } 91 cout << res << endl; 92 } 93 } 94 return 0; 95 }
以上是关于焦作网络赛L-Poor God Water矩阵快速幂的主要内容,如果未能解决你的问题,请参考以下文章
ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)
2018焦作网络赛 - Poor God Water 一道水题的教训
ACM-ICPC 2018 焦作赛区网络预赛 Poor God Water 矩阵快速幂
2018ICPC焦作- Poor God Water 求递推式+矩阵快速幂