BZOJ 3239--Discrete Logging(BSGS)
3239: Discrete Logging
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 635 Solved: 413
[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
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587
题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=3239
Solution
BSGS的模板题。。。。
对于本题的做法。。一般是先设 L = i * e + j 或 L = i * e - j 。。。
e = ceil(sqrt(P))。。就是假如算出 sqrt(P)= 1.14 ,e就等于2,往大的取整
然后枚举 i 和 j 的值就能做到 O(sqrt(P))。。。
代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
#define LL long long
using namespace std; LL P,B,N,e,now;
map<LL,int>mp;
LL pow(LL p,LL q){
LL s=1;
while(q){
if(q&1) s=s*p%P;
q>>=1;
p=p*p%P;
}
return s;
}
void solve(){
mp.clear();
if(N==1 && B>0){
printf("0\n");
return;
}
if( (!B) && (!N) ){printf("1\n");return;}
if(!B){printf("no solution\n");return;}
e=ceil(sqrt(P));
now=N%P;
for(int j=1;j<=e;j++){
now=now*B%P;
if(!mp[now]) mp[now]=j;
}
B=pow(B,e);
now=1;
for(int i=1;i<=e;i++){
now=now*B%P;
if(mp[now]>0){
N=e*i-mp[now];
printf("%lld\n",N);
return;
}
}
printf("no solution\n");
return;
}
int main(){
while(scanf("%lld%lld%lld",&P,&B,&N)!=EOF) solve();
return 0;
}
This passage is made by Iscream-2001.
BZOJ 3239--Discrete Logging(BSGS)的更多相关文章
- BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)
我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...
- BZOJ 3239 Discrete Logging(BSGS)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...
- 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 \)这里-的作用是避 ...
- BZOJ 3239: Discrete Logging [BGSG]
裸题 求\(ind_{n,a}b\),也就是\(a^x \equiv b \pmod n\) 注意这里开根不能直接下取整 这个题少了一些特判也可以过... #include <iostream& ...
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- 【BZOJ】3239: Discrete Logging
http://www.lydsy.com/JudgeOnline/problem.php?id=3239 题意:原题很清楚了= = #include <bits/stdc++.h> usi ...
- bzoj 3239 poj 2417 BSGS
BSGS算法,预处理出ϕ(c)−−−−√内的a的幂,每次再一块一块的往上找,转移时将b乘上逆元,哈希表里O(1)查询即可 #include<cstdio> #include<cstr ...
- POJ 2417 Discrete Logging BSGS
http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...
- poj2417 Discrete Logging BSGS裸题
给a^x == b (mod c)求满足的最小正整数x, 用BSGS求,令m=ceil(sqrt(m)),x=im-j,那么a^(im)=ba^j%p;, 我们先枚举j求出所有的ba^j%p,1< ...
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
随机推荐
- 解决Notepad++ Plugin Manager无法加载插件的方法
今天正好安装了Notepad++,结果发现Plugin Manager里插件一个也没有,网上对应的方法都没有,最后无意间看到Plugin Manager Settings 里的提示,试了一下居然成功了 ...
- Frequently Used Shell Commands
[Common Use Shell Commands] 1.ps aux:查看当前所有进程 ,以用户名为主键.可以查看到 USER.PID.COMMAND(binary所有位置) 2.netstat ...
- java关于密码的加密解密
密码的加密方法有多种,常见的为Aes.Md5 Aes加密,可逆. 其中,Md5加密是采用了散列算法,也就是哈希算法,可以进行多次散列加密.Md5加密是不可逆的,无法解密. MD5是不可逆的单向加密方式 ...
- Rigidbody中 Angular Drag (角阻力):
Rigidbody中 Angular Drag (角阻力):同样指的是空气阻力,只不过是用来阻碍物体旋转的.如果设置成无限的话,物体会立即停止旋转.如果设置成0,物体在上升过程中,会发生侧翻旋转. ...
- nat转发
http://www.revsys.com/writings/quicktips/nat.html https://www.alibabacloud.com/zh/product/nat?spm=a3 ...
- Linux命令:cp (copy)复制文件或目录
复制文件,只有源文件较目的文件的修改时间新时,才复制文件 cp -u -v file1 file2 .将文件file1复制成文件file2 cp file1 file2 .采用交互方式 ...
- RNA-Seq differential expression analysis: An extended review and a software tool RNA-Seq差异表达分析: 扩展评论和软件工具
RNA-Seq differential expression analysis: An extended review and a software tool RNA-Seq差异表达分析: 扩展 ...
- Oracle GoldenGate 四、数据过滤和数据项匹配
写在开始前 从两周前我花了大量的业余时间阅读GoldenGate官方文档,并根据文档实践和进一步学习了解GoldenGate,以下便是根据官方文档理解总结的GoldenGate学习内容: Oracle ...
- BZOJ 1503 郁闷的出纳员 (treap)
1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 13370 Solved: 4808[Submit][Stat ...
- linux源码安装php7.2.0
1. 源码包下载地址 2. 解压php压缩包 tar –zxvf php-7.2.0.tar.gz 3. 进入解压后的 cd php7.2.0 4.安装php需要的扩展 yum install lib ...