矩阵乘法是可以分块的,而且幂的和也是具有线性的。

不难得到 Si = Si-1+A*Ai-1,Ai = A*Ai-1。然后矩阵快速幂就可以了。

  1. /*********************************************************
  2. * ------------------ *
  3. * author AbyssalFish *
  4. **********************************************************/
  5. #include<cstdio>
  6. #include<iostream>
  7. #include<string>
  8. #include<cstring>
  9. #include<queue>
  10. #include<vector>
  11. #include<stack>
  12. #include<vector>
  13. #include<map>
  14. #include<set>
  15. #include<algorithm>
  16. #include<cmath>
  17. #include<ctime>
  18. using namespace std;
  19.  
  20. typedef long long ll;
  21. typedef vector<int> row;
  22. typedef vector<row> mat;
  23.  
  24. int n, k, M;
  25.  
  26. mat Mul;
  27. mat &operator *(mat &A, mat& B)
  28. {
  29. mat &R = Mul;
  30. R.assign(n,row(n));
  31. for(int i = ; i < n; i++){
  32. for(int j = ; j < n; j++){
  33. for(int k = ; k < n; k++){
  34. R[i][j] = (R[i][j] +A[i][k]*B[k][j])%M;
  35.  
  36. }
  37. }
  38. }
  39.  
  40. return R;
  41. }
  42.  
  43. //#define LOCAL
  44. #ifdef LOCAL
  45. void censor(mat &B)
  46. {
  47. for(auto r: B){
  48. for(int c: r)
  49. cout<<c<<' ';
  50. cout<<endl;
  51. }
  52. }
  53. #endif
  54.  
  55. mat operator ^(mat A,int q)
  56. {
  57. mat Re(n,row(n));
  58. for(int i = ; i < n; i++) Re[i][i] = ;
  59. while(q){
  60. if(q&) Re = Re*A;
  61. A = A*A;
  62. q >>= ;
  63. }
  64. return Re;
  65. }
  66.  
  67. int main()
  68. {
  69. #ifdef LOCAL
  70. freopen("in.txt","r",stdin);
  71. #endif
  72. int nn; scanf("%d%d%d",&nn,&k,&M);
  73. n = *nn;
  74. mat A(nn,row(nn));
  75. for(int i = ; i < nn; i++){
  76. for(int j = ; j < nn; j++){
  77. scanf("%d",&A[i][j]);
  78. }
  79. }
  80. mat B(n,row(n));
  81. for(int i = ; i < nn; i++) {
  82. B[i][i] = ;
  83. copy(A[i].begin(),A[i].end(),B[i].begin()+nn);
  84. copy(A[i].begin(),A[i].end(),B[i+nn].begin()+nn);
  85. }
  86. B = B^k;
  87. for(int i = ; i < nn; i++){
  88. for(int j = ; j < nn; j++){
  89. printf("%d%c",B[i][j+nn],j==nn-?'\n':' ');
  90. }
  91. }
  92. #ifdef LOCAL
  93. cout<<"rum time:"<<clock()<<"ms"<<endl;
  94. #endif // LOCAL
  95. return ;
  96. }

POJ 3233 Matrix Power Series (矩阵分块,递推)的更多相关文章

  1. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  2. poj 3233 Matrix Power Series(矩阵二分,高速幂)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted:  ...

  3. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

  4. POJ 3233 Matrix Power Series 矩阵快速幂

    设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...

  5. poj 3233 Matrix Power Series 矩阵求和

    http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...

  6. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  7. POJ 3233 Matrix Power Series(矩阵等比求和)

    题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...

  8. 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

    poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  9. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  10. POJ 3233 Matrix Power Series (矩阵乘法)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11954   Accepted:  ...

随机推荐

  1. Codeforces - 102222A - Maximum Element In A Stack - 模拟

    https://codeforc.es/gym/102222/problem/F 注意到其实用unsigned long long不会溢出. #include<bits/stdc++.h> ...

  2. Ocelot(三)- 服务发现

    Ocelot(三)- 服务发现 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10907856.html 源码地址:https ...

  3. c++第四次实验2

    Part 1 车辆基本信息管理 1.代码 #include<iostream> using namespace std; #include"car.h" #includ ...

  4. 文件上传Django

    当Django在处理文件上传的时候,文件数据被保存在request.FILES FILES中的每个键为<input type="file" name="" ...

  5. Linux学习常用命令大全

    Linux知识大全 转载须说明出处,整理不易 一.常用的linux命令 1.2 ls 命令说明 1.3 ls 通配符的使用 2.切换目录cd命令 3.创建和删除文件操作 4.移动和拷贝文件 4.3.m ...

  6. 使用HTTP协议访问网络(Android)

    在做项目的过程中需要连接服务器访问数据,还没有接触过Android网络编程方面,参考了<Android第一行代码>,在做的过程中遇到了很多的问题,这里就此记录一下. 先给出访问网络的代码: ...

  7. thinkphp5使用querylist采集图片示例

    首先composer引入querylist composer require jaeger/querylist 注意需要php7.0以上版本 <?php namespace app\index\ ...

  8. HTTP/TCP协议基础

    HTTP协议 基本概念 HTTP协议(超文本传输协议 HyperText Transfer Protocol):是用于从WWW服务器传输超文本到本地浏览器的传送协议.它不仅保证计算机正确快速地传输超文 ...

  9. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

  10. 记录一个直接操作mediawiki数据库遇到的坑

    我的mediawiki使用的是postgresql数据库,当你进入到mediawiki数据库时,运行sql select * from pg_tables; 你会发现mediawiki的数据表的sch ...