求解 的最小非负整数解 。
费马小定理:。
则 。
令 ,则
因为 ,所以令 ,则此时 。
于是对所有 处理出 存储,并枚举 ,求出最小答案。
code
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a,b,p,k;
map<int,int> fst;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
cin>>p>>a>>b;
k=ceil(sqrt(p));
int nw=1;
for(int i=0;i<=k;i++,nw=(nw*a)%p)
if(!fst.count(nw*b%p))
fst[nw*b%p]=i;
nw=a;
for(int i=2;i<=k;i++)
a=(a*nw)%p;
nw=1;
for(int i=0;i<=k;i++,nw=(nw*a)%p)
if(fst.count(nw)&&i*k-fst[nw]>=0){
cout<<i*k-fst[nw];
return 0;
}
cout<<"no solution";
return 0;
}