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

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

(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. 解决全站ie6下PNG图片不透明问题只要几行代码

    解决全站ie6下PNG图片不透明问题只要复制下面这几行代码粘贴在你的文档最底部,需要用到的包DD_belatedPNG_0.0.8a.js自己网上下载吧 代码走起 /*在文档底部加入以下代码*/ &l ...

  2. codeforces 633G. Yash And Trees dfs序+线段树+bitset

    题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...

  3. Java NIO read/write file through FileChannel

    referee:  Java NIO FileChannel A java nio FileChannel is an channel that is connected to a file. Usi ...

  4. Python2.7.3 学习——准备开发环境

    安装环境搭建参考:http://blog.163.com/sunshine_linting/blog/static/4489332320129187464222/ 第一种方式,通过命令行方式安装Pyt ...

  5. mysql alter table

    准备: create table t(x int,y int); 用法 1: 修改列的数据类 alter table t modify column y nvarchar(32); 用法2: 给表加一 ...

  6. Linux平台下使用rman进行oracle数据库迁移

        实验目的:将oracle数据库从一台机器迁移到另外的一台机器(同为linux平台),设置为不同的路径,不同的实例名 源端: ORACLE_BASE=/u01/app/oracle ORACLE ...

  7. JAVA泛型接口

    事例代码: package com.xt.thins_15_3; import java.util.Iterator; /** * 泛型接口 * * @author xue * * @param &l ...

  8. NAND Flash中常用的纠错方式(ECC算法)

    Hanming,RS,BCH —— NAND Flash中常用的纠错方式 因为闪存中会有出错的可能,如果没有使用ECC模块,读出的数据和写入的数据会有不匹配的可能,也许一个文件中只有一两个bit不匹配 ...

  9. POJ 1721 CARDS(置换群)

    [题目链接] http://poj.org/problem?id=1721 [题目大意] 给出a[i]=a[a[i]]变换s次后的序列,求原序列 [题解] 置换存在循环节,因此我们先求出循环节长度,置 ...

  10. cocos2d-x过程动作CCProgressTo示例学习笔记

    // // SpriteProgressToRadial // //------------------------------------------------------------------ ...