【poj3233】Matrix Power Series 矩阵+快速幂
来源:互联网 发布:淘宝嘉年华2017 编辑:程序博客网 时间:2024/05/03 01:32
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 40 11 1
Sample Output
1 22 3
Source
POJ Monthly–2007.06.03, Huang, Jinsong
题意:给你一个n*n的矩阵A,求
其中每个元素mod m。
二分,可以像快速幂一样递归处理。
先把A想象成数字,则:
对于矩阵,重载了+和*,1为单位矩阵,就一样做了。
代码(不知道为何,很慢):
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int n,mod;struct matrix{ int num[32][32]; void init() { memset(num,0,sizeof(num)); for(int i = 1;i <= n;i ++) num[i][i] = 1; }};matrix operator *(const matrix &a,const matrix &b){ matrix ans; for(int i = 1;i <= n;i ++) { for(int j = 1;j <= n;j ++) { ans.num[i][j] = 0; for(int k = 1;k <= n;k ++) { ans.num[i][j] += a.num[i][k] * b.num[k][j]; ans.num[i][j] %= mod; } } } return ans;}matrix operator +(const matrix &a,const matrix &b){ matrix ans; for(int i = 1;i <= n;i ++) { for(int j = 1;j <= n;j ++) { ans.num[i][j] = (a.num[i][j] + b.num[i][j]) % mod; } } return ans;}matrix ksm(matrix a,int b){ matrix ans; ans.init(); while(b) { if(b & 1) ans = ans * a; a = a * a; b >>= 1; } return ans;}void print(const matrix &ans){ for(int i = 1;i <= n;i ++) { for(int j = 1;j <= n;j ++) { printf("%d ",ans.num[i][j]); } puts(""); } }matrix ask(const matrix &a,int b){ if(b == 1) return a; matrix ans; ans.init(); ans = (ans + ksm(a,b >> 1)) * ask(a,b >> 1);// print(ans); if(b & 1) return ans + ksm(a,b); else return ans;}int main(){ matrix ans; int k; while(~scanf("%d%d%d",&n,&k,&mod)) { for(int i = 1;i <= n;i ++) { for(int j = 1;j <= n;j ++) { scanf("%d",&ans.num[i][j]); } } ans = ask(ans,k); print(ans); } return 0;}
0 0
- 【矩阵快速幂】poj3233 Matrix Power Series
- poj3233 - Matrix Power Series-矩阵快速幂
- poj3233 Matrix Power Series,矩阵快速幂
- POJ3233 Matrix Power Series【矩阵快速幂】
- poj3233 Matrix Power Series(矩阵快速幂)
- 【poj3233】Matrix Power Series 矩阵+快速幂
- Matrix Power Series poj3233矩阵快速幂
- [poj3233] Matrix Power Series 矩阵快速幂
- Matrix Power Series(poj3233快速幂+矩阵二分幂+分治)
- POJ3233 - Matrix Power Series - 二分矩阵快速幂
- POJ3233---Matrix Power Series(矩阵快速幂+二分)
- POJ3233 Matrix Power Series 矩阵快速幂+二分求和
- poj3233 Matrix Power Series (矩阵快速幂+二分)
- 【poj3233】Matrix Power Series(递推+矩阵快速幂)
- 【poj3233】Matrix Power Series——矩阵快速幂
- poj3233 Matrix Power Series (二分+矩阵+快速幂)
- POJ3233---Matrix Power Series (矩阵快速幂(升级):矩阵套矩阵)
- poj3233 Matrix Power Series 矩阵快速幂 坑死我了
- Git版本恢复命令reset
- python 列表和元组
- 训练指南(白书)第二章:数学基础
- SGI Allocator内存管理(一)
- 【剑指offer】4.3举例让抽象问题具体化——面试题24:二叉搜索树的后序遍历序列
- 【poj3233】Matrix Power Series 矩阵+快速幂
- 自己开发网站全文检索系统
- 【剑指offer】4.3举例让抽象问题具体化——面试题25:二叉树中和为某一值的路径
- 1/t的傅里叶变换证明
- 常用正则表达式
- 页面加载显示进度条
- Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files defi
- 禁止Mac Safari下载zip文件后自动解压缩
- linux“管道”