【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; ...
随机推荐
- 【Unity3D技术文档翻译】第1.6篇 使用 AssetBundle Manager
上一章:[Unity3D技术文档翻译]第1.5篇 使用 AssetBundles 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced Develo ...
- Android App 压力测试方法(Monkey)
一.为什么要开展压力测试 a.提高产品的稳定性:b.提高产品的留存率 二.什么时候开展压力测试 a.首轮功能测试通过后:b.下班后的夜间进行 三.7个基础知识(理论部分) 3.1 手动测试场景与自动测 ...
- mysql3 - 常规数据检索、常见操作与函数
一.常规数据检索 二.常见操作与函数
- 构造N位格雷码(递归,面向对象)
问题:递归打印出N位格雷码(相邻两个编码只有一位数字不同): 问题化归为:现有前N位的格雷码,如何构造N+1位的格雷码? 解决方法:采用递归构造格雷码集和. 递归出口:n = 1; 此时格雷码{0,1 ...
- Java集合框架(三)—— List、ArrayList、Vector、Stack
List接口 List集合代表一个有序集合,集合中每一个元素都有其对应的顺序索引.List集合容许使用重复元素,可以通过索引来访问指定位置的集合对象. ArrayList和Vector实现类 Arra ...
- 记录 serverSocket socket 输入,输出流,关闭顺序,阻塞,PrintWriter的一些问题.
关于socket.getOutputStream() 的一些问题, OutputStream的flush是一个空方法,所以需要另一个实现了Flush的流来包装一下 这里为什么使用PrintWriter ...
- 使用phpstorm提交svn代码版本管理系统遇到的问题解决办法
1.当自己提交代码的时候显示out of date的时候,表示我们本地的代码过时啦,需要更新一下再提交. 即:更新一下再提交即可. 2.当自己的代码和服务器上的冲突的时候,我们右键点击冲突的文件,选择 ...
- 基于Python的Flask的开发实战(第一节Flask安装)
1.安装python虚拟环境 easy_install virtualenv easy_install pip cd /home/admin virtualenv flask-website sour ...
- react 父子组件互相通信
1,父组件向子组件传递 在引用子组件的时候传递,相当于一个属性,例如: class Parent extends Component{ state = { msg: 'start' }; render ...
- R︱高效数据操作——data.table包(实战心得、dplyr对比、key灵活用法、数据合并)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于业务中接触的数据量很大,于是不得不转战开始 ...