POJ 2417 Discrete Logging

http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html

给p,a,b求a^n==b%p

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<map>
#define ll long long
ll p;
std::map<ll , int> mp;
ll Pow(ll x,ll y){
ll res=;
while (y){
if (y%) res=(res*x)%p;
x=(x*x)%p;
y/=;
}
return res;
}
ll solve(ll a,ll b){
a%=p;
if (a==){
if (b==) return ;
else return -;
}
mp.clear();
ll m=sqrt(p)+,t=,I=;
for (int i=;i<m;i++){
t=t*a%p;
if (!mp[t]) mp[t]=i;
}
mp[]=m+;
ll Im=Pow(a,p--m);
for (int k=;k<m;k++){
int i=mp[I*b%p];
if (i!=){
if (i==m+) i=;
return i+k*m;
}
I=I*Im%p;
}
return -;
}
int main(){
ll a,b;
while (scanf("%lld%lld%lld",&p,&a,&b)!=EOF){
ll ans=solve(a,b);
if (ans==-) puts("no solution");
else printf("%lld\n",ans);
}
}
上一篇:nexus-2.13.0-01.war


下一篇:(原创)OpenStack服务如何使用Keystone(三)---详细配置Keystone中间件