Matrix Power Series
Time Limit: 3000MS   Memory Limit: 131072K
Total Submissions: 20309   Accepted: 8524

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

题目意思:

已知n,k,m,S = A + A2 + A3 + … + Ak求:S%m

解题思路:

模板题,快速幂取模+二分优化。

补充一个关于等比序列分治的结论:

对于 
Sn=(A^1+A^2+A^3+……+A^(n-1)+A^n) mod p 
当n为偶数的时候 
s[n]=(1+A^(n/2))* (A^1+A^2+A^3+……+A^(n/2)) =(1+A^(n/2))*S[n/2] 
当n为奇数的时候 
s[n]=(1+A^((n-1)/2+1))* (A^1+A^2+A^3+……+A^(n-1)/2)+A^((n-1)/2+1] 
=(1+A^((n-1)/2+1))* S[(n-1)/2] + A^((n-1)/2+1) =(1+A^(n/2+1))*S[n/2] + A^(n/2+1)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0xfffffff
using namespace std;
const long long MAXN=;
long long n,mod;
struct Mat
{
long long m[MAXN][MAXN];
};
Mat a,per;
void init()
{
long long i,j;
for(i=; i<n; ++i)
for(j=; j<n; ++j)
{
cin>>a.m[i][j];
a.m[i][j]%=mod;
per.m[i][j]=(i==j);
}
} Mat mul(Mat A,Mat B)
{
Mat ans;long long i,j,k;
for(i=; i<n; i++)
for(j=; j<n; j++)
{
ans.m[i][j]=;
for(k=; k<n; k++)
ans.m[i][j]+=(A.m[i][k]*B.m[k][j]);
ans.m[i][j]%=mod;
}
return ans;
}
Mat power(long long k)
{
Mat p,ans=per;
p=a;
while(k)
{
if(k&)
{
ans=mul(ans,p);
--k;
}
else
{
k/=;
p=mul(p,p);
}
}
return ans;
} Mat add(Mat a,Mat b)
{
Mat c;long long i,j;
for(i=; i<n; ++i)
for(j=; j<n; ++j)
c.m[i][j]=(a.m[i][j]+b.m[i][j])%mod;
return c;
}
Mat sum(long long k)
{
if(k==) return a;
Mat temp,b;
temp=sum(k/);
if(k&)
{
b=power(k/+);
temp=add(temp,mul(temp,b));
temp=add(temp,b);
}
else
{
b=power(k/);
temp=add(temp,mul(temp,b));
}
return temp;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
long long i,j,k;
while(cin>>n>>k>>mod)
{
init();
Mat ans=sum(k);
for(i=; i<n; ++i)
{
for(j=; j<n-; ++j)
cout<<ans.m[i][j]<<" ";
cout<<ans.m[i][j]<<endl;
}
}
return ;
}

POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)的更多相关文章

  1. 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

    poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  2. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  3. 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. ...

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

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

  5. [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)

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

  6. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  7. 线性代数(矩阵乘法):POJ 3233 Matrix Power Series

    Matrix Power Series   Description Given a n × n matrix A and a positive integer k, find the sum S = ...

  8. POJ 3233 Matrix Power Series(二分等比求和)

    Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...

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

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

随机推荐

  1. idea 启动web项目

    1>Run>Edit Configurations... 2>Server>Open browser>URL>http://localhost:8888/demo2 ...

  2. Tosca : 把 inner text 放到变量里,定义变量,使用变量

    XB的是分开取 注意颜色要变成蓝色的,才可用 上面是定义 下面是使用 键盘输入变量

  3. UML建模——活动图(Activity Diagram)

    活动图概述 •活动图和交互图是UML中对系统动态方面建模的两种主要形式 •交互图强调的是对象到对象的控制流,而活动图则强调的是从活动到活动的控制流 •活动图是一种表述过程基理.业务过程以及工作流的技术 ...

  4. Android开发三步骤

    产品经理给需求,UI给图片 开发 *写布局文件 *写Java代码 测试

  5. shell编程系列13--文本处理三剑客之sed利用sed追加文件内容

    shell编程系列13--文本处理三剑客之sed利用sed追加文件内容 追加用法总结: .a 在匹配行后面追加 .i 在匹配行前面追加 .r 将文件内容追加到匹配行后面 .w 将匹配行写入指定文件 追 ...

  6. JS 数字相加出现多个小数的问题

    今天在页面上用到了js进行小数相加119.01+0.01,结果大家都知道应该是:119.02的,然而结果是119..0200000…. ,莫名其妙的,还以为是我写的程序有问题,后来查了下才知道这是ja ...

  7. Qt QLabel加载图片

    QLabel加载图片 //在对应的控件中显示图片 void qm_img::DisplayImg(cv::Mat imgParam, QLabel *labelParam) { if (!imgPar ...

  8. Spring cloud微服务安全实战-5-8实现基于session的SSO(认证服务器的session有效期)

    认证服务器 session的有效期. 也就是认证服务器上的session的有效期 生成环境下,认证服务器一定是一个集群.集群.那么session一定是要在所有的服务器之间进行共享的.最简单的方式是用S ...

  9. matlab @(x)构造匿名函数

    一起来学演化计算-matlab@(x)构造匿名函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 https://www.ilovematlab.cn/thread-81614-1 ...

  10. iphone那些事儿

    1.微信文件如何发送到钉钉? 打开微信的某文件,点击右上角三个点,选择其他应用打开,选择钉钉.即可复制文件到钉钉. 2.无限网地址 设置-通用-关于本机-无线局域网地址 锂离子电池,受气温影响,气温低 ...