POJ 2417 Discrete Logging【BSGS】【离散对数】

题目链接:http://poj.org/problem?id=2417
AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<cstring>
using namespace std;
typedef long long ll;
map<ll,ll>hashmp;
ll qpow(ll x,ll y,ll mo)
{
    ll ans=1;
    while(y){
        if(y&1){
            ans=ans*x%mo;
        }
        x=x*x%mo;
        y>>=1;
    }
    return ans;
}
int main()
{
    ll p,b,n;
    while(cin>>p>>b>>n){
        hashmp.clear();
        if(b%p==0){
            cout<<"no solution"<<endl;
            continue;
        }
        ll m=ceil(sqrt(p));
        ll t=n%p;
        hashmp[t]=0;
        for(ll j=1;j<=m;j++){
            t=t*b%p;
            hashmp[t]=j;
        }
        ll tmp=qpow(b,m,p);
        ll tt=1;
        ll j=0;
        for(ll i=1;i<=m;i++){
            tt=tt*tmp%p;
            j=hashmp[tt];
            if(j){
                cout<<(i*m%p-j+p)%p<<endl;
                break;
            }
        }
        if(j==0)
            cout<<"no solution"<<endl;
    }
    return 0;
}

参考:https://blog.youkuaiyun.com/clove_unique/article/details/50740412
https://blog.youkuaiyun.com/clover_hxy/article/details/50683832
https://blog.youkuaiyun.com/ydd97/article/details/48032257

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值