矩阵快速幂模板题

题目描述

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

  


  

原文地址:https://www.cnblogs.com/lengsong/p/11296842.html

时间: 2024-11-07 22:06:55

矩阵快速幂模板题的相关文章

hdu 2604 矩阵快速幂模板题

/* 矩阵快速幂: 第n个人如果是m,有f(n-1)种合法结果 第n个人如果是f,对于第n-1和n-2个人有四种ff,fm,mf,mm其中合法的只有fm和mm 对于ffm第n-3个人只能是m那么有f(n-4)种 对于fmm那么对于第n-3个人没有限制有f(n-3)种 顾f(n)=f(n-1)+f(n-3)+f(n-4); 求出前四个结果分别是 a[1]=2;a[2]=4;a[3]=6;a[4]=9; A=|a[4],a[3],a[2],a[1]| 可以构造矩阵 |1 1 0 0 | B= |0

hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)

Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. Output对应每组数据,输出Tr(A^k)%9973. Sample Input22 21 00 13 999999991 2 34

CodeForces 450B (矩阵快速幂模板题+负数取模)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N项.注意负数取模的方式:-1%(10^9+7)=10^9+6. 解题思路: 首先解出快速幂矩阵.以f3为例. [f2]  * [1 -1] = [f2-f1]=[f3]  (幂1次) [f1]  * [1  0]     [f2]      [f2] 于是fn=[f2] *[1 -1]^(n-2)

HDU1757又是一道矩阵快速幂模板题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 按照题目的要求构造矩阵 //Author: xiaowuga //矩阵: //a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 9 // 1 0 0 0 0 0 0 0 0 0 8 // 0 1 0 0 0 0 0 0 0 0 7 // 0 0 1 0 0 0 0 0 0 0 6 // 0 0 0 1 0 0 0 0 0 0 5 // 0 0 0 0 1 0 0 0 0 0 4 //

POJ3070:Fibonacci(矩阵快速幂模板题)

http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdlib.h> #include <cstdio> #include <algorithm> #define mod 10000 using namespace std; struct m { int a[3][3]; } init,res; int n; m Mult(m x,m

HDU1575:Tr A(矩阵快速幂模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1575 #include <iostream> #include <string.h> #include <stdlib.h> #include <cstdio> #include <algorithm> #define mod 9973 using namespace std; struct matrix { int a[11][11]; } init,res

51nod1113(矩阵快速幂模板)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 题意:中文题诶- 思路:矩阵快速幂模板 代码: 1 #include <iostream> 2 #define ll long long 3 using namespace std; 4 5 const int mod = 1e9+7; 6 const int MAXN = 1e2+10; 7 int n, m; 8 9 typedef struct

HDU-1575-Tr A(矩阵快速幂模板)

Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. Output 对应每组数据,输出Tr(A^k)%9973. Sample Input 2 2 2 1 0 0 1 3 99999

矩阵快速幂模板篇

转载请注明出处:http://blog.csdn.net/u012860063 或许你们看不太懂,纯属自用: 第一种: Description Let's define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n-1) + f(n-2), n > 1 When a = 0 and b = 1, this sequence gives the Fibonacci seq