POJ 2417 Discrete Logging BSGS
http://poj.org/problem?id=2417
BSGS 大步小步法( baby step giant step )
sqrt( p )的复杂度求出 ( a^x ) % p = b % p中的x
https://www.cnblogs.com/mjtcn/p/6879074.html
我的代码中预处理a==b和b==1的部分其实是不必要的,因为w=sqrt(p)(向上取整),大步小步法所找的x包含从0到w^2。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define LL long long
LL p,a,b;
map<LL,LL>q;
int main(){
while(~scanf("%lld%lld%lld",&p,&a,&b)){
if(b==){printf("0\n");continue;}
if(a==b){printf("1\n");continue;}
LL w=(LL)sqrt((double)p),x=,y=;if(w*w!=p)++w;
q[b]=-;
for(int i=;i<=w;++i){x=(x*a)%p;LL z=(b*x)%p;if(q[z]==)q[z]=i;}
bool f=;
for(int i=;i<=w;++i){
y=(y*x)%p;
if(q[y]!=){
LL z=q[y];if(z==-) z=;
z=(LL)i*w-z;f=;
printf("%lld\n",z);
break;
}
}
q[b]=;x=;
for(int i=;i<=w;++i){x=(x*a)%p;LL z=(b*x)%p;q[z]=;}
if(!f)printf("no solution\n");
}
return ;
}
POJ 2417 Discrete Logging BSGS的更多相关文章
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2819 Accepted: 1386 ...
- POJ - 2417 Discrete Logging(Baby-Step Giant-Step)
d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- POJ 2417 Discrete Logging ( Baby step giant step )
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3696 Accepted: 1727 ...
- POJ 2417 Discrete Logging 离散对数
链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...
- poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)
http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B.C.求x. 这里C是素数,能够用普通的baby_step. 在寻找最小的x的过程中,将x设为i* ...
- POJ 2417 Discrete Logging(离散对数-小步大步算法)
Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...
- POJ 2417 Discrete Logging
http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ...
随机推荐
- BackBone结合ASP.NET MVC实现页面路由操作
1. 问题的背景 什么是页面路由操作,就是通过浏览器地址栏的标记来实现页面内部的一些操作,这些操作具有异步性和持久性.应用场景主要有页面操作过程中的添加收藏夹的操作.后退操作等过程中能完全恢复界面. ...
- 深入理解linux内核v4l2框架之videobuf2【转】
转自:https://blog.csdn.net/ramon1892/article/details/8444193 Videobuf2框架 1. 什么是videobuf2框架? 它是一个针对多媒体设 ...
- ubuntu 禁用 guest 账户
第一步: run the command(s) below: (编辑如下文件) sudo vi /usr/share/lightdm/lightdm.conf.d/50-ubuntu.c ...
- kafka系列十、kafka常用管理命令
一.Topic管理 1.创建topic kafka-topics.sh --zookeeper 47.52.199.52:2181 --create --topic test-15 --replica ...
- CAS 策略已被 .NET Framework 弃用
背景 本来想这里有啥写的,就算了吧.突然看到dev了,我的天啊,这个.net大神,坑了多少开发人员了.功能太强大,以至于后来很长时间我都不知道jquery.当时为了操作dev,为了实现一个功能,都把官 ...
- C# 文件拖放
将控件的属性AllowDrop设置为true,然后添加DragDrop.DragEnter时间处理函数 private void FormMain_DragEnter(object sender, D ...
- hping网络安全工具的安装及使用
hping是用于生成和解析TCPIP协议数据包的开源工具.创作者是Salvatore Sanfilippo.目前最新版是hping3,支持使用tcl脚本自动化地调用其API.hping是安全审计.防火 ...
- 【一通百通】c/php的printf总结
程序语言都是触类旁通的,讲人话就是[一通百通].so今天说说工作中常用的printf的用法吧. 1.先说说PHP printf()函数: printf()函数的调用格式为: printf(" ...
- 【转载】linux下升级npm以及node
原文:http://blog.csdn.net/qq_16339527/article/details/73008708 npm升级 废话不多说,直接讲步骤.先从容易的开始,升级npm. npm这款包 ...
- BOM下的属性和方法---上
-------------BOM------------------------------------------------ 三个系统对话框 浏览器可以通过alert().confirm()和 ...