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

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

Source
POJ Monthly–2007.06.03, Huang, Jinsong

当k为偶数
s(k) = (E+A^(k / 2)) * (A + A^2 + … + A ^ (k / 2))
后面部分就是s(k / 2)
k是奇数的话,后面再加一个A^k
然后就可以递归二分了

/************************************************************************* > File Name: POJ3233.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年03月10日 星期二 19时46分21秒 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int n, k, m;

struct MARTIX
{
    int mat[35][35];
}A, E;

MARTIX mul (MARTIX a, MARTIX b)
{
    MARTIX c;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            c.mat[i][j] = 0;
            for (int k = 0; k < n; ++k)
            {
                c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
                c.mat[i][j] %= m;
            }
        }
    }
    return c;
}

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

MARTIX fastpow (MARTIX ret, int cnt)
{
    MARTIX tmp;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            tmp.mat[i][j] = (i == j);
        }
    }
    while (cnt)
    {
        if (cnt & 1)
        {
            tmp = mul (tmp, ret);
        }
        cnt >>= 1;
        ret = mul (ret, ret);
    }
    return tmp;
}

MARTIX BinSearch (int k)
{
    if (k == 1)
    {
        return A;
    }
    if (k & 1)
    {
        MARTIX c = fastpow (A, k);
        MARTIX d = BinSearch (k >> 1);
        MARTIX e = fastpow (A, k >> 1);
        e = add (e, E);
        d = mul (d, e);
        c = add (c, d);
        return c;
    }
    else
    {
        MARTIX d = BinSearch (k >> 1);
        MARTIX e = fastpow (A, k >> 1);
        e = add (e, E);
        d = mul (d, e);
        return d;
    }
}

int main ()
{
    int k;
    while (~scanf("%d%d%d", &n, &k, &m))
    {
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                scanf("%d", &A.mat[i][j]);
                E.mat[i][j] = (i == j);
            }
        }
        MARTIX ans = BinSearch (k);
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                printf("%d", ans.mat[i][j]);
                if (j < n - 1)
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

你可能感兴趣的:(矩阵)