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;

struct Mat
{
    LL mat[MAX_SIZE][MAX_SIZE];
    Mat()
    {
        CLR( mat, 0 );
    }
    void zero()
    {
        CLR( mat, 0 );
    }
    void setv( int v )
    {
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j )
                mat[i][j] = v;
    }
    void init()
    {
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j )
                mat[i][j] = ( i == j );
    }
    Mat operator + ( const Mat &b ) const
    {
        Mat c;
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j )
                c.mat[i][j] = ( mat[i][j] + b.mat[i][j] ) % MOD;
        return c;
    }
    Mat operator * ( const Mat &b ) const
    {
        Mat c;
        for( int k = 0; k < n; ++k )
            for( int i = 0; i < n; ++i ) if( mat[i][k] )
                for( int j = 0; j < n; ++j )
                    c.mat[i][j] = ( c.mat[i][j] + mat[i][k] * b.mat[k][j] ) % MOD;
        return c;
    }
    void debug()
    {
        for( int i = 0; i < n; ++i )
        {
            for( int j = 0; j < n; ++j )
            {
                if( j != 0 )    putchar( ‘ ‘ );
                printf( "%lld", mat[i][j] );
            }
            putchar( ‘\n‘ );
        }
    }
};

Mat fast_mod( Mat a, int b )
{
    Mat res;
    res.init();
    while( b )
    {
        if( b & 1 )    res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

Mat solve( Mat a, int b )
{
    if( b == 1 )
        return a;
    Mat res;
    res.init();
    res = res + fast_mod( a, b >> 1);
    res = res * solve( a, b >> 1 );
    if( b & 1 )
        res = res + fast_mod( a, b );
    return res;
}

void Orz()
{
    while( ~scanf( "%d %d %d", &n, &k, &MOD ) )
    {
        Mat c;
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j )
                scanf( "%d", &c.mat[i][j] );
        Mat res = solve( c, k );
        res.debug();
    }
}

int main()
{
    Orz();
    return 0;
}

代码君

时间: 2024-10-23 05:32:35

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)的相关文章

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 (矩阵快速幂+二分)

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 - 矩阵快速幂

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 + 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: 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

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

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

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

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