题目链接:http://poj.org/problem?id=3233

题意:给出一个公式求这个式子模m的解;

分析:本题就是给的矩阵,所以非常显然是矩阵高速幂,但有一点。本题k的值非常大。所以要用二分求和来降低执行时间。

代码:

  1. #include <set>
  2. #include <map>
  3. #include <stack>
  4. #include <queue>
  5. #include <math.h>
  6. #include <vector>
  7. #include <string>
  8. #include <utility>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <functional>
  15.  
  16. using namespace std;
  17. struct Matrax{
  18. long long m[50][50];
  19. }ter;
  20. int n,m;
  21. Matrax add(Matrax a,Matrax b){
  22. Matrax p;
  23. for(int i=0;i<n;i++){
  24. for(int j=0;j<n;j++){
  25. p.m[i][j]=a.m[i][j]+b.m[i][j];
  26. p.m[i][j]%=m;
  27. // cout<<p.m[i][j]<<" ";
  28. }
  29. // cout<<endl;
  30. }
  31. return p;
  32. }//矩阵加法
  33. Matrax muli(Matrax a,Matrax b){
  34. Matrax p;
  35. for(int i=0;i<n;i++)
  36. for(int j=0;j<n;j++){
  37. p.m[i][j]=0;
  38. for(int k=0;k<n;k++){
  39. p.m[i][j]+=a.m[i][k]*b.m[k][j];
  40. p.m[i][j]%=m;
  41. }
  42. }
  43. return p;
  44. }//矩阵乘法
  45. Matrax quick_mod(Matrax a,int b){
  46. Matrax ans=ter;
  47. while(b){
  48. if(b&1){
  49. ans=muli(ans,a);
  50. b--;
  51. }
  52. else {
  53. b>>=1;
  54. a=muli(a,a);
  55. }
  56. }
  57. return ans;
  58. }//高速幂
  59. Matrax sum(Matrax a,int k){
  60. if(k==1)return a;
  61. Matrax ans,b;
  62. ans=sum(a,k/2);
  63. if(k&1){
  64. b=quick_mod(a,k/2+1);
  65. ans=add(ans,muli(ans,b));
  66. ans=add(ans,b);
  67. }
  68. else {
  69. b=quick_mod(a,k/2);
  70. ans=add(ans,muli(ans,b));
  71. }
  72. return ans;
  73. }//二分求和
  74. int main(){
  75. int k;
  76. while(scanf("%d%d%d",&n,&k,&m)!=EOF){
  77. Matrax A,tmp;
  78. for(int i=0;i<n;i++)
  79. for(int j=0;j<n;j++){
  80. scanf("%I64d",&A.m[i][j]);
  81. ter.m[i][j]=(i==j);
  82. tmp.m[i][j]=0;
  83. }
  84. tmp=sum(A,k);
  85. for(int i=0;i<n;i++){
  86. for(int j=0;j<n;j++)
  87. cout<<tmp.m[i][j]<<" ";
  88. cout<<endl;
  89. }
  90.  
  91. }
  92. return 0;
  93. }

poj 3233(矩阵高速幂)的更多相关文章

  1. poj 3233 矩阵快速幂

    地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive i ...

  2. POJ 3233 矩阵快速幂&二分

    题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...

  3. Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板

    题目链接  请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...

  4. poj 3233 矩阵快速幂+YY

    题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...

  5. poj 2778 AC自己主动机 + 矩阵高速幂

    // poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...

  6. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  7. POJ 3613 Cow Relays (floyd + 矩阵高速幂)

    题目大意: 求刚好经过K条路的最短路 我们知道假设一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B  B[i][j]  就表示   i-j 刚好走过两条路的方法数 那么同理 我们把 ...

  8. UVA 11551 - Experienced Endeavour(矩阵高速幂)

    UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...

  9. UVA10518 - How Many Calls?(矩阵高速幂)

    UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...

随机推荐

  1. vim 将文件所有行合并到一行

      vim 将文件所有行合并到一行   在 Normal Mode下执行: ggvGJ gg 用于跳到行首 v 转换成 visual 模式 G 跳到最后一行 J 合并行

  2. No-2.注释

    01. 注释的作用 使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释) 以 # 开头,# 右边的所有东西都被当做说明文字,而不是真正要执行的程序,只起到 ...

  3. python compare with other language

    java http://dirtsimple.org/2004/12/python-is-not-java.htmlhttp://twistedmatrix.com/users/glyph/rant/ ...

  4. Android自动化测试之Monkey

    本来是做Web后端的,来公司实习变成微信小程序前端了,到这周变成Android APP测试人员了,也是微醺啊. 由于对手工测试终究是有些抵触,所有昨天小试了一下不用写代码的自动化压力测试,在此记下我的 ...

  5. [Python3网络爬虫开发实战] 4.1-使用XPath

    XPath,全称XML Path Language,即XML路径语言,它是一门在XML文档中查找信息的语言.它最初是用来搜寻XML文档的,但是它同样适用于HTML文档的搜索. 所以在做爬虫时,我们完全 ...

  6. HTTP实验:分别使用httpd-2.2和httpd-2.4实现

    1. 需求描述 1.建立httpd服务,要求: (1) 提供两个基于名称的虚拟主机: www1.stuX.com,页面文件目录为/web/vhosts/www1:错误日志为/var/log/httpd ...

  7. Python之字符串计算(计算器)

    Python之字符串计算(计算器) import re expression = '-1-2*((60+2*(-3-40.0+42425/5)*(9-2*5/3+357/553/3*99/4*2998 ...

  8. python链家网高并发异步爬虫and异步存入数据

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  9. Python面试快问快答,理论要的就是速度与精准,Python面试题No2

    今天的面试题 第1题:python2和python3的range(100)的区别 range()函数的含义 range函数是一个用来创建算数级数序列的通用函数,返回一个[start, start + ...

  10. LeetCode(60) Permutation Sequence

    题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...