poj 3233(矩阵快速幂+二分)
2016-04-15 19:50
344 查看
Matrix Power Series
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
Sample Output
Source
POJ Monthly--2007.06.03, Huang, Jinsong
a^1+a^2+a^3+a^4+a^5=(a^1+a^2+a^3+a^4)+a^5
(a^1+a^2+a^3+a^4)=(a^1+a^2+a^2(a^1+a^2))=(a^1+a^2)*(1+a^2)
(a^1+a^2)=a^1(1+a^1)
Time Limit: 3000MS | Memory Limit: 131072K | |
Total Submissions: 19323 | Accepted: 8152 |
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
a^1+a^2+a^3+a^4+a^5=(a^1+a^2+a^3+a^4)+a^5
(a^1+a^2+a^3+a^4)=(a^1+a^2+a^2(a^1+a^2))=(a^1+a^2)*(1+a^2)
(a^1+a^2)=a^1(1+a^1)
#include <algorithm> #include <iostream> #include <stdio.h> #include <string.h> using namespace std; int n,k,m; struct Matrix { int ma[30][30]; }p,q; Matrix Multi(Matrix a,Matrix b) { Matrix res; memset(res.ma,0,sizeof(res.ma)); for(int i=0;i<n;i++) for(int j=0;j<n;j++) for(int k=0;k<n;k++) res.ma[i][j]=(res.ma[i][j]+a.ma[i][k]*b.ma[k][j])%m; return res; } Matrix quick_pow(Matrix m,int k) { Matrix res=q; while(k) { if(k&1) res=Multi(res,m); k>>=1; m=Multi(m,m); } return res; } Matrix sum(Matrix a,Matrix b) { for(int i=0;i<n;i++) for(int k=0;k<n;k++) a.ma[i][k]=(a.ma[i][k]+b.ma[i][k])%m; return a; } Matrix recursion(Matrix m,int k) { if(k==1) return m; else if(k&1) return sum(recursion(m,k-1),quick_pow(m,k)); else return Multi(recursion(m,k/2),sum(q,quick_pow(m,k/2))); } int main() { while(~scanf("%d%d%d",&n,&k,&m)) { memset(q.ma,0,sizeof(q.ma)); for(int i=0;i<n;i++) q.ma[i][i]=1; for(int i=0;i<n;i++) for(int k=0;k<n;k++) scanf("%d",&p.ma[i][k]); Matrix res; res=recursion(p,k); for(int i=0;i<n;i++) { for(int k=0;k<n-1;k++) printf("%d ",res.ma[i][k]); printf("%d\n",res.ma[i][n-1]); } } return 0; }
相关文章推荐
- centos6环境创建局域网http方式的yum源
- 详细解析BluetoothAdapter的详细api
- 怎么用oracle数据泵导数据
- no transaction is in progress
- 时间格式化函数--strftime
- 在word中插入高亮缩进的代码
- 统计代码的行数
- UVa409_Excuses, Excuses!(小白书字符串专题)
- 最近的面试总结
- iOS小技巧 - 利用GCD创建更加准确的定时器
- KNN与KD-tree
- 读写文件:每次读入大文件里的一行、读写.CSV文件
- android使用matrix控制图片的旋转,缩放
- 欢迎使用CSDN-markdown编辑器
- Android UI组件
- 【linux】linux下安装Node.js
- mt7628 gpio模拟spi驱动si32171
- 挂断电话并清除通讯录中的记录
- 实验室相关--实现相机程序自动检测和运行功能
- 动态规划 最长公共子序列 求长度和最长字符串