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

贴题目:

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. PHP中常见错误

    1.Notice: Undefined variable: 变量名 in 注:使用了一个没有被定义的变量 2.Parse error: syntax error, unexpected T_ELSE ...

  2. spring中InitializingBean接口使用理解

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...

  3. 腾讯开放平台web第三方登录获取信息类(包含签名)

    不清楚具体参数的可以先看下第三方登录的文档: class QQ { //$appid 你的appid //$openid 获取到的唯一的用户openid //$openkey 获取到的openkey ...

  4. Burp Suite使用详解一

    本文由阿德马翻译自国外网站,请尊重劳动成果,转载注明出处 Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描web应用程序漏洞,以暴力破解 ...

  5. BZOJ 2096: [Poi2010]Pilots

    Description 求一个最长的序列,最大值最小值之差不超过 \(k\) . Sol 单调队列. 一个队列直接上就行.. Code /******************************* ...

  6. 【sdoi2013】森林 BZOJ 3123

    Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数.第三行包含N个非负整数 ...

  7. 关于python的10个建议,比较适合新手吧.

    关于python的十个建议 http://safehammad.com/downloads/python-idioms-2014-01-16.pdf

  8. swing中JTable的使用方法

    public static void main(String[] args) { Student s1 = new Student("张三", "001", 0 ...

  9. Xcode工作区间xxxx.xcworkspace不包含xxxx.xcodeproj

    一.问题描述 项目用到cocoapods管理第三方框架,所以需要打开xxxx.xcworkspace,Pods正常显示,但xxxx.xcodeproj显示红色,不包含xxxx.xcodeproj并且无 ...

  10. node08-express

    目录:node01-创建服务器 node02-util node03-events node04-buffer node05-fs node06-path node07-http node08-exp ...