题目分析
注意到 的范围是 的,我们直接枚举显然不行。
于是我们考虑进行分块,假设块长是 。
对于 一定可以表示为 。
于是式子变成了 。
考虑变形一下
由于要求最小解。
先枚举 ,记录一个多重集 表示所有出现过的数和对应的 。
然后枚举 ,依次匹配看有没有出现,显然第一次出现 一定最小, 也一定最小。
时间复杂度 ,瓶颈在快速幂。
代码实现
#include<bits/stdc++.h>
#define int long long
using namespace std;
int p,b,n;
int qp(int a,int b){
int ret=1%p;
a%=p;
while(b){
if(b&1)
ret=(ret*a)%p;
a=(a*a)%p,b>>=1;
}
return ret;
}
int bsgs(){
map<int,int>mp;
int t=sqrt(p),val=qp(b,t);
for(int j=0;j<t;j++)
mp[qp(b,j)*n%p]=j;
for(int i=1;i<=t+1;i++){
int power=qp(val,i);
if(mp.count(power))
return i*t-mp[power];
}
return-1;
}
signed main(){
cin>>p>>b>>n;
int ans=bsgs();
if(ans!=-1)
cout<<ans;
else
cout<<"no solution";
return 0;
}