POJ 2417 Discrete Logging

http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html

给p,a,b求a^n==b%p

 1 #include<algorithm>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<map>
 7 #define ll long long
 8 ll p;
 9 std::map<ll , int> mp;
10 ll Pow(ll x,ll y){
11     ll res=1;
12     while (y){
13         if (y%2) res=(res*x)%p;
14         x=(x*x)%p;
15         y/=2;
16     }
17     return res;
18 }
19 ll solve(ll a,ll b){
20     a%=p;
21     if (a==0){
22         if (b==0) return 0;
23         else return -1;
24     }
25     mp.clear();
26     ll m=sqrt(p)+1,t=1,I=1;
27     for (int i=1;i<m;i++){
28         t=t*a%p;
29         if (!mp[t]) mp[t]=i;
30     }
31     mp[1]=m+1;
32     ll Im=Pow(a,p-1-m);
33     for (int k=0;k<m;k++){
34         int i=mp[I*b%p];
35         if (i!=0){
36             if (i==m+1) i=0;
37             return i+k*m;
38         }
39         I=I*Im%p;
40     }
41     return -1;
42 }
43 int main(){
44     ll a,b;
45     while (scanf("%lld%lld%lld",&p,&a,&b)!=EOF){
46         ll ans=solve(a,b);
47         if (ans==-1) puts("no solution");
48         else printf("%lld\n",ans);
49     }
50 }
时间: 2024-10-05 12:28:08

POJ 2417 Discrete Logging的相关文章

poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)

http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B,C,求x. 这里C是素数,可以用普通的baby_step. 在寻找最小的x的过程中,将x设为i*M+j.从而原始变为A^M^i * A^j = B(mod C),D = A^M,那么D^i * A^j = B(mod C ), 预先将A^j存入hash表中,然后枚举i(0~M-1),根据扩展欧几里得求出A^j,再去hash表中查找相应的j,那么x = i*M+j. 确定x是否有解,就是在循环i

POJ 2417 Discrete Logging 离散对数

链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 以下转载自:AekdyCoin [普通Baby Step Giant Step] [问题模型] 求解 A^x = B (mod C) 中 0 <= x < C 的解,C 为素数 [思路] 我们可以做一个等价 x = i * m + j  ( 0 <= i < m, 0 <=j < m) m = Ceil ( sqrt( C

BSGS算法+逆元 POJ 2417 Discrete Logging

POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accepted: 2211 Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarith

[poj 2417]Discrete Logging 数论 BSGS

Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3516   Accepted: 1651 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.

POJ 2417 Discrete Logging ( Baby step giant step )

Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 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, b

POJ 2417 Discrete Logging BSGS

Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4011   Accepted: 1849 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, b

poj 2417 Discrete Logging 数论baby_step,giant_step算法

题意: 给p,b,n求最小的l使b^l==n(mod p). 题意: 相当于在0~p-1内搜索l满足同余式,baby_step,giant_step相当于分块的二分搜索,设m=sqrt(p), 则将p分为m*m,在m块内每块进行m个数的二分搜索. 代码: //poj 2417 //sep9 #include <iostream> #include <cmath> #include <algorithm> using namespace std; const int ma

离散对数二连 poj 2417 Discrete Logging &amp; HDU 2815 Mod Tree

题目就不贴了.都是一样的套路求解a^x % c = b的最小x 注意HDU的题目很坑,有b比c大和题目中输出信息为全角引号的坑 下面贴的是HDU的版本,poj的改一下a,b,c顺序和输出就好 #include <iostream> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long LL; #define MOD 999

POJ 2417 Discrete Logging Baby-Step-Gaint-Step

题目大意:给出A,B,C,求A^x=B(mod C)的最小x的值. 思路:著名的BSGS算法.将C拆分成根号块,先对一个根号内的东西暴力插入一个Hash表中(别问我为什么不用map,因为这个题卡map... 另我们要求的x=i * m + j,原式可以写成A^(i * m) * A^j = B(mod C).这是ax=b(mod c)的形式我们只需要枚举i,然后看有没有符合要求的j就可以了. CODE: #define _CRT_SECURE_NO_WARNINGS #include <cmat