HDU3930 (原根)
给定方程 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 (原根)的更多相关文章
- HDU3930(离散对数与原根)
题目:Broot 题意:给出k,m,newx的值,求方程x^k(mod m)=newx的解,其中m为素数. 解法步骤: (1)先暴力求m的原根g (2)大步小步求g^t1(mod m)=newx (3 ...
- 51nod1135(求最小原根)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1135 题意:中文题诶- 思路:设m是正整数,a是整数,若a模 ...
- [POJ1284]Primitive Roots(原根性质的应用)
题目:http://poj.org/problem?id=1284 题意:就是求一个奇素数有多少个原根 分析: 使得方程a^x=1(mod m)成立的最小正整数x是φ(m),则称a是m的一个原根 然后 ...
- 51nod 1135 原根
题目链接:51nod 1135 原根 设 m 是正整数,a是整数,若a模m的阶等于φ(m),则称 a 为 模m的一个原根.(其中φ(m)表示m的欧拉函数) 阶:gcd(a,m)=1,使得成立的最小的 ...
- hdu4992 Primitive Roots(所有原根)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4992 题意:给出n,输出n的所有原根. 思路:求出n的一个原根x,那么对于所以的i,i<phi( ...
- HDU5478 原根求解
看别人做的很简单我也不知道是怎么写出来的 自己拿到这道题的想法就是模为素数,那必然有原根r ,将a看做r^a , b看做r^b那么只要求出幂a,b就能得到所求值a,b 自己慢慢化简就会发现可以抵消n然 ...
- 【poj1284-Primitive Roots】欧拉函数-奇素数的原根个数
http://poj.org/problem?id=1284 题意:给定一个奇素数p,求p的原根个数. 原根: { (xi mod p) | 1 <= i <= p-1 } is equa ...
- 【BZOJ 1319】 Sgu261Discrete Rootsv (原根+BSGS+EXGCD)
1319: Sgu261Discrete Roots Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 389 Solved: 172 Descriptio ...
- 【HDU 4992】 Primitive Roots (原根)
Primitive Roots Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...
随机推荐
- Java 正则表达式 向前、向后匹配
//向后匹配 String a = "I paid $90 for 10 oranges, 12 pears and 8 apples. I saved $5 on "; Patt ...
- DOM节点-appendChild
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- uva 11624(bfs)
11624 - Fire! Time limit: 1.000 seconds Joe works in a maze. Unfortunately, portions of the maze hav ...
- Div CSS absolute与relative的区别小结
absolute:绝对定位,CSS 写法“ position: absolute; ”,它的定位分两种情况,如下: 1. 没有设定 Top.Right.Bottom.Left 的情况,默认依据父级的“ ...
- sqlserver 2008 左补齐字符串
SQLServer:right函数 语法 Right(string, length) Right 函数的语法具有下面的命名参数: 部分 说明 string 必要参数.字符串表达式,从中最右边的 ...
- 目前几款基于html5的前端框架:如Bootstrap、Foundation、Semantic UI 、Amaze UI
Bootstrap是由Twitter在2011年8月推出的开源WEB前端框架,集合CSS 和HTML,使用了最新的浏览器技术,为快速WEB开发提供了一套前端工具包,包括布局.网格.表格.按钮.表单.导 ...
- 在 Ubuntu 14.04/15.04 上配置 Node JS v4.0.0
大家好,Node.JS 4.0 发布了,这个流行的服务器端 JS 平台合并了 Node.js 和 io.js 的代码,4.0 版就是这两个项目结合的产物——现在合并为一个代码库.这次最主要的变化是 N ...
- Fair Photography
题目大意: 给出直线上N个点的位置和颜色(0或1),求最大的区间,使得区间内0的个数大于等于1的个数且0的个数减去1的个数为偶数. 解题过程: 1.先贴个lsdsjy大牛的线段树的做法:http:// ...
- 创建ROS功能包(四)
为了方便直接用ROS的create-pkg命令行工具 roscreate-pkg chapter2_tutorials std_msgs rospy roscpp std_msgs 包含了常见的消息类 ...
- javascript photo http://www.cnblogs.com/5ishare/tag/javascript/