矩阵高速幂模板

#define MOD 1000000007
typedef long long ll;
typedef struct matrixnod{
    ll m[2][2];
}matrix;
matrix mat(matrix a,matrix b){
    matrix c;
    int mod=MOD-1;
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++){
            c.m[i][j]=0;
            for(int k=0;k<2;k++){
               c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
               c.m[i][j]%=mod;
            }
        }
    return c;
}
matrix doexpmat(matrix a,ll num){
    matrix t={
        1,0,
        0,1
    };
    while(num){
        if(num&1) t=mat(a,t);
        num=num>>1;
        a=mat(a,a);
    }
    return t;
}
时间: 2024-11-05 12:23:49

矩阵高速幂模板的相关文章

hdu 5411 CRB and Puzzle 矩阵高速幂

链接 题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/ 给定n个点 常数m 以下n行第i行第一个数字表示i点的出边数.后面给出这些出边. 问:图里存在多少条路径使得路径长度<=m.路径上的点能够反复. 思路: 首先能得到一个m*n*n的dp.dp[i][j]表示路径长度为i 路径的结尾为j的路径个数 . 答案就是sigma(dp[i][j]) for every i from 1 to m, j from 1 to n; 我们

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,

HDU3117-Fibonacci Numbers(矩阵高速幂+log)

题目链接 题意:斐波那契数列,当长度大于8时.要输出前四位和后四位 思路:后四位非常easy,矩阵高速幂取模,难度在于前四位的求解. 已知斐波那契数列的通项公式:f(n) = (1 / sqrt(5)) * (((1 + sqrt(5)) / 2) ^ n - ((1 + sqrt(5)) / 2) ^ n).当n >= 40时((1 + sqrt(5)) / 2) ^ n近似为0. 所以我们如果f(n) = t * 10 ^ k(t为小数),所以当两边同一时候取对数时.log10(t * 10

51nod1113(矩阵快速幂模板)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 题意:中文题诶- 思路:矩阵快速幂模板 代码: 1 #include <iostream> 2 #define ll long long 3 using namespace std; 4 5 const int mod = 1e9+7; 6 const int MAXN = 1e2+10; 7 int n, m; 8 9 typedef struct

HDOJ 4686 Arc of Dream 矩阵高速幂

矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2164    Accepted Submission(s): 680 Problem Description An Arc of Dream is a curve defined by following fun

HDU 4965 Fast Matrix Calculation(矩阵高速幂)

HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个仅仅有6x6.就能够用矩阵高速幂搞了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int M = 10; int n, m; int A[N][M], B[M][N], C[M][M], CC[N

矩阵快速幂模板篇

转载请注明出处:http://blog.csdn.net/u012860063 或许你们看不太懂,纯属自用: 第一种: Description Let's define another number sequence, given by the following function: f(0) = a f(1) = b f(n) = f(n-1) + f(n-2), n > 1 When a = 0 and b = 1, this sequence gives the Fibonacci seq

hdu 5318 The Goddess Of The Moon 矩阵高速幂

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 438    Accepted Submission(s): 150 Problem Description Chang'e (嫦娥) is

HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)

CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 483    Accepted Submission(s): 198 Problem Description CRB is now playing Jigsaw Puzzle. There are  kinds of pieces with infinite