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 3 continuous hours when he eats only one kind of food, he will be unhappy. Besides, if there are 3 continuous hours when he eats all kinds of those, with chocolate at the middle hour, it will be dangerous. Moreover, if there are 3 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 NNN 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 1000000007.
Input
The fist line puts an integer T that shows the number of test cases. (T≤1000)
Each of the next T lines contains an integer N that shows the number of hours. (1≤N≤1010)
Output
For each test case, output a single line containing the answer.
样例输入
3
3
4
15
样例输出
20
46
435170
首先考虑2位 有1:mc 2:mf 3:cf 4:cm 5:fc 6:fm 7:cc 8:mm 9:ff;
所以第三位必须满足题意 则mc后面只能m和c 则生成的新的2位是 4:cm和7:cc
即1生成4,7,;2生成5,6,9;.........;9生成5,6;
所以可以写出以下打表代码
ll a[2][15]; int main(){ int op=0; for(int i=1;i<=9;i++) a[op][i]=1; for(int i=3;i<=30;i++){ op^=1; for(int j=1;j<=9;j++) a[op][j]=0; a[op][1]=(a[op^1][6]+a[op^1][8])%MOD; a[op][2]=(a[op^1][4]+a[op^1][6]+a[op^1][8])%MOD; a[op][3]=(a[op^1][5]+a[op^1][7])%MOD; a[op][4]=(a[op^1][1]+a[op^1][7])%MOD; a[op][5]=(a[op^1][2]+a[op^1][9])%MOD; a[op][6]=(a[op^1][2]+a[op^1][3]+a[op^1][9])%MOD; a[op][7]=(a[op^1][1]+a[op^1][5])%MOD; a[op][8]=(a[op^1][4]+a[op^1][6])%MOD; a[op][9]=(a[op^1][2]+a[op^1][3])%MOD; ll ans=0; for(int j=1;j<=9;j++){ printf("%lld ",a[op][j]); ans=(ans+a[op][j])%MOD; } printf("%lld\n",ans); } return 0; }
所以由以上公式构建9维矩阵,矩阵快速幂求解
#include#include #include #include #include #include #include #include #include #include #include #include #include