• 矩阵快速幂——HDU5667


    题目链接

    分析全在纸上,博客上懒得写

    我是个蒟蒻,只有源代码

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    typedef long long LL;
    const int maxn=107;
    LL mod;
    struct mat{
        LL m[maxn][maxn];
        int x,y;///x是行数y是列数
    }unit;
    void init_unit(){
        memset(unit.m,0,sizeof(unit.m));
        for(int i=0;i<maxn;i++)
            unit.m[i][i]=1;
    }
    void show(mat a){
        for(int i=1;i<=a.x;i++){
            for(int j=1;j<=a.y;j++)
                printf("%d ",a.m[i][j]);
            printf("
    ");
        }
    }
    mat mat_mul(mat a,mat b){
        mat c;
        c.x=a.x,c.y=b.y;
        memset(c.m,0,sizeof(c.m));
        for(int i=1;i<=c.x;i++)
        for(int j=1;j<=c.y;j++){
            for(int k=1;k<=a.y;k++){
                c.m[i][j]+=(a.m[i][k]*b.m[k][j])%(mod-1);
                c.m[i][j]%=mod-1;
            }
        }
        return c;
    }
    mat mat_QuickPow(mat a,LL b){
        mat ans=unit;
        ans.x=a.x,ans.y=a.y;
        while(b){
            if(b&1)
                ans=mat_mul(ans,a);
            a=mat_mul(a,a);
            b>>=1;
        }
        return ans;
    }
    LL QuickPow(LL a,LL b){
        LL ans=1;
        while(b){
            if(b&1)ans=(ans*a)%mod;
            a=(a*a)%mod;
            b>>=1;
        }
        return ans;
    }
    int t;
    LL n,x,y,z;
    int main(){
        scanf("%d",&t);
        init_unit();
        while(t--){
            scanf("%I64d%I64d%I64d%I64d%I64d",&n,&x,&y,&z,&mod);
            mat a;
            a.x=a.y=3;
            a.m[1][1]=1,a.m[1][2]=y,a.m[1][3]=0;
            a.m[2][1]=0,a.m[2][2]=z,a.m[2][3]=1;
            a.m[3][1]=0,a.m[3][2]=1,a.m[3][3]=0;
            if(n==1){
                printf("1
    ");
                continue;
            }
            if(n==2){
                printf("%I64d
    ",QuickPow(x,y));
                continue;
            }
            a=mat_QuickPow(a,n-2);
            LL t=(a.m[1][2]+a.m[2][2]*y);
            printf("%I64d
    ",QuickPow(x,t));
        }
        return 0;
    }
  • 相关阅读:
    JDBC连接各种数据库的字符串,就是不好记
    HTTP协议详解
    gson 简要使用
    maven 仓库地址:
    HTTP请求头详解
    HTTP协议---HTTP请求中的常用请求字段和HTTP的响应状态码及响应头
    如何终止java线程
    oracle 函数大全及运算符
    Java集合的线程安全用法
    哈希算法快速查表的原理
  • 原文地址:https://www.cnblogs.com/helman/p/11323639.html
Copyright © 2020-2023  润新知