zoukankan      html  css  js  c++  java
  • ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)

    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 NNN 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.
    Input

    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≤1010)
    Output

    For each test case, output a single line containing the answer.
    样例输入

    3
    3
    4
    15

    样例输出

    20
    46
    435170

    首先考虑2位 有1:mc  2:mf  3:cf 4:cm 5:fc 6:fm 7:cc 8:mm 9:ff;

    所以第三位必须满足题意 则mc后面只能mc 则生成的新的2位是 4:cm7:cc

    1生成4,7,;2生成5,6,9;.........;9生成5,6

    所以可以写出以下打表代码

    ll a[2][15];
    int main(){
        int op=0;
        for(int i=1;i<=9;i++)
            a[op][i]=1;
        for(int i=3;i<=30;i++){
            op^=1;
            for(int j=1;j<=9;j++)
                a[op][j]=0;
            a[op][1]=(a[op^1][6]+a[op^1][8])%MOD;
            a[op][2]=(a[op^1][4]+a[op^1][6]+a[op^1][8])%MOD;
            a[op][3]=(a[op^1][5]+a[op^1][7])%MOD;
            a[op][4]=(a[op^1][1]+a[op^1][7])%MOD;
            a[op][5]=(a[op^1][2]+a[op^1][9])%MOD;
            a[op][6]=(a[op^1][2]+a[op^1][3]+a[op^1][9])%MOD;
            a[op][7]=(a[op^1][1]+a[op^1][5])%MOD;
            a[op][8]=(a[op^1][4]+a[op^1][6])%MOD;
            a[op][9]=(a[op^1][2]+a[op^1][3])%MOD;
            ll ans=0;
            for(int j=1;j<=9;j++){
                printf("%lld ",a[op][j]);
                ans=(ans+a[op][j])%MOD;
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }

    所以由以上公式构建9维矩阵,矩阵快速幂求解

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime>
    #include <map>
    #include <set>
    using namespace std;
    #pragma comment(linker, "/stck:1024000000,1024000000")
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>=y?x:y)
    #define min(x,y) (x<=y?x:y)
    #define MAX 100000000000000000
    #define MOD 1000000007
    #define pi acos(-1.0)
    #define ei exp(1)
    #define PI 3.1415926535897932384626433832
    #define ios() ios::sync_with_stdio(true)
    #define INF 0x3f3f3f3f
    #define mem(a) ((a,0,sizeof(a)))
    typedef long long ll;
    ll A,B,n;
    struct matrix
    {
        ll a[10][10];
    };
    matrix mutiply(matrix u,matrix v)
    {
        matrix res;
        memset(res.a,0,sizeof(res.a));
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
                for(int k=0;k<9;k++)
                    res.a[i][j]=(res.a[i][j]+u.a[i][k]*v.a[k][j])%MOD;
        return res;
    }
    matrix quick_pow(ll n)
    {
        matrix ans,res;
        memset(res.a,0,sizeof(res.a));
        memset(ans.a,0,sizeof(ans.a));
        for(int i=0;i<9;i++)
            res.a[i][i]=1;
        ans.a[5][0]=ans.a[7][0]=1;
        ans.a[3][1]=ans.a[5][1]=ans.a[7][1]=1;
        ans.a[4][2]=ans.a[6][2]=1;
        ans.a[0][3]=ans.a[6][3]=1;
        ans.a[1][4]=ans.a[8][4]=1;
        ans.a[1][5]=ans.a[2][5]=ans.a[7][5]=1;
        ans.a[0][6]=ans.a[4][6]=1;
        ans.a[3][7]=ans.a[5][7]=1;
        ans.a[1][8]=ans.a[2][8]=1;
        while(n)
        {
            if(n&1) res=mutiply(res,ans);
            n>>=1;
            ans=mutiply(ans,ans);
        }
        return res;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--){
            ll n;
            scanf("%lld",&n);
            if(n==1) printf("3
    ");
            else{
                ll pos=0;
                matrix ans=quick_pow(n-2);
                for(int i=0;i<9;i++)
                    for(int j=0;j<9;j++)
                        pos=(pos+ans.a[i][j])%MOD;
                printf("%lld
    ",pos);
            }
        }
        return 0;
    }
  • 相关阅读:
    OAuth 2 深入介绍
    浅谈 EF CORE 迁移和实例化的几种方式
    为什么我们不应该使用微信或者 QQ 作为团队协作的 IM 工具?
    三值 bool? 进行与或运算后的结果
    Slack 开发入门之 Incoming Webhooks:往 Slack 的 Channel 中发消息
    int? 竟然真的可以是 null!.NET/C# 确定可空值类型 Nullable 实例的真实类型
    .NET 中使用 Mutex 进行跨越进程边界的同步
    UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等)
    谨慎使用 FileInfo.Exists 实例方法,而是使用 File.Exists 静态方法替代
    只需 5 秒钟,你就能取到 WPF 程序的超高分辨率超高清截图
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9651533.html
Copyright © 2011-2022 走看看