POJ 3233_Matrix Power Series
题意:
求n*n矩阵的幂和
分析:
逐个加起来时间复杂度太高,通过在矩阵中套个矩阵和,再利用矩阵快速幂,最后时间复杂度为O(n3logn)
代码:
#include<cstdio>
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N= 85;
int n;
struct Matrix
{
int row,cal;
ll m[N][N];
};
Matrix init(Matrix a, ll t)
{
for(int i = 0; i < a.row; i++)
for(int j = 0; j < a.cal; j++)
a.m[i][j] = t;
return a;
}
Matrix mul(Matrix a,Matrix b, int mod)
{
Matrix ans;
ans.row = a.row, ans.cal = b.cal;
ans = init(ans,0);
for(int i = 0; i < a.row; i++)
for(int j = 0; j < b.cal; j++)
for(int k = 0; k < a.cal; k++)
ans.m[i][j] = (ans.m[i][j] + a.m[i][k] * b.m[k][j])%mod;
return ans;
}
Matrix quick_pow(int k, Matrix A, int mod)
{
Matrix I;
I.row = 2 * n, I.cal = n;
I = init(I,0);
for(int i = 0; i < n; i++)
I.m[i][i] = 1;
int cnt = 1;
while(k){
if(k&1) I = mul(A, I, mod);
A = mul(A, A, mod);
k>>=1;
}
return I;
}
int main (void)
{
int k, mod;scanf("%d%d%d",&n,&k,&mod);
Matrix A, I;
A.row= A.cal = 2 * n;
A = init(A, 0);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
scanf("%d",&A.m[i][j]);
for(int i = n; i < 2 * n; i++){
A.m[i][i] = 1;
A.m[i][i - n] = 1;
}
Matrix res = quick_pow(k+1, A, mod);
int tmp;
for(int i = n; i < 2 * n; i++){
for(int j = 0; j < n; j++){
if(i - j == n) tmp = (res.m[i][j] - 1+mod)%mod;
else tmp = res.m[i][j];
printf("%d%c",tmp, j == n-1?'\n':' ');
}
}
return 0;
}
样例都调不出来,半天才发现自己原来的模板乘法写错了,神奇的竟然用错的模板过了两道题。。。真是连自己都不能信了,懒惰的后果,下次一定要自己重新动手!!
哦因为之前习惯写I∗Ak的形式,所以a.cal=b.cal,现在换个右乘,错误就暴露了。
POJ 3233_Matrix Power Series的更多相关文章
- POJ 3233Matrix Power Series
妈妈呀....这简直是目前死得最惨的一次. 贴题目: http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Mem ...
- POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20309 Accepted: ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- [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: ...
- 矩阵十点【两】 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(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- 线性代数(矩阵乘法):POJ 3233 Matrix Power Series
Matrix Power Series Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
随机推荐
- 2019最新Android面试题
原文链接:https://blog.csdn.net/wen_haha/article/details/88362469版权声明:本文为博主原创文章,转载请附上博文链接! 前言 金三银四到来了,找工作 ...
- Mac 为啥不显示图片尺寸,点了显示简介也不显示~???
这个问题困扰我好几天,然后今天想法子解决,我这个强迫症患者是真的难受,不能直接一目了然的,每次都要ps打开图片去看,真的好心累???? 网上98%的解决方法如下: 在 Finder 中,按快捷键 co ...
- 短视频SDK在广电系统的解决方案
锐动视频编辑SDK集手机视频拍摄和视频剪辑主要功能于一体,同时包含手机端视频配音配乐,字幕特效,滤镜,转场特效等各种功能,全方位满足开发者的需求,并可以快速植入到APP中. 手机端的摄像头录制和直播功 ...
- c++通过管道pipe获取cmd输出的字符
#include <stdio.h>#include<iostream>#include<string>using namespace std; // 描述:exe ...
- WebService 生成客户端
webservice 生成客户端 wsdl2java -encoding UTF-8 -p com.trm -d D:\happywork -client http://localhost/trm-t ...
- Jmeter之https请求
Jmeter之录制https脚本,网上介绍了好多种方法,大家自行百度. 如果手写https脚本,该如何做呢? 方法:http信息头管理器,加入User-Agent参数 案例:手写百度的搜索:哈哈 请 ...
- hibernate inverse属性
修改街道对应的区道信息: 修改后会发现程序执行了两次修改操作: 原因: 区道与街道是一对多的关系: 由于Hibernate是双向维护外键,所以当修改区道中的街道时,会修改一次外键:在修改街道中的区道时 ...
- /etc/auto.master - automounter的主映射文件
描述(DESCRIPTION) 当机器启动自动挂载器时, autofs(8) 脚本就会查寻 auto.master 这个主映射文件.文件中的每行分别指明,一个挂载点以及与对应的需要被挂载的文件系统.通 ...
- base64记载
一丶 js /** * * Base64 encode / decode * * @author haitao.tu * @date 2010-04-26 * @email tuhaitao@foxm ...
- IIS更改根目录
服务器中打开IIS管理器,选择网站,再选展开后的站点,点击右边高级设置,最后更改弹框中的物理地址即可: