https://codeforces.com/gym/101853/problem/G
当板子记住把
#include<bits/stdc++.h>
using namespace std;
const int MAX=5e5+10;
typedef long long ll;
ll BSGS(ll a,ll b,ll m)
{
a%=m;
b%=m;
if(b==1)return 0;
unordered_map<ll,ll>ma;
ll n=sqrt(2*m)+1;
ll e=1;
for(int i=0;i<n;i++)
{
if(ma.count(b*e%m)==0)ma[b*e%m]=i;//很奇怪去掉ma.count(b*e%m)==0就会WA
e=e*a%m;
}
ll t=1;
for(int i=1;i<=n+1;i++)
{
t=t*e%m;
if(ma.count(t))return i*n-ma[t];
}
return -1;
}
int main()
{
int T;
cin>>T;
while(T--)
{
ll a,b,m;
scanf("%lld%lld%lld",&a,&b,&m);
printf("%lld\n",BSGS(a,b,m));
}
return 0;
}
2649



被折叠的 条评论
为什么被折叠?



