ACM-ICPC 2018 焦作赛区网络预赛 L(矩阵快速幂)

本文介绍了如何使用矩阵快速幂方法解决ACM-ICPC 2018焦作赛区网络预赛中的一道题目,该题目涉及避免连续33小时吃同一种食物或特定组合的食物以保证安全。内容包括输入输出格式,以及解决超时问题的技巧,提供了两个博客链接作为参考。
摘要由CSDN通过智能技术生成
  •  41.03%
  •  1000ms
  •  65536K

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 33 continuous hours when he eats only one kind of food, he will be unhappy. Besides, if there are 33 continuous hours when he eats all kinds of those, with chocolate at the middle hour, it will be dangerous. Moreover, if there are 33 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 NN 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 10000000071000000007.

Input

The fist line puts an integer TT that shows the number of test cases. (T \le 1000T≤1000)

Each of the next TT lines contains an integer NN that shows the number of hours. (1 \le N \le 10^{10}1≤N≤1010)

Output

For each test cse, output a single line containing the answer.

样例输入复制

3
3
4
15

样例输出

20
46
435170

自己画的图传上看不清,就看这俩博客理解方法,由博客的转化图理解用矩阵快速幂

https://blog.csdn.net/Mr_Treeeee/article/details/82728953

http://www.cnblogs.com/lhclqslove/p/9651816.html

做时一直超时,原因是:lld换成%I64d会超时,(但是记得之前一道题吧%I64d换成lld会超时。)

即求n的n-2次方

//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
struct Matrix
{
    ll m[9][9];
};
Matrix Mul(Matrix a,Matrix b)
{
    Matrix c;
    memset(c.m,0,sizeof(c.m));
    for(int i=0;i<9;i++)
    for(int j=0;j<9;j++)
    for(int k=0;k<9;k++)
    c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j])%mod)%mod;
    return c;
}
Matrix fastm(Matrix a,ll n)
{
    Matrix res;
    memset(res.m,0,sizeof(res.m));
    for(int i=0;i<9;i++)
    res.m[i][i]=1;
    while(n)
    {
        if(n&1) res=Mul(res,a);
        n>>=1;
        a=Mul(a,a);
    }
    return res;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        Matrix b;
        memset(b.m,0,sizeof(b.m));
        b.m[0][3]=b.m[0][6]=1;
        b.m[1][0]=b.m[1][3]=1;
        b.m[2][0]=b.m[2][6]=1;
        b.m[3][4]=b.m[3][7]=1;
        b.m[4][1]=b.m[4][7]=1;
        b.m[5][1]=b.m[5][4]=b.m[5][7]=1;
        b.m[6][5]=b.m[6][8]=1;
        b.m[7][2]=b.m[7][5]=b.m[7][8]=1;
        b.m[8][2]=b.m[8][5]=1;
        scanf("%lld",&n);
        if(n==1)
        {
            printf("3\n");
            continue;
        }
        ll sum=0;
        Matrix ans=fastm(b,n-2);//即求n的n-2次方
        for(int i=0;i<9;i++)
        for(int j=0;j<9;j++)
        sum=(sum+ans.m[i][j])%mod;
        printf("%lld\n",sum);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值