给定方程 X^A = B (mol C)  ,求 在[0,C) 中所有的解 , 并且C为质数。

设 rt 为 C 的原根 , 则 X = rt^x  (这里相当于求 A^x =B (mol C) 用大步小步算法即可)

那么 ( rt^x ) ^ A = b (mol C)

   rt^Ax = b (mol C)

由费马小定理, 设 Ax = (C-1)*y +t1   ---------------- ( * )

可得  rt^t1 =b ( mod C)

这里运用大步小步算法可以计算出 t1 。

得到 t1 后反代会 (*)式 , 利用扩展欧几里得求出符合条件的x解。

由于此方程相当于解 Ax mod (C-1) = t1 , 共用 gcd ( a , C-1 ) 组解。

最后用快速幂计算出所有的X解即可。

 const maxn=;
maxh=;
var a,b,c,rt,t1,t2,x,y,d:int64;
i:longint;
ans,pm,pri:array[..maxn*] of int64;
pd:array[..maxn*] of boolean;
cnt,nm:longint;
h:array[..maxh,..] of int64;
procedure init;
var i,j:longint;
begin
fillchar(pd,sizeof(pd),false);
i:=; nm:=;
while i<=maxn do
begin
inc(nm);
pm[nm]:=i;
j:=i;
while j<=maxn do
begin
pd[j]:=true;
j:=j+i;
end;
while pd[i] do inc(i);
end;
end;
function pow(x,y,p:int64):int64;
var sum:int64;
begin
x:=x mod p;
sum:=;
while y> do
begin
if y and = then sum:=sum*x mod p;
x:=x*x mod p;
y:=y >> ;
end;
exit(sum);
end;
procedure divide(n:int64);
var i:longint;
begin
cnt:=;
i:=;
while pm[i]*pm[i]<=n do
begin
if n mod pm[i]= then
begin
inc(cnt);
pri[cnt]:=pm[i];
while n mod pm[i]= do n:=n div pm[i];
end;
inc(i);
end;
if n> then
begin
inc(cnt);
pri[cnt]:=n;
end;
end;
function findrt(p:int64):int64;
var g,t:int64;
flag:boolean;
begin
divide(p-);
g:=;
while true do
begin
flag:=true;
for i:= to cnt do
begin
t:=(p-) div pri[i];
if pow(g,t,p)= then
begin
flag:=false;
break;
end;
end;
if flag then exit(g);
inc(g);
end;
end;
procedure insert(x,y:int64); inline;
var hash:int64;
begin
hash:=x mod maxh;
while (h[hash,]<>x) and (h[hash,]<>) do hash:=(hash+) mod maxh;
h[hash,]:=x;
h[hash,]:=y;
end;
function find(x:int64):int64; inline;
var hash:int64;
begin
hash:=x mod maxh;
while (h[hash,]<>x) and (h[hash,]<>) do hash:=(hash+) mod maxh;
if h[hash,]= then exit(-) else exit(h[hash,]);
end;
function work(a,b,p:int64):int64;
var j,m,x,cnt,ans,t:int64;
i:longint;
begin
ans:=;
m:=trunc(sqrt(p))+;
x:=pow(a,m,p);
j:=;
for i:= to m do
begin
j:=j*x mod p;
if find(j)=- then insert(j,i);
end;
j:=;
for i:= to m- do
begin
t:=find(j*b mod p);
if t<>- then
begin
cnt:=m*t-i;
if cnt<ans then ans:=cnt;
end;
j:=j*a mod p;
end;
exit(ans);
end;
function gcd(x,y:int64):int64;
begin
if y= then exit(x) else exit(gcd(y,x mod y));
end;
procedure exgcd(a,b:int64;var x,y:int64);
var t:int64;
begin
if b= then
begin
x:=;
y:=;
exit;
end;
exgcd(b,a mod b,x,y);
t:=x;
x:=y;
y:=t-a div b*y;
end;
procedure swap(var a,b:int64); inline;
var c:longint;
begin
c:=a; a:=b; b:=c;
end;
procedure sort(l,r:int64);
var i,j,x:int64;
begin
i:=l; j:=r; x:=ans[(l+r) div ];
while i<=j do
begin
while ans[i]<x do inc(i);
while x<ans[j] do dec(j);
if i<=j then
begin
swap(ans[i],ans[j]);
inc(i); dec(j);
end;
end;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
init;
readln(b,a,c);
rt:=findrt(c);
t1:=work(rt,b,c);
t2:=c-;
d:=gcd(a,t2);
if t1 mod d<> then
begin
writeln();
exit;
end;
exgcd(a,t2,x,y);
t1:=t1 div d;
t2:=t2 div d;
ans[]:=((x*t1 mod t2)+ t2) mod t2;
for i:= to d do ans[i]:=ans[i-]+t2;
for i:= to d do ans[i]:=pow(rt,ans[i],c);
sort(,d);
writeln(d);
for i:= to d- do write(ans[i],' ');
writeln(ans[d]);
end.

HDU3930 (原根)的更多相关文章

  1. HDU3930(离散对数与原根)

    题目:Broot 题意:给出k,m,newx的值,求方程x^k(mod m)=newx的解,其中m为素数. 解法步骤: (1)先暴力求m的原根g (2)大步小步求g^t1(mod m)=newx (3 ...

  2. 51nod1135(求最小原根)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1135 题意:中文题诶- 思路:设m是正整数,a是整数,若a模 ...

  3. [POJ1284]Primitive Roots(原根性质的应用)

    题目:http://poj.org/problem?id=1284 题意:就是求一个奇素数有多少个原根 分析: 使得方程a^x=1(mod m)成立的最小正整数x是φ(m),则称a是m的一个原根 然后 ...

  4. 51nod 1135 原根

    题目链接:51nod 1135 原根 设 m 是正整数,a是整数,若a模m的阶等于φ(m),则称 a 为 模m的一个原根.(其中φ(m)表示m的欧拉函数) 阶:gcd(a,m)=1,使得成立的最小的 ...

  5. hdu4992 Primitive Roots(所有原根)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4992 题意:给出n,输出n的所有原根. 思路:求出n的一个原根x,那么对于所以的i,i<phi( ...

  6. HDU5478 原根求解

    看别人做的很简单我也不知道是怎么写出来的 自己拿到这道题的想法就是模为素数,那必然有原根r ,将a看做r^a , b看做r^b那么只要求出幂a,b就能得到所求值a,b 自己慢慢化简就会发现可以抵消n然 ...

  7. 【poj1284-Primitive Roots】欧拉函数-奇素数的原根个数

    http://poj.org/problem?id=1284 题意:给定一个奇素数p,求p的原根个数. 原根: { (xi mod p) | 1 <= i <= p-1 } is equa ...

  8. 【BZOJ 1319】 Sgu261Discrete Rootsv (原根+BSGS+EXGCD)

    1319: Sgu261Discrete Roots Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 389  Solved: 172 Descriptio ...

  9. 【HDU 4992】 Primitive Roots (原根)

    Primitive Roots   Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...

随机推荐

  1. 转:怎样在VMware ESXi上 克隆虚拟机

    Cloning virtual machines on VMware ESXi 翻译自http://www.dedoimedo.com/computers/vmware-esxi-clone-mach ...

  2. Football(POJ3071)

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3469   Accepted: 1782 Descript ...

  3. [转]Java8-本地缓存

    这里我将会给大家演示用ConcurrentHashMap类和lambda表达式实现一个本地缓存.因为Map有一个新的方法可以在key为Null的时候自动计算一个新的value值.非常完美的实现cach ...

  4. 在ASP.NET MVC中使用Area

    前言: 这段时间小猪花了不少功夫在研究ASP.NET MVC的源码上面,可谓思想是了解了不少,用的上用不上却是另外一回事了.! 应用场景: ASP.NET MVC中,是依靠某些文件夹以及类的固定命名规 ...

  5. 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器

    Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...

  6. 优化MYSQL数据库的方法

    1.选取最适用的字段属性,尽可能减少定义字段长度,尽量把字段设置NOT NULL,例如'省份,性别',最好设置为ENUM2.使用连接(JOIN)来代替子查询: a.删除没有任何订单客户:DELETE ...

  7. HDU 1754 单点更新,求区间最大值

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. 二模 (10)day1

    第一题: 题目描述: 一个阅览室每天都要接待大批读者.阅览室开门时间是0,关门时间是T.每位读者的到达时间都不一样,并且想要阅读的刊物不超过5本.每位读者心里对自己想看的刊物都有一个排位,到达之后他会 ...

  9. Ibatis.Net 各种配置说明(二)

    一.各个配置文件的作用说明 providers.config:指定数据库提供者,.Net版本等信息. xxxxx.xml:映射规则. SqlMap.config:大部分配置一般都在这里,如数据库连接等 ...

  10. c# MVC在WEB.Config中配置MIME

    在IIS中,默认没有添加.json格式的MIME,所有无法读取服务器中的.json格式的文件,返回结果404 方式一:在IIS中手动添加MIME 1.点击MIME进入MIME列表 2.添加MIME 3 ...