Problem Description

  A sequence Sn is defined as:

Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.

  You, a top coder, say: So easy!

Input

  There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.

Output

  For each the case, output an integer Sn.

Sample Input

2 3 1 2013 2 3 2 2013 2 2 1 2013

Sample Output

4 14 4

Source

2013 ACM-ICPC长沙赛区全国邀请赛——题目重现

Recommend

zhoujiaqi2010 | We have carefully selected several similar problems for you: 5185 5184 5181 5180 5177

(a+sqrt(b))^n最后的形式一定形如

X+Y * sqrt(b)

n范围非常大,须要用矩阵来加速

推出转移矩阵为

a 1

b a

(a+sqrt(b))^n = X+Y * sqrt(b)

(a-sqrt(b))^n = X-Y * sqrt(b)

a - 1< sqrt(b) < a

所以z = (a-sqrt(b))^n 大于0 小于1

设ans = (a+sqrt(b))^n

ans+z = 2 * X

2 * X - 1 < ans = 2 * X - z < 2 * X

那么向上取整就是2 * X

  1. /*************************************************************************
  2. > File Name: hdu4565.cpp
  3. > Author: ALex
  4. > Mail: zchao1995@gmail.com
  5. > Created Time: 2015年03月14日 星期六 11时01分21秒
  6. ************************************************************************/
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <stack>
  11. #include <vector>
  12. #include <cmath>
  13. #include <cstdio>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <iostream>
  17. #include <algorithm>
  18. using namespace std;
  19. const double pi = acos(-1.0);
  20. const int inf = 0x3f3f3f3f;
  21. const double eps = 1e-15;
  22. typedef long long LL;
  23. typedef pair <int, int> PLL;
  24. LL mod;
  25. class MARTIX
  26. {
  27. public:
  28. LL mat[5][5];
  29. MARTIX();
  30. MARTIX operator * (const MARTIX &b)const;
  31. MARTIX& operator = (const MARTIX &b);
  32. };
  33. MARTIX::MARTIX()
  34. {
  35. memset (mat, 0, sizeof(mat));
  36. }
  37. MARTIX MARTIX :: operator * (const MARTIX &b)const
  38. {
  39. MARTIX ret;
  40. for (int i = 0; i < 2; ++i)
  41. {
  42. for (int j = 0; j < 2; ++j)
  43. {
  44. for (int k = 0; k < 2; ++k)
  45. {
  46. ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
  47. ret.mat[i][j] %= mod;
  48. }
  49. }
  50. }
  51. return ret;
  52. }
  53. MARTIX& MARTIX :: operator = (const MARTIX &b)
  54. {
  55. for (int i = 0; i < 2; ++i)
  56. {
  57. for (int j = 0; j < 2; ++j)
  58. {
  59. this -> mat[i][j] = b.mat[i][j];
  60. }
  61. }
  62. return *this;
  63. }
  64. MARTIX fastpow(MARTIX ret, LL n)
  65. {
  66. MARTIX ans;
  67. for (int i = 0; i < 2; ++i)
  68. {
  69. ans.mat[i][i] = 1;
  70. }
  71. while (n)
  72. {
  73. if (n & 1)
  74. {
  75. ans = ans * ret;
  76. }
  77. ret = ret * ret;
  78. n >>= 1;
  79. }
  80. return ans;
  81. }
  82. void Debug(MARTIX A)
  83. {
  84. for (int i = 0; i < 2; ++i)
  85. {
  86. for (int j = 0; j < 2; ++j)
  87. {
  88. printf("%lld ", A.mat[i][j]);
  89. }
  90. printf("\n");
  91. }
  92. }
  93. int main ()
  94. {
  95. LL a, b, n;
  96. while (~scanf("%lld%lld%lld%lld", &a, &b, &n, &mod))
  97. {
  98. MARTIX A;
  99. A.mat[0][0] = a;
  100. A.mat[0][1] = 1;
  101. A.mat[1][0] = b;
  102. A.mat[1][1] = a;
  103. A = fastpow(A, n - 1);
  104. MARTIX F;
  105. F.mat[0][0] = a;
  106. F.mat[0][1] = 1;
  107. F = F * A;
  108. printf("%lld\n", (2 * F.mat[0][0]) % mod);
  109. }
  110. return 0;
  111. }

hdu4565---So Easy!(矩阵)的更多相关文章

  1. hdu4565 So Easy! 矩阵快速幂

    A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example ...

  2. HDU4565 So Easy! 矩阵高速幂外加数学

    easy 个屁啊,一点都不easy,题目就是要求公式的值,但是要求公式在最后的取模前的值向上取整.再取模,无脑的先试了高速幂 double  fmod来做,结果发现是有问题的.这题要做肯定得凑整数,凑 ...

  3. HDU4565 So Easy! —— 共轭构造、二阶递推数列、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4565 So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  4. HDU4565 So Easy!

    /* HDU4565 So Easy! http://acm.hdu.edu.cn/showproblem.php?pid=4565 数论 快速幂 矩阵快速幂 题意:求[(a+sqrt(b))^n ] ...

  5. hdu4565 So Easy!(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题解:(a+√b)^n=xn+yn*√b,(a-√b)^n=xn-yn*√b, (a+√b)^n ...

  6. 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. HDU 4565 So Easy(矩阵解公式)

    So Easy [题目链接]So Easy [题目类型]矩阵解公式 &题解: 感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的. 所以最后2Xn就是答案 [时间复杂度]\(O ...

  8. HDU 4565 So Easy!(矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题意: 题意: #include <iostream>#include <cs ...

  9. HDU 4565 So Easy! 矩阵快速幂

    题意: 求\(S_n=\left \lceil (a+\sqrt{b})^n \right \rceil mod \, m\)的值. 分析: 设\((a+\sqrt{b})^n=A_n+B_n \sq ...

随机推荐

  1. thinkphp5项目--个人博客(一)

    thinkphp5项目--个人博客(一) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...

  2. 44.AngularJS Bootstrap

    转自:https://www.cnblogs.com/best/tag/Angular/ Bootstrap 你可以在你的 AngularJS 应用中加入 Twitter Bootstrap,你可以在 ...

  3. django 笔记9 分页知识整理

    感谢老男孩 自定义分页 XSS:攻击 默认字符串返回 {{page_str|safe}} 前端 from django.utils.safestring import mark_safe page_s ...

  4. Word或Excel里画柱状图和折线图组合体

    不多说,直接上干货! 最近,在帮导师,干此项目.其中需要 现在,我带你来一步一步地画出来. 第一步:插入  ->  图表 第二步: 第三步:弹出,默认的数据和图表. 第四步: 第五步: 第六步: ...

  5. HDFS的底层原理

  6. Android 两步搞定Fragment的返回键

    Fragment可以说是在Android开发必需要使用到技术,项目中的界面基本上都是使用Fragment来实现,而Activity只是作为Fragment的载体,但有些特殊情况下Fragment也不得 ...

  7. <Sicily>Fibonacci

    一.题目描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn-1 + Fn-2 for n ≥ 2. For exampl ...

  8. linux 下 .sh 文件语法

    转自:http://blog.sina.com.cn/s/blog_54f82cc201010hfz.html 介绍: 1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh ...

  9. [笔记-统计学习方法]感知机模型(perceptron) 原理与实现

    前几天认把感知机这一章读完了,顺带做了点笔记 现在把笔记做第三次的整理 (不得不说博客园的LaTex公式和markdown排版真的不太舒服,该考虑在服务器上建一个博客了) 零.总结 适用于具有线性可分 ...

  10. python_webApp

    提高开发效率:当更改代码后,不重启服务器就能使用新效果 参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df ...