【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod
Description
已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x。
Input
每个测试文件中最多包含100组测试数据。
每组数据中,每行包含3个正整数a,p,b。
当a=p=b=0时,表示测试数据读入完全。
Output
对于每组数据,输出一行。
如果无解,输出“No Solution”(不含引号),否则输出最小自然数解。
Sample Input
5 58 33
2 4 3
0 0 0
2 4 3
0 0 0
Sample Output
9
No Solution
No Solution
HINT
100%的数据,a,p,b≤1e9。
题解:EXBSGS
因为A和C不互质,所以我们令A,B,C同时除以gcd(A,C),得到的C'可能与A还不互质,所以我们再除上gcd(A,C'),直到A,C互质。
此时得到方程$A^{x-k}*{A^k\over g_1*g_2...g_k}=B'(mod C')$
将${A^k\over g_1*g_2...g_k}$除过去,然后套用BSGS即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
map<int,int> mp;
int pm(int a,int b,int c)
{
int ret=1;
while(b)
{
if(b&1) ret=(ll)ret*a%c;
a=(ll)a*a%c,b>>=1;
}
return ret;
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int BSGS(int a,int b,int c,int d)
{
int x,y,i;
mp.clear();
int M=ceil(sqrt(c));
for(x=b,i=0;i<=M;i++,x=(ll)x*a%c) mp[x]=i;
for(y=d,x=pm(a,M,c),i=1;i<=M;i++)
{
y=(ll)y*x%c;
int tmp=mp[y];
if(tmp) return (ll)i*M-tmp;
}
return -1;
}
void work(int a,int b,int c)
{
int A=1,k=0;
for(int i=0;(1<<i)<=c;i++)
{
if(pm(a,i,c)==b)
{
printf("%d\n",i);
return ;
}
}
while(1)
{
int g=gcd(a,c);
if(g==1) break;
if(b%g!=0)
{
printf("No Solution\n");
return ;
}
b/=g,c/=g,A=((ll)A*a/g)%c,k++;
}
int tmp=BSGS(a,b,c,A);
if(tmp==-1) printf("No Solution\n");
else printf("%d\n",tmp+k);
}
int main()
{
int a,b,c;
while(1)
{
scanf("%d%d%d",&a,&c,&b);
if(!a&&!b&&!c) return 0;
if(!a&&!b) printf("1\n");
else a%=c,b%=c,work(a,b,c);
}
}