Description
Given a prime P, 2 <= P < 231, 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 显然,这是一道bsgs的裸题
那么bsgs是什么玩意呢,
我们先玩一玩式子
令 m=ceil(sqrt(p))设a^(m*i+j)=b(mod p) 显然 a^j*a^(m*i)=b(mod p) 
  a^j=b*a^(-m*i) (mod p) 因此,我们预处理所有可能的a^j丢进哈希表中然后我们枚举i,
看看有没有可能对应的j所以我们的算法时间复杂度为O(n^0.5)
 #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
struct hash_set{
ll v[];
int next[],g[],w[],tot;
il void clear(){
memset(g,false,sizeof(g));tot=;
}
il void insert(ll h,int f){
v[++tot]=h;
w[tot]=f;
next[tot]=g[h%];
g[h%]=tot;
}
il int find(ll h){
for(re int i=g[h%];i;i=next[i])
if(h==v[i]) return w[i];
return -;
}
} p;
ll A,B,P,m,t,s;
il ll ksm(re ll base,re ll pow){
if(pow<){
cout<<"-1";exit();
}
ll ans=;
for(;pow;pow>>=){
if(pow&) ans=ans*base%P;
base=base*base%P;
}
return ans;
}
il ll rev(re ll a){
return ksm(a,P-);
}
il void init(){
p.clear();
m=ceil(sqrt(P));t=;
for(int i=;i<m;i++){
if(p.find(t)<) p.insert(t,i);
t=t*A%P;
}
//cout<<endl;
for(int i=,l;i<=P/m;i++){
t=rev(ksm(A,m*i));
// cout<<t<<" "<<m*i<<" ";
s=t*B%P;
// cout<<s<<endl;
l=p.find(s);
if(l>=){
printf("%lld\n",m*i+l);
return;
}
}
printf("no solution\n");
}
int main(){
while(scanf("%lld%lld%lld",&P,&A,&B)!=EOF){
init();
}
return ;
}
												

最新文章

  1. Java正则速成秘籍(二)之心法篇
  2. requireJs--简单的使用方法
  3. 51Nod-1136 欧拉函数
  4. 谢欣伦 - OpenDev原创教程 - 设备查找类CxDeviceFind &amp; CxDeviceMapFind
  5. Adobe Air移动开发本人体会
  6. Android实现两个ScrollView互相联动,同步滚动的效果
  7. Linq之Linq to Objects
  8. 使用Jquery+EasyUI 进行框架项目开发案例讲解之三---角色管理源码分享
  9. Apache httpd和JBoss构建高可用集群环境
  10. OpenCV码源笔记——RandomTrees (一)
  11. java中运算符——进度1
  12. UC浏览器 分享到朋友圈和微信好友
  13. linux内核——进程,轻量级进程,线程,线程组
  14. JAVAscript学习笔记 jsBOM 第七节 (原创) 参考js使用表
  15. js if for 详解 获取元素方式 及一些js 基础知识
  16. 0x11栈之火车进栈
  17. Laravel 系列入门教程(四)【最适合中国人的 Laravel 教程】
  18. C++学习笔记44:继承与派生
  19. Android四大组件framework层
  20. dump函数

热门文章

  1. SOAPUI参数中xml中CDATA包含问题
  2. Jmeter资源监控工具ServerAgent运行原理的一些研究
  3. Jenkins+git+Nginx
  4. sklearn中的交叉验证(Cross-Validation)
  5. 如何更改Arcmap里经纬度小数点后面的位数?
  6. Segments CodeForces 909B (找规律)
  7. 团队介绍 you i
  8. 404 Note Found&#183; 第七次作业 - 需求分析报告
  9. Windows Forms编程实战学习:第一章 初识Windows Forms
  10. 软工网络15团队作业4-DAY7