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. leetcode 50. Pow(x, n) 、372. Super Pow

    50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...

  2. IDEA优化配置,提高启动和运行速度

    IDEA优化配置,提高启动和运行速度   参考链接:https://blog.csdn.net/riju4713/article/details/83217013,http://www.pc0359. ...

  3. 无法调用到appcode下的类

    解决方法: 右键 appp_code下的类, 点击 “属性”, 里面 [生成操作] 一项 由内容 改为 编译 即可

  4. Android studio 运行打包 Ionic 项目

    1.创建项目 ionic start myapp tabs 2.cd 到项目文件夹中 3.ionic cordova platfrom add android 执行这个命令后建议修改一下应用包名称,参 ...

  5. 阿里重磅开源在线分析诊断工具Arthas(阿尔萨斯)

    github地址: Arthas English version goes here. Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. 当你遇到以下类似问题而束手无策时,Art ...

  6. Delphi 操作SQL 插入一万条数据 三种方式速度测试

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. Laya的场景以及场景的加载

    参考: Laya项目发布详解 Laya2.0 内嵌模式.加载模式.分离模式.文件模式的场景加载创建和场景打开关闭 版本2.1.1.1 白鹭中的场景是exml制作,发布后exml代码都会打包到defau ...

  8. Laya的图文混排

    参考: Laya图文混排 Laya的图文混排教程 编辑模式F9,增加laya.html.js库 在层级窗口右键,添加一个HtmlDivElement组件 大致的原理: 1. 例如输入框的字符串是 &q ...

  9. Jrebel激活方法(转)

    本次服务长期稳定提供给各位同学使用哦!服务器地址:https://jrebel.qekang.com/{GUID}在线GUID地址:在线生成GUID如果失效刷新GUID替换就可以!打开jrebel 激 ...

  10. LODOP统计table自动分页后的每页的某列合计值

    LODOP中超文本会根据打印项高度或超过纸张,自动分页.(相关博文:Lodop打印控件 超文本自动分页.LODOP中ADD_PRINT_TABLE.HTM.HTML表格自动分页测试.Lodop打印表格 ...