Discrete Logging
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 3104 | Accepted: 1495 |
Description
Given a prime P, 2 <= P < 2
31, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that
BL == N (mod P)
Input
Read several lines of input, each containing P,B,N separated by a space.
Output
For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".
Sample Input
5 2 1 5 2 2 5 2 3 5 2 4 5 3 1 5 3 2 5 3 3 5 3 4 5 4 1 5 4 2 5 4 3 5 4 4 12345701 2 1111111 1111111121 65537 1111111111
Sample Output
0 1 3 2 0 3 1 2 0 no solution no solution 1 9584351 462803587
Hint
The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat's theorem that states
for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat's theorem is that for any m
B(P-1) == 1 (mod P)
for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat's theorem is that for any m
B(-m) == B(P-1-m) (mod P) .
Source
已知X^y mod Z=K以及X,Z,K的值,求y的值。
//1660K 172MS
#include<stdio.h>
#include<cmath>
#define Maxn 65535
struct hash
{
int a,b,next;
}Hash[Maxn*2];
int flg[Maxn+66];
int top,idx;
void ins(int a,int b)
{
int k=b&Maxn;
if(flg[k]!=idx)
{
flg[k]=idx;
Hash[k].next=-1;
Hash[k].a=a;
Hash[k].b=b;
return;
}
while(Hash[k].next!=-1)
{
if(Hash[k].b==b)return;
k=Hash[k].next;
}
Hash[k].next=++top;
Hash[top].next=-1;
Hash[top].a=a;
Hash[top].b=b;
}
int find(int b)
{
int k=b&Maxn;
if(flg[k]!=idx)return -1;
while(k!=-1)
{
if(Hash[k].b==b)return Hash[k].a;
k=Hash[k].next;
}
return -1;
}
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int ex_gcd(int a,int b,int& x,int& y)
{
int t,ret;
if(!b){x=1,y=0;return a;}
ret=ex_gcd(b,a%b,x,y);
t=x,x=y,y=t-a/b*y;
return ret;
}
int Inval(int a,int b,int n)
{
int x,y,e;
ex_gcd(a,n,x,y);
e=(long long)x*b%n;
return e<0?e+n:e;
}
int pow_mod(long long a,int b,int c)
{
long long ret=1%c;a%=c;
while(b)
{
if(b&1){ret=ret*a%c;}
a=a*a%c;
b>>=1;
}
return ret;
}
int BabyStep(int A,int B,int C)
{
top=Maxn;++idx;
long long buf=1%C,D=buf,K;
int i,d=0,tmp;
for(i=0;i<100;buf=buf*A%C,++i)
if(buf==B)return i;
while((tmp=gcd(A,C))!=1)
{
if(B%tmp)return -1;
++d;
C/=tmp;
B/=tmp;
D=D*A/tmp%C;
}
int M=(int)ceil(sqrt(C*1.0));
for(buf=1%C,i=0;i<=M;buf=buf*A%C,++i)ins(i,buf);
for(i=0,K=pow_mod((long long)A,M,C);i<=M;D=D*K%C,++i)
{
tmp=Inval((int)D,B,C);int w;
if(tmp>=0&&(w=find(tmp))!=-1)
return i*M+w+d;
}
return -1;
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&c,&b),a|b|c)
{
b%=c;
int ans=BabyStep(a,b,c);
if(ans==-1)printf("No Solution\n");
else printf("%d\n",ans);
}
return 0;
}