BZOJ_3239_Discrete Logging_BSGS

题意:Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 2 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that BL == N (mod P)

分析:BSGS裸题

数据很水,少了很多特判依然能过

BSGS思想核心思想:根号预处理+哈希/map

代码:

  1. // luogu-judger-enable-o2
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. #include <map>
  6. #include <math.h>
  7. using namespace std;
  8. #define LL long long
  9. map<LL,int> mp;
  10. LL qp(LL x,LL y,LL mod){
  11. LL re=1;
  12. while(y){
  13. if(y&1ll)re=re*x%mod;
  14. x=x*x%mod;
  15. y>>=1ll;
  16. }
  17. return re;
  18. }
  19. void exgcd(LL a,LL b,LL &x,LL &y,LL &p){
  20. if(!b){x=1;y=0;p=a;return ;}
  21. exgcd(b,a%b,y,x,p);
  22. y-=(a/b)*x;
  23. }
  24. LL BSGS(LL n,LL a,LL b){
  25. if(n==1)if(!b)return a!=1; else return -1;
  26. if(b==1)if(a)return 0; else return -1;
  27. if(a%n==0)if(!b)return 1; else return -1;
  28. LL m=ceil(sqrt(n)),d=1,base=1;
  29. mp.clear();
  30. for(int i=0;i<m;i++)
  31. {
  32. if(!mp.count(base))mp[base]=i;
  33. base=(base*a)%n;
  34. }
  35. for(int i=0;i<m;i++)
  36. {
  37. LL x,y,s;
  38. exgcd(d,n,x,y,s);
  39. x=(x*b%n+n)%n;
  40. if(mp.count(x))return i*m+mp[x];
  41. d=(d*base)%n;
  42. }
  43. return -1;
  44. }
  45. int main()
  46. {
  47. LL n,a,b;
  48. while(scanf("%lld%lld%lld",&n,&a,&b)!=EOF)
  49. {
  50. LL x=BSGS(n,a,b);
  51. if(x==-1)puts("no solution");
  52. else printf("%lld\n",x);
  53. }
  54. }

BZOJ_3239_Discrete Logging_BSGS的更多相关文章

  1. [poj2417]Discrete Logging_BSGS

    Discrete Logging poj-2417 题目大意:求$a^x\equiv b(mod\qquad c)$ 注释:O(分块可过) 想法:介绍一种算法BSGS(Baby-Step Giant- ...

随机推荐

  1. ORACLE分页SQL语句(转载)

    1.根据ROWID来分select * from t_xiaoxi where rowid in(select rid from (select rownum rn,rid from(select r ...

  2. Linux的关机详解

    Linux如果是关机不想Windows,特别是命令界面.如果使用的是虚拟机,我们经常通过虚拟机来关闭.有点笨重啊.着特意找到关于Linux关机命令分享给大家. 在linux下一些常用的关机/重启命令有 ...

  3. java原子操作

    一.何谓Atomic? Atomic一词跟原子有点关系,后者曾被人认为是最小物质的单位.计算机中的Atomic是指不能分割成若干部分的意思.如果一段代码被认为是Atomic,则表示这段代码在执行过程中 ...

  4. 使用mpvue开发微信小程序

    更多内容请查看 我的新博客 地址 : 前言 16年小程序刚出来的时候,就准备花点时间去学学.无奈现实中手上项目太多,一个接着一个,而且也没有开发小程序的需求,所以就一拖再拖. 直到上周,终于有一个小程 ...

  5. 与班尼特·胡迪一起攻破浮空城 (HZNU-2264)

    与班尼特·胡迪一起攻破浮空城 AC Time Limit:  1 s      Memory Limit:   256 MB Description 桐人为了拯救被困在浮空城堡最顶层的亚丝娜,决定从第 ...

  6. EF Code First 数据迁移配置

    这里我想讲清楚code first 数据迁移的两种模式,还有开发环境和生产环境数据迁移的最佳实践. 1.1 数据迁移综述 EF Code first 虽然已经有了几种不同的数据库初始化策略,但是大部分 ...

  7. SOFA 源码分析 — 负载均衡和一致性 Hash

    前言 SOFA 内置负载均衡,支持 5 种负载均衡算法,随机(默认算法),本地优先,轮询算法,一致性 hash,按权重负载轮询(不推荐,已被标注废弃). 一起看看他们的实现(重点还是一致性 hash) ...

  8. Hadoop生态圈初识

    一.简介 Hadoop是一个由Apache基金会所开发的分布式系统基础架构.Hadoop的框架最核心的设计就是:HDFS和MapReduce.HDFS为海量的数据提供了存储,则MapReduce为海量 ...

  9. Effective C++ 读书笔记(46-50)

    条款四十六:需要类型转换时请为模板定义非成员函数 条款四十七:请使用traits classes 表现类型信息 1.整合重载技术后,traits classes 有可能在编译期对类型执行if...el ...

  10. Tomcat PermGen space的解决方案

    Tomcat报告 Caused by: java.lang.OutOfMemoryError: PermGen space异常 内存溢出PermGen space的全称是Permanent Gener ...