zoukankan      html  css  js  c++  java
  • 2017多校第10场 HDU 6172 Array Challenge 猜公式,矩阵幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6172

    题意:如题。

    解法:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const LL mod = 1e9+7;
    struct Matrix{
        LL a[3][3];
        void set1(){
            memset(a, 0, sizeof(a));
        }
        void set2(){
            memset(a, 0, sizeof(a));
            for(int i=0; i<3; i++) a[i][i]=1;
        }
        void set3()
        {
            a[0][0]=4,a[0][1]=17,a[0][2]=-12;
            a[1][0]=1,a[1][1]=0,a[1][2]=0;
            a[2][0]=0,a[2][1]=1,a[2][2]=0;
        }
    };
    Matrix operator * (const Matrix &a, const Matrix &b){
        Matrix res;
        res.set1();
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                for(int k=0; k<3; k++){
                    res.a[i][j] = (res.a[i][j]%mod+a.a[i][k]*b.a[k][j]%mod + mod)%mod;
                }
            }
        }
        return res;
    }
    Matrix qsm(Matrix a, LL n){
        Matrix res;
        res.set2();
        while(n){
            if(n&1) res = res*a;
            a = a*a;
            n/=2LL;
        }
        return res;
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            LL n;
            scanf("%lld", &n);
            if(n==2){
                puts("31");
            }
            else if(n==3){
                puts("197");
            }
            else if(n==4){
                puts("1255");
            }
            else{
                Matrix A,B;
                A.set3();
                B = qsm(A, n-4);
                LL ans = (B.a[0][0]*1255%mod+B.a[0][1]*197%mod+B.a[0][2]*31%mod+mod)%mod;
                printf("%lld
    ", ans%mod);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    windows下安装redis
    十五oracle 触发器
    Flask 学习 六 大型程序结构
    Flask 学习 五 电子邮件
    Flask 学习 四 数据库
    Flask学习 三 web表单
    Flask学习 二 模板
    Flask学习 一 基本结构
    Python操作Redis
    Python操作MySQL
  • 原文地址:https://www.cnblogs.com/spfa/p/7439934.html
Copyright © 2011-2022 走看看