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 <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <iomanip> #include <cmath> #include <cassert> #include <ctime> #include <map> #include <set> using namespace std; #pragma comment(linker, "/stck:1024000000,1024000000") #define lowbit(x) (x&(-x)) #define max(x,y) (x>=y?x:y) #define min(x,y) (x<=y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.1415926535897932384626433832 #define ios() ios::sync_with_stdio(true) #define INF 0x3f3f3f3f #define mem(a) ((a,0,sizeof(a))) typedef long long ll; ll A,B,n; struct matrix { ll a[10][10]; }; matrix mutiply(matrix u,matrix v) { matrix res; memset(res.a,0,sizeof(res.a)); for(int i=0;i<9;i++) for(int j=0;j<9;j++) for(int k=0;k<9;k++) res.a[i][j]=(res.a[i][j]+u.a[i][k]*v.a[k][j])%MOD; return res; } matrix quick_pow(ll n) { matrix ans,res; memset(res.a,0,sizeof(res.a)); memset(ans.a,0,sizeof(ans.a)); for(int i=0;i<9;i++) res.a[i][i]=1; ans.a[5][0]=ans.a[7][0]=1; ans.a[3][1]=ans.a[5][1]=ans.a[7][1]=1; ans.a[4][2]=ans.a[6][2]=1; ans.a[0][3]=ans.a[6][3]=1; ans.a[1][4]=ans.a[8][4]=1; ans.a[1][5]=ans.a[2][5]=ans.a[7][5]=1; ans.a[0][6]=ans.a[4][6]=1; ans.a[3][7]=ans.a[5][7]=1; ans.a[1][8]=ans.a[2][8]=1; while(n) { if(n&1) res=mutiply(res,ans); n>>=1; ans=mutiply(ans,ans); } return res; } int main() { int t; scanf("%d",&t); while(t--){ ll n; scanf("%lld",&n); if(n==1) printf("3\n"); else{ ll pos=0; matrix ans=quick_pow(n-2); for(int i=0;i<9;i++) for(int j=0;j<9;j++) pos=(pos+ans.a[i][j])%MOD; printf("%lld\n",pos); } } return 0; }
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。