zoukankan      html  css  js  c++  java
  • 矩阵快速幂模板题

    题目描述

    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.

    输入

    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)

    输出

    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
    ");
            else if(n==2)printf("9
    ");
            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
    ",sum%mod);
            }
        }
        return 0;
    }
    

      


      

  • 相关阅读:
    07-selenium、PhantomJS(无头浏览器)
    06爬虫-异步协程
    Numpy数值类型与数值运算-03
    05爬虫-requests模块基础(2)
    初识Matplotlib-01
    03爬虫-requests模块基础(1)
    Django安装与简单事例-02
    JavaWeb学习总结(二):Http协议
    Tomcat学习总结(一):目录简介
    Web服务器学习总结(一):web服务器简介
  • 原文地址:https://www.cnblogs.com/lengsong/p/11296842.html
Copyright © 2011-2022 走看看