POJ 3233Matrix Power Series
妈妈呀....这简直是目前死得最惨的一次。
贴题目:
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
- #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 ;
- }
- #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的更多相关文章
- 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
题意: 求n*n矩阵的幂和 分析: 逐个加起来时间复杂度太高,通过在矩阵中套个矩阵和,再利用矩阵快速幂,最后时间复杂度为O(n3logn) 代码: #include<cstdio> #in ...
- 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写的,总是超时,不知道为什么,之后就改用 ...
随机推荐
- MVC开发模式下的用户角色权限控制
前提: MVC开发模式 大概思想: 1.在MVC开发模式下,每个功能都对应着不同的控制器或操作方法名(如修改密码功能可能对应着User/changepd),把每个功能对应的控制器名和操作方法名存到数据 ...
- title换行
- Docker - 入门
术语 1. 镜像(image)与容器(container) 镜像是指文件系统快照或tar包. 容器是指镜像的运行态(时) 2.宿主机管理 设置/配置一台物理服务器或虚拟机,以便用于运行Docker容器 ...
- 额。。万恶之源就是c
http://blog.csdn.net/feeltouch/article/details/45155529
- excel链接sharepoint 用于 Excel 的 Microsoft Power Query
https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=39379
- iOS 编译时的警告导致无法通过编译
今天编译react native的代码,发现了2个警告,但是系统却当做错误,不能编译成功,查看了一下编译选项,看到了如下配置: 注意到这个-Werror 了吗? 就是这个标志导致系统把所有的 警告都当 ...
- 在VisualStudio 编辑器文本替换中使用正则表达式
替换时使用正则表达式,其优点在于可以通过正则分组捕获,并在替换字符串中使用. 在VS2012之前的版本中,捕获内容用{}包含,引用时,使用 \1 形式: 在VS2012及以后版本中,捕获内容符合正则表 ...
- ios语音识别
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000; min-height: 15.0px } p.p ...
- 在ubuntu下安装google chrome
由于手上有两台电脑,再加上我那个选择困难症加上纠结劲.所以果断把其中一台电脑只装linux系统,另一台电脑只装windows了.免得我老纠结!于是linux便选择了ubuntu. 由于浏览器一直用的是 ...
- Redis之清除所有缓存
方法: /// <summary> /// 清除redis所有缓存 /// </summary> /// <param name="redisUrl" ...