POJ 3233 Matrix Power Series [矩阵快速幂]【数论】[水]
2016-09-13 17:00
525 查看
题目链接 : http://poj.org/problem?id=3233
—————————————–.
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 20930 Accepted: 8760
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 4
0 1
1 1
Sample Output
1 2
2 3
Source
——————————————.
题目大意:
不用解释了吧 就是求Sn
Sn = A + A^2 + A^3 + … + A^k.
解题思路:
这种题目一定想到矩阵快速幂
然后就是怎么构造矩阵了
[A O] 乘 [A E] 等 [A^2 S1]
[O O] 号 [O E]号 [O S0]
矩阵大致就是这么构造出来的
然后注意的事E只有主对角线是1 剩下的都是0 (Sb的我全写成E然后WA的都怀疑人生了)
附本题代码
———————————–.
—————————————–.
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 20930 Accepted: 8760
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 4
0 1
1 1
Sample Output
1 2
2 3
Source
——————————————.
题目大意:
不用解释了吧 就是求Sn
Sn = A + A^2 + A^3 + … + A^k.
解题思路:
这种题目一定想到矩阵快速幂
然后就是怎么构造矩阵了
[A O] 乘 [A E] 等 [A^2 S1]
[O O] 号 [O E]号 [O S0]
矩阵大致就是这么构造出来的
然后注意的事E只有主对角线是1 剩下的都是0 (Sb的我全写成E然后WA的都怀疑人生了)
附本题代码
———————————–.
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; using namespace std; typedef long long int LL ; #define INF 0x3f3f3f3f #define pb push_back #define lalal puts("*******"); /*************************************/ int MOD; const int M = 32*2; struct Matrix { LL m[M][M]; void display(int N) { for(int i=0; i<N; i++) { for(int j=0; j<N; j++) { if(j) printf(" "); printf("%I64d",m[i][j]); } puts(""); } } void clearI() { for(int i=0; i<M; i++) for(int j=0; j<M; j++) m[i][j]=(i==j); } void clearO() { for(int i=0; i<M; i++) for(int j=0; j<M; j++) m[i][j]=0; } }; Matrix operator * (Matrix &a,Matrix &b) { Matrix c; c.clearO(); for(int k=0; k<M; k++) for(int i=0; i<M; i++) { if(a.m[i][k]==0) continue; for(int j=0; j<M; j++) { if(b.m[k][j]==0) continue; c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%MOD; } } return c; } Matrix operator ^ (Matrix &a,LL b) { Matrix c; c.clearI(); while(b) { if(b&1) c=c*a; b>>=1; a=a*a; } return c; } int main() { LL n,k,m; while(~scanf("%I64d%I64d%I64d",&n,&k,&m)) { MOD = m; Matrix a,b; a.clearO(),b.clearO(); for(int i=0; i<n; i++) { b.m[i][n+i]=b.m[n+i][n+i]=1; for(int j=0; j<n; j++) { scanf("%I64d",&a.m[i][j]); b.m[i][j]=a.m[i][j]; } } b=b^(k); a=a*b; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(j) printf(" "); printf("%I64d",a.m[i][j+n]); } puts(""); } } return 0; }
相关文章推荐
- 数论 快速矩阵幂 POJ 3233 Matrix Power Series 二分和
- poj——3233(数论之矩阵快速幂)
- poj 3233 Matrix Power Series(矩阵快速幂)
- POJ 3233 快速矩阵乘法
- POJ 3233 Matrix Power Series (矩阵快速幂和)
- POJ 3233 && NYOJ 298 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 —— 矩阵快速幂
- poj 3233 Matrix Power Series 矩阵快速幂
- POJ 3233 Matrix Power Series(矩阵快速幂)