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
 
题意:给出了原程序,显然,看到内涵的递推式明显可以使用矩阵快速幂了。
思路:原递推式是当n为偶数时fn=2*f(n-1)+1 奇数时fn=2*f(n-1) 找规律得到递推式为 f(n) = *f(n-1)+*f(n-2) +*1
 
(下面是没学过线代的鶸的一点理解)
其实可以把矩阵看做一个储存多数据的容器,例如这题
运算为等式右红框分别乘以左边三个红框得到的三个值
对应的就是等式左侧的f(n),f(n-1),1。
 
化简
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <string.h>
  5. #define ll __int64
  6. using namespace std;
  7. ll mod;
  8. struct matrix
  9. {
  10. ll x[][];
  11. void init()
  12. {
  13. for(int i = ; i < ; i++)
  14. for(int j = ; j < ; j++)
  15. x[i][j] = ;
  16. }
  17. };
  18. matrix mul(matrix a, matrix b)
  19. {
  20. matrix c;
  21. c.init();
  22. for( int i = ; i < ; i++)
  23. for(int j = ; j < ; j++)
  24. {
  25. for(int k = ; k < ; k++)
  26. {
  27. c.x[i][j] += a.x[i][k] * b.x[k][j];
  28. }
  29. c.x[i][j] %= mod;
  30. }
  31. return c;
  32. }
  33. matrix powe(matrix x, ll n)
  34. {
  35. matrix r;
  36. r.init();
  37. r.x[][] = r.x[][] = r.x[][] = ; //初始化
  38. while(n)
  39. {
  40. if(n & )
  41. r = mul(r , x);
  42. x = mul(x , x);
  43. n >>= ;
  44. }
  45. return r;
  46. }
  47. int main()
  48. {
  49. ll x, y, n, ans;
  50. //while(~scanf("%I64d%I64d", &n, &mod))
  51. while(cin >> n >> mod)
  52. {
  53. if(n == )
  54. printf("%I64d\n", %mod);
  55. else if(n == )
  56. printf("%I64d\n", %mod);
  57. else
  58. {
  59. matrix d;
  60. d.init();
  61. d.x[][] = ;
  62. d.x[][] = ;
  63. d.x[][] = ;
  64. d.x[][] = ;
  65. d.x[][] = ;
  66. d = powe(d, n - );
  67. ans = d.x[][] * + d.x[][] * + ; //如果使用手动乘,不知为何还要再判断
  68. matrix e;
  69. e.init();
  70. e.x[][] = ;
  71. e.x[][] = ;
  72. e.x[][] = ;
  73. d = mul(e , d);
  74. /*if( n % 2 ) //再判断
  75. ans-=2;
  76. else
  77. ans-=1;*/
  78. cout << d.x[][] % mod << endl;
  79. }
  80. }
  81. }
 
 

HDU 4990 Reading comprehension 简单矩阵快速幂的更多相关文章

  1. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  2. HDU 4990 Reading comprehension(矩阵快速幂)题解

    思路: 如图找到推导公式,然后一通乱搞就好了 要开long long,否则红橙作伴 代码: #include<set> #include<cstring> #include&l ...

  3. HDU 1575 Tr A( 简单矩阵快速幂 )

    链接:传送门 思路:简单矩阵快速幂,算完 A^k 后再求一遍主对角线上的和取个模 /********************************************************** ...

  4. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  5. hdu 1575 Tr A(矩阵快速幂,简单)

    题目 和 LightOj 1096 - nth Term  类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...

  6. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  7. hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)

    题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...

  8. hdu 4686 Arc of Dream(矩阵快速幂)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...

  9. HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...

随机推荐

  1. ASP.NET MVC5 学习系列之视图

    一.视图约定 当创建一个项目模版时,可以注意到,项目以一种非常具体的方式包含了一个结构化的Views目录.在每一个控制器的View文件夹中,每一个操作方法都有一个同名的视图文件与其对应.(约定大于配置 ...

  2. Traffic Steering for Service Function Chaining

    Introduction 目前通过vlan标签来把流量引向对应的sfc 以前的sfc静态(SFs相邻组成SFC),有了sdn之后具有动态性.(SFs不需要彼此相邻.将流量动态地导向所需的SFs.) 流 ...

  3. 福大软工1816:Beta(1/7)

    Beta 冲刺 (1/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 答辩 组织会议 复习课本 展示GitH ...

  4. webService —— soap

    package soupTest; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.Endpo ...

  5. J2EE体系

    J2EE的概念 目前,Java 2平台有3个版本,它们是适用于小型设备和智能卡的Java 2平台Micro版(Java 2 Platform Micro Edition,J2ME).适用于桌面系统的J ...

  6. rabbitmqctl 的常用命令

    # 查看服务器的状态 rabbitmqctl status   # 查看环境变量 rabbitmqctl environment   # 停止rabbitmq的应用 rabbitmqctl stop_ ...

  7. QJsonDocument实现Qt下JSON文档读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QJsonDocument实现Qt下JSON文档读写     本文地址:http://tech ...

  8. [历史百科]抗战时期兵团简介 From 百度知道

    中央军委1948年11月1日和1949年1月15日两次关于统一全军组织和部队番号的训令,我军先后进行了整编.西北野战军改称第一野战军,司令员兼政治委员彭德怀,第一副司令员张宗逊,第二副司令员赵寿山,参 ...

  9. Oracle忘记密码如何重置

    昨天安装Oracle11g R2的时候给scott用户设置密码,当时没有显示而且还只以输入一次,可能密码输入错误,结果今天用scott用户登录果然密码不对,还好sys和system用户都正常,就进去给 ...

  10. chrome调试selenium。其实我是无聊了

    from selenium import webdriverdriver = webdriver.Chrome()driver.get("http://www.baidu.com" ...