第一个点RE,为啥?
查看原帖
第一个点RE,为啥?
856309
oiyang楼主2023/10/2 11:42
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
ll p,b,n;
ll qpow(ll a,ll b,ll mod)
{
    ll ans=1%mod,base=a;
    while(b>0)
    {
        if(b&1)
        {
            ans*=base;
            ans%=mod;
        }
        base*=base;
        base%=mod;
        b>>=1;
    }
    return ans;
}
ll bsgs(ll a,ll b,ll p)
{
    map<ll,ll>hash;
    hash.clear();
    b%=p;
    ll t=sqrt(p)+1;
    for(ll j=0;j<t;j++)
    {
        ll val=b*qpow(a,j,p)%p;
        hash[val]=j;
    }
    a=qpow(a,t,p);
    if(a==0)
    {
        if(b==0)
            return 1;
        else
            return -1;
    }
    for(ll i=0;i<=t;i++)
    {
        ll val=qpow(a,i,p);
        ll temp;
        if(hash.find(val)==hash.end())
            temp=-1;
        else
            temp=hash[val];
        if(temp>=0 && i*t-temp>=0)
            return i*t-temp;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>p>>b>>n;
    ll ans=bsgs(b,n,p);
    if(ans==-1)
        cout<<"no solution";
    else
        cout<<ans;
    return 0;
}
2023/10/2 11:42
加载中...