题干:

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

解题报告:

根据题干找规律,发现f(n) = f(n-1) + 2 * f(n-2) +1

AC代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#define ll long long
struct Matrix {ll arr[4][4];
}unit,trans;
ll n,m,ans;
Matrix mul( Matrix a,Matrix b,ll mod) {Matrix c;for(int i = 1; i<=3; i++) {for(int j = 1; j<=3; j++) {c.arr[i][j] = 0; for(int k = 1; k<=3; k++) {
//              if(a.arr[i][k] && b.arr[k][j])c.arr[i][j] = (c.arr[i][j] + a.arr[i][k]*b.arr[k][j])%mod;}}}return c;
}
Matrix q_pow(Matrix a, ll k,ll mod) {Matrix ans;ans = unit;while(k) {if(k&1) {ans=mul(ans,a,m);}k>>=1;a=mul(a,a,m);}return ans;
}
void init() {memset(unit.arr,0,sizeof(unit.arr) );for(int i = 1; i<=3; i++) {unit.arr[i][i] = 1;trans.arr[i][1] = trans.arr[i][2] = trans.arr[i][3] = 0;}trans.arr[1][1]=trans.arr[1][3] =trans.arr[2][1]=trans.arr[3][3] = 1;trans.arr[1][2] = 2;
}
int main()
{ Matrix ans;ll sum=0;while(scanf("%lld%lld",&n,&m)!=EOF) {sum = 0;init();if(n==1) {printf("%lld\n",1%m);continue; }else if(n == 2) {printf("%lld\n",2%m);continue;}ans = q_pow(trans,n-2,m);sum = ans.arr[1][1]*2 + ans.arr[1][2]*1 + ans.arr[1][3];printf("%lld\n",sum%m);}return 0;
}

总结:

这题坑很多啊,总是巧妙的跳了进去。

1.全局变量ll的n和m,结果输入用%d这个题的Mul函数 不是i<=n了!而是i<=3即可,i<=n就错了!  此题和HDU - 5015不一样,不是矩阵是个不确定的矩阵!所以这里是小于等于3!!即

2.输出的时候n=1和n=2需要特判一下,因为你传参是n-2。即:这种地方一定要小心!你的函数是有使用条件的,这一点不仅适用于矩阵快速幂,还有很多其他的地方。

3.输出n==2的时候,别忘了也需要取模!!

4.最后的输出 也需要取模!

【HDU - 4990】 Reading comprehension (构造+矩阵快速幂)相关推荐

  1. HDU - 4990 Reading comprehension(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:给出一段程序,进行优化后提交 题目分析:其实就是找规律,大水题一个,偶尔也是需要做做水题找找自信(逃) 先将题目中的程序拿下来,跑上100项,然后拿到oeis里找一下规律 ...

  2. 又见斐波那契数列(矩阵构造+矩阵快速幂)

    //补题~~~ 链接:https://ac.nowcoder.com/acm/problem/15666 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其 ...

  3. HDU - 4686 Arc of Dream(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:给出定义: 现在依次给出n,A0,AX,AY,B0,BX,BY 求Aod的第n项对1e9+7取模后的结果 题目分析: 简单矩阵快速幂 首先化简一下: 初始矩阵:(取n=1 ...

  4. HDU 5950 Recursive sequence(矩阵快速幂)

    题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...

  5. hdu 4990 Reading comprehension

    题意:读一段程序,输出程序的输出.程序的大意是,输入n,m,一个数列an,a1=1,an=(an-1)*2+1(n为奇数),an=(an-1)*2(n为偶数).输出an mod m. 思路:这个数列奇 ...

  6. HDU 4291 A Short problem 矩阵快速幂 循环节

    题解思路: 构造矩阵,矩阵乘法计算还是t; 需要找循环节;   (注意因为是复合函数,不可以在里面取mod) 暴力跑只有可以找到g(222222224)%1e9==g(0)%1e9; 所以 g(g(n ...

  7. HDU 4990 Reading comprehension

    快速幂 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #i ...

  8. HDU - 5411 CRB and Puzzle 矩阵快速幂

    HDU - 5411 考虑直接dp会T, 用矩阵优化一下就好了. #include<bits/stdc++.h> #define LL long long #define LD long ...

  9. HDU - 5451 Best Solver(循环群+矩阵快速幂)

    题目链接:点击查看 题目大意:给定x和M,已知 求%M 题目解析:这种公式题在之前已经证明过做法,详见:点击查看 这道题与hdu4565的不同之处就是,这里的n=给的特别的大,而且M不确定,所以没法用 ...

最新文章

  1. Stream Processing:Apache Flink快照(snapshot)原理
  2. python list find_一篇文章带你了解Python爬虫常用选择器
  3. 2018-2019-1 20165236 《信息安全系统设计基础》第4周学习总结
  4. itextpdf api帮助文档_我开源了一个小工具,可以帮你轻松生成 SpringBoot API 文档...
  5. pycharm切换虚拟环境
  6. MySQL(七)关于MySQL不同版本下临键锁锁定范围不同
  7. java 指针 引用_java中的引用与c中的指针
  8. c语言,在主函数中输入一个整数,求该整数各位数字的乘积,[求助]求由键盘输入的任意两个整数的积...
  9. 利用预渲染解决优化性能问题IOS
  10. python中matplotlib出错_Python中使用matplotlib的报错问题
  11. 未定义函数或变量 'wavplay'。原因:2014a已经移除函数
  12. Android如何进行反编译
  13. 完全干净卸载 iTunes 步骤
  14. 2022年G3锅炉水处理考试模拟100题及答案
  15. 51单片机系统板/开发板原理图以及烧写方法
  16. 安卓原生页面与react-native页面相互跳转实现
  17. 阿里云服务器最新价格表(标准收费报价表)
  18. 安卓/苹果手机直播声卡方案,实现一边直播一边充电功能
  19. 浅谈 CMap 与 map
  20. 【转载】VS2019使用技巧

热门文章

  1. java 域的隐藏_Windows Server 2008R2\2012\2016使用域策略自定义隐藏指定驱动器
  2. ios信号从4g变成无服务器,苹果信号满格显示是4g却没网络
  3. php 红包算法,PHP语言:实现微信红包拆分算法
  4. Python画板画图之美
  5. 1451C. String Equality
  6. mysql数据库代码_【代码总结】MYSQL数据库的常见操作
  7. spring security:基于MongoDB的认证
  8. HEC-RAS如何修改SA/2D Connection的名称
  9. oracle 匿名段,这段匿名块看着没什么问题啊
  10. You must install 'makeinfo' on your build machine