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;
}

 固定由一些项推出一些项的多维递推可以考虑矩阵快速幂。

猜你喜欢

转载自blog.csdn.net/weixin_41466575/article/details/82748718
今日推荐