POJ 3233 Matrix Power Series
来源:互联网 发布:国家中级程序员证书 编辑:程序博客网 时间:2024/06/08 17:33
//// main.cpp// Richard//// Created by 邵金杰 on 16/7/29.// Copyright © 2016年 邵金杰. All rights reserved.//#include<cstdio>#include<cstring>using namespace std;int n,k,p;typedef struct node{ int a[32][32]; void make(){ memset(a,0,sizeof(a)); for(int i=0;i<n;i++) a[i][i]=1; }}Mat;Mat operator * (Mat a,Mat b){ Mat c; memset(c.a,0,sizeof(c.a)); for(int k=0;k<n;k++) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%p; } } } return c;}Mat operator + (Mat a,int x){ Mat b; memset(b.a,0,sizeof(b.a)); for(int i=0;i<n;i++) b.a[i][i]=x; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { a.a[i][j]=(a.a[i][j]+b.a[i][j])%p; } } return a;}Mat operator + (Mat a,Mat b){ Mat c; memset(c.a,0,sizeof(c.a)); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { c.a[i][j]=(a.a[i][j]+b.a[i][j])%p; } } return c;}Mat Multiply(Mat a,int h){ Mat result; Mat base=a; result.make(); while(h) { if(h&1) result=result*base; base=base*base; h=(h>>1); } return result;}Mat PowSumMod(Mat Matrix,int h){ if(h==1){ Mat q; q.make(); return q*Matrix; } else if(h%2==0){ return (Multiply(Matrix,h/2)+1)*PowSumMod(Matrix,h/2); } else return ((Multiply(Matrix,(h-1)/2)+1)*PowSumMod(Matrix,(h-1)/2))+Multiply(Matrix,h);}int main(){ Mat Matrix; Mat result; scanf("%d%d%d",&n,&k,&p); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%d",&Matrix.a[i][j]); Matrix.a[i][j]=(Matrix.a[i][j])%p; } } result=PowSumMod(Matrix,k); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { printf("%d ",result.a[i][j]); } printf("\n"); } return 0;}
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
- Linux进程监控
- 【php】会潮支付接口
- Hdu 5768 Lucky7
- 宏观和微观的结合---不可取代的专业能力系列(一)
- Hibernate的interceptors与events
- POJ 3233 Matrix Power Series
- HDU5768 Lucky7
- xml(pull)解析
- 8-20 Android网络请求编程 笔记
- POJ-3083-Children of the Candy Corn
- html5 canvas 绘制曲线图
- 相机使用
- js中操作json,json对象和json字符串的转换与区别
- 让Windows下的Tomcat将控制台信息记录到日志