初涉矩阵快速幂

一般用来加速递推。

简单的,对于fib数列有,f0 = 1,f1 = 1,fn = fn-1 + fn-2(n >= 2)。

则对于fn有:

一般的,对于fn = A1*f(n-1) + A2*f(n-2)  + .... +A(n-1)*f1,有:

又因为矩阵乘法满足结合律,所以可以用快速幂来求A^n,从而达到递推的效果。

顺便即一个小技巧:

以POJ 3233为例

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f

using namespace std;

int Mod;

const int MAXN = 61;

struct Mat
{
    LL mat[MAXN][MAXN];
    int r,c;

    void Init(int val,int R,int C)
    {
        r = R,c = C;
        for(int i = 1;i <= r; ++i)
            for(int j = 1;j <= c; ++j)
                if(i != j)
                    mat[i][j] = 0;
                else
                    mat[i][j] = val;
    }
};

Mat MatrixMult(Mat a,Mat b)
{
    Mat p;
    p.Init(0,a.r,b.c);

    for(int i = 1;i <= a.r; ++i)
    {
        for(int j = 1;j <= b.c; ++j)
        {
            for(int k = 1;k <= b.r; ++k)
            {
                p.mat[i][j] += a.mat[i][k]*b.mat[k][j];
                p.mat[i][j] %= Mod;
            }
        }
    }

    return p;
}

Mat QuickMult(_LL k,Mat coe)
{
    Mat p;

    p.Init(1,coe.r,coe.c);

    while(k >= 1)
    {
        if(k&1)
            p = MatrixMult(p,coe);
        coe = MatrixMult(coe,coe);
        k >>= 1;
    }

    return p;
}

int main()
{
    _LL n,k,m;

    int i,j;

    Mat A,B;

    scanf("%lld %lld %lld",&n,&k,&m);

    Mod = m;

    for(i = 1;i <= n; ++i)
    {
        for(j = 1;j <= n; ++j)
            scanf("%lld",&A.mat[i][j]);
    }

    for(i = 1;i <= n; ++i)
    {
        for(j = 1;j <= n; ++j)
        {
            if(i == j)
                A.mat[i][j+n] = 1;
            else
                A.mat[i][j+n] = 0;
        }
    }

    for(i = 1;i <= n; ++i)
    {
        for(j = 1;j <= n; ++j)
        {
            A.mat[i+n][j] = 0;
        }
    }

    for(i = 1;i <= n; ++i)
    {
        for(j = 1;j <= n; ++j)
        {
            if(i == j)
                A.mat[i+n][j+n] = 1;
            else
                A.mat[i+n][j+n] = 0;
        }
    }

    A.r = 2*n,A.c = 2*n;

    A = QuickMult(k+1,A);

    for(i = 1;i <= n; ++i)
    {
        if(A.mat[i][i+n])
            A.mat[i][i+n]--;
        else
            A.mat[i][i+n] = m-1;
    }

    for(i = 1;i <= n; ++i)
    {
        for(j = 1;j <= n; ++j)
        {
            printf("%lld",A.mat[i][j+n]);
            if(j == n)
                printf("\n");
            else
                printf(" ");
        }
    }

    return 0;
}

初涉矩阵快速幂

时间: 2024-10-10 15:51:17

初涉矩阵快速幂的相关文章

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

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

模板 快速幂模板 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

233 Matrix 矩阵快速幂

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333...

2017省夏令营Day7 【快速幂,筛法,矩阵快速幂,线段树】

题解:首先,我们可以得到一个规律:经过2次变换后,a和b的值都分别乘2了,所以只要用快速幂就能过啦,但是,要特判n为0的情况. 代码如下: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #define Mod 1000000007 5 using namespace std; 6 long long a,b,n,ans1,ans2; 7 long long power(long long x)