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

      


      

  • 相关阅读:
    JQuery实现Ajax应用
    访问本地json文件因跨域导致的问题
    python_控制台输出带颜色的文字方法
    查询数据量大时,你会选择关联表查询还是循环中依个查询? up
    自己动手写SQL字符串分解函数Split up
    关于Access数据库执行Update语句后,不报错,但影响行数总是返回0的问题...... up
    TCP和UDP的区别
    Linxu系统下怎么安装vim编辑器
    网络编程 委托
    学习POS的数据包分析
  • 原文地址:https://www.cnblogs.com/lengsong/p/11296842.html
Copyright © 2011-2022 走看看