A^x = D (mod P)

0 <= x <= M, here M is a given integer.

1 <= A, P < 2^31, 0 <= D < P, 1 <= M < 2^63

----------------------------------------------------------

裸拓展baby step giant step

先转成非拓展版,然后如果在转的过程中就出解了,则return 1,否则就找出=D的起点和循环节长度,然后直接求解。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. const int Hash_MOD = 1000003;
  6. typedef long long LL;
  7. typedef unsigned long long ULL;
  8. LL times;
  9. struct link
  10. {
  11. LL link;
  12. int val,next;
  13. }es[100000];
  14. struct triple
  15. {
  16. LL x,y,g;
  17. triple(const LL _x = 0,const LL _y = 0,const LL _g = 0):
  18. x(_x),y(_y),g(_g){}
  19. };
  20. int H[Hash_MOD * 2],ec;
  21. triple exgcd(const LL a,const LL b)
  22. {
  23. if (!b) return triple(1,0,a);
  24. const triple last(exgcd(b,a%b));
  25. return triple(last.y,last.x - a / b * last.y,last.g);
  26. }
  27. LL rem_equ(const LL a,const LL b,const LL c)
  28. {
  29. //ax == c (mod b)
  30. //ax mod b == c
  31. const triple tmp(exgcd(a,b));
  32. const LL MOD = (b / tmp.g);
  33. return ((tmp.x * (c / tmp.g) % MOD) + MOD) % MOD;
  34. }
  35. LL find(const LL x)
  36. {
  37. for (int i = H[x%Hash_MOD]; i; i = es[i].next)
  38. if (es[i].link == x) return es[i].val;
  39. return -1;
  40. }
  41. void insert(LL x,const LL v)
  42. {
  43. if (find(x) != -1) return;
  44. const LL key = x % Hash_MOD;
  45. es[++ec].next = H[key];
  46. H[key] = ec;
  47. es[ec].link = x;
  48. es[ec].val = v;
  49. }
  50. LL BSGS(LL A,LL P,LL D)
  51. {
  52. LL AA = 1 % P,x = 1 % P,MOD = P,DD = D,m = static_cast<LL>(std::ceil(std::sqrt((double)P)));
  53. times = 0;
  54. for (triple tmp; (tmp = exgcd(A,P)).g != 1; ) {
  55. if (x == DD) return times++;
  56. if (D % tmp.g) return -1;
  57. P /= tmp.g;
  58. D /= tmp.g;
  59. (AA *= A / tmp.g) %= P;
  60. ++times;
  61. (x *= A) %= MOD;
  62. }
  63. A %= P;
  64. ec = 0;
  65. memset(H,0,sizeof H);
  66. LL tmp = 1 % P;
  67. for (int i = 0; i < m; ++i,(tmp *= A) %= P)
  68. insert(tmp,i);
  69. x = 1 % P;
  70. for (LL i = 0; i < m; ++i,(x *= tmp) %= P) {
  71. const int j = find(rem_equ(AA*x%P,P,D));
  72. if (j != -1) return i * m + j + times;
  73. }
  74. return -1;
  75. }
  76. LL getphi(LL x)
  77. {
  78. LL res = 1;
  79. for (LL i = 2; i * i <= x; ++i)
  80. if (x % i == 0) {
  81. x /= i;
  82. res *= i - 1;
  83. while (x % i == 0) x /= i,res *= i;
  84. }
  85. if (x > 1) res *= x - 1;
  86. return res;
  87. }
  88. LL power(LL a,LL k,LL MOD)
  89. {
  90. //a ^ k % MOD
  91. LL res = 1 % MOD;
  92. for (; k; k/=2,(a *= a) %= MOD)
  93. if (k & 1) (res *= a) %= MOD;
  94. return res;
  95. }
  96. LL calc_len(const LL start,const LL A,const LL P,const LL D)
  97. {
  98. //A ^ (start + *this) == A ^ start == D (mod P)
  99. LL phi = getphi(P),res = phi;
  100. for (LL i = 2; i * i <= phi; ++i) {
  101. for (; phi % i == 0; phi /= i);
  102. for (; res % i == 0 && power(A,start + res/i,P) == D; res /= i);
  103. }
  104. if (phi > 1)
  105. for (; res % phi == 0 && power(A,start + res/phi,P) == D; res /= phi);
  106. return res;
  107. }
  108. ULL work(const LL A,const LL P,const LL D,const ULL M)
  109. {
  110. LL start = BSGS(A,P,D);
  111. //printf("%lld\n",start);
  112. if (start == -1 || start > M) return 0;
  113. else if (start < times) return 1;
  114. // LL phi=getphi(P);
  115. // if (power(A,start + phi,P) != D) return 1;
  116. ULL len = calc_len(start,A,P,D);
  117. return (M - start) / len + 1;
  118. }
  119. int main()
  120. {
  121. #ifndef ONLINE_JUDGE
  122. freopen("3254.in","r",stdin);
  123. freopen("3254.out","w",stdout);
  124. #endif
  125. LL A,P,D;
  126. ULL M;
  127. while (~scanf("%lld%lld%lld%llu",&A,&P,&D,&M))
  128. printf("%llu\n",work(A%P,P,D,M));
  129. }

[ZOJ3254] MON 9.2009Secret Code的更多相关文章

  1. 【zoj3254】Secret Code

    题意: 给出a.p.d.m 求a^x=d(mod p) 0<=x<=m 的解的个数 题解: 今天一整天的时间大部分都在调这题Orz BSGS什么的还是太不熟了 我们可以用BSGS拓展版求出 ...

  2. Visual Studio Code 代理设置

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...

  3. 我们是怎么做Code Review的

    前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...

  4. Code Review 程序员的寄望与哀伤

    一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...

  5. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM

    刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...

  6. 在Visual Studio Code中配置GO开发环境

    一.GO语言安装 详情查看:GO语言下载.安装.配置 二.GoLang插件介绍 对于Visual Studio Code开发工具,有一款优秀的GoLang插件,它的主页为:https://github ...

  7. 代码的坏味道(14)——重复代码(Duplicate Code)

    坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...

  8. http status code

    属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...

  9. Visual Studio Code——Angular2 Hello World 之 2.0

    最近看到一篇用Visual Studio Code开发Angular2的文章,也是一篇入门教程,地址为:使用Visual Studio Code開發Angular 2專案.这里按部就班的做了一遍,感觉 ...

随机推荐

  1. python学习笔记(十六)之文件

    打开文件用open函数 open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=Tru ...

  2. 【leetcode 简单】第四题 罗马数字转整数

    罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列 ...

  3. 自定义li项目符号

    使用background-image属性 先清除ul的默认list-style ul{ list-style:none } li{ background-image:url('./image/symb ...

  4. Hibernate总结之Hello,World

    1. 引入相关maven依赖: <dependency> <groupId>org.hibernate</groupId> <artifactId>hi ...

  5. Fiddler -工具使用介绍(附:拦截请求并修改返回数据)(转)

    一.Fiddler 介绍 Fiddler 是一个使用 C# 编写的 http 抓包工具.它使用灵活,功能强大,支持众多的 http 调试任务,是 web.移动应用的开发调试利器. 1,功能特点 同 H ...

  6. java中的matches -> 完全匹配

    matches是完全匹配.跟matcher不一样, matcher像perl正则, 能匹配到符合的都会返回true, 而这个matches要完全一模一样才行. import java.util.reg ...

  7. Django框架<一>

    Django框架 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Sess ...

  8. Linux下如何打开img镜像文件

    有些镜像文件为IMG格式,在Linux如何打开呢?例如从微软dreampark下载的Windows Server 2008 R2镜像文件,使用file命令查看: $ file chs_windows_ ...

  9. Machine Learning系列--TF-IDF模型的概率解释

    信息检索概述 信息检索是当前应用十分广泛的一种技术,论文检索.搜索引擎都属于信息检索的范畴.通常,人们把信息检索问题抽象为:在文档集合D上,对于由关键词w[1] ... w[k]组成的查询串q,返回一 ...

  10. linux中getmntent setmntent endmntent 用法例子

    mntent 结构是在 <mntent.h> 中定义,如下:               struct mntent {                      char    *mnt ...