考虑一个问题: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. JVM 内存区域划分

    一.运行时数据区包括哪几部分? 根据<Java虚拟机规范>的规定,运行时数据区通常包括这几个部分:程序计数器(Program Counter Register).Java栈(VM Stac ...

  2. xml操作-Nested exception: org.xml.sax.SAXParseException: White spaces are required between publicId and systemId. 异常处理

    异常如下: org.dom4j.DocumentException: Error on line 2 of document file:///D:/workspaces/struts2/lesson0 ...

  3. mysql自动增长的有关问题,怎么恢复从1开始

    mysql自动增长的问题,如何恢复从1开始在一个表中我设置到autoid为自动增长列例如有如下数据 1 张三 男 202 王五 男 223 李四 男 254 陈大 男 19 现在我把 autoid=3 ...

  4. IE bug集锦

    ie8 iframe 不显示 问题描述: IE8的非兼容模式下(兼容模式是ie7,不存在),iframe会不显示: 可以通过Ctrl+A全选或者是调整窗口大小显示出来. 解决办法: 这是由于要显示的i ...

  5. oracle sql*loader的使用

    用法: SQLLDR keyword=value [,keyword=value,...] 有效的关键字:     userid -- ORACLE 用户名/口令    control -- 控制文件 ...

  6. Codeforces_The least round way

    B. The least round way time limit per test 2 seconds memory limit per test 64 megabytes input standa ...

  7. vs Could Not Connect

    解决,   在win7上卸载IIS 10.0 Express ,安装 IIS7.5 Express

  8. 【转载】Linux 通过mount -o loop 配置本地.iso镜像为yum源(yum仓库)

    原文地址:https://www.jb51.net/os/RedHat/2682_all.html 如果拷贝到本地,可以使用mount mount fileName mountPoint -o loo ...

  9. How To : Modify ASM SYS password using asmcmd 11g R2 and upper

    修改RAC 11gR2及以上版本的ASM的SYS的密码方法 [grid]$ asmcmd ASMCMD> orapwusr --modify --password sys Enter passw ...

  10. Bequeath Connection and SYS Logon

    The following example illustrates how to use the internal_logon and SYSDBA arguments to specify the ...