Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
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
给定矩阵A,求A + A^2 + A^3 + … + A^k的结果
/*
矩阵乘法经典题.
一开始并没有想出来orz.
发现正解好神奇.
这种题就应该先推出递推式子再构造矩阵.
本来还想矩阵套矩阵来着
弱啊.
学习了一下单位矩阵的用法.
题解:http://www.cnblogs.com/justPassBy/p/4448630.html
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 61
#define LL long long
using namespace std;
LL n,m,k,ans[MAXN][MAXN],b[MAXN][MAXN],c[MAXN][MAXN];
LL mul(LL x,LL y)
{
LL tot=0;
while(y)
{
if(y&1) tot=(tot+x)%m;
x=(x+x)%m;
y>>=1;
}
return tot;
}
void mi()
{
while(k)
{
if(k&1)
{
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
for(int k=1;k<=n*2;k++)
c[i][j]=(c[i][j]+ans[i][k]*b[k][j]%m)%m;
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
ans[i][j]=c[i][j],c[i][j]=0;
}
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
for(int k=1;k<=n*2;k++)
c[i][j]=(c[i][j]+b[i][k]*b[k][j]%m)%m;
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
b[i][j]=c[i][j],c[i][j]=0;
k>>=1;
}
}
void slove()
{
k--;
mi();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<ans[i][j]<<" ";
printf("\n");
}
}
void Clear()
{
memset(b,0,sizeof b);
memset(ans,0,sizeof ans);
}
int main()
{
while(~scanf("%lld%lld%lld",&n,&k,&m))
{
Clear();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>b[i][j];b[i][j]%=m;
ans[i][j]=ans[i][n+j]=b[i][j];
}
b[n+i][i]=b[n+i][n+i]=1;
}
slove();
}
return 0;
}
Poj 3233 Matrix Power Series(矩阵乘法)的更多相关文章
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- poj 3233 Matrix Power Series 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- POJ 3233 Matrix Power Series 矩阵快速幂
设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- POJ 3233 Matrix Power Series(矩阵等比求和)
题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...
- 矩阵十点【两】 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的迹(就是主对角线上各项的 ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
随机推荐
- appium实例1:启动淘宝app
1.在android-sdk里面双击SDK-manager,下载buidl-tools 2.勾选build-tools,随便选一个版本,我这里选的是24的版本 3.下载完成后,在D:\androi ...
- (二)Django自定义标签
1.创建自定义标签 在项目的APP中新建一个Python Package-->blog_tags.py 代码: from django import template from ..models ...
- Zookeeper学习笔记(三)——java客户端代码操作
Zookeeper客户端java代码操作 上篇博客记录了shell命令操作zookeeper集群的方式,这次尝试采用java代码来操作.通过查阅API,发现并不困难. 1. 首先获得客户端与服务器的连 ...
- JAVA线程Disruptor核心链路应用(八)
import java.util.concurrent.atomic.AtomicInteger; /** * Disruptor中的 Event * @author Alienware * */ p ...
- 【洛谷 P4248】 [AHOI2013]差异(后缀自动机)
题目链接 \[ans=\sum_{1<=i<j<=n}len(T_i)+len(T_j)-2*lcp(T_i,T_j)\] 观察这个式子可以发现,前面两个\(len\)是常数,后面的 ...
- 【洛谷 P3041】 [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机,dp)
题目链接 手写一下AC自动机(我可没说我之前不是手写的) Trie上dp,每个点的贡献加上所有是他后缀的串的贡献,也就是这个点到根的fail链的和. #include <cstdio> # ...
- js 一些有意思的小Demo
- js原型,原型链
先铺垫下原型规则: 1.所有的引用类型(数组,对象,函数)都具有对象特性,可自由扩展属性(出了null外) 2.所有的引用类型(数组,对象,函数)都有一个__proto__属性(隐式原型),属性值是一 ...
- Jmeter学习笔记(十二)——断言
一.断言简介 jmeter中有个元件叫做断言(Assertion).用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. 使用断言的目的:在request的返回层面 ...
- 如何基于Restful ABAP Programming模型开发并部署一个支持增删改查的Fiori应用
Jerry之前的文章30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用 发布之后,有朋友问我,"没错, 我是在你的文章里看到了Fiori应用的 ...