矩阵快速幂模板题
Posted lengsong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了矩阵快速幂模板题相关的知识,希望对你有一定的参考价值。
题目描述
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 N 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.
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 N 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.
输入
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≤10^10)
Each of the next T lines contains an integer N that shows the number of hours. (1≤N≤10^10)
输出
For each test case, output a single line containing the answer.
样例输入
3
3
4
15
样例输出
20
46
435170
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll mod=1e9+7; void mul(ll a[9][9],ll b[9][9])//a*b ll temp[9][9]; for(register int i=0;i<9;++i) for(register int j=0;j<9;++j) temp[i][j]=0; for(register int k=0;k<9;++k) temp[i][j]=(temp[i][j]+a[i][k]*b[k][j]%mod)%mod; for(register int i=0;i<9;++i) for(register int j=0;j<9;++j) a[i][j]=temp[i][j]; void qpow(ll ans[9][9],ll n)//ans的n次方 ll temp[9][9]; memset(temp,0,sizeof(temp)); for(register int i=0;i<9;++i)temp[i][i]=1; while(n) if(n&1)mul(temp,ans); n>>=1; mul(ans,ans); for(register int i=0;i<9;++i) for(register int j=0;j<9;++j) ans[i][j]=temp[i][j]%mod; int main() int t; scanf("%d",&t); while(t--) ll n; scanf("%lld",&n); if(n==1)printf("3\n"); else if(n==2)printf("9\n"); else ll ans[9][9]=0,1,1,0,0,0,0,0,0, 0,0,0,1,1,0,0,0,0, 0,0,0,0,0,0,1,1,1, 1,0,1,0,0,0,0,0,0, 0,0,0,1,0,1,0,0,0, 0,0,0,0,0,0,1,0,1, 1,1,1,0,0,0,0,0,0, 0,0,0,0,1,1,0,0,0, 0,0,0,0,0,0,1,1,0, ;//状态转移矩阵,其中是以2为chocola,每一行为初态,每一列为次态 qpow(ans,n-2);//调用函数 ll sum=0; for(register int i=0;i<9;++i) for(register int j=0;j<9;++j) sum=(sum+ans[i][j])%mod;//求方案数和 printf("%lld\n",sum%mod); return 0;
以上是关于矩阵快速幂模板题的主要内容,如果未能解决你的问题,请参考以下文章