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 ...
随机推荐
- ARMV8 datasheet学习笔记2:概述
1. 前言 本文主要概括的介绍ARMV8体系结构定义了哪些内容,概括的说: ARM体系结构定义了PE的行为,不会定义具体的实现 ARM体系结构也定义了debug体系结构和trace体系结构 ARM体系 ...
- genstr.py
#!/usr/bin/python #-*- coding:utf-8 –*- import os import sys import re import shutil import xlrd imp ...
- Visual Studio 2017 + Python3.6安装scipy库
Windows10下安装scipy很麻烦,直接在命令行下使用pip install scipy无法安装,但可以借助VS2017的集成环境来安装. (1)首先在Visual Studio Install ...
- Go语言规格说明书 之 类型(Types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...
- WebMvcConfigurerAdapter已过时
Spring Boot2.0的版本(创建的时候自动选择的这个版本),然后编译器告诉我WebMvcConfigurerAdapter已过时了 @Deprecated public abstract cl ...
- PYTHON-匿名函数,递归与二分法,面向过程编程-练习
# 四 声明式编程练习题 # 1.将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写names = ['egon', 'alex_sb', ' ...
- Inno Setup 系列之安装、卸载时调用bat
需求 想在安装的时候调用install.bat,在卸载的时候调用uninstall.bat 解决 可以这样写 Inno Setup 的脚本: [Setup] ; NOTE: The value of ...
- SPLAY,LCT学习笔记(一)
写了两周数据结构,感觉要死掉了,赶紧总结一下,要不都没学明白. SPLAY专题: 例:NOI2005 维修数列 典型的SPLAY问题,而且综合了SPLAY常见的所有操作,特别适合新手入门学习(比如我这 ...
- apache tomcat 集群!
公司需要一个内部测试局域网, 要求可以支持3000并发访问!以前也没做过服务器这方面.临时抱佛脚,查看了N多文档,他人经验,布置好之后,又遇到了N多问题,功夫不负有心人.终于还是完成了要求!观他人的布 ...
- kotlin 插件更新到 1.2.41 程序出错 Please use kotlin-stdlib-jdk7 instead
buildscript { ext.kotlin_version = '1.2.41' repositories { google() jcenter() } dependencies { class ...