【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 729 Solved: 485
[Submit][Status][Discuss]
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 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(baby step giant step)
用于解决离散对数问题即求解ax≡b(mod p)中的x
先处理出sqrt(p)范围内的b*ax的值,丢进map里
然后依次求出k*ak*sqrt(p)(k=1……sqrt(p))看答案是否有出现过
本质上就是分块的思想
代码
//by 减维
#include<set>
#include<map>
#include<queue>
#include<ctime>
#include<cmath>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define il inline
#define rg register
#define db double
#define mpr make_pair
#define maxn
#define inf (1<<30)
#define eps 1e-8
#define pi 3.1415926535897932384626L
using namespace std; inline int read()
{
int ret=;bool fla=;char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-'){fla=;ch=getchar();}
while(ch>=''&&ch<=''){ret=ret*+ch-'';ch=getchar();}
return fla?-ret:ret;
} ll x,y,p; ll ksm(ll x,ll y,ll p)
{
ll ret=;x%=p;
for(;y;y>>=,x=x*x%p)
if(y&) ret=ret*x%p;
return ret;
} ll bsgs(ll x,ll y,ll p)
{
map<ll,ll> mp;
x%=p;y%=p;
if(!x&&!y) return ;
if(y==) return ;
if(!x) return -;
ll m=sqrt(p+0.5);
ll o=y;
for(int i=;i<=m;++i,o=o*x%p)
if(!mp.count(o)) mp[o]=i;
ll tmp=ksm(x,m,p);o=tmp;
for(int i=;i<=m;++i,o=o*tmp%p)
if(mp.count(o)) return ((i*m-mp[o])%p+p)%p;
return -;
} int main()
{
while(scanf("%lld%lld%lld",&p,&x,&y)!=EOF)
{
ll ans=bsgs(x,y,p);
if(ans==-) printf("no solution\n");
else printf("%lld\n",ans);
}
return ;
}
【BSGS】BZOJ3239 Discrete Logging的更多相关文章
- 【BZOJ】3239: Discrete Logging
http://www.lydsy.com/JudgeOnline/problem.php?id=3239 题意:原题很清楚了= = #include <bits/stdc++.h> usi ...
- POJ2417 Discrete Logging【BSGS】
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5577 Accepted: 2494 ...
- POJ2417 Discrete Logging【BSGS】(模板题)
<题目链接> 题目大意: P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P). 解题分析: 这题是bsgs算法的模板题. #incl ...
- bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】
都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...
- BZOJ2242 [SDOI2011]计算器 【BSGS】
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 4741 Solved: 1796 [Submit][Sta ...
- Python开发【杂货铺】:模块logging
logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...
- 【Python】 日志管理logging
logging *****本文参考了http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html ■ 最最基本的用法 logging模块用 ...
- 【python】实用的logging封装
#!/usr/bin/python import logging import logging.handlers def set_logger(filename, logmod): log_size ...
- Codeforces1106F 【BSGS】【矩阵快速幂】【exgcd】
首先矩阵快速幂可以算出来第k项的指数,然后可以利用原根的性质,用bsgs和exgcd把答案解出来 #include<bits/stdc++.h> using namespace std; ...
随机推荐
- Navi.Soft31.任务管理器(定时同步+数据采集)
1系统简介 1.1功能简述 在众多的软件分类中,有几类的软件不是很重要,但也很重要.它们有的是每隔一段时间需要执行一些任务的软件,我们叫它定时类软件:还有一种软件是采集网页中的数据,我们叫它采集类软件 ...
- Java经典编程题50道之十三
一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? public class Example13 { public static void main(St ...
- unix网络编程环境搭建
unix网络编程环境搭建 网络编程 环境 1.点击下载源代码 可以通过下列官网中的源代码目录下载最新代码: http://www.unpbook.com/src.html 2.解压文件 tar -xz ...
- 《设计模式之禅》--设计模式大PK
创建类模式包括工厂方法模式.建造者模式.抽象工厂模式.单例模式和原型模式. 其中单例模式要保持在内存中只有一个对象,原型模式是要求通过复制的方式产生一个新的对象. [工厂方法(抽象工厂) VS 建造者 ...
- uva211 回溯
大致题意:每个多米诺骨牌可能横着,也可能竖着,请你判断有哪些合法的摆放方式. 这题的dfs需要注意一下,不能以某个点直接开始延伸,如果这样延伸可能会无法到达终点(也就是遍历全图).我的dfs方法就是枚 ...
- java 集合类Array、List、Map区别和优缺点
Java集合类主要分为以下三类: 第一类:Array.Arrays 第二类:Collection :List.Set第三类:Map :HashMap.HashTable 一.Array , Array ...
- 思科ASA5520防火墙telnet、SSH及DHCP设置
ASA5520远程登录telnet 注:最低安全级别的接口不支持telnet登陆,如OutsideASA(config)# telnet 172.16.0.0 255.255.0.0 inside ...
- linux 运维 nginx服务器
nginx(web服务器) nginx是一个高性能的http和反向代理服务器,同时也是一个imap/pop3/smtp 代理服务器比apache简单官网:http://nginx.org nginx配 ...
- Java Web项目部署Tomcat运行出错
1.在部署Java Web项目的过程中,启动Tomcat出现报错提示 具体报错如下: Could not load the Tomcat server configuration at \Server ...
- Flex和Servlet结合上传文件报错(一)
1.具体错误如下 一个表单域 不是一个表单域 java.io.FileNotFoundException: D:\MyEclipse\workspace\FlexFileUpload\Web\null ...