POJ2417 Baby-Step-Gaint-Step 算法
考虑一个问题:A^x%p=B,给定A,B,p,求x的最小非负整数解。
在p是质数的情况下,这个问题比較简单。
A^x=B(mod P) (P is a Prime, A,B<P)
Let m = floor(sqrt(P))
Put A^0,A^1,...A^(m-1) into HashSet(You Can Also Use Map in STL),for Example M[A^i]=i.
if A^i=A^j(i<j), M[A^i=A^j]=i.
Enumerate i, Let x=i*m+j, A^(i*m)*A^j=B(mod C)
Let E=A^(i*m),F=A^j,E*F=B(mod P) because (E,C)=1,(E,C)|B,we can use EXTgcd to get F.
If F is in the HashSet,then the minimum answer x=i*m+M[F].
Because P is a Prime, and A,B<P, then A^(P-1)=1(mod P).
Then if a answer exists, the minimum answer must less then P.
So the range of i is [0,P/m].
If for all i we cannot find a answer, then no solution.
我亲手胡乱写的东西,能看懂才怪!
再附上代码吧:(我的小数据范围是暴力的)
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
inline void Exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (!b) { d = a, x = 1, y = 0; }
else { Exgcd(b, a % b, d, y, x), y -= x * (a / b); }
}
inline LL gcd(LL a, LL b) {
return (!b) ? a : gcd(b, a % b);
}
inline LL Solve(LL a, LL b, LL c) { // ax=b(mod c)
LL d, x, y;
Exgcd(a, c, d, x, y);
return (x + c) % c * b % c;
}
LL ksm(LL x, LL y, LL p) {
LL res = 1, t = x;
for(; y; y >>= 1) {
if (y & 1) res = res * t % p;
t = t * t % p;
}
return res;
} const int mod = 13131;
struct Hashset {
int head[mod], next[40010], f[40010], v[40010], ind;
void reset() {
ind = 0;
memset(head, -1, sizeof head);
}
void insert(int x, int _v) {
int ins = x % mod;
for(int j = head[ins]; j != -1; j = next[j])
if (f[j] == x) {
v[j] = min(v[j], _v);
return;
}
f[ind] = x, v[ind] = _v;
next[ind] = head[ins], head[ins] = ind++;
}
int operator [] (const int &x) const {
int ins = x % mod;
for(int j = head[ins]; j != -1; j = next[j])
if (f[j] == x)
return v[j];
return -1;
}
}S; int main() {
LL A, B, C;
LL i;
while(scanf("%I64d%I64d%I64d", &C, &A, &B) == 3) {
if (C <= 100) {
LL d = 1;
bool find = 0;
for(i = 0; i < C; ++i) {
if (d == B) {
find = 1;
printf("%I64d\n", i);
break;
}
d = d * A % C;
}
if (!find)
puts("no solution");
}
else {
int m = (int)sqrt(C);
S.reset();
LL d = 1;
for(i = 0; i < m; ++i) {
S.insert(d, i);
d = d * A % C;
}
bool find = 0;
int ins;
for(i = 0; i * m < C; ++i) {
LL t = Solve(ksm(A, i * m, C), B, C);
if ((ins = S[t]) != -1) {
printf("%I64d\n", i * m + ins);
find = 1;
break;
}
}
if (!find)
puts("no solution");
}
} return 0;
}
POJ2417 Baby-Step-Gaint-Step 算法的更多相关文章
- Baby Step Gaint Step
给定同余式,求它在内的所有解,其中总是素数. 分析:解本同余式的步骤如下 (1)求模的一个原根 (2)利用Baby Step Giant Step求出一个,使得,因为为素数,所以有唯一解. (3)设, ...
- BSGS算法 (小步大步 Baby Step Gaint Step)
当你要求满足: $$ A^x \equiv B \ (\bmod \ P) $$ 的最小非负整数 x (gcd(A,P)==1)就可以用到 BSGS 了 设 $ m=\sqrt{P} $ 向上取整 处 ...
- POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)
不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...
- 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法
先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝 扩展Baby Step Gian ...
- 【POJ2417】baby step giant step
最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- 『高次同余方程 Baby Step Giant Step算法』
高次同余方程 一般来说,高次同余方程分\(a^x \equiv b(mod\ p)\)和\(x^a \equiv b(mod\ p)\)两种,其中后者的难度较大,本片博客仅将介绍第一类方程的解决方法. ...
- HDU 2815 扩展baby step giant step 算法
题目大意就是求 a^x = b(mod c) 中的x 用一般的baby step giant step 算法会超时 这里参考的是http://hi.baidu.com/aekdycoin/item/2 ...
- 【学习笔记】Baby Step Giant Step算法及其扩展
1. 引入 Baby Step Giant Step算法(简称BSGS),用于求解形如\(a^x\equiv b\pmod p\)(\(a,b,p\in \mathbb{N}\))的同余方程,即著名的 ...
- 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)
什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...
随机推荐
- 【转】Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...
- jQuery五屏轮播手风琴切换代码
jQuery五屏轮播手风琴切换代码 在线演示本地下载
- 正则表达式 \D 元字符
\D元字符可以匹配非数字字符,等价于"[^0-9]". 语法结构: (1).构造函数方式: new RegExp("\\D") (2).对象直接量方式: /\D ...
- SAS学习笔记之《SAS编程与数据挖掘商业案例》(1)系统简介和编程基础
SAS学习笔记之<SAS编程与数据挖掘商业案例>(1)系统简介和编程基础 1. SAS系统简介 1.1 SAS是先编译后执行的语言,data步标志着编译的开始. 数据指针:当前内存缓存区, ...
- 一个有趣的 ”Validation of viewstate MAC failed” 错误的发现和解决
在ASP.NET里面,View State使用较为广泛.它作为一个隐藏字段,可以帮助服务端”记住“客户端的改变,这样客户端 收到服务器对PostBack的响应后,仍然可以展现在PostBack之前设定 ...
- Style在Android中的继承关系
Style在Android中的继承关系 Android的Styles(样式)和Themes(主题)非常类似Web开发里的CSS,方便开发者将页面内容和布局呈现分开.Style和Theme在Androi ...
- C# 字符串到字节数组,字节数组转整型
; , ); byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组 num = BitConverter.ToInt32(bytes, ) ...
- 解决 i5 6500 安装黑苹果 Sierra 显卡不正常问题
i5 6500内置HD 530显卡,装好Sierra显卡驱动不太正常. 先下载Clover configurator 用Clover configurator加载 EFI (Mount EFI)分区 ...
- Java中字符串的常用属性与方法
•字符串常用的属性 string.length()————>返回字符串的长度,int类型. •字符串常用的方法 String.contains(";")——————>判 ...
- 谈一谈a:link、a:visited、a:hover、a:active的正确使用顺序
前端路上,未来还远,所以基础部分必须扎实,走好现在脚下的每一步才是现在最重要的. 下面进入正题吧. 1. <a>标签 我们先说一说<a>标签是干啥用的. <a> 标 ...