2995: 同余方程
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 188 Solved: 68
[Submit][Status][Discuss]
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
Sample Output
9
No Solution
【数据规模】
10%的数据,a,p,b≤10000;
对于另外30%的数据,p为质数;
100%的数据,a,p,b≤10^9。
HINT
Source
.
2480: Spoj3105 Mod
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 394 Solved: 175
[Submit][Status][Discuss]
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
Sample Output
9
No Solution
HINT
10%的数据,a,p,b≤10000;
对于另外30%的数据,p为质数;
100%的数据,a,p,b≤1e9。
Source
鸣谢 Hewr
.
3239: Discrete Logging
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 374 Solved: 240
[Submit][Status][Discuss]
Description
Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 2 <= 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”.
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
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) .
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
Source
.
关于EXBSGS的讲解:http://dashgua.coding.io/about-BSBS/
自己打的模板,蛋疼了….(Claris的模板看不懂TAT)
code:(三倍经验题,改一下no solution的大小写,和读入顺序即可)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
long long read()
{
long long x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
long long gcd(long long a,long long b)
{
if (b==0) return a; else return gcd(b,a%b);
}
long long quick_pow(long long x,long long y,long long p)
{
long long re=1; x%=p; y%=p;
for (long long i=y; i; i>>=1,x=x*x%p)
if (i&1) re=re*x%p;
return re;
}
long long bsgs(long long a,long long b,long long p){
if(a%=p,b%=p,b==1)return 0;
long long t=1;long long f,g,delta=0,m=sqrt(p)+1,i;
for(g=gcd(a,p);g!=1;g=gcd(a,p)){
if(b%g)return -1;
b/=g,p/=g,t=t*(a/g)%p,delta++;
if(b==t)return delta;
}
map<long long,long long>hash;
for(i=0;i<m;i++,b=b*a%p)hash[b]=i;
for(i=1,f=quick_pow(a,m,p);i<=m+1;i++)
if(t=t*f%p,hash.count(t))return i*m-hash[t]+delta;
return -1;
}
int main()
{
long long a,b,p;
while (cin>>a>>p>>b)
{
//p=read();a=read();b=read();
if (a==0 && b==0 && p==0) break;
//if (b>=p) {puts("No Solution");continue;}
long long ans=bsgs(a,b,p);
if (ans==-1) puts("No Solution");
else printf("%lld\n",ans);
}
return 0;
}