妈妈呀....这简直是目前死得最惨的一次。

贴题目:

http://poj.org/problem?id=3233

Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 19128 Accepted: 8068

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
首先我在克服重重困难后解决了矩阵的问题(工商管理第一学期还不学线性代数2333333)
 
原来的代码:
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <cstdlib> #define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i>=a;i--)
#define pb push_back
#define VI vector<int>
#define QI queue<int>
#define logM(N) log10(N)/log10(M)
#define eps 1e-8 typedef long long ll; using namespace std; int n,m,k; struct node{
ll mat[][];
}h,sum; node operator * (const node &a,const node &b){
node ret;
memset(ret.mat,,sizeof(ret.mat));
rep(i,,n-){
rep(j,,n-){
rep(k,,n-){
ret.mat[i][j] += (a.mat[i][k] * b.mat[k][j])%m;
//cout<<"a.mat["<<i<<"]["<<k<<"]="<<a.mat[i][k]<<" b.mat["<<k<<"]["<<j<<"]="<<b.mat[k][j]<<endl;
//cout<<"i = "<<i<<" j = "<<j<<" ret.mat["<<i<<"]["<<j<<"]="<<ret.mat[i][j]<<endl;
}
if(ret.mat[i][j] > m) ret.mat[i][j] %= m;
}
}
return ret;
} node operator + (const node &a,const node &b){
node ret;
memset(ret.mat,,sizeof(ret.mat));
rep(i,,n-){
rep(j,,n-){
ret.mat[i][j] = a.mat[i][j] + b.mat[i][j];
if(ret.mat[i][j] > m) ret.mat[i][j] %= m;
}
}
return ret;
} void pow_mod(int x){
x--;
node a,b;
a = b = h;
while(x){
if(x&) a = a * b;
b = b * b;
x >>= ;
}
/*cout<<"========"<<endl;
rep(i,0,n-1){
rep(j,0,n-1){
printf("%d ",a.mat[i][j]);
}puts("");
}
cout<<"========"<<endl;
*/
sum = sum + a;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
while(~scanf("%d%d%d",&n,&k,&m)){
memset(sum.mat,,sizeof(sum.mat));
rep(i,,n-){
rep(j,,n-){
scanf("%I64d",&h.mat[i][j]);
}
}
rep(i,,k){
pow_mod(i);
}
rep(i,,n-){
rep(j,,n-){
if(j != n-){
printf("%I64d ",sum.mat[i][j]%m);
}
else{
printf("%I64d\n",sum.mat[i][j]%m);
}
}
}
}
return ;
}
其实在这边代码之前已经错了十多遍了。看了学姐的代码,也仔细审视了自己的代码。总的小小的零零碎碎的错误找出不少。
现在这个代码的情况就是TLE
估计里面的测试数据很大,还得优化一下,那么还可以优化的地方就是求sum这个地方。
原理直接盗用学姐给的图片:
优化之后的代码:
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <cstdlib> #define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i>=a;i--)
#define pb push_back
#define VI vector<int>
#define QI queue<int>
#define logM(N) log10(N)/log10(M)
#define eps 1e-8 typedef long long ll; using namespace std; int n,m,k; struct node{
ll mat[][];
}h,sum; node operator * (const node &a,const node &b){
node ret;
memset(ret.mat,,sizeof(ret.mat));
rep(i,,n-){
rep(j,,n-){
rep(k,,n-){
ret.mat[i][j] += (a.mat[i][k] * b.mat[k][j])%m;
}
if(ret.mat[i][j] > m) ret.mat[i][j] %= m;
}
}
return ret;
} node operator + (const node &a,const node &b){
node ret;
memset(ret.mat,,sizeof(ret.mat));
rep(i,,n-){
rep(j,,n-){
ret.mat[i][j] = a.mat[i][j] + b.mat[i][j];
if(ret.mat[i][j] > m) ret.mat[i][j] %= m;
}
}
return ret;
} node pow_mod(int x){
x--;
node a,b;
a = b = h;
while(x){
if(x&) a = a * b;
b = b * b;
x >>= ;
}
return a;
} node work(int p){
if(p == ) return h;
node ret = work(p>>);
ret = ret + ret * pow_mod(p>>);
if(p&) ret = ret + pow_mod(p);
return ret;
} int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
while(~scanf("%d%d%d",&n,&k,&m)){
memset(sum.mat,,sizeof(sum.mat));
rep(i,,n-){
rep(j,,n-){
scanf("%I64d",&h.mat[i][j]);
}
}
sum = work(k);
rep(i,,n-){
rep(j,,n-){
if(j != n-){
printf("%I64d ",sum.mat[i][j]%m);
}
else{
printf("%I64d\n",sum.mat[i][j]%m);
}
}
}
}
return ;
}

POJ 3233Matrix Power Series的更多相关文章

  1. 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:  ...

  2. POJ 3233_Matrix Power Series

    题意: 求n*n矩阵的幂和 分析: 逐个加起来时间复杂度太高,通过在矩阵中套个矩阵和,再利用矩阵快速幂,最后时间复杂度为O(n3logn) 代码: #include<cstdio> #in ...

  3. POJ 3233 Matrix Power Series (矩阵乘法)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11954   Accepted:  ...

  4. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  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 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的迹(就是主对角线上各项的 ...

  7. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  8. 线性代数(矩阵乘法):POJ 3233 Matrix Power Series

    Matrix Power Series   Description Given a n × n matrix A and a positive integer k, find the sum S = ...

  9. POJ 3233 Matrix Power Series(二分等比求和)

    Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...

随机推荐

  1. bzoj4349: 最小树形图

    最小树形图模板题…… 这种\(O(nm)\)的东西真的能考到么…… #include <bits/stdc++.h> #define N 60 #define INF 1000000000 ...

  2. PHP 文件夹操作「复制、删除、查看大小」迭代实现

    "既然递归能很好的解决,为什么还要用迭代呢"?主要的原因还是效率问题-- 递归的概念是函数调用自身,把一个复杂的问题分解成与其相似的多个子问题来解决,可以极大的减少代码量,使得程序 ...

  3. title换行

  4. [SHOI2008]堵塞的交通traffic

    我是萌萌的传送门 这题说白了就是一个支持加边和删边的图连通性维护,不过鉴于图的特殊性,可以直接线段树(听说标算就是这个--). 然而我人比较懒,不想思考怎么线段树,于是乎写了一发分治并查集,1A我真是 ...

  5. 【JS】字符串操作

    1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符 ...

  6. 内存动态分配之realloc(),malloc(),calloc()与new运算符

    1,malloc与free是C/C++的标准库函数,new/delete是C++的运算符,是C++面向对象的特征,它们都可用于申请动态内存和释放内存.2,对于非内部数据类型的对象而言,光用maloc/ ...

  7. 如何封装JS ----》JS设计模式《------ 封装与信息隐藏

    1. 封装与 信息隐藏之间的关系 实质是同一个概念的两种表达,信息隐藏式目的,二封装是借以达到目的的技术方法.封装是对象内部的数据表现形式和实现细节,要想访问封装过额对象中的数据,只有使用自己定义的操 ...

  8. 建表过程-列名&列类型&修改表B

    怎么建表? 主键 名称 重量 价格 生产日期 保质期 产地 种类                       分析:我们只要把第一行的表头建好后,这张表也就完成了.  术语:建表的过程就是声明字段过程 ...

  9. Linux posix线程库总结

    由于历史原因,2.5.x以前的linux对pthreads没有提供内核级的支持,所以在linux上的pthreads实现只能采用n:1的方式,也称为库实现. 线程的实现,经历了如下发展阶段: Linu ...

  10. 非旋treap模板

    bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...