Discrete Logging
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4624   Accepted: 2113

Description

Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that

  1. B

L

  1. == N (mod P)

Input

Read several lines of input, each containing P,B,N separated by a space.

Output

For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".

Sample Input

  1. 5 2 1
  2. 5 2 2
  3. 5 2 3
  4. 5 2 4
  5. 5 3 1
  6. 5 3 2
  7. 5 3 3
  8. 5 3 4
  9. 5 4 1
  10. 5 4 2
  11. 5 4 3
  12. 5 4 4
  13. 12345701 2 1111111
  14. 1111111121 65537 1111111111

Sample Output

  1. 0
  2. 1
  3. 3
  4. 2
  5. 0
  6. 3
  7. 1
  8. 2
  9. 0
  10. no solution
  11. no solution
  12. 1
  13. 9584351
  14. 462803587

题意:

a^x = b(mod n) ,求解x(模板题)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <cmath>
  7. using namespace std;
  8. typedef long long ll;
  9. typedef long double ld;
  10.  
  11. using namespace std;
  12. #define MOD 76543
  13. int hs[MOD],head[MOD],Next[MOD],id[MOD],top;
  14.  
  15. void insert(int x,int y)
  16. {
  17. int k = x % MOD;
  18. hs[top] = x,id[top] = y,Next[top] = head[k],head[k] = top++;
  19. }
  20.  
  21. int find(int x)
  22. {
  23. int k = x % MOD;
  24. for(int i = head[k];i != -;i= Next[i])
  25. {
  26. if(hs[i] == x)
  27. return id[i];
  28. }
  29. return -;
  30. }
  31.  
  32. int BSGS(int a,int b,int n)
  33. {
  34. memset(head,-,sizeof(head));
  35. top = ;
  36. if(b == )
  37. return ;
  38. int m = sqrt(n*1.0),j;
  39. long long x = ,p =;
  40. for(int i = ;i < m;i++,p = p*a%n)
  41. insert(p*b%n,i);
  42. for(ll i = m;;i+=m)
  43. {
  44. if((j = find(x = x*p % n)) != -) return i-j;
  45. if(i > n) break;
  46. }
  47. return -;
  48. }
  49.  
  50. int main()
  51. {
  52. int p,b,n;
  53. while(scanf("%d%d%d",&p,&b,&n) != EOF)
  54. {
  55. int ans = BSGS(b,n,p);
  56. if(ans == -)
  57. printf("no solution\n");
  58. else
  59. printf("%d\n",ans);
  60. }
  61. return ;
  62. }

poj 2417 && poj3243(Baby-Step Giant-Step)的更多相关文章

  1. POJ 2417 Discrete Logging ( Baby step giant step )

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

  2. POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)

    不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...

  3. 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法

    先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝  扩展Baby Step Gian ...

  4. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

  5. 【POJ2417】baby step giant step

    最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...

  6. [置顶] hdu2815 扩展Baby step,Giant step入门

    题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会 ...

  7. HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法

    联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  8. 『高次同余方程 Baby Step Giant Step算法』

    高次同余方程 一般来说,高次同余方程分\(a^x \equiv b(mod\ p)\)和\(x^a \equiv b(mod\ p)\)两种,其中后者的难度较大,本片博客仅将介绍第一类方程的解决方法. ...

  9. HDU 2815 扩展baby step giant step 算法

    题目大意就是求 a^x = b(mod c) 中的x 用一般的baby step giant step 算法会超时 这里参考的是http://hi.baidu.com/aekdycoin/item/2 ...

  10. 【学习笔记】Baby Step Giant Step算法及其扩展

    1. 引入 Baby Step Giant Step算法(简称BSGS),用于求解形如\(a^x\equiv b\pmod p\)(\(a,b,p\in \mathbb{N}\))的同余方程,即著名的 ...

随机推荐

  1. bzoj千题计划251:bzoj3672: [Noi2014]购票

    http://www.lydsy.com/JudgeOnline/problem.php?id=3672 法一:线段树维护可持久化单调队列维护凸包 斜率优化DP 设dp[i] 表示i号点到根节点的最少 ...

  2. 2017北京国庆刷题Day1 afternoon

    期望得分:100+100+100=300 实际得分:100+100+100=300 T1 一道图论好题(graph) Time Limit:1000ms   Memory Limit:128MB 题目 ...

  3. JAVA_SE基础——65.StringBuffer类 ②

    字符串特点:字符串是常量:它们的值在创建之后不能更改.    字符串的内容一旦发生了变化,那么马上会创建一个新 的对象.    注意: 字符串的内容不适宜频繁修改,因为一旦修改马上就会创建一个新的对象 ...

  4. 电梯模拟C++

    1.问题描述与要求 模拟某校九层教学楼的电梯系统.该楼有一个自动电梯,能在每层停留,其中第一层是大楼的进出层,即是电梯的"本垒层",电梯"空闲"时,将来到该层候 ...

  5. Docker学习笔记 - 创建私有的镜像仓库

    一.查找镜像仓库 https://hub.docker.com/ 二.下载镜像仓库 docker pull registry:2.6.2 三.安装镜像仓库 docker run -d -p 6000: ...

  6. SpringCloud的EurekaClient : 客户端应用访问注册的微服务(无断路器场景)

    演示客户端应用如何访问注册在EurekaServer里的微服务 一.概念和定义 采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,2. ...

  7. window.open()参数详解及对浏览器的兼容性

    因为篇幅,window.open()浏览器的兼容性请点击 这里 Part1:参数详解 window.open(url,name,param) url:即将打开的子窗口的地址:比如 "http ...

  8. Let's Encrypt,免费好用的 HTTPS 证书

    很早之前我就在关注 Let's Encrypt 这个免费.自动化.开放的证书签发服务.它由 ISRG(Internet Security Research Group,互联网安全研究小组)提供服务,而 ...

  9. java子类重写父类的要点

    子类不能重写父类的静态方法,私有方法.即使你看到子类中存在貌似是重写的父类的静态方法或者私有方法,编译是没有问题的,但那其实是你重新又定义的方法,不是重写.具体有关重写父类方法的规则如下:重写规则之一 ...

  10. NOPI实现导入导出泛型List,支持自定义列

    概述 业务上需要自定义列的Excel的导入导出,在网上看了好多资料,很多都是有Bug而且都是支持Excel和DataTable的转换,所以自己总结了一下,应用.NET平台上的NPOI封装了支持自定义列 ...