BZOJ3239Discrete Logging
来源:互联网 发布:linux中的cat命令 编辑:程序博客网 时间:2024/06/06 17:10
3239: Discrete Logging
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 372 Solved: 238
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
拓展BSGS。。
附上本蒟蒻的代码:
#include<cstdio>#include<map>#include<cmath>#include<iostream>using namespace std;long long gcd(long long x,long long y){ return y==0?x:gcd(y,x%y);}long long exgcd(long long a,long long b,long long &x,long long &y){ if (!b) { x=1,y=0; return a; } else { long long g=exgcd(b,a%b,x,y),t=x; x=y,y=t-a/b*y; return g; }}long long inv(long long a,long long p){ long long x,y,d=exgcd(a,p,x,y); return d==1?(x+p)%p:-1;}long long pow(long long x,long long a,long long p){ long long t; if (!a) return 1%p; if (a==1) return x%p; t=pow(x,a/2,p); if (a%2) return ((t*t)%p*x)%p; else return (t*t)%p;}int BSGS(long long a,long long b,long long p){ long long m=0; for (;m*m<=p;m++); b%=p; map<long long,int>hash; hash[b]=0; long long e=b,v=inv(a,p),mul=pow(a,m,p); for (int i=1;i<m;i++) { e=e*v%p; if (!hash.count(e)) hash[e]=i; else break; } e=1; for (int i=0;i<=m;i++) { if (hash.count(e)) return hash[e]+i*m; e=e*mul%p; } return -1;}void solve(long long a,long long b,long long p){ long long e=1; b%=p,a%=p; for (int i=0;i<100;i++) { if (e==b) { printf("%d\n",i); return; } e=e*a%p; } int sum=0; while (gcd(a,p)!=1) { long long d=gcd(a,p); if (b%d) { printf("no solution\n"); return; } p/=d,sum++,b/=d; b=b*inv(a/d,p)%p; } int ans=BSGS(a,b,p); if (ans==-1) { printf("no solution\n"); return; } printf("%d\n",ans+sum);}int main(){ long long a,p,b; while (cin>>p>>a>>b) solve(a,b,p); return 0;}
- BZOJ3239Discrete Logging
- Logging
- Logging
- logging
- Logging
- logging
- logging
- logging API
- LOGGING| NOLOGGING
- 关于Logging
- commons-logging
- Python logging
- python logging
- upplemental Logging
- python logging
- python logging
- commons.logging
- HTTP logging
- PPP学习疑难解决
- 天声人語 20160327 道産子コーラ
- Zookeeper安装以及集群搭建
- 日经春秋 20160327
- XAudio2学习之采样率转换
- BZOJ3239Discrete Logging
- 常见的时间复杂度
- 第五周项目1-三角形类雏形(2)
- python简单浮点数匹配
- PHPCMSV9 乱解读 之 PHPCMS V9的MVC
- windows下Eclipse的Hadoop运行调试环境搭建
- Spark集群的安装与使用
- git命令行
- 串的三种实现方式