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

贴题目:

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. Markdown 语法简要介绍

    =================MarkDown================= Markdown 是一种方便记忆.书写的纯文本标记语言,用户可以使用这些标记符号以最小的输入代价生成极富表现力的文 ...

  2. Integer与int的区别

    简述:int与Integer的区别: 对于它们,我们可能只是知道简单的区别.Integer是int的一个封装类,int的初始值为0,而Integer的初始值为null.但是他们之间真的仅仅只有这些区别 ...

  3. js 对数据转换成数据容量单位

    function bytesToSize(value) { alert(value); alert('value'); debugger; if (value === 0) return '0 B'; ...

  4. 用遗传算法GA改进CloudSim自带的资源调度策略

    首先理解云计算里,资源调度的含义: 看了很多云计算资源调度和任务调度方面的论文,发现很多情况下这两者的意义是相同的,不知道这两者是同一件事的不同表述还是我没分清吧,任务调度或者资源调度大概就是讲这样一 ...

  5. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  6. 设计模式--外观模式Facade(结构型)

    一.外观模式 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观模式定义了一个高层接口,让子系统更容易被使用. 二.UML图 三.例子 举个编译器的例子,假设编译一个程序需要经过四个步骤: ...

  7. URAL 1827 Indigenous Wars(排序、乱搞)

    题意:给一个长度为n数组{a[i]}.有m个操作Ti,Si,Li表示找以Ti值结束,以Si值开始,长度为Li的连续子串.找到后,将区间的答案值设为1.一开始答案值全部为0.最后输出n个答案值. 好久没 ...

  8. Linux 添加完硬盘后,如何挂载和分区、以及其他的分区不足,如何从新的硬盘上挂载借用

    挂载好新硬盘后输入fdisk -l命令看当前磁盘信息 可以看到除了当前的第一块硬盘外还有一块sdb的第二块硬盘,然后用fdisk /dev/sdb 进行分区 进入fdisk命令,输入h可以看到该命令的 ...

  9. 461. Hamming Distance and 477. Total Hamming Distance in Python

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  10. 【纯css】响应式图片列表

    示例演示 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...