poj 3233 Matrix Power Series

点击打开poj 3233

思路: 二分求和+矩阵快速幂

分析:

1 题目给定一个n*n的矩阵A,要求(A+A^2+....A^K)%m后的矩阵

2 对于任意的A^x,我们都能够利用矩阵快速幂求出,但是我们现在要求的是和。

   仔细观察整个式子,那么我们可以对原式进行变形

   如果k为偶数,那么(A+A^2+....A^K) = (A+...+A^K/2)+A^K/2*(A+...+A^K/2)

   如果k为奇数,那么(A+A^2+....A^K) = (A+...+A^K/2)+A^K/2*(A+...+A^K/2)+A^k

3 那么对于上面的式子的变形,就是二分的思想,那么我们可以利用二分来求和,然后对于单个的矩阵的x次方我们利用快速幂

代码:

/************************************************
 * By: chenguolin                               *
 * Date: 2013-08-28                             *
 * Address: http://blog.csdn.net/chenguolinblog *
 ************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 30;

int n , k , MOD;
struct Matrix{
    int mat[N][N];
    Matrix operator*(const Matrix& m)const{
        Matrix tmp;
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                tmp.mat[i][j] = 0;
                for(int k = 0 ; k < n ; k++)
                    tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%MOD;
                tmp.mat[i][j] %= MOD;
            }
        }
        return tmp;
    }
    Matrix operator+(const Matrix& m)const{
        Matrix tmp;
        for(int i = 0 ; i < n ; i++)
            for(int j = 0 ; j < n ; j++)
                tmp.mat[i][j] = (mat[i][j]+m.mat[i][j])%MOD;
        return tmp;
    }
};

Matrix Pow(Matrix m , int t){
    Matrix ans;
    memset(ans.mat , 0 , sizeof(ans.mat));
    for(int i = 0 ; i < n ; i++)
        ans.mat[i][i] = 1;
    while(t){
        if(t&1)
            ans = ans*m;
        t >>= 1;
        m = m*m;
    }
    return ans;
}

Matrix solve(Matrix m , int t){
    Matrix A;
    memset(A.mat , 0 , sizeof(A.mat));
    for(int i = 0 ; i < n ; i++)
        A.mat[i][i] = 1;
    if(t == 1)
        return m;
    if(t&1)
        return (Pow(m,t>>1)+A)*solve(m,t>>1)+Pow(m , t);
    else
        return (Pow(m,t>>1)+A)*solve(m,t>>1);
}

void output(Matrix ans){
    for(int i = 0 ; i < n ; i++){
        printf("%d" , ans.mat[i][0]%MOD);
        for(int j = 1 ; j < n ; j++)
            printf(" %d" , ans.mat[i][j]%MOD);
        puts("");
    }
}

int main(){
    Matrix m , ans;
    while(scanf("%d%d%d" , &n , &k , &MOD) != EOF){
         for(int i = 0 ; i < n ; i++)
            for(int j = 0 ; j < n ; j++)
                scanf("%d" , &m.mat[i][j]);
         ans = solve(m , k);
         output(ans);
    }
    return 0;
}
时间: 2024-08-03 19:16:57

poj 3233 Matrix Power Series的相关文章

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

点击打开链接 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 19189   Accepted: 8099 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 矩阵连乘+二分

这题2829MS险过..应该会有更好的方法 首先矩阵连乘这没说的 重要的是在于二分 有公式 k为奇数时 s(k)=a+(a+a^(k/2+1))*s(k/2) k为偶数时 s(k)=(1+a^(k/2))*s(k/2)  例如s(7)=a+(a+a^4)*s(3) s(6)=(1+a^3)*s(3) 有了二分的方法这题明显思路就清楚了 #include<cstdio> #include<cmath> #include<iostream> using namespace

poj 2155 Matrix

点击打开poj 2155 思路: 二维树状数组+区间更新,单点查询 分析: 点击打开查看论文  建议先看看这篇论文,比较好理解 1 题目给定两种操作,第一种是给定左上角和右下角的下标,把这个子矩形里面的0/1进行互换,第二种是问某个点的值 2 我们先看一维的情况 假设题目给定的是一个长度为n的一维数组 那么我们现在要把区间[i,j]里面的值进行0/1互换 首先我们先来看一个定理,假设一个数原先为0,那么它经过奇数次的变换为1,偶数次的变换为0. 所以我们可以这么这么想[i,j]区间要变换那么就是

poj 2155 Matrix 二维树状数组

   经典题,把一个-=写成=-了查了半天都没查出来-- /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include

矩阵快速幂专题【完结】

第一题 hdu 1757 A Simple Math Problem 点击打开链接 思路:矩阵快速幂 分析: 1 最简单的矩阵快速幂的题目,直接利用矩阵求解即可 点击打开查看代码 第二题 hdu 1575 Tr A 点击打开hdu 1575 思路: 矩阵快速幂 分析: 1 题目给定一个n*n的矩阵要求矩阵的k次幂之后的矩阵的对角线的和 2 矩阵快速幂的裸题 点击打开查看代码 第三题 hdu 2604 Queuing 点击打开hdu 2604 思路: 递推+矩阵快速幂 分析; 1 根据题目的意思,

[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]Contents

I find it may cost me so much time in doing such solutions to exercises and problems....I am sorry that I could not be persistent in doing it...Wish I could just recover it later on.   [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]PrI.6

[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.6

If $\sen{A}<1$, then $I-A$ is invertible, and $$\bex (I-A)^{-1}=I+A+A^2+\cdots, \eex$$ aa convergent power series. This is called the Neumann series.   Solution.  Since $\sen{A}<1$, $$\bex \sum_{n=0}^\infty \sen{A}^n=\frac{1}{1-\sen{A}}<\infty. \

树状数组专题【完结】

1 国外的论文点击打开链接 2 我的总结点击打开链接 任何能够造成树状数组下表为0的操作都会使得程序TLE,这是个很重要的地方! 第一题 poj 2299 Ultra-QuickSort 点击打开poj 2299 思路: 离散化+树状数组 分析: 1 题目的意思就是要求逆序数对 2 题目的输入个数有500000的规模但是每个数的最大值为999999999,因此我们需要离散化这些数据 3 对于数据9 1 0 5 4我们离散化成5 2 1 4 3 那么对于输入一个树a[i]我们去求一下它的离散化后的

OpenCASCADE Interpolation - Lagrange

OpenCASCADE Interpolation - Lagrange eryar@163.com Abstract. Power basis polynomial is the most simple polynomial function. It also be called power series. OpenCASCADE provides basic computation functions for polynomial functions, such as evaluate th