点击这里查看原题
模板题,这里贴BSGS教程
/*
User:Small
Language:C++
Problem No.:2417
*/
#include<stdio.h>
#include<iostream>
#include<iomanip>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#define ll long long
#define inf 999999999
using namespace std;
ll a,b,c;
map<ll,int> mp;
ll pow(ll a,ll b,ll p){
ll res=1;
while(b){
if(b&1LL) res=res*a%p;
a=a*a%p;
b>>=1LL;
}
return res;
}
int main(){
freopen("data.in","r",stdin);//
ios::sync_with_stdio(false);
while(cin>>c>>a>>b){
bool flag=0;
mp.clear();
ll m=(ceil)(sqrt(c)),ans=1;
ans=b%c;
mp[ans]=0;
for(int j=1;j<=m;j++){
ans=ans*a%c;
mp[ans]=j;
}
ll t=pow(a,m,c);
ans=1;
for(int i=1;i<=m;i++){
ans=ans*t%c;
if(mp[ans]){
t=i*m-mp[ans];
flag=1;
cout<<(t%c+c)%c<<endl;
break;
}
}
if(!flag) cout<<"no solution\n";
}
return 0;
}