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

贴题目:

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. maven 配置问题

    ① 错误 Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver' 原因:pom.xml文件下载ojdbc14-10.2.0.3. ...

  2. word20161225

    Waiting for Call / 等待呼叫 wallpaper / 墙纸 WAN, wide area network / 广域网 warning level / 警告级别 Web folder ...

  3. C++11的简单线程池代码阅读

    这是一个简单的C++11实现的线程池,代码很简单. 原理就是管理一个任务队列和一个工作线程队列. 工作线程不断的从任务队列取任务,然后执行.如果没有任务就等待新任务的到来.添加新任务的时候先添加到任务 ...

  4. c coroutine

    今天看了下云风c coroutine  代码 博客,发现 coroutine 实现原理其实还比较简单,就用户态栈切换,只需要几十行汇编,特别轻量级. 具体实现 1. 创建一个coroutine: 也就 ...

  5. 高性能MySQL(四):schema陷阱

    一.schema陷阱 二.缓存表和汇总表 三.范式和反范式

  6. 【iBeacon】iBeacon前沿初探技术备忘

    iBeacon是工作在蓝牙4.0(BLE)硬件下的一种协议,属于蓝牙4.0广播协议的一种,通过该协议和一个蓝牙模块可以实现非接触的身份识别.位置检测等. How does BLE communicat ...

  7. html5 canvas 实现倒计时 功能

    function showTime(a) { var b = { id: "showtime", //canvasid x: 60, //中心点坐标 X轴; y: 60, //中心 ...

  8. ionic + cordova+angularJs 搭建的H5 App完整版总结

      为期半个月的项目实践开发,已完整告一段落,团队小组获得第一名,辛苦总算没有白费,想起有一天晚上,整个小组的人,联调到12点才从公司回去,真是心酸.这里总结一下,项目过程中遇到的问题 和感悟.哈哈, ...

  9. Vi命令备忘

    备忘 Ctrl+u:向文件首翻半屏: Ctrl+d:向文件尾翻半屏: Ctrl+f:向文件尾翻一屏: Ctrl+b:向文件首翻一屏: Esc:从编辑模式切换到命令模式: ZZ:命令模式下保存当前文件所 ...

  10. Android语录

    1. application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期.因为它是全局的单例的,所以在不同的Activity,Service中获得的对象都是同一个对象.因此在 ...