zoukankan      html  css  js  c++  java
  • bzoj2875: [Noi2012]随机数生成器

    矩阵乘法。

    x[n] = {x[0],1} * ( {a,0} ^ n )

                              {b,1}

    写成这样谁能看懂。。。。

    noi里的大水题。我居然

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #define LL long long 
    using namespace std;
    const int maxn = 2;
    
    LL a,b,mod,g,x,n,ans;
    
    LL mul(long long a,long long b) {
            a%=mod; b%=mod;
            long long res=0;
            while(b) {
                if(b&1) res=(res+a)%mod;
                a=(a+a)%mod;
                b>>=1;
            }
            return res;
        }
    
    struct Matrix {
        const static LL n = 1;
        LL a[maxn][maxn];
        
        inline LL* operator [] (int x) {
            return a[x];
        }
        
        Matrix operator* (Matrix b) {
            Matrix res;
            for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
            for(int k=0;k<=n;k++) 
                res[i][k]=(res[i][k]+mul(a[i][j],b[j][k]))%mod;
            return res;
        }
        
        void build() {
            for(int i=0;i<=n;i++) a[i][i]=1;
        }
        
        Matrix operator^ (long long e) {
            Matrix res,tmp=*this;
            res.build();
            while(e) {
                if(e&1) res=res*tmp;
                tmp=tmp*tmp;
                e>>=1;
            }
            return res;
        }
    
        Matrix () {
            memset(a,0,sizeof(a));
        }
    }res;
    
    
    int main() {
        cin >> mod >> a >> b >> x >> n >> g;
        res[0][0]=a; res[1][0]=b; res[1][1]=1;
        res=res^n;
        ans=(mul(x,res[0][0])+res[1][0])%mod;
        ans=ans%g;
        cout << ans <<'
    ';
        return 0;    
    }

    都1A了。

  • 相关阅读:
    leetcode刷题37
    leetcode刷题36
    leetcode刷题38
    leetcode刷题35
    leetcode刷题34
    leetcode刷题33
    记一次Unity使用XNode插件时自动连线问题
    Unity中UGUI图片跟随文本自适应
    Unity中多个物体交换位置
    使用VSCode编译C
  • 原文地址:https://www.cnblogs.com/invoid/p/5592104.html
Copyright © 2011-2022 走看看