题目链接:https://vjudge.net/problem/HDU-4990

Reading comprehension

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2329    Accepted Submission(s): 954

Problem Description
Read the program below carefully then answer the question.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>

const int MAX=100000*2;
const int INF=1e9;

int main()
{
  int n,m,ans,i;
  while(scanf("%d%d",&n,&m)!=EOF)
  {
    ans=0;
    for(i=1;i<=n;i++)
    {
      if(i&1)ans=(ans*2+1)%m;
      else ans=ans*2%m;
    }
    printf("%d\n",ans);
  }
  return 0;
}

 
Input
Multi test cases,each line will contain two integers n and m. Process to end of file.
[Technical Specification]
1<=n, m <= 1000000000
 
Output
For each case,output an integer,represents the output of above program.
 
Sample Input
1 10
3 100
 
Sample Output
1
5
 
Source
 
Recommend
heyang

题解:

当n为奇数时,f[n] = 2*f[n-1]+1,f[n-1] = 2*f[n-2],所以:f[n] = f[n-1] + 2*f[n-2] + 1;

当n为偶数时,f[n] = 2*f[n-1],f[n-1] = 2*f[n-2] + 1,所以:f[n] = f[n-1] + 2*f[n-2] + 1;

综上:f[n] = f[n-1] + 2*f[n-2] + 1,构造矩阵:

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <cmath>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. const int INF = 2e9;
  15. const LL LNF = 9e18;
  16. //const int MOD = 10000007;
  17. const int MAXN = 1e6+;
  18.  
  19. LL MOD;
  20. const int Size = ;
  21. struct MA
  22. {
  23. LL mat[Size][Size];
  24. void init()
  25. {
  26. for(int i = ; i<Size; i++)
  27. for(int j = ; j<Size; j++)
  28. mat[i][j] = (i==j);
  29. }
  30. };
  31.  
  32. MA mul(MA x, MA y)
  33. {
  34. MA ret;
  35. memset(ret.mat, , sizeof(ret.mat));
  36. for(int i = ; i<Size; i++)
  37. for(int j = ; j<Size; j++)
  38. for(int k = ; k<Size; k++)
  39. ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%MOD, ret.mat[i][j] %= MOD;
  40. return ret;
  41. }
  42.  
  43. MA qpow(MA x, LL y)
  44. {
  45. MA s;
  46. s.init();
  47. while(y)
  48. {
  49. if(y&) s = mul(s, x);
  50. x = mul(x, x);
  51. y >>= ;
  52. }
  53. return s;
  54. }
  55.  
  56. MA tmp = {
  57. , , ,
  58. , , ,
  59. , ,
  60. };
  61.  
  62. int main()
  63. {
  64. LL n, m;
  65. while(scanf("%lld%lld",&n,&m)!=EOF)
  66. {
  67. MOD = m;
  68. if(n<=)
  69. {
  70. printf("%lld\n", n%MOD);
  71. continue;
  72. }
  73.  
  74. MA s = tmp;
  75. s = qpow(s, n-);
  76.  
  77. LL ans = ((2LL*s.mat[][]%MOD + s.mat[][])%MOD+s.mat[][])%MOD;
  78. printf("%lld\n", ans);
  79. }
  80. }

HDU4990 Reading comprehension —— 递推、矩阵快速幂的更多相关文章

  1. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  3. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  4. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  5. HDU6030 Happy Necklace(递推+矩阵快速幂)

    传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...

  6. 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)

    题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...

  7. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  8. [递推+矩阵快速幂]Codeforces 1117D - Magic Gems

    传送门:Educational Codeforces Round 60 – D   题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...

  9. 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)

    Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

随机推荐

  1. Codeforces Gym 100203H Highways 最小生成树

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 给你平面上若干点,生成一颗完全图,让 ...

  2. Html中的 http-equiv="X-UA-Compatible" 解释

    1.X-UA-Compatible X-UA-Compatible是自从IE8新加的一个设置,对于IE8以下的浏览器是不识别的. 通过在meta中设置X-UA-Compatible的值,可以指定网页的 ...

  3. iOS -- SKPhysicsWorld类

    SKPhysicsWorld类 继承自 NSObject 符合 NSCodingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit. ...

  4. Node之父ry发布新项目deno:下一代Node

    https://mp.weixin.qq.com/s/1LcO3EqGV2iRlZ1aIrQeqw

  5. 第四讲_图像识别之图像分类Image Classification

    第四讲_图像识别之图像分类Image Classification 目录 图片分类 性能指标:top1,top5 ILSVRC:每种任务数据集不一样 imageNet:根据WorldNet组织的图片集 ...

  6. jquery_ajax 入门实例

    序:本文通过几个小样例,简单介绍怎样使用jqueryAjax异步载入. 1. $(selector).load(url,[data],[callback]) :加载远程HTML文件代码并插入DOM中. ...

  7. python_获得列表中重复的项的索引

    a = ['b','a', 'b', 'c', 'a', 'c','d'] b=[] f=[] for i in a: c=[] for item in enumerate(a): if item[1 ...

  8. jQeury入门:遍历

    一旦用jQuery创建一个初始的包装集.你就能深度遍历刚刚选择的包装集. 遍历能够被分为三个基础元件:父级.子级,同级.jQuery为全部这些元件提供丰富易用的方法.注意每个方法都能轻易的传递给字符串 ...

  9. Irrlicht 3D Engine 笔记系列之 教程4 - Movement

    作者: i_dovelemon 日期: 2014 / 12 / 16 来源: CSDN 主题: Event Receiver, Animator, Framerate independent move ...

  10. linux基础(5)- nginx服务、nfs服务

    一.nginx服务 源码安装: yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -yls ...