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. Tosca 一不小心,我把那一排模块全关闭了,怎么打开

    #写在前面, 之前用的时候,学了很多,基本都忘记了,现在再重新用,啥啥都不记得了,我还是应该事无巨细的全部记下来 红线这一排我关了好多,在哪儿打开 在这打开

  2. 阿里云配置WAF的步骤

    date:2019-07-04  17:59:19 author: headsen chen 配置WAF防护策略 本页目录 操作步骤 网站接入Web应用防火墙(WAF)后,WAF以默认防护策略为其过滤 ...

  3. 处理 Bootstrap CSS 冲突

    问题: Bootstrap 是Web上最流行的CSS框架.它使创建漂亮,响应迅速的设计非常容易. 但是,有时候并不希望在整个网站上使用 Bootstrap,您只需要使用Bootstrap CSS的一部 ...

  4. shell编程系列20--文本处理三剑客之awk常用选项

    shell编程系列20--文本处理三剑客之awk常用选项 awk选项总结 选项 解释 -v 参数传递 -f 指定脚本文件 -F 指定分隔符 -V 查看awk的版本号 [root@localhost s ...

  5. shell编程系列2--字符串的处理

    shell编程系列2--字符串的处理 字符串的处理 .计算字符串的长度 方法1 ${#string} 方法2 expr length "$string" (如果string中间有空 ...

  6. Linux下文件乱码问题

    通常编码会在windows环境下进行,当把windows下的代码拷贝到linux环境时,会出现 error: “build.sh /bin/bash^M: 坏的解释器:没有那个文件或目录” 等等类似问 ...

  7. linux记录-安装zabbix监控系统

    1. 安装依赖yum -y install libcurl-devel libxml2-devel net-snmp net-snmp-devel2. 安装 nginxyum -y install n ...

  8. auto和decltype的用法总结

    一, auto 1, auto的作用     一般来说, 在把一个表达式或者函数的返回值赋给一个对象的时候, 我们必须要知道这个表达式的返回类型, 但是有的时候我们很难或者无法知道这个表达式或者函数的 ...

  9. jQuery调用WebService返回JSON数据

    相信大家都比较了解JSON格式的数据对于ajax的方便,不了解的可以从网上找一下这方面的资料来看一下,这里就不多说了,不清楚的可以在网上查一下,这里只说一下因为参数设置不当引起的取不到返回值的问题. ...

  10. __proto__和prototype的一些理解

    var Person = function(name) { this.name = name; } var p = new Person(); new操作符的操作是 var p = {} p.__pr ...