Hdu3233Matrix Power Series矩阵

前面就做过了。。。二分搞下

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string.h>
typedef long long LL;
using namespace std;
int n, M;
struct Matrix
{
    int m[40][40];
};

Matrix Mul(Matrix a, Matrix b)
{
    Matrix ans;
    for (int i = 0; i < n;i++)
    for (int j = 0; j < n; j++){
        ans.m[i][j] = 0;
        for (int k = 0; k < n; k++){
            ans.m[i][j] += a.m[i][k] * b.m[k][j];
            ans.m[i][j] %= M;
        }
    }
    return ans;
}

Matrix quick(Matrix a, int b)
{
    Matrix ans;
    for (int i = 0; i < n;i++)
    for (int j = 0; j < n; j++)
        ans.m[i][j] = (i == j);
    while (b){
        if (b & 1) ans = Mul(ans, a);
        a = Mul(a, a);
        b >>= 1;
    }
    return ans;
}

Matrix add(Matrix a, Matrix b)
{
    for (int i = 0; i < n;i++)
    for (int j = 0; j < n; j++)
        a.m[i][j] += b.m[i][j], a.m[i][j] %= M;
    return a;
}

Matrix gao(Matrix a, int len)
{
    if (len == 1) return a;
    Matrix ans;
    for (int i = 0; i < n;i++)
    for (int j = 0; j < n; j++) ans.m[i][j] = 0;
    Matrix t = gao(a, len >> 1);
    ans = add(ans, t);
    ans = add(ans, Mul(t, quick(a, (len >> 1))));
    if (len & 1) return add(ans, quick(a, len));
    else return ans;
}

int main()
{
    int k;
    while (cin >> n >> k >> M){
        Matrix ans;
        for (int i = 0; i < n;i++)
        for (int j = 0; j < n; j++)
            scanf("%d", &ans.m[i][j]);
        Matrix sum = gao(ans, k);
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++)
                cout << sum.m[i][j] << " ";
            cout << endl;
        }
    }
    return 0;
}
时间: 2024-10-10 12:15:18

Hdu3233Matrix 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

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

题目大意:求由矩阵 A构成的矩阵 S = A + A^2 + A^3 + - + A^k.k的取值范围是:10^9数据很大,应该二分. 对于一个k来说,s(k) = (1+A^(k/2)) *( A+A^2+--+A^(k/2)).如果k为奇数的话需要加上A^(k/2 + 1). 所以二分求和,复杂度就降下来了,当然还得用到矩阵快速幂. Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions

poj 3233 Matrix Power Series(矩阵二分,快速幂)

Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted: 6724 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

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

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

C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速

Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: n (n ≤ 30) , k (k ≤ 109) ,m (m < 104) 输入 输入三个正整数n,k,m 输出 输出矩阵S mod m 样例输入 2 2 4 0 1 1 1 样例输出 1 2 2 3 这道题不多说,可以得出加速矩阵(E为单位矩阵,也就是形为\(\begin{bmatrix}1&

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

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

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