题目传送门

  戳我来传送

题目大意

  给定$k, p, a$,求$x^{k}\equiv a \pmod{p}$在模$p$意义下的所有根。

  考虑模$p$下的某个原根$g$。

  那么$x  = g^{ind_{g}x}, a = g^{ind_{g}a}$。

  所以原方程转化为$g^{k\cdot ind_{g}x}\equiv g^{ind_{g}a} \pmod{p}$。

  所以方程等价于$k\cdot ind_{g}x \equiv ind_{g}a \pmod{\varphi(p)}$。

  用exgcd解出$ind_{g}x$的所有可能的解,再用快速幂算出$x$即可。

Code

 /**
* bzoj
* Problem#1420
* Accepted
* Time: 44ms
* Memory: 2032k
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef bool boolean; typedef class HashMap {
private:
static const int M = ;
public:
int ce;
int h[M], key[M], val[M], next[M]; HashMap() { } void insert(int k, int v) {
int ha = k % M;
for (int i = h[ha]; ~i; i = next[i])
if (key[i] == k) {
val[i] = v;
return;
}
++ce, key[ce] = k, val[ce] = v, next[ce] = h[ha], h[ha] = ce;
} int operator [] (int k) {
int ha = k % M;
for (int i = h[ha]; ~i; i = next[i])
if (key[i] == k)
return val[i];
return -;
} void clear() {
ce = -;
memset(h, -, sizeof(h));
}
}HashMap; void exgcd(int a, int b, int& d, int& x, int& y) {
if (!b)
d = a, x = , y = ;
else {
exgcd(b, a % b, d, y, x);
y -= (a / b) * x;
}
} int qpow(int a, int pos, int p) {
int pa = a, rt = ;
for ( ; pos; pos >>= , pa = pa * 1ll * pa % p)
if (pos & )
rt = rt * 1ll * pa % p;
return rt;
} int inv(int a, int n) {
int d, x, y;
exgcd(a, n, d, x, y);
return (x < ) ? (x + n) : (x);
} int p, k, a;
int g; inline void init() {
scanf("%d%d%d", &p, &k, &a);
} HashMap mp;
int ind(int a, int b, int p) {
mp.clear();
int cs = sqrt(p - 0.5), ac = qpow(a, cs, p), iac = b * 1ll * inv(ac, p) % p, pw = ;
for (int i = cs - ; ~i; i--)
iac = iac * 1ll * a % p, mp.insert(iac, i);
for (int i = ; i < p; i += cs, pw = pw * 1ll * ac % p)
if (~mp[pw])
return mp[pw] + i;
return -;
} int top = ;
int fac[];
void getfactors(int x) {
for (int i = ; i * i <= x; i++) {
if (!(x % i)) {
fac[++top] = i;
while (!(x % i)) x /= i;
}
}
if (x != ) fac[++top] = x;
} boolean isroot(int x) {
int pos = p - ;
for (int i = ; i <= top; i++)
if (qpow(x, pos / fac[i], p) == )
return false;
return true;
} inline void solve() {
if (p == || !a) {
printf("1\n0");
return;
}
if (p == )
g = ;
else {
getfactors(p - );
for (g = ; !isroot(g); g++);
} int inda = ind(g, a, p), d, x, y, cir;
exgcd(k, p - , d, x, y);
if (inda % d) {
puts("");
return;
}
cir = (p - ) / d;
x = x * 1ll * (inda / d) % cir;
(x <= ) ? (x += cir) : ();
vector<int> res;
for (; x < p; x += cir)
res.push_back(qpow(g, x, p));
sort(res.begin(), res.end());
printf("%d\n", (signed) res.size());
for (int i = ; i < (signed) res.size(); i++)
printf("%d\n", res[i]);
} int main() {
init();
solve();
return ;
}

bzoj 1420 Discrete Root - 原根 - exgcd - BSGS的更多相关文章

  1. BZOJ 1420: Discrete Root (原根+BSGS)

    题意 已知kkk, aaa, ppp. 求 xk≡a (mod p)x^k\equiv a\ (mod\ p)xk≡a (mod p) 的所有根. 根的范围[0,p−1][0,p-1][0,p−1]. ...

  2. BZOJ 1420 Discrete Root

    思路:数学大汇总 提交:\(3\)次 错因:有一个\(j\)写成\(i\) 题解: 求:\(x^k \equiv a \mod p\) 我们先转化一下:求出\(p\)的原根\(g\) 然后我们用\(B ...

  3. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

  4. 【bzoj2242】[SDOI2011]计算器 EXgcd+BSGS

    题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p, ...

  5. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

  6. BZOJ 2242 [SDOI2011]计算器(快速幂+Exgcd+BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2242 [题目大意] 给出T和K 对于K=1,计算 Y^Z Mod P 的值 对于K=2 ...

  7. bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】

    都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...

  8. bzoj1420/1319 Discrete Root

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1420 http://www.lydsy.com/JudgeOnline/problem.ph ...

  9. 【BZOJ2242】【SDoi2011】计算器 快速幂+EXGCD+BSGS

    Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...

随机推荐

  1. selenium PO模式

    思想: 1.定义basepage.py用来写公共方法,比如找元素,打开url,切换frame.这样的部分都写在这里.不必每次用都重写. 2.LoginPage.py 每个功能模块一个文件或者一个类 这 ...

  2. Ecshop表结构 order_info

    CREATE TABLE IF NOT EXISTS `ecs_order_info` (  `order_id` mediumint(8) unsigned NOT NULL AUTO_INCREM ...

  3. Linux 配置yum源.

    Linux 配置yum源. 环境:虚拟机中安装了RedHat ,在进行安装mariadb的时候,出现如下错误.是因为yum源的问题,需要进行配置yum源.本教程是配置本地yum源. [root@loc ...

  4. Ubuntu系统添加搜狗输入法

    前端开发时有时候要接触到Ubuntu系统,但由于本身没有拼音输入,故需要自己安装搜狗,记录方法如下: 1.安装前先升级资源库并安装输入法依赖包: $sudo apt-get update $sudo ...

  5. JS中常用的输出方式(五种)

    1.alert("要输出的内容"); ->在浏览器中弹出一个对话框,然后把要输出的内容展示出来 ->alert都是把要输出的内容首先转换为字符串然后在输出的 2.doc ...

  6. jQuery筛选--find(expr|obj|ele)和siblings([expr])

    find(expr|obj|ele) 概述 搜索所有与指定表达式匹配的元素.这个函数是找出正在处理的元素的后代元素的好方法 参数 expr  用于查找的表达式 jQuery object   一个用于 ...

  7. uvalive 3887 Slim Span

    题意: 一棵生成树的苗条度被定义为最长边与最小边的差. 给出一个图,求其中生成树的最小苗条度. 思路: 最开始想用二分,始终想不到二分终止的条件,所以尝试暴力枚举最小边的长度,然后就AC了. 粗略估计 ...

  8. c# ref和out参数

    向方法传递参的时候,对应的参数通常会用实参的拷贝来初始化.就是说随便在方法内部进行怎样的修改,都不会影响作为参数传递的变量的原始值. 通过上面的例子我们可以看出来,如果一个方法的参数是引用类型,那么使 ...

  9. 转:C# 小数位数保留的方法集锦

    转载自:http://www.jb51.net/article/17010.htm 1. System.Globalization.NumberFormatInfo provider = new Sy ...

  10. Spring boot学习1 构建微服务:Spring boot 入门篇

    Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...