数学 ( 解高次同余方程 )——Discrete Logarithm Problem ( UVA 7457 )

#include<iostream>
#include<map>
#include<cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 65535;
struct hash{
int a,b,next;
}Hash[maxn << 1];
int flg[maxn + 66];
int top,idx;

void ins(int a,int b)
{
    int k = b & maxn;
    if(flg[k] != idx)
    {
        flg[k] = idx;
        Hash[k].next = -1;
        Hash[k].a = a;
        Hash[k].b = b;
        return ;
    }
    while(Hash[k].next != -1)
    {
        if(Hash[k].b == b) 
            return ;
        k = Hash[k].next;
    }
    Hash[k].next = ++ top;
    Hash[top].next = -1;
    Hash[top].a = a;
    Hash[top].b = b;
}

int find(int b)
{
    int k = b & maxn;
    if(flg[k] != idx) 
        return -1;
    while(k != -1)
    {
        if(Hash[k].b == b) return Hash[k].a;
        k = Hash[k].next;
    }
    return -1;
}

int gcd(int a,int b){return b?gcd(b,a%b):a;}

int ext_gcd(int a,int b,int& x,int& y)
{
    int t,ret;
    if (!b)
    {
        x=1,y=0;
        return a;
    }
    ret=ext_gcd(b,a%b,x,y);
    t=x,x=y,y=t-a/b*y;
    return ret;
}
int Inval(int a,int b,int n)
{
    int x,y,e;
    ext_gcd(a,n,x,y);
    e=(LL)x*b%n;
    return e<0?e+n:e;
}
int pow_mod(LL a,int b,int c){LL ret=1%c;a%=c;while(b){if(b&1)ret=ret*a%c;a=a*a%c;b>>=1;}return ret;}

int BabyStep(int A,int B,int C)
{
    top = maxn; ++ idx;
    LL buf=1%C,D=buf,K;
    int i,d=0,tmp;
    for(i=0;i<=100;buf=buf*A%C,++i)
        if(buf==B)
            return i;
    while((tmp=gcd(A,C))!=1)
    {
        if(B%tmp)return -1;
            ++d;
            C/=tmp;
            B/=tmp;
            D=D*A/tmp%C;
    }
    int M=(int)ceil(sqrt((double)C));
    for(buf=1%C,i=0;i<=M;buf=buf*A%C,++i)
        ins(i,buf);
    for(i=0,K=pow_mod((LL)A,M,C);i<=M;D=D*K%C,++i)
    {
        tmp=Inval((int)D,B,C);
        int w ;
        if(tmp>=0&&(w = find(tmp)) != -1)
            return i*M+w+d;
    }
    return -1;
}

int P;
int a,b;
int main()
{
    scanf("%d", &P);
    while(~scanf("%d", &a) && a )
    {
        scanf("%d", &b);
        int Ans = BabyStep(a , b, P);
        if(Ans<0)
            cout << 0 << endl;
        else
            cout << Ans << endl;
    }


    return 0;
}
  • 模拟做法:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))

const  int maxn = 10000;
int vis[maxn];
int P;
int a,b;

int main()
{
    scanf("%d", &P);
    while(~scanf("%d", &a) && a )
    {
        scanf("%d", &b);
        CLR(vis);
        a%=P, b&=P;
        int flag = 0;
        int tmp = 1;
        for(int i=1;i<=maxn;i++ )
        {
            tmp = (tmp*a) %P;
            if(tmp == b)
            {
                flag = i;
                break;
            }
            else if(vis[tmp]) break;
            else vis[tmp] = 1;
        }
        cout << flag << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值