给定同余式,求它在内的所有解,其中总是素数。

分析:解本同余式的步骤如下

(1)求模的一个原根

(2)利用Baby Step Giant Step求出一个,使得,因为为素数,所以有唯一解。

(3)设,这样就有,其中,那么得到

(4)求出所有的,可以知道一共有个解,我们求出所有的,然后排个序即可。

  O(sqrt(n))的时间复杂度

  BSGS如下(前向星版本)

  

const maxn=;

type node=record
data,next,id:longint;
end; type LL=int64; var edge:array [..maxn] of node;
head:array [..maxn] of longint;
cnt:longint;
a,b,c:ll; procedure insert(data,id:longint);
var i,k:longint;
begin
k:=data mod maxn;
i:=head[k];
while i<>- do
begin
if edge[i].data=data then exit;
edge[cnt].data:=data;
edge[cnt].id:=id;
edge[cnt].next:=head[k];
head[k]:=cnt;
inc(cnt);
i:=edge[i].next;
end;
end; function find(data:ll):longint;
var i,k:longint;
begin
k:=data mod maxn;
i:=head[k];
while i<>- do
begin
if edge[i].data=data then exit(edge[i].id);
i:=edge[i].next;
end;
exit(-);
end; procedure extend_gcd(a,b:ll;var x,y:ll);
var t:ll;
begin
if b= then
begin
x:=;
y:=;
exit;
end;
extend_gcd(b,a mod b,x,y);
t:=x;
x:=y;
y:=t-(a div b)*y;
end; function gcd(x,y:ll):ll;
begin
if x mod y= then exit(y)
else exit(gcd(y,x mod y));
end; function modd(x,p:ll):ll;
begin
if x>=p then exit(x mod p);
if x< then exit((x mod p+p) mod p);
exit(x);
end; function quick_mod(a,n,p:ll):ll;
var ans,t:ll;
begin
ans:=; t:=modd(a,p);
while n<> do
begin
if (n and )= then ans:=modd(ans*t,c);
n:=n>>;
t:=modd(t*t,c);
end;
exit(ans);
end; function bsgs(a,b,c:ll):ll;
var x,y,k,t,d,len,m:ll; i:longint;
begin
fillchar(head,sizeof(head),$ff);
cnt:=;
b:=modd(b,c);
for i:= to do
begin
if b=t then exit(i);
t:=modd(t*a,c);
end;
d:=; len:=;
while true do
begin
t:=gcd(a,c);
if t= then break;
if (b mod t)<> then exit(-);
c:=c div t;
b:=b div t;
d:=modd(d*a div t,c);
inc(len);
end;
m:=trunc(sqrt(c));
t:=;
for i:= to m do
begin
insert(t,i);
t:=modd(t*a,c);
end;
k:=quick_mod(a,m,c);
for i:= to m do
begin
extend_gcd(d,c,x,y);
t:=modd(b*x,c);
if (y=find(t)) and (y<>-) then exit(i*m+y+len);
d:=modd(d*k,c);
end;
exit(-);
end; begin
readln(a,b,c);
writeln(bsgs(a,b,c));
end.

Baby Step Gaint Step的更多相关文章

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

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

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

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

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

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

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

  5. 【POJ2417】baby step giant step

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

  6. [置顶] hdu2815 扩展Baby step,Giant step入门

    题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会 ...

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

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

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

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

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

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

随机推荐

  1. 使用x manager 连接Linux桌面

    /usr/bin/xterm -ls -display $DISPLAY 需要安装xterm 服务

  2. Chrome NativeClient创建 (转)

    Chrome NativeClient创建 该demo目标是让chrome扩展启动本地exe 1创建一个名叫nativeMsgDemo的控制台程序 #include <Windows.h> ...

  3. ExtJS4.2学习(二)——入门基础

    1.工程的目录结构: src里放后台的文件,WebRoot里放前台的文件. index.html或者index.jsp等是整个项目的首页,在首页我们要引入ExtJS的CSS样式文件和ExtJS的核心类 ...

  4. ThinkPHP第二十二天(表单令牌、相对路径、扩展配置载入、$Think获取系统变量、$_SERVER('HTTP_REFERER')前页地址)

    1.表单令牌开启配置 'TOKEN_ON'=>true 2.相对路径:在thinkphp中,存在单入口index.php,所以程序中的根目录都是以index.php所在的文件夹为根目录,故用./ ...

  5. 关于CSS动画几点要注意的地方

    关于CSS动画几点要注意的地方 js操作transition无效果 先看这个demo以及stackoverflow的问题 http://jsfiddle.net/ThinkingStiff/QNnnQ ...

  6. 常用CSS HACK

    常用CSS HACK IE6 3像素bug和双边距bug一样的经典 现象: IE6下浮动元素和不浮动元素之间会有3px间隙(3px bug,div.float-left + div.float-non ...

  7. .NET(C#):XmlArrayItem特性和XmlElement特性在序列化数组的差别

    原文http://www.cnblogs.com/mgen/archive/2011/12/04/2276238.html 比如这样一个类,我们用XmlArrayItem特性标明数组内出现的元素类型: ...

  8. Hive Server 2 安装部署测试

    Hive 0.11 包含了Hive Server 1 和 Hive Server 2,还包含1的原因是为了做到向下兼容性.从长远来看都会以Hive Server 2作为首选 1. 配置hive ser ...

  9. 在wdcp环境下架设VSFTPD虚拟用户只上传功能服务器

    检查系统是否已安装vsftp rpm -q vsftpd package vsftpd is not installed #说明系统没有安装vsftpd 如果生成虚拟用户数据文件的时候出现以下错误 u ...

  10. IOS框架概览

    iOS是执行在iPhone.iPod Touch或iPad上的操作系统,之前叫做iPhone OS,iOS与Mac OS X有共同的基础架构和底层技术.但iOS是依据移动设备的特点而设计的,所以和Ma ...