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写的,总是超时,不知道为什么,之后就改用 ...
随机推荐
- C# .net实现下载,带进度条
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- Webform 内置对象(Response对象、Request对象,QueryString)
Response对象:响应请求 Response.Write("<script>alert('添加成功!')</script>"); Response.Re ...
- MYSQL5.7 忘记ROOT密码/初始化ROOT密码
编辑my.cnf允许空密码登录 [root@7Core ~]# vi /etc/my.cnf #在[mysqld]下加入一行 skip-grant-tables=1 重新启动Mysql服务 [root ...
- JavaScript实现JQuery的功能
- C#过时方法标记
1.当遇到过时或废弃的方式 函数怎么办 [Obsolete]特性解决你的困惑 1.1:当方法已经完成相关兼容 可以保留时
- ViewPager讲解以及ViewFlipper
1.加入ViewPager最好导入<android.support.v4.view.ViewPager>兼容低版本 2.将布局转换为View的方法 3.适配器类型 PagerAdapter ...
- Quartz2D知识点聚合案例
Quartz2D知识点聚合 基本 //画图片 UIImage *image = [UIImage imageNamed:@"阿狸头像"]; [image drawInRect:re ...
- 合并百度影音的离线数据 with python 2.1 bdv格式的更新
最近百度影音的离线下载文件,格式有新变化. 经过分析,是bdv格式又有新格式,从最初的bdv0001,到bdv.config 的file....,这次更新的格式是直接包含一个片段,其中还有使用guid ...
- 迅为iTOP-4418开发板串口虚拟控制台配置为普通串口
如何将 iTOP-4418 的串口控制台配置为普通串口.在最终的产品中,如果需要额外的串口,可以将调试用的控制台串口作为普通串口使用,因为交付给最终用户手中的产品,一般不需要调试串口.屏蔽串口控制台需 ...
- vsphere中的linux虚拟机安装vmware-tools
先在vcenter中选中虚拟机点击安装这个工具,如图 然后这台linux虚拟机的控制台操作,挂载先建立挂载目录 cd /mnt #在挂载建一个用来挂载的文件. mkdir cdrom 使用mount命 ...