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 思路:令S=I+A+A^2+...+A^N-1;
构造矩阵[S,A^N]*[1,1]
    [I, I ] [1,A]
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
typedef long long int LL;
using namespace std; int n,k,mod; struct Matrix
{
int a[*][*];
Matrix(){memset(a,,sizeof(a));}
Matrix operator* (const Matrix &p)
{
Matrix res;
for(int i=;i<*n;i++)
{
for(int j=;j<*n;j++)
{
for(int k=;k<*n;k++)
{
res.a[i][j]+=(a[i][k]*p.a[k][j]%mod);
}
res.a[i][j]%=mod;
}
}
return res;
}
}ans,base; Matrix quick_pow(Matrix base,int k)
{
Matrix res;
for(int i=;i<*n;i++)
{
res.a[i][i]=;
}
while(k)
{
if(k&) res=res*base;
base=base*base;
k>>=;
}
return res;
} void init_Matrix()
{
for(int i=;i<*n;i++)
{
ans.a[i][i]=;
}
for(int i=;i<n;i++)
{
base.a[n+i][i]=;
base.a[n+i][n+i]=;
}
} int main()
{
while(~scanf("%d%d%d",&n,&k,&mod))
{
init_Matrix();
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&base.a[i][j]);
}
}
ans=ans*quick_pow(base,k+);
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
int tmp=ans.a[n+i][j]%mod;
if(i==j) tmp=(tmp+mod-)%mod;
printf("%d%c",tmp,j==n-?'\n':' ');
}
}
}
return ;
}
 

POJ 3233 Matrix Power Series(构造矩阵求等比)的更多相关文章

  1. POJ 3233 Matrix Power Series (矩阵乘法)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11954   Accepted:  ...

  2. POJ 3233 Matrix Power Series(矩阵快速幂)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...

  3. 题解报告:poj 3233 Matrix Power Series(矩阵快速幂)

    题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, fin ...

  4. POJ 3233 Matrix Power Series 二分+矩阵乘法

    链接:http://poj.org/problem?id=3233 题意:给一个N*N的矩阵(N<=30),求S = A + A^2 + A^3 + - + A^k(k<=10^9). 思 ...

  5. Poj 3233 Matrix Power Series(矩阵二分快速幂)

    题目链接:http://poj.org/problem?id=3233 解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k ...

  6. POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)

    题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ...

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

    题目地址:http://poj.org/problem?id=3233 题意:给你一个矩阵A,让你求A+A^2+……+A^k模p的矩阵值 题解:我们知道求A^n我们可以用二分-矩阵快速幂来求,而 当k ...

  8. POJ 3233 Matrix Power Series (矩阵快速幂)

    题目链接 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A^2 + A^3 + - ...

  9. POJ - 3233 Matrix Power Series (矩阵等比二分求和)

    Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. ...

随机推荐

  1. jQuery 取选中的radio的值方法

    var val=$('input:radio[name="sex"]:checked').val(); 附三种方法都可以: $('input:radio:checked').val ...

  2. BT客户端实现 Peer协议设计

    与peer建立tcp连接后,首先发送handshake消息进行握手 handshake消息格式如下: 一个字节0x19 + 一个字符串'BitTorrent protocol' + 8 byte 保留 ...

  3. 用jsonp格式的数据进行ajax post请求变成get

    因为 dataType 是 jsonp 而不是 json jsonp不支持POST跨域,所以会自动转成GET而关于jsonp为什么不支持post请求,百度到的答案是jsonp为动态的script,没有 ...

  4. mockjs模拟前后端交互

    mockjs是用于mock数据(造假数据)的组件. mockjs官网链接为:http://mockjs.com/:mockjs官网有mockjs的源代码.API以及示例. mockjs拦截ajax请求 ...

  5. ios通知机制

  6. CentOS进不了mysql

    在tty1里输入命令:“mysql -uroot -p+密码” 可以正常进入mysql,但在tty2里就不行了, 提示:Error 1045(28000): Access denied for use ...

  7. GPS部标平台的架构设计(五)-地图服务算法库

    GPS平台,需要和各种地图打交道,需要解决以下的问题: 1.坐标偏移,这个不用多说,需要将原始坐标加偏,然后在百度地图或谷歌上显示出来,需要注意的是百度地图的加偏是偏上再偏,谷歌.高德地图等是火星坐标 ...

  8. postgresql修炼之道学习笔记(1)

    好好学习吧. 本笔记 仅作为摘要记录 前两章,主要是数据库对比和安装等. 对比,就比多说了,总是和别人比较,会显得自己身价低,呵呵. 安装也有很多文章,不多说. 第二章提到了一些简单的配置, 其在 d ...

  9. CallBack实践。

    第一:它的应用场景是什么? if you call me ,i will call back.目前我接触到两种需要回调的需求 1.系统管理平台登录,登录所需要的人员和部门数据都是在另一个基础信息系统中 ...

  10. WebForm 内置对象

    内置对象: Response对象:响应请求 Response.Write("<script>alert('添加成功!')</script>"); Respo ...