1. for(i=;i<=n;i++)
  2. {
  3. if(i&)ans=(ans*+)%m;
  4. else ans=ans*%m;
  5. }

给定n,m。让你用O(log(n))以下时间算出ans。

打表,推出 ans[i] = 2^(i-1) + f[i-2]

故 i奇数:ans[i] = 2^(i-1) + 2^(i-3) ... + 1;

  i偶数:ans[i] = 2^(i-1) + 2^(i-3) ... + 2;

故可以用等比数列求和公式。

公式涉及除法。我也没弄懂为啥不能用逆元,貌似说是啥逆元可能不存在。

所以a/b % m == a%(b*m) / b 直接搞了。

  1. #include <cstdio>
  2. #include<iostream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include<vector>
  7. typedef long long LL;
  8.  
  9. LL quick_pow(LL a, LL b, LL m)
  10. {
  11. LL ans = , base = a % m;
  12. while(b)
  13. {
  14. if (b & ) ans = (ans * base) % m;
  15. base = (base * base) % m;
  16. b >>= ;
  17. }
  18. return ans;
  19. }
  20.  
  21. int main()
  22. {
  23. LL n, m;
  24.  
  25. while(~scanf("%lld%lld", &n, &m))
  26. {
  27. LL a1 = (n % ) ? : ;
  28. LL sum = a1 * (quick_pow(, (n+)/, *m) - );
  29. LL ans = (sum % ( * m)) / ;
  30. printf("%lld\n", ans);
  31. }
  32. }

第一次学矩阵快速幂,再贴个矩阵快速幂的板子。

f[n] = f[n-1] + 2 * f[n-2] + 1,故可以构造矩阵

  1. f[n-] f[n-] f[n-] f[n]
  2. =

模板来源:https://blog.csdn.net/u012860063/article/details/39123605

  1. #include <cstdio>
  2. #include<iostream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include<vector>
  7. typedef long long LL;
  8.  
  9. struct Matrix
  10. {
  11. LL m[][];
  12. } I, A, B, T;
  13.  
  14. LL a,b,n, mod;
  15. int ssize = ;
  16.  
  17. Matrix Mul(Matrix a,Matrix b)
  18. {
  19. int i,j,k;
  20. Matrix c;
  21. for (i = ; i <= ssize; i++)
  22. for(j = ; j <= ssize; j++)
  23. {
  24. c.m[i][j]=;
  25. for(k = ; k <= ssize; k++)
  26. {
  27. c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
  28. c.m[i][j]%=mod;
  29. }
  30. }
  31.  
  32. return c;
  33. }
  34.  
  35. Matrix quickpagow(int n)
  36. {
  37. Matrix m = A, b = I;
  38. while(n)
  39. {
  40. if(n & ) b = Mul(b, m);
  41. n >>= ;
  42. m = Mul(m, m);
  43. }
  44. return b;
  45. }
  46.  
  47. int main()
  48. {
  49. while(~scanf("%lld%lld",&n, &mod))
  50. {
  51. memset(I.m,,sizeof(I.m));
  52. memset(A.m,,sizeof(A.m));
  53. memset(B.m,,sizeof(B.m));
  54.  
  55. for(int i = ; i <= ssize; i++) I.m[i][i] = ;
  56. //I是单位矩阵
  57.  
  58. B.m[][] = , B.m[][] = , B.m[][] = ;
  59. A.m[][] = ;
  60. A.m[][] = A.m[][] = A.m[][] = A.m[][] = ;
  61.  
  62. if(n == ) printf("%lld\n", % mod);
  63. else if(n == ) printf("%lld\n", % mod);
  64. else
  65. {
  66. T = quickpagow(n-);
  67. T = Mul(B, T);
  68. printf("%lld\n",T.m[][] % mod);
  69. }
  70. }
  71. }

Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)的更多相关文章

  1. Reading comprehension HDU - 4990

    Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024 ...

  2. HDU4990 Reading comprehension —— 递推、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4990 Reading comprehension Time Limit: 2000/1000 MS (Java/Others ...

  3. hdu-4990 Reading comprehension(快速幂+乘法逆元)

    题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  4. 【模拟题(电子科大MaxKU)】解题报告【树形问题】【矩阵乘法】【快速幂】【数论】

    目录: 1:一道简单题[树形问题](Bzoj 1827 奶牛大集会) 2:一道更简单题[矩阵乘法][快速幂] 3:最简单题[技巧] 话说这些题目的名字也是够了.... 题目: 1.一道简单题 时间1s ...

  5. 求幂大法,矩阵快速幂,快速幂模板题--hdu4549

    hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...

  6. hdu 4291 矩阵幂 循环节

    http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,并且矩阵的更奇妙: g(g(g(n))) mod 109  ...

  7. Cognitive Graph for Multi-Hop Reading Comprehension at Scale(ACL2019) 阅读笔记与源码解析

    论文地址为:Cognitive Graph for Multi-Hop Reading Comprehension at Scale github地址:CogQA 背景 假设你手边有一个维基百科的搜索 ...

  8. 快速幂 ,快速幂优化,矩形快速幂(java)

    快速幂形式 public static int f(int a,int b,int c){ int ans =1; int base=a; while(b!=0){ if((b&1)!=0) ...

  9. 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification

    论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification 目前,阅读理解通常会给出 ...

随机推荐

  1. git 如何生成 SSH 公钥

    1.打开你的git bash 窗口 2.进入.ssh目录:cd ~/.ssh 3.找到id_rsa.pub文件:ls 4.查看公钥:cat id_rsa.pub    或者vim id_rsa.pub ...

  2. kickstart2019 round_A B. Parcels

    思路: 利用了曼哈顿距离和切比雪夫距离之间的转化. 参考: https://blog.csdn.net/Dylan_Frank/article/details/88985444 https://www ...

  3. echarts折柱混合(图表数据与x轴对应显示)

    一天24个小时,每个小时不一定都有对应的数据,所以后台给出的数据,只有每个时间点对应的数据,比如4点,给的是112,5点的242,其他时间没有,则只显示4点,5点时候的数据,那么现在对应的时间点就是后 ...

  4. Eucalyptus-instance启动后查看运行状态

    1.前言 在eucalyptus中通过虚拟机模板,创建并启动一个虚拟机,这个时候虚拟机启动正常,但是外部一直无法访问也ping不通,正对这种情况我们如何检查排除问题呢? 两种检查问题的方法: 1).在 ...

  5. docker使用centos7系统构建oraclejdk镜像

    FROM centos:7.4.1708 MAINTAINER huqiang:2018/10/12 ENV JAVA_VERSION=8 \ JAVA_UPDATE=191 \ JAVA_BUILD ...

  6. 卸载gitlab

    一.停止gitlab sudo gitlab-ctl stop 二.卸载gitlab sudo rpm -e gitlab-ce三.查看gitlab进程 杀掉第一个守护进程 kill -9 4473 ...

  7. 虚拟机中Ubuntu安装vmtools

    1.解压vmtools文件为VMWARETO.TGZ VMtools文件一般在系统桌面,如果没有可以点击左上方的"虚拟机-安装VMware Tools"即可出现在桌面,也可以通过U ...

  8. windows下php7.1.5、mysql环境搭建

    php http://windows.php.net/download/ 如果是使用ISAPI的方式来运行PHP就必须用Thread Safe(线程安全)的版本:而用FastCGI模式运行PHP的话就 ...

  9. Win 10 Google 云端硬盘 网页证书问题导致无法登录解决办法

    操作方法 按照图示,将以下3项勾去并确定. 效果 设置以后,可成功访问:

  10. pta 编程题15 列出连通集

    其它pta数据结构编程题请参见:pta 题目 题目要求分别以深度优先搜索和广度优先搜索输出图的连通集. 广度优先搜索要用到队列,先回顾一下循环队列: struct QNode { int* Data; ...