1. /*
  2. HDU4565 So Easy!
  3. http://acm.hdu.edu.cn/showproblem.php?pid=4565
  4. 数论 快速幂 矩阵快速幂
  5. 题意:求[(a+sqrt(b))^n ]%m [ ]代表向上取证
  6. 联想到斐波那契数列,所以第一感觉是矩阵快速幂
  7. 后来发现根本不用这么麻烦,我们认为a+b*sqrt(c)中的a为实部
  8. b为虚部,就能直接用二元乘法模拟矩阵快速幂了,因此这两种方法
  9. 是等价的。于是乎,我们就解决了精度问题。
  10. 然而即使我们得出了结果中的a+b*sqrt(c)也不能这么算
  11. 因为[b*sqrt(c)]%m不和[b%m*sqrt(c)]%m同余,因此只能另辟蹊径。
  12. 注意到题目中的(a-1)^2< b < a^2 <=> 0<a-sqrt(b)<1
  13. 所以 0<(a+sqrt(b))^n<1 而 (a+sqrt(b))^n与(a-sqrt(b))^n是公轭的
  14. 所以其和只剩下了实部的二倍。因此只需求出实部,将其乘以2就是答案。
  15. */
  16. #include <cstdio>
  17. #include <algorithm>
  18. #include <cstring>
  19. #include <cmath>
  20. #include <vector>
  21. #include <queue>
  22. #include <iostream>
  23. #include <map>
  24. #include <set>
  25. #define test
  26. using namespace std;
  27. const int Nmax=;
  28. //const long long mod=2147483647LL;
  29. //double eps=1e-8;
  30. long long a,b,n,m;
  31. struct Num
  32. {
  33. long long a;
  34. long long b;
  35. long long c;
  36. Num(long long _a,long long _b,long long _c)
  37. {
  38. a=_a;
  39. b=_b;
  40. c=_c;
  41. }
  42. friend Num operator * (Num x,Num y)
  43. {
  44. long long a1=x.a%m,b1=x.b%m,a2=y.a%m,b2=y.b%m,c=x.c;
  45. long long a=(a1*a2%m+((c*b1*b2)%m))%m;
  46. long long b=((a1*b2)%m+((b1*a2)%m))%m;
  47. return Num(a,b,c);
  48. }
  49. friend Num qpow(Num base,long long n)
  50. {
  51. Num ans(1LL,0LL,base.c);
  52. //ans.print();
  53. while(n>0LL)
  54. {
  55. if(n&1LL)
  56. ans=ans*base;
  57. base=base*base;
  58. n>>=;
  59. //ans.print();
  60. }
  61. //printf("%lld %lld %lld\n",ans.a,ans.b,ans.c);
  62. return ans;
  63. }
  64. void print()
  65. {
  66. printf("a:%lld b:%lld c:%lld\n",a,b,c);
  67. }
  68. };
  69.  
  70. void work()
  71. {
  72. Num base(a,1LL,b);
  73. Num ans=qpow(base,n);
  74. //ans.print();
  75. long long a=ans.a,b=ans.b,c=ans.c;
  76. //long long res=(long long)ceil(a+b*sqrt(c));//不能这么写,因为整数和小数直接不能模
  77. //while(a<=0)
  78. //a+=m;
  79. //while(b<=0)
  80. //b+=m;
  81. //ans.print();
  82. //long long res=(long long)ceil(a+b*sqrt(c));
  83. //printf("res:%lld\n",res);
  84. //res=res%m;
  85. //printf("%lld %lld %ll\n",a,b);
  86. //ans.print();
  87. //if(2LL*a!=ceil(a+b*sqrt(c)))
  88. //printf("NO\n");
  89. //res=(2LL*a)%m;
  90. //printf("ans:%lld myans:%lld\n",(2LL*a)%m,(long long)ceil(a+b*sqrt(c))%m);
  91. long long res=2LL*a%m;
  92. printf("%lld\n",res);
  93.  
  94. }
  95. int main()
  96. {
  97. #ifdef test
  98. //freopen("hdu4565.in","r",stdin);
  99. #endif
  100. while(scanf("%lld%lld%lld%lld",&a,&b,&n,&m)==)
  101. work();
  102. return ;
  103. }

HDU4565 So Easy!的更多相关文章

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

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

  2. 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 ...

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

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

  4. 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 ...

  5. 【HDU4565】So Easy!

    [HDU4565]So Easy! 题面 要你求 \[ \lceil (a+\sqrt b)^n \rceil \% m \] 其中\(0<a,m<2^{15},(a-1)^2<b& ...

  6. HDU 4565 So Easy!(公式化简+矩阵)

    转载:http://www.klogk.com/posts/hdu4565/ 这里写的非常好,看看就知道了啊. 题意很easy.a,b,n都是正整数.求 Sn=⌈(a+b√)n⌉%m,(a−1)2&l ...

  7. HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  8. hdu4565---So Easy!(矩阵)

    Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ce ...

  9. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

随机推荐

  1. pat1013:数素数

    https://www.patest.cn/contests/pat-b-practise/1013 #include "stdio.h" #include "math. ...

  2. linux patch 命令小结【转】

    本文转载自:http://blog.csdn.net/wh_19910525/article/details/7515540 说到patch命令,就不得不提到diff命令,也就是制作patch的必要工 ...

  3. 倒排列表压缩算法汇总——分区Elias-Fano编码貌似是最牛叉的啊!

    来看看倒排索引压缩.压缩是拿CPU换IO的最重要手段之一,不论索引是放在硬盘还是内存中.索引压缩的算法有几十种,跟文本压缩不同,索引压缩算法不仅仅需要考虑压缩率,更要考虑压缩和解压性能,否则会解压太慢 ...

  4. mount -o remount,rw /

    init=/bin/sh mount -o remount,rw /; sed -i 's/指定字符串/&希望插入的字符串/' 文件 在一文件中指定的字符后插入内容 sed -i 's/指定的 ...

  5. Visual Studio q启动卡顿

    在开发人员CMD下面执行 Devenv.exe /ResetSettings ,然后顺利打开 总发现vs2015经常把cpu给占满了,导致电脑卡的不要不要的.这是CodeLens引起的,因为装了VAs ...

  6. ubuntu 软件桌面图标创建

    sublime text 的安装目录是:/usr/local/sublimetext $cd 桌面 $vim Sublime\ Text.desktop 添加如下内容: [Desktop Entry] ...

  7. HDFS Shell命令操作与java代码操作

    (一)编程实现以下功能,并利用 Hadoop 提供的 Shell 命令完成相同任务: (1)     向 HDFS 中上传任意文本文件,如果指定的文件在 HDFS 中已经存在,则由用户来指定是追加到原 ...

  8. 关于MYSQL 存储过程的文章摘录

    1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储 ...

  9. [Advanced Algorithm] - Symmetric Difference

    题目 创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3}和集合 B = ...

  10. drf05 路由Routers

    对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework提供了两个router ...