POJ3233
来源:互联网 发布:权重轮询调度算法 编辑:程序博客网 时间:2024/06/05 14:10
1.题目描述:
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
[Submit] [Go Back] [Status] [Discuss]
2.题意概述:
题意是给定n*n矩阵A和整数k,要求一个矩阵S=A+A^2+...+A^k,输出S的每一个元素
3.解题思路:
很显然S[i]应该有这样的关系:
S[i+1]=S[i]+A^(i+1)
而对于A^(i+1),又有
A^(i+1)=A^(i) * A
如果构造这样的矩阵,其中矩阵每一个元素也都是矩阵:
S[i] A^(i+1) A
0 0 0 (其中0指的是零矩阵)
0 0 0
注意到这样的矩阵可以乘上一个矩阵而实现进行一步变换的目的,即
S[i] A^(i+1) A I 0 0 S[i+1] A^(i+2) A
0 0 0 x I A 0 = 0 0 0
0 0 0 0 0 I 0 0 0
(其中I指的是单位矩阵)
很显然这样由S[1]构造出一开始的矩阵然后连乘k-1次可以得到带有S[k]的矩阵。
然后因为中间乘上的k-1个矩阵完全相同,因此可以用快速幂加速。
4.AC代码:#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>#include <ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define N 30#define pi acos(-1.0)int MOD, n;struct Matrix {int mat[N][N];Matrix operator*(const Matrix& m)const {Matrix tmp;for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {tmp.mat[i][j] = 0;for (int k = 0; k < n; k++)tmp.mat[i][j] += mat[i][k] * m.mat[k][j] % MOD;tmp.mat[i][j] %= MOD;}return tmp;}Matrix operator+(const Matrix& m)const {Matrix tmp;for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)tmp.mat[i][j] = (mat[i][j] + m.mat[i][j]) % MOD;return tmp;}};Matrix Pow(Matrix m, int t){Matrix ans;memset(ans.mat, 0, sizeof(ans.mat));for (int i = 0; i < n; i++)ans.mat[i][i] = 1;while (t){if (t & 1)ans = ans*m;t >>= 1;m = m*m;}return ans;}Matrix solve(Matrix m, int t) {Matrix A;memset(A.mat, 0, sizeof(A.mat));for (int i = 0; i < n; i++)A.mat[i][i] = 1;if (t == 1)return m;if (t & 1)return (Pow(m, t >> 1) + A)*solve(m, t >> 1) + Pow(m, t);elsereturn (Pow(m, t >> 1) + A)*solve(m, t >> 1);}int main(){/*#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endif*/int k;Matrix m, ans;while (scanf("%d%d%d", &n, &k, &MOD) != EOF){for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)scanf("%d", &m.mat[i][j]);ans = solve(m, k);for (int i = 0; i < n; i++){for (int j = 0; j < n; j++)if (j == 0)printf("%d", ans.mat[i][j]%MOD);elseprintf(" %d", ans.mat[i][j]%MOD);puts("");}}return 0;}
- POJ3233
- poj3233
- poj3233
- POJ3233
- POJ3233
- poj3233
- POJ3233矩阵快速幂
- poj3233 - Matrix Power Series
- POJ3233 Matrix Power Series
- POJ3233 矩阵幂求和
- poj3233——矩阵
- poj3233 Matrix Power Series
- 矩阵快速幂--poj3233
- poj3233-矩阵构造
- POJ3233 Matrix Power Series
- poj3233 hdu 1588 (矩阵)
- 快速矩阵幂POJ3233
- POJ3233解题报告
- STL--PAT.1039.Course List for Student [map/set/string/vector]
- 过得了自己这关的,就一定能过得了客户那关——龙睛舫加盟品牌全案策划案例分享
- Gephi+Inkscape图论画图
- Gradle中使用Tomcat插件
- [kuangbin带你飞]专题五 【并查集】 【--完结--】
- POJ3233
- HDU1159 Common Subsequence
- 设计模式之责任链
- c++中创建一个指针指向一个一维数组、二维数组的方法
- Java中的Comparable与Comparator
- 挂在光盘出现写保护mount: block device /dev/sr0 is write-protected, mounting read-only
- log4j.properties 详解与配置步骤
- javascript:void(0)的作用
- 进程和线程