POJ 3233 Matrix Power Series [矩阵快速幂]【数论】[水]

39 篇文章 0 订阅

题目链接 : http://poj.org/problem?id=3233

—————————————–.
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 20930 Accepted: 8760
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
——————————————.

题目大意:
不用解释了吧 就是求Sn
Sn = A + A^2 + A^3 + … + A^k.

解题思路:
这种题目一定想到矩阵快速幂
然后就是怎么构造矩阵了

[A O] 乘 [A E] 等 [A^2 S1]
[O O] 号 [O E]号 [O S0]

矩阵大致就是这么构造出来的

然后注意的事E只有主对角线是1 剩下的都是0 (Sb的我全写成E然后WA的都怀疑人生了)

附本题代码
———————————–.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
using namespace std;
typedef long long int LL ;
#define INF 0x3f3f3f3f
#define pb push_back

#define lalal puts("*******");
/*************************************/

int MOD;
const int M = 32*2;

struct Matrix
{
    LL m[M][M];
    void display(int N)
    {
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<N; j++)
            {
                if(j) printf(" ");
                printf("%I64d",m[i][j]);
            }
            puts("");
        }
    }
    void clearI()
    {
        for(int i=0; i<M; i++)
            for(int j=0; j<M; j++)
                m[i][j]=(i==j);
    }
    void clearO()
    {
        for(int i=0; i<M; i++)
            for(int j=0; j<M; j++)
                m[i][j]=0;
    }
};
Matrix operator * (Matrix &a,Matrix &b)
{
    Matrix c;
    c.clearO();

    for(int k=0; k<M; k++)
        for(int i=0; i<M; i++)
        {
            if(a.m[i][k]==0) continue; 
            for(int j=0; j<M; j++)
            {
                if(b.m[k][j]==0) continue; 
                c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%MOD;
            }
        }
    return c;
}

Matrix operator ^ (Matrix &a,LL b)
{
    Matrix c;
    c.clearI();
    while(b)
    {
        if(b&1) c=c*a;
        b>>=1;
        a=a*a;
    }
    return c;
}

int main()
{
    LL n,k,m;
    while(~scanf("%I64d%I64d%I64d",&n,&k,&m))
    {
        MOD = m;
        Matrix a,b;
        a.clearO(),b.clearO();
        for(int i=0; i<n; i++)
        {
            b.m[i][n+i]=b.m[n+i][n+i]=1;
            for(int j=0; j<n; j++)
            {
                scanf("%I64d",&a.m[i][j]);
                b.m[i][j]=a.m[i][j];

            }
        }

        b=b^(k);
        a=a*b;

        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(j) printf(" ");
                printf("%I64d",a.m[i][j+n]);
            }
            puts("");
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值