poj 3233 Matrix Power Series
来源:互联网 发布:亿网域名 编辑:程序博客网 时间:2024/04/29 23:22
矩阵快速幂,最后求和的时候还要采用二分的思想,递归求和,尽量减少重复计算的次数
#include<iostream>using namespace std;#include<cstring>typedef struct{ int m[35][35];}Matrax;Matrax a,per;int n,m;void init(){ for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { cin>>a.m[i][j]; a.m[i][j]%=m; per.m[i][j]=(i==j); } }}Matrax multi(Matrax a,Matrax b){ Matrax c; int i,j,p; for(i=0;i<n;i++) { for(j=0;j<n;j++) { c.m[i][j]=0; for(p=0;p<n;p++) { c.m[i][j]+=a.m[i][p]*b.m[p][j]; } c.m[i][j]%=m; } } return c;}Matrax M_add(Matrax a,Matrax b){ for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { a.m[i][j]+=b.m[i][j]; a.m[i][j]%=m; } } return a;}Matrax power(int k){ Matrax c,p,ans=per; p=a; while(k) { if(k&1) { ans=multi(ans,p); k--; } else { k>>=1; p=multi(p,p); } } return ans;}Matrax sum(int k){ if(k==1) return a; Matrax temp,b; temp=sum(k/2); if(k&1) { b=power(k/2+1); temp=M_add(temp,multi(temp,b)); temp=M_add(temp,b); } else { b=power(k/2); temp=M_add(temp,multi(temp,b)); } return temp;}int main(){ int k; while(cin>>n>>k>>m) { init(); Matrax p; p=sum(k); for(int i=0;i<n;i++) { for(int j=0;j<n-1;j++) { cout<<p.m[i][j]<<' '; } cout<<p.m[i][n-1]<<endl; } }}
0 0
- POJ 3233 Matrix Power Series
- POJ 3233Matrix Power Series
- poj 3233 Matrix Power Series
- poj 3233 Matrix Power Series
- Poj 3233 Matrix Power Series
- POJ 3233 Matrix Power Series
- poj 3233 Matrix Power Series
- poj 3233 Matrix Power Series
- POJ 3233 Matrix Power Series
- poj 3233 Matrix Power Series
- poj 3233 Matrix Power Series
- poj 3233 Matrix Power Series
- POJ 3233 Matrix Power Series
- poj 3233:Matrix Power Series
- POJ 3233 Matrix Power Series
- poj 3233 Matrix Power Series
- poj 3233 Matrix Power Series
- POJ 3233 Matrix Power Series
- 【BZOJ】【P2006】【NOI2010】【超级钢琴】【题解】【堆+ST】
- 收集循环引用
- map转json格式的返回一个数组的格式
- UVa 485 - Pascal's Triangle of Death
- 幂律分布现象-power-law
- poj 3233 Matrix Power Series
- Cocos2d-X 对static CCScene *scene();的封装方法
- windows 7 添加本地回环口
- MATLAB学习过程常用命令记录(函数)
- 短停顿收集
- 压缩感知与稀疏表示
- linux进程中常用的全局数据之环境变量
- Ubuntu 10.10更新源列表
- 什么是Firmware Engineer?