Matrix Power Series(矩阵快速幂)

矩阵快速幂:http://www.cnblogs.com/kuangbin/archive/2012/08/17/2643347.html

Matrix Power Series

Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 16341   Accepted: 6966

Description

Given a n × n matrix A and a positive integer k, find the sum
S = A + A2 + A3 + … +
Ak
.

Input

The input contains exactly one test case. The first line of input contains three positive integers
n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow
n lines each containing n nonnegative integers below 32,768, giving
A’s elements in row-major order.

Output

Output the elements of S modulo m in the same way as A is given.

Sample Input

2 2 4
0 1
1 1

Sample Output

1 2
2 3

题意:简单易懂;

题解:两次二分矩阵,具体看代码吧!

AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-9

using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int M = 1e5 + 100;
const int INF = 0x3f3f3f3f;
int n,mod,k;

struct Mac {
    int c[33][33]; //原始矩阵
    void unit1() { //原始矩阵
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                scanf("%d",&c[i][j]);
            }
        }
    }
    void unit2() { //单位矩阵
        c[0][0] = c[1][1] = 1;
        c[1][0] = c[0][1] = 0;
    }
};
Mac tmp,sum; //原始矩阵,和求和矩阵

Mac mul(Mac a,Mac b) { //矩阵相乘
    Mac p;
    memset(p.c,0,sizeof(p.c));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(a.c[i][j]) {
                for(int k = 0; k < n; k++) {
                    p.c[i][k] += (a.c[i][j] * b.c[j][k]) % mod;
                    p.c[i][k] %= mod;
                }
            }
        }
    }
    return p;
}

Mac add(Mac a,Mac b) { //矩阵加法
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            a.c[i][j] += b.c[i][j] % mod;
            a.c[i][j] %= mod;
        }
    }
    return a;
}

Mac quickmod(Mac a,int n) { //矩阵快速幂
    Mac p;
    p.unit2();
    while(n) {
        if(n & 1) p = mul(p,a);
        a = mul(a,a);
        n >>= 1;
    }
    return p;
}

Mac binary_matrix1(int k) { //二分矩阵快速幂
    Mac res;
    if(k == 1) {
        return tmp;
    }
    res = binary_matrix1(k / 2);
    res = mul(res,res);
    if(k & 1) res = mul(res,tmp);
    return res;
}

Mac binary_matrix2(int k){ //二分矩阵求矩阵幂之和 A1+A2+A3...;
    if(k == 1){
        return tmp;
    }
    Mac res = binary_matrix2(k / 2);
    Mac tp = binary_matrix1(k / 2);
    tp = mul(tp,res);
    sum = add(sum,tp);
    if(k & 1) sum = add(sum,binary_matrix1(k));
    return sum;
}

int main() {
    cin>>n>>k>>mod;
    tmp.unit1();
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            sum.c[i][j] = tmp.c[i][j];
        }
    }
    Mac res = binary_matrix2(k);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            printf(j == n - 1 ? "%d\n" : "%d ",res.c[i][j]);
        }
    }
    return 0;
}
时间: 2024-12-31 06:18:30

Matrix Power Series(矩阵快速幂)的相关文章

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

POJ3233:Matrix Power Series(矩阵快速幂+二分)

http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k<=10^9.这道题两次二分,相当经典.首先我们知道,A^i可以二分求出.然后我们需要对整个题目的数据规模k进行二分.比如,当k=6时,有:A + A^2 + A^3 + A^4 + A^5 + A^6 =(A + A^2 + A^3) + A^3*(A + A^2 + A^3)应用这个式子后,规模

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

Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 16403   Accepted: 6980 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. Input The input contains exactly one test cas

POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+Ak/2+A(k/2)*(A+A2+...+Ak/2)    k为偶数时: sum=A+A2+...+A(k-1)/2+A((k-1)/2)*(A+A2+...+A(k-1)/2)+Ak    k为奇数时. 然后递归二分求和 PS:刚开始mat定义的是__int64,于是贡献了n次TLE... #i

poj 3233 Matrix Power Series - 矩阵快速幂

Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. Input The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n li

poj3233 Matrix Power Series 矩阵快速幂

题目链接: http://poj.org/problem?id=3233 题意: 给你A矩阵,A矩阵是n*n的一个矩阵,现在要你求S = A + A^2 + A^3 + - + A^k.那么s一定也是一个N*N的矩阵,最后要你输出s,并且s的每一个元素对m取余数 思路: 令Sk-1=I+A+A^2+.....+A^(k-1) 则Sk=I+A+A^2+.....+A^k+A^k 所以 Sk=Sk-1+A^k 答案就是 mat[i+n][j] 代码: 1 #include <iostream> 2

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

Matrix Power Series Time Limit:3000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3233 Appoint description:  System Crawler  (2015-02-28) Description Given a n × n matrix A and a positive integer k, find the

poj3233Matrix Power Series(矩阵快速幂,两种写法)

Matrix Power Series Time Limit:3000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. Input The input contains exactly one t

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]