给定方程 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. PowerDesigner生成SQL脚本时,对象带有双引号的问题解决

    在pdm查看脚本时,发现表名和字段名带有双引号: 1.create table"cfg_user_card_account"  ( 2.  "user_card_acco ...

  2. 课堂练习&课下作业

    设计思路: 列举出买十本的所有情况:1.一本的时候不打折扣 2.两本的时候买两本价最低 3.三本的时候买三本价最低 4.四本的时候买四本价最低 5.五本的时候买五本价最低 6.六本的时候分一本和五本价 ...

  3. struts2视频学习笔记 01-02

    网易云课堂-<struts2> 课时1 Struts2: WebWork2基础上发展而来,MVC框架,无侵入式设计. 提供了拦截器,类型转换器,支持多种表现层技术(JSP, freeMar ...

  4. 读取DBF文件的部分代码

    private void BtnOpenInitial_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDi ...

  5. uva 10668

    #include <iostream> #include <cstdlib> #include <cstdio> #include <cmath> us ...

  6. 自己定制ListView,上拉刷新和下拉刷新,加载网络图片,并且添加缓存机制。

    package com.lixu.listviewrefresh; import java.util.ArrayList; import java.util.HashMap; import java. ...

  7. S1:变量

    接触JS一段时间了,但总感觉不得要领,技术得不到提升,翻来覆去,决定对基础知识做一次系统的整理,要坚持每一天都有新的收获 ! 变量,即通过一个名字将一个值关联起来,以后通过变量就可以引用到该值,比如: ...

  8. c实现的iOS http下载类。支持自己设定http 头(比如cookie等)

    也许有人要问为什么要自己用c写? 原因是: 1 我是菜鸟 2 我最最初选择了AsyncSocket,弄了很久,基本上稳定了,但有时候出现了数据不完整,但我又没办法在这个时候识别到并重试:所以不完美,最 ...

  9. 二模 (5)day1

    第一题: 题目大意:解一元一次方程(只有+-符号): 解题过程:直接处理处两边的x的系数和常数项,字符串的处理即可. 第二题: 题目大意:求逆序对数. 解题过程:直接归并排序. 第三题: 题目大意:多 ...

  10. 用C#操作vss、msbuild、reactor

    一.命令行 凡是支持命令行的工具,都可以通过cmd.exe操作.如下: var p = new Process(); p.StartInfo.FileName = "cmd.exe" ...