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写的,总是超时,不知道为什么,之后就改用 ...
随机推荐
- Spark学习之Spark Streaming(9)
Spark学习之Spark Streaming(9) 1. Spark Streaming允许用户使用一套和批处理非常接近的API来编写流式计算应用,这就可以大量重用批处理应用的技术甚至代码. 2. ...
- QPushButton注册事件过滤器后按钮消失
版权声明:本文为博主原创文章,转载需要注明出处. RT,代码如下: ui.btn_set->installEventFilter(this); bool MousrHoverTest::even ...
- Winform之GDI绘制验证码
主要功能:点击验证码可更换,输入验证码进行登陆 需要导入命名空间System.Drawing; 产生五位的随机字符串: 1 Random random = new Random(); //产生5个随机 ...
- UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。
题意: 给一个由n*m个正方形格子组成的矩形,其中每个格子的边都是可以走的,长度给定,规定:如果在进入该路前需要拐弯,或者走完该路需要拐弯,都是需要付出双倍距离的(每条路最多算2倍).问从起点到终点的 ...
- 怎样在nexus 中 搜索到远程maven仓库中的jar 文件
怎样在nexus 中 搜索到远程maven仓库中的jar 文件 url: http://www.oschina.net/question/95712_21999 点击Administration菜单下 ...
- 让xamarin的Entry绑定时,支持Nullable类型
xamarin.forms默认情况下,如果属性是double?类型,绑定到Entry上,是无法实现双向绑定的, 可以自定义Converter实现双向绑定 public class NullableCo ...
- D1. Toy Train (Simplified)
D1. Toy Train (Simplified) time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- RestTemplate接收HashMap变为LinkedHashMap,RestTemplate接收数据后转成json数据出现反斜杠
使用postForObject方法远程调用接口,正常会返回List<HashMap>,然而实际上却返回List<LinkedHashMap>,同时将此数据进行json转换,变成 ...
- pom.xml配置引用项目时不生效
1 在项目pom.xml配置中引用项目A,但是编译时,取提数引起是B: 2 原因是:[Java Build Path - Projects] 引用的还是老的项目B,删除该引用即可解决.
- [C语言]输入一行整数,用空格分开,回车结束。
在屏幕一行中的字符会保留在缓冲区,例如 1 2 3 4 5 6 ; i < n; i++) { scanf("%d",&cur); array[i] = cur; c ...