考虑一个问题: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 算法的更多相关文章

  1. Baby Step Gaint Step

    给定同余式,求它在内的所有解,其中总是素数. 分析:解本同余式的步骤如下 (1)求模的一个原根 (2)利用Baby Step Giant Step求出一个,使得,因为为素数,所以有唯一解. (3)设, ...

  2. BSGS算法 (小步大步 Baby Step Gaint Step)

    当你要求满足: $$ A^x \equiv B \ (\bmod \ P) $$ 的最小非负整数 x (gcd(A,P)==1)就可以用到 BSGS 了 设 $ m=\sqrt{P} $ 向上取整 处 ...

  3. 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 ...

  4. 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法

    先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝  扩展Baby Step Gian ...

  5. 【POJ2417】baby step giant step

    最近在学习数论,然而发现之前学的baby step giant step又忘了,于是去翻了翻以前的代码,又复习了一下. 觉得总是忘记是因为没有彻底理解啊. 注意baby step giant step ...

  6. HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法

    联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  7. 『高次同余方程 Baby Step Giant Step算法』

    高次同余方程 一般来说,高次同余方程分\(a^x \equiv b(mod\ p)\)和\(x^a \equiv b(mod\ p)\)两种,其中后者的难度较大,本片博客仅将介绍第一类方程的解决方法. ...

  8. HDU 2815 扩展baby step giant step 算法

    题目大意就是求 a^x = b(mod c) 中的x 用一般的baby step giant step 算法会超时 这里参考的是http://hi.baidu.com/aekdycoin/item/2 ...

  9. 【学习笔记】Baby Step Giant Step算法及其扩展

    1. 引入 Baby Step Giant Step算法(简称BSGS),用于求解形如\(a^x\equiv b\pmod p\)(\(a,b,p\in \mathbb{N}\))的同余方程,即著名的 ...

  10. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

随机推荐

  1. 实现PC延迟执行函数

    头文件内容: #pragma once typedef function<void ()> DelayClickHandler; typedef void (*pDelayFun)(); ...

  2. tomcat报错org.springframework.web.context.ContextLoaderListener找不到

    tomcat报错org.springframework.web.context.ContextLoaderListener找不到. 最后解决办法:将jar包copy到web-inf下面的lib中. 你 ...

  3. 移动web——touch事件应用

    基本概况 1.touch事件在移动端被用来代替click事件,因为click事件的触发会延迟影响了用户体验 2.touch事件还可以与translate构成吸附效果 3.现行有一种排版方式是左边宽度是 ...

  4. 7、scala面向对象编程之类

    1.  定义一个简单的类 2.getter与setter 3.自定义getter与setter方法 4.仅暴露field的getter方法 5.private[this]的使用 6.Java风格的ge ...

  5. 【技术累积】【点】【java】【22】UUID

    基础概念&使用 UUID是Universally Unique Identifier的缩写,它是在一定的范围内(从特定的名字空间到全球)唯一的机器生成的标识符. 说白了就是个唯一键,只不过到处 ...

  6. sqlserver 2014 删除主键约束

    truncate table menu SELECT * FROM sys.foreign_keys WHERE referenced_object_id=OBJECT_ID('menu'); --找 ...

  7. excel求1加到100的和

    强大的数组公式

  8. day04-交互、格式化输出及基本运算符

    目录 与用户交互 python2和python3交互的区别 格式化输出 1 字符串拼接 2 占位符 3 format格式 4 f-string格式 基本运算符 算术运算符 比较运算符 赋值运算符 逻辑 ...

  9. R语言图表

    条形图 在R语言中创建条形图的基本语法是 barplot(H, xlab, ylab, main, names.arg, col) H是包含在条形图中使用的数值的向量或矩阵 xlab是x轴的标签 yl ...

  10. CorelDRAW快速制作抖音幻影图像效果

    本教程讲解非常受欢迎的幻影图像效果(Anaglyph 3d),也叫图像分色立体效果,这其中我们要用到CorelDRAW中的透明度工具. 在开始实施Anaglyph效应之前,应当知道,Anaglyph  ...