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) )
而这么分解的目的无非是为了转化为:
(A^i)^m * A^j = B ( mod C)

之后做少许暴力的工作就可以解决问题:
(1) for i = 0 -> m, 插入Hash (i, A^i mod C)
(2) 枚举 i ,对于每一个枚举到的i,令  AA = (A^m)^i mod C
我们有
AA * A^j = B (mod C)
显然AA,B,C均已知,而由于C为素数,那么(AA,C)无条件为1
于是对于这个模方程解的个数唯一(可以利用扩展欧几里得或 欧拉定理来求解)
那么对于得到的唯一解X,在Hash表中寻找,如果找到,则返回 i * m + j 
注意:由于i从小到大的枚举,而Hash表中存在的j必然是对于某个剩余系内的元素X 是最小的(就是指标)
所以显然此时就可以得到最小解

如果需要得到 x > 0的解,那么只需要在上面的步骤中判断 当 i * m + j > 0 的时候才返回

(转载结束)

本题只是最基础的应用,复杂度是

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <set>
#define PI acos(-1.0)
#define maxn 10005
#define INF 0x7fffffff
#define eps 1e-8
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
LL pow_mod(LL aa,LL ii,LL nn)
{
    if(ii==0)
        return 1%nn;
    LL temp=pow_mod(aa,ii>>1,nn);
    temp=temp*temp%nn;
    if(ii&1)
        temp=temp*aa%nn;
    return temp;
}
struct b_step
{
    int i,m;
} bb[100005];
bool cmp(b_step a,b_step b)
{
    return a.m==b.m?a.i<b.i:a.m<b.m;
}
int BiSearch(int m,LL num)
{
    int low=0,high=m,mid;
    while(low<=high)
    {
        mid=(low+high)>>1;
        if(bb[mid].m==num)
            return bb[mid].i;
        if(bb[mid].m<num)
            low=mid+1;
        else
            high=mid-1;
    }
    return -1;
}
void giant_step_baby_step(LL b,LL n,LL p)
{
    int m=(int)ceil(sqrt((double)p));
    bb[0].i=0,bb[0].m=1;
    for(int i=1; i<m; i++)
    {
        bb[i].i=i;
        bb[i].m=bb[i-1].m*b%p;
    }
    sort(bb,bb+m,cmp);
    int top=0;
    for(int i=1; i<m; i++)
        if(bb[i].m!=bb[top].m)
            bb[++top]=bb[i];
    LL bm=pow_mod(pow_mod(b,p-2,p),m,p);
    LL ans=-1;
    LL tmp=n;
    for(int i=0; i<m; i++)
    {
        int pos=BiSearch(top,tmp);
        if(~pos)
        {
            ans=m*i+pos;
            break;
        }
        tmp=((LL)tmp*bm)%p;
    }
    if(!~ans)
        puts("no solution");
    else
        printf("%d\n",ans);
}
int main()
{
    LL p,b,n;
    while(~scanf("%lld%lld%lld",&p,&b,&n))
    {
        giant_step_baby_step(b,n,p);
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值