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. 事务处理-回滚(转账操作)(转自http://www.cnblogs.com/void-m/p/6143540.html)

    JDBC事务处理-四大原则 原子性一致性隔离性持久性 第一步:实现转账操作 假设在账户中,盖伦有余额5000元,赵信有余额2000元, 盖伦要向赵信转账1000元. 1 2 3 4 5 6 7 8 9 ...

  2. pyspider 安装时 Could not run curl-config

    sudo aptitude install libcurl4-openssl-dev

  3. js获取cookie 和 模仿php的&_GET方法

    //获取get参数 function _get(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)( ...

  4. IDEA +maven+ ContextLoaderListener not find

    tomcat 启动失败:SEVERE: Context [] startup failed due to previous errors 查看pox.xml 有spring-web依赖 查看tomca ...

  5. WordPress忘记登录密码

    后台的登录密码使用的是md5加密的,有时候会忘记登录密码,那么可以修改数据库,把密码改为你知道的字符串的md5加密值 如 hello对应的md5加密值为:5d41402abc4b2a76b9719d9 ...

  6. cloudera learning2:HDFS

    存入HDFS的文件会按块(block)划分,默认每块128MB.默认1个block还有2个备份.备份增加了数据的可靠性和提高计算效率(数据本地化). HDFS部署可选择不支持HA,也可选择支持HA. ...

  7. php面试题

    1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) <?php date_default_timezone_set('Asia/shanghai'); echo d ...

  8. Issue 5:Hadoop博客系列转载

    Alex 的 Hadoop 菜鸟教程: 第1课 hadoop体系介绍 Alex 的 Hadoop 菜鸟教程: 第2课 hadoop 安装教程 (CentOS6 CDH分支 yum方式) Alex 的 ...

  9. New line

    The concepts of line feed (LF) and carriage return (CR) are closely associated, and can be either co ...

  10. 什么是Cookie对象,Session对象,Application对象。

    Cookie是: 一个由网页服务器放在您硬盘上的非常小的文本文件. 它本质上就像您的身份证明一样,并且不能像代码那样被执行或被用来散布病毒.它只能被您使用并且只能由提供的服务器读取. 使用Cookie ...