Matrix Power Series(矩阵快速幂)
来源:互联网 发布:mysql覆盖索引limit 编辑:程序博客网 时间:2024/06/05 15:12
矩阵快速幂:http://www.cnblogs.com/kuangbin/archive/2012/08/17/2643347.html
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072KTotal Submissions: 16341 Accepted: 6966
Description
Given a n × n matrix A and a positive integer k, find the sumS =A + A2 + A3 + … + Ak.
Input
The input contains exactly one test case. The first line of input contains three positive integersn (n ≤ 30),k (k ≤ 109) and m (m < 104). Then follown lines each containingn nonnegative integers below 32,768, givingA’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
题意:简单易懂;
题解:两次二分矩阵,具体看代码吧!
AC代码:
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <vector>#include <cmath>#include <queue>#include <map>#include <set>#define eps 1e-9using namespace std;typedef long long ll;typedef pair<int,int>P;const int M = 1e5 + 100;const int INF = 0x3f3f3f3f;int n,mod,k;struct Mac { int c[33][33]; //原始矩阵 void unit1() { //原始矩阵 for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { scanf("%d",&c[i][j]); } } } void unit2() { //单位矩阵 c[0][0] = c[1][1] = 1; c[1][0] = c[0][1] = 0; }};Mac tmp,sum; //原始矩阵,和求和矩阵Mac mul(Mac a,Mac b) { //矩阵相乘 Mac p; memset(p.c,0,sizeof(p.c)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(a.c[i][j]) { for(int k = 0; k < n; k++) { p.c[i][k] += (a.c[i][j] * b.c[j][k]) % mod; p.c[i][k] %= mod; } } } } return p;}Mac add(Mac a,Mac b) { //矩阵加法 for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { a.c[i][j] += b.c[i][j] % mod; a.c[i][j] %= mod; } } return a;}Mac quickmod(Mac a,int n) { //矩阵快速幂 Mac p; p.unit2(); while(n) { if(n & 1) p = mul(p,a); a = mul(a,a); n >>= 1; } return p;}Mac binary_matrix1(int k) { //二分矩阵快速幂 Mac res; if(k == 1) { return tmp; } res = binary_matrix1(k / 2); res = mul(res,res); if(k & 1) res = mul(res,tmp); return res;}Mac binary_matrix2(int k){ //二分矩阵求矩阵幂之和 A1+A2+A3...; if(k == 1){ return tmp; } Mac res = binary_matrix2(k / 2); Mac tp = binary_matrix1(k / 2); tp = mul(tp,res); sum = add(sum,tp); if(k & 1) sum = add(sum,binary_matrix1(k)); return sum;}int main() { cin>>n>>k>>mod; tmp.unit1(); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ sum.c[i][j] = tmp.c[i][j]; } } Mac res = binary_matrix2(k); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { printf(j == n - 1 ? "%d\n" : "%d ",res.c[i][j]); } } return 0;}
0 0
- 【矩阵快速幂】poj3233 Matrix Power Series
- poj Matrix Power Series (矩阵快速幂)
- poj3233 - Matrix Power Series-矩阵快速幂
- poj3233 Matrix Power Series,矩阵快速幂
- Matrix Power Series(矩阵快速幂)
- POJ3233 Matrix Power Series【矩阵快速幂】
- poj3233 Matrix Power Series(矩阵快速幂)
- 【poj3233】Matrix Power Series 矩阵+快速幂
- Matrix Power Series poj3233矩阵快速幂
- [poj3233] Matrix Power Series 矩阵快速幂
- POJ 3233 Matrix Power Series 【矩阵快速幂,矩阵加速】
- POJ 3233 Matrix Power Series 【矩阵快速幂+等比矩阵】
- Matrix Power Series(poj3233快速幂+矩阵二分幂+分治)
- Matrix Power Series(矩阵快速幂+求幂和)
- poj 3233 Matrix Power Series 矩阵快速幂
- pku 3233 Matrix Power Series(矩阵快速幂)
- poj 3233 Matrix Power Series(矩阵快速幂)
- poj 3233 Matrix Power Series(矩阵快速幂)
- ios摘要认证编程
- misc(shell)
- c语言全局变量和局部变量问题及解决汇总
- iOS中 Http 摘要(DIGEST)认证
- N皇后问题
- Matrix Power Series(矩阵快速幂)
- plsql development工具在本地没有oracle数据库情况下连接远程服务器数据库的方法
- 在微机中或者C语言中指针的功能是如何实现的?
- [LeetCode]Min Stack
- 郁泰-国信增益1号资产管理计划(保底收益105)
- poj 2449 (第K短路) A* + dij
- HTTP摘要认证
- Win2008 r2 远程桌面授权已过期的解决办法
- c# 字符a+1=b