zoukankan      html  css  js  c++  java
  • 矩阵快速幂模板

    struct mat{
        LL a,b;
        mat(LL A=0,LL B=0):a(A),b(B){}
    
        bool isIdentity()const{ return a==1 && b==0; }
    
        mat operator+(const mat &rhs)const{
            return mat( (a+rhs.a)%MOD, (b+rhs.b)%MOD );
        }
    
        mat operator*(const mat &rhs)const{
            return mat(
                    (a*rhs.a+b*rhs.b)%MOD,
                    (a*rhs.b+b*(rhs.a+rhs.b))%MOD);
        }
    
        mat power(LL b)const{
            mat a=*this,ans(1,0);
            while(b){
                if(b&1)ans=ans*a;
                b>>=1; a=a*a;
            }
            return ans;
        }
    };
    View Code

    来源题目:http://codeforces.com/contest/719/problem/E

    代码来源:http://codeforces.com/contest/719/standings 的 sk_aswd

    或者用下面这个模板,不过要一直用struct而已

    来自http://www.cnblogs.com/heimao5027/p/5923401.html

    struct Node{
        LL mat[2][2];
        void reset(){memset(mat, 0, sizeof(mat));}
        void getone(){
            reset();
            mat[0][0] = mat[1][1] = 1;
        }
    };
    
    inline Node mul(Node A, Node B){
        Node ans; ans.reset();
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)
                for (int k = 0; k < 2; k++)
                    ans.mat[i][j] = (ans.mat[i][j] + A.mat[i][k] * B.mat[k][j]) % mod;
        return ans;
    }
    
    inline Node add(Node a, Node b){
        Node ans; ans.reset();
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)
                ans.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % mod;
        return ans;
    }
    
    inline Node kpow(LL k){
        Node ans; ans.reset();
        ans.mat[0][0] = ans.mat[1][1] = 1;
        Node A;
        A.mat[0][0] = A.mat[1][0] = A.mat[0][1] = 1;
        A.mat[1][1] = 0;
        while (k){
            if (k & 1) ans = mul(ans, A);
            A = mul(A, A);
            k >>= 1;
        }
        return ans;
    }
  • 相关阅读:
    iOS 设计模式-委托模式
    python中时间操作总结
    list、dict、tuple的一些小操作总结
    DataFrame的构建及一些操作
    python连接mysql、oracle小例子
    sqlalchemy 映射的小例子
    crontab定时任务以及其中中文乱码问题
    vs2008试用版的评估期已经结束解决办法
    MongoDB 常用shell命令汇总
    把py文件打成exe
  • 原文地址:https://www.cnblogs.com/heimao5027/p/5921765.html
Copyright © 2011-2022 走看看