[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. That is, find an integer L such that

    BL == 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

5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111

Sample Output

0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587

Hint

The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat‘s theorem that states

   B(P-1) == 1 (mod P)

for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat‘s theorem is that for any m

   B(-m) == B(P-1-m) (mod P) .

Source

Waterloo Local 2002.01.26

题目大意

给出B,N,P 求出 B^L == N (mod P)中L的最小值,无解输出no solution

解题思路

直接按照poj 1395的思路模拟铁定超时,我们这个时候考虑:

P是质数那么B^(P-1)=1(mod P) [据费马小定理,题中B<P]

可知剩余系的循环节为p-1

根据同余定理,我们可以把L拆解成两部分

此时 B^A * B^(L-A) = N (MOD P)

考虑A为何值时我们可以简化计算

BSGS就是利用这样的思想 将剩余系的余数分拆成floor(sqrt(P))组

再设每一组元素为c个

每次我们的A都可以取得A=c*i ,同时满足c*i<(p-1)

则L-A<c

c约为sqrt(P),我们可以直接打出前sqrt(p)项的表存到map之中待处理

那么我们再枚举i,来改变A的值

通过求逆元来推测一个B^(L-A) mod P可能的值,查map,看这种方案是否有对应解。

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#define LL long long
using namespace std;
typedef int Ma[10][10];
map <LL , LL>inv;
void egcd(LL a,LL b,LL &x,LL &y)
{
  if (b==0)
  {
    x=1;
    y=0;
    return;
  }
  egcd(b,a%b,x,y);
  LL t=x;
  x=y;y=t-a/b*y;
}
int main()
{
  LL p,b,n;
  while (~scanf("%I64d%I64d%I64d",&p,&b,&n))
  {
    inv.clear();
    LL c=sqrt((double) p);
    // printf("%I64d\n",c);
    LL cur=1;
    for (LL i=0;i<c;i++)
    {
      inv[cur]=i+1;
      cur=cur*b%p;
    }
    LL t=cur;
    LL ans=p+1;
    cur=1;
    for (LL i=0;i<=(p/c+1);i++)
    {
      LL x,y;
      egcd(cur,p,x,y);
      x=(x+p)%p;
      // printf("%I64d %I64d\n",cur,i);
      if (inv[n*x%p]) {
        ans=min(ans,inv[n*x%p]+i*c-1);
        // printf("%I64d\n",ans);
      }
      cur=cur*t%p;
    }
    if (ans>p) puts("no solution");
    else printf("%I64d\n",ans);
  }
  return 0;
}

[poj 2417]Discrete Logging 数论 BSGS

时间: 2024-08-24 19:27:36

[poj 2417]Discrete Logging 数论 BSGS的相关文章

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

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(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

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 )

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 &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

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 lon