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
题目来源
题目链接:
解题思路:
当有3h我们可以很容易列举出一共20种可行方案(除去中毒的7种),当需要在3h基础上增加1h时,当前方案数 = 当前能到达结尾两种食物状态的数量 * (能到达下一个可行食物的方案总数) ,由于两种食物两两搭配只有9种情况,假设连续三种食物 M F C 那么就可以用M F 当做二维矩阵的行, F C当做列 ,这样就能简单地使用9*9矩阵表示所有初始状态,可行的方案可以初始为1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
struct matrix
{
ll a[10][10] = {{0}};
matrix operator * (const matrix &b) const
{
matrix ret;
for(int i = 1;i<=9 ; i++)
{
for(int j = 1; j<=9; j++)
{
ret.a[i][j] = 0;
for(int k = 1;k<=9;k++)
{
ret.a[i][j] += (a[i][k] * b.a[k][j])%mod;
ret.a[i][j]%=mod;
}
}
}
return ret;
}
};
matrix quick_pow(matrix t, ll n)
{
n -= 3;
matrix ret = t;
matrix temp = t;
while(n)
{
if(n&1) ret = ret * temp;
n>>= 1;
temp = temp * temp;
}
return ret;
}
int main(void)
{
int T;
cin>>T;
while(T--)
{
ll n;cin>>n;
if(n<3) { cout<<pow(3,n)<<endl; continue;}
matrix m;
m.a[1][3] = m.a[1][2] = 1;
m.a[2][4] = m.a[2][5] = m.a[2][6] = 1;
m.a[3][7] = m.a[3][9] = 1;
m.a[4][1] = m.a[4][2] = m.a[4][3] = 1;
m.a[5][4] = m.a[5][6] = 1;
m.a[6][9] = m.a[6][7] = 1;
m.a[7][1] = m.a[7][2] = 1;
m.a[8][4] = m.a[8][5] = 1;
m.a[9][7] = m.a[9][8] = 1;
m = quick_pow(m,n);
ll ans = 0;
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
ans += m.a[i][j] ;
ans %= mod;
}
}
cout<<ans<<endl;
}
}