矩阵快速幂【模板】

const int MAXN = 110;
struct Matrax
{
    int m[MAXN][MAXN];
}a,per;

int N,M;

void Init()
{
    for(int i = 0; i < N; ++i)
        for(int j = 0; j < N; ++j)
        {
            scanf("%d",&a.m[i][j]);
            a.m[i][j] %= M;
            per.m[i][j] = (i == j);
        }
}

Matrax Multi(Matrax a,Matrax b) //a * b
{
    Matrax c;
    for(int i = 0; i < N; ++i)
        for(int j = 0; j < N; ++j)
        {
            c.m[i][j] = 0;
            for(int k = 0; k < N; ++k)
                c.m[i][j] += a.m[i][k]*b.m[k][j];
            c.m[i][j] %= M;
        }
    return c;
}

Matrax Power(int k) //a^k % M
{
    Matrax c,p,ans = per;
    p = a;
    while(k)
    {
        if(k&1)
            ans = Multi(ans,p);
        p = Multi(p,p);
        k >>= 1;
    }
    return ans;
}

Matrax Add(Matrax a,Matrax b)   // a + b
{
    Matrax c;
    for(int i = 0; i < N; ++i)
        for(int j = 0; j < N; ++j)
            c.m[i][j] = (a.m[i][j] + b.m[i][j]) % M;
    return c;
}
Matrax MatraxSum(int k) //a + a^2 + a^3 + … + a^k
{
    if(k == 1)
        return a;
    Matrax temp,b;
    temp = MatraxSum(k/2);
    if(k&1)
    {
        b = Power(k/2+1);
        temp = Add(temp,Multi(temp,b));
        temp = Add(temp,b);
    }
    else
    {
        b = Power(k/2);
        temp = Add(temp,Multi(temp,b));
    }
    return temp;
}

时间: 2024-08-09 16:23:09

矩阵快速幂【模板】的相关文章

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

矩阵快速幂模板篇

转载请注明出处: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

矩阵快速幂 模板

矩阵快速幂模板 1 #include<stdio.h> 2 #include<math.h> 3 #include<set> 4 #include<string.h> 5 using namespace std; 6 int const mod=1e9+7; 7 struct matrix 8 { 9 long long m[3][3]; 10 matrix() 11 { 12 memset(m,0,sizeof(m)); 13 } 14 matrix op

HDU1575--Tr A(矩阵快速幂模板)

Tr A Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n

HDU-1575-Tr A(矩阵快速幂模板)

Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. Output 对应每组数据,输出Tr(A^k)%9973. Sample Input 2 2 2 1 0 0 1 3 99999

矩阵快速幂 模板与简单讲解

模板 快速幂模板 1 void solve(matrix t,long long o) 2 { 3 matrix e; 4 5 memset(e.a,0,sizeof(e.a)); 6 7 for (int i = 0;i < d;i++) 8 e.a[i][i] = 1; 9 10 while (o) 11 { 12 if (o & 1) 13 { 14 e = mul(e,t); 15 } 16 17 o >>= 1; 18 19 t = mul(t,t); 20 } 21

快速幂和矩阵快速幂模板

快速幂模板: ll qmod(ll x,ll n,ll mod) { ll res=1; while(n){ if(n&1) res=(res*x)%mod; x=(x*x)%mod; n/=2; } return res; } 例题:hdu 1097 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> usin

hdu 2604 矩阵快速幂模板题

/* 矩阵快速幂: 第n个人如果是m,有f(n-1)种合法结果 第n个人如果是f,对于第n-1和n-2个人有四种ff,fm,mf,mm其中合法的只有fm和mm 对于ffm第n-3个人只能是m那么有f(n-4)种 对于fmm那么对于第n-3个人没有限制有f(n-3)种 顾f(n)=f(n-1)+f(n-3)+f(n-4); 求出前四个结果分别是 a[1]=2;a[2]=4;a[3]=6;a[4]=9; A=|a[4],a[3],a[2],a[1]| 可以构造矩阵 |1 1 0 0 | B= |0

hdu 1575 求一个矩阵的k次幂 再求迹 (矩阵快速幂模板题)

Problem DescriptionA为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据.每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. Output对应每组数据,输出Tr(A^k)%9973. Sample Input22 21 00 13 999999991 2 34

POJ 3070 Fibonacci(矩阵快速幂模板)

Description: In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternative formula for the Fibonacci sequence i