poj 3233 Matrix Power Series

爱被打了一巴掌 2022-05-15 00:09 216阅读 0赞

题目链接:点我
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

这个题有两种转移矩阵
详细看图:
这里写图片描述

首先第一种方法,用到矩阵快速幂。

  1. #include<stdio.h>
  2. #include<string.h>
  3. int mod,len,n;
  4. const int ssize=66;
  5. struct Matrix {
  6. int a[ssize][ssize];
  7. Matrix() {
  8. memset(a,0,sizeof(a));
  9. }
  10. void init() {
  11. for(int i=1; i<=len; i++)
  12. for(int j=1; j<=len; j++)
  13. a[i][j]=(i==j);
  14. }
  15. Matrix operator * (const Matrix &B)const {
  16. Matrix C;
  17. for(int i=1; i<=len; i++)
  18. for(int k=1; k<=len; k++)
  19. for(int j=1; j<=len; j++)
  20. C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%mod;
  21. return C;
  22. }
  23. Matrix operator ^ (const int &t)const {
  24. Matrix A=(*this),res;
  25. res.init();
  26. int p=t;
  27. while(p) {
  28. if(p&1)res=res*A;
  29. A=A*A;
  30. p>>=1;
  31. }
  32. return res;
  33. }
  34. };
  35. int main()
  36. {
  37. int i,j,k;
  38. while(scanf("%d%d%d",&n,&k,&mod)!=EOF)
  39. { len=n*2;
  40. Matrix a,b;
  41. a.init();
  42. for(i=1;i<=n;i++)
  43. for(j=1;j<=n;j++)
  44. scanf("%d",&a.a[i][j]);
  45. for(i=1;i<=n;i++)//右上部分
  46. for(j=n+1;j<=n*2;j++)
  47. if(i+n==j)
  48. a.a[i][j]=1;
  49. else
  50. a.a[i][j]=0;
  51. a=a^(k+1);
  52. for(i=1;i<=n;i++)//减去单位矩阵
  53. for(j=n+1;j<=len;j++)
  54. {
  55. if(i+n==j)
  56. a.a[i][j]--;
  57. while(a.a[i][j]<0)//为了防止溢出
  58. a.a[i][j]+=mod;
  59. }
  60. for(i=1;i<=n;i++)
  61. {
  62. for(j=n+1;j<len;j++)
  63. printf("%d ",a.a[i][j]);
  64. printf("%d\n",a.a[i][len]);
  65. }
  66. }
  67. return 0;
  68. }

接下来第二种,用到自己的矩阵快速幂板子会超时
改了下转移矩阵,过了,700ms,说明这个转移矩阵是没问题的,
没想到结构体重载运算符竟然会这么慢

  1. #include<stdio.h>
  2. #include<string.h>
  3. struct node {
  4. int p[65][65];
  5. };
  6. int mod,len;
  7. struct node suan(struct node a,struct node b) { //矩阵a乘以矩阵b
  8. int i,j,k;
  9. struct node c;
  10. for(i=1; i<=len; i++) {
  11. for(j=1; j<=len; j++) {
  12. c.p[i][j]=0;
  13. for(k=1; k<=len; k++)
  14. c.p[i][j]=(a.p[i][k]*b.p[k][j]+c.p[i][j])%mod;
  15. }
  16. }
  17. return c;
  18. }
  19. struct node haha(struct node a,struct node b,int n){
  20. while(n) { //矩阵的快速幂
  21. if(n&1)
  22. b=suan(b,a);
  23. n>>=1;
  24. a=suan(a,a);
  25. }
  26. return b;
  27. }
  28. int main(){
  29. int i,j,n,k;
  30. struct node a,b,origin;
  31. while(scanf("%d%d%d",&n,&k,&mod)!=EOF) {
  32. len=n*2;
  33. for(i=1;i<=n;i++){
  34. for(j=1;j<=n;j++){
  35. a.p[i][j]=(i==j);
  36. }
  37. }
  38. for(i=1; i<=n; i++) {
  39. for(j=n+1; j<=(n<<1); j++) {
  40. scanf("%d",&a.p[i][j]);
  41. a.p[i+n][j]=a.p[i][j];
  42. b.p[i][j-n]=a.p[i][j];
  43. b.p[i+n][j-n]=b.p[i][j-n];
  44. }
  45. }
  46. for(i=1; i<=n*2; i++) //把b变成单位矩阵
  47. for(j=1; j<=n*2; j++)
  48. origin.p[i][j]=(i==j);
  49. a=haha(a,origin,k-1);
  50. b=suan(a,b);
  51. for(i=1;i<=n;i++){
  52. for(j=1;j<=n;j++){
  53. printf("%d ",b.p[i][j]);
  54. }
  55. printf("\n");
  56. }
  57. }
  58. return 0;
  59. }

发表评论

表情:
评论列表 (有 0 条评论,216人围观)

还没有评论,来说两句吧...

相关阅读