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

      


      

  • 相关阅读:
    SQL Server数据库一直显示“正在还原”的解决方法
    查看MSSQL 数据表信息使用情况
    sql server 查询缺失的索引
    windows server2012R2 上 .net core IIS 部署--应用程序池 自动停止
    MSSQL 数据库日志文件收缩
    SQL Server GUID转19位字符串
    SQL SERVER 之全文索引使用
    MSSQL 统计整个数据库所有表数据记录数
    freePCRF免费版体验
    Hortonwork Ambari配置Hive集成Hbase的java开发maven配置
  • 原文地址:https://www.cnblogs.com/lengsong/p/11296842.html
Copyright © 2011-2022 走看看