1. 程式人生 > >L. Poor God Water(ACM-ICPC 2018 焦作賽區網路預賽 快速矩陣冪)

L. Poor God Water(ACM-ICPC 2018 焦作賽區網路預賽 快速矩陣冪)

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

題目來源

ACM-ICPC 2018 焦作賽區網路預賽

 

題意:有肉,魚,巧克力三種食物,有幾種禁忌,對於連續的三個食物,1.這三個食物不能都相同;2.若三種食物都有的情況,巧克力不能在中間;3.如果兩邊是巧克力,中間不能是肉或魚,求方案數

//以下借鑑大佬的思路( 都說這題水,我只想說“真香” )

我用的是矩陣快速冪,將meat ,  chocolate,fish 用 0 ,1 , 2 表示

對於 n 來說,我們只關注後兩位,因為 若 n - 1 的所有方案解決的話,我們在 n - 1 的方案新增0, 1,2 就行,然後根據禁忌 去除不可能的

 我們根據次狀態 來更新現狀態,然後矩陣快速冪

  

注意:特別要注重細節,由於取模的位置不同一直ac不了,ac不了原因下面程式碼我會標註下

 


#include "bits/stdc++.h"
#define rep(i,j,k) for(int i=j;i<=k;i++)
const int MOD = 1e5+7;
typedef long long ll;
using namespace std;
const int N =9;
int mod = 1e9+7;

struct mat
{
	ll a[N][N];
};

mat mul_mat(mat &a,mat &b)
{
	mat res;
	memset(res.a,0,sizeof(res.a)); 
	rep(i,0,8)
		rep(j,0,8)
			rep(k,0,8)
			{
				res.a[i][j]=(res.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
			}//就是這個位置了,之前用res.a[i][j]+a.a[i][k]*b.a[k][j]%mod;
                         //一直ac不了,還是要打括號保險
	return res;
}
mat pow_mat(ll n,mat &a)
{
	mat res;
	memset(res.a,0,sizeof(res.a));
	for(int i=0;i<=8;i++)
	res.a[i][i]=1;
	while(n)
	{
		if(n&1) res=mul_mat(res,a);
		a=mul_mat(a,a);
		n>>=1;
	}
	return res;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		if(n == 1)  printf("3\n");
        else if(n == 2) printf("9\n");
        else
        {
        	mat res=
			{
                0,0,0, 1,0,0, 1,0,0,
                1,0,0, 0,0,0, 1,0,0,
                1,0,0, 1,0,0, 1,0,0,

                0,1,0, 0,1,0, 0,0,0,
                0,1,0, 0,0,0, 0,1,0,
                0,0,0, 0,1,0, 0,1,0,

                0,0,1, 0,0,1, 0,0,1,
                0,0,1, 0,0,0, 0,0,1,
                0,0,1, 0,0,1, 0,0,0				
			};
        	mat cnt=pow_mat(n-2,res);
        	ll ans=0;
        	rep(i,0,8)
        		rep(j,0,8)
        			{
        				ans=(ans+cnt.a[i][j])%mod;
					}
			 printf("%lld\n",ans);
		}
	}
	return 0;
}

 固定由一些項推出一些項的多維遞推可以考慮矩陣快速冪。