POJ2417 Discrete Logging【BSGS】
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5577 | Accepted: 2494 |
Description
B
L
== N (mod P)
Input
Output
Sample Input
5 2 1
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
Hint
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) .
Source
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
struct Thash{
static const int MOD=;
static const int MAXN=1e6+;
int tot,head[MOD+],next[MAXN],h[MAXN],val[MAXN];
inline void clear(){tot=;memset(head,,sizeof head);}
inline void insert(int H,int VAL){
for(int i=head[H%MOD];i;i=next[i]) if(h[i]==H){val[i]=VAL;return ;}
h[++tot]=H;val[tot]=VAL;next[tot]=head[H%MOD];head[H%MOD]=tot;
}
inline int get(int H){
for(int i=head[H%MOD];i;i=next[i]) if(h[i]==H) return val[i];
return ;
}
}M;
inline ll fpow(ll a,ll p,ll mod){
int res=;
for(;p;p>>=,a=a*a%mod) if(p&) res=res*a%mod;
return res;
}
int BSGS(ll A,ll B,ll mod){
A%=mod;
if(!A){
if(!B) return ;
return -;
}
ll m=sqrt(mod)+,ni=fpow(A,mod-m-,mod);
ll t=,y=;
M.clear();
M.insert(,m+);
for(int i=;i<m;i++){
t=t*A%mod;
if(!M.get(t)) M.insert(t,i);
}
for(int i=;i<m;i++){
int u=M.get(B*y%mod);
if(u){
if(u==m+) u=;
return i*m+u;
}
y=y*ni%mod;
}
return -;
}
int main(){
int a,b,c,ans(-);
while(scanf("%d%d%d",&c,&a,&b)==){
ans=BSGS(a,b,c);
if(~ans) printf("%d\n",ans);
else puts("no solution");
}
return ;
}
POJ2417 Discrete Logging【BSGS】的更多相关文章
- POJ2417 Discrete Logging【BSGS】(模板题)
<题目链接> 题目大意: P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P). 解题分析: 这题是bsgs算法的模板题. #incl ...
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
- BZOJ 3239 Discrete Logging(BSGS)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...
- [POJ2417]Discrete Logging(指数级同余方程)
Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...
- 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 \)这里-的作用是避 ...
- 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< ...
- POJ2417 Discrete Logging | A,C互质的bsgs算法
题目: 给出A,B,C 求最小的x使得Ax=B (mod C) 题解: bsgs算法的模板题 bsgs 全称:Baby-step giant-step 把这种问题的规模降低到了sqrt(n)级别 首 ...
- BZOJ2242 [SDOI2011]计算器 【BSGS】
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 4741 Solved: 1796 [Submit][Sta ...
- POJ2417 Discrete Logging
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
随机推荐
- [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript
Sometimes we might want to make a function more generic by having it accept a union of different typ ...
- Mysql 中文中繁杂的字 插入报错的 解决方案
首先 数据库默认编码选用 utf8 连接字符串也相应改成utf8,不能是gb2312
- B1:模板方法模式 TemplateMethod
定义一个操作中的算法骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤 应用场景: A.操作步骤稳定,而具体细节延迟到子类. UML: 示例代码: ...
- attempt to dereference a generic a pointer(转)
在Linux下的GDB环境中,用p 命令查看一个void *型的变量的时候,提示为: "attempt to dereference a generic a ...
- Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net
Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net 1. Base64编码, 1 1.1. 子模式 urlsafe Or url ...
- Delphi7 Just In Time debugger 与VS冲突
这是设定系统当前调试器的,你可以在注册表中自己设置一下,位置是:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDe ...
- C语言变长參数的认识以及宏实现
1.认识 变长參数是C语言的特殊參数形式.比如例如以下函数声明: int printf(const char *format, ....); 如此的声明表明,printf函数除了第一个參数类型为con ...
- python模块学习之re
正则表达式本质就是表示某种规则的一串字符. 匹配的规则叫做模式(pattern),模式作用于对象. 模式和对象可以是Unicode或者字节,但是,不能够混用,比如:模式为Unicode,对象为字节,像 ...
- Linux下同步模式、异步模式、阻塞调用、非阻塞调用总结
转自:http://www.360doc.com/content/13/0117/12/5073814_260691714.shtml 同步和异步:与消息的通知机制有关. 本质区别 现实例子 同步模式 ...
- zookeeper 批量启动的脚本
#!/bin/shecho "start zkServer"for i in 2 3 4dossh mini$i "source /etc/profile;/usr/l ...