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的更多相关文章

  1. BSGS算法+逆元 POJ 2417 Discrete Logging

    POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accept ...

  2. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  3. 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 ...

  4. poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2831   Accepted: 1391 ...

  5. POJ 2417 Discrete Logging ( Baby step giant step )

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

  6. POJ 2417 Discrete Logging 离散对数

    链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...

  7. 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* ...

  8. POJ 2417 Discrete Logging(离散对数-小步大步算法)

    Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...

  9. POJ 2417 Discrete Logging

    http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ...

随机推荐

  1. K-means聚类算法原理和C++实现

    给定训练集$\{x^{(1)},...,x^{(m)}\}$,想把这些样本分成不同的子集,即聚类,$x^{(i)}\in\mathbb{R^{n}}$,但是这是个无标签数据集,也就是说我们再聚类的时候 ...

  2. Three.js基础探寻二——正交投影照相机

    本篇主要介绍照相机中的正交投影照相机. 第一篇传送门:Three.js基础探寻一 1.照相机 图形学中的照相机定义了三维空间到二维屏幕的投影方式. 针对投影方式照相机分为正交投影照相机和透视投影照相机 ...

  3. ubuntu 安装lightgbm

    前提:安装好git.cmake 1. 安装lightgbm git clone --recursive https://github.com/Microsoft/LightGBM 2. 编译 下载好的 ...

  4. Linux input子系统简介

    1.前言 本文主要对Linux下的input子系统进行介绍 2. 软件架构 图 input子系统结构图 input子系统主要包括三个部分:设备驱动层.核心层和事件层.我们可以分别理解为:具体的输入设备 ...

  5. linux内核驱动中对字符串的操作【转】

    转自:http://www.360doc.com/content/12/1224/10/3478092_255969530.shtml Linux内核中关于字符串的相关操作,首先包含头文件: #inc ...

  6. mysql系列四、mySQL四舍五入函数用法总结

    一.MySQL四舍五入函数ROUND(x) ROUND(x)函数返回最接近于参数x的整数,对x值进行四舍五入. 实例: 使用ROUND(x)函数对操作数进行四舍五入操作.SQL语句如下: mysql& ...

  7. 006_nginx动态upstream和安全检查模块

    一.参考Tengine   http://tengine.taobao.org/document_cn/http_dyups_cn.html ngx_http_dyups_module Descrip ...

  8. tomcat生产环境JDK部署及虚拟主机等常用配置详解

    jdk和tomcat环境部署: 1.删除系统自带的openjdk # java -version java version "1.7.0_45" OpenJDK Runtime E ...

  9. 错误代码 1045 Access denied for user 'root'@'localhost' (using password:YES)

    1 前言 现象是用MySQL 5.7 Command Line Client可以使用root账号进入,但是其它navicat,phpsqladmin,mysql workbench,heidisql用 ...

  10. PYTHON-文件指针的移动,移动和函数基础

    # 文件内指针的移动 #大前提:文件内指针的移动是Bytes为单位的,唯独t模式下的read读取内容个数是以字符为单位 # f.seek(指针移动的字节数,模式控制): 控制文件指针的移动# 模式控制 ...