题目传送门

  戳我来传送

题目大意

  给定$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. 关于bjam编译自己模块出错的问题

    将这个目录下C:\Users\Regubed的user-config.jam改为下面内容 # MSVC configurationusing msvc : 9.0 ; # Python configu ...

  2. 关于promise自己的理解

    参考文档:http://es6.ruanyifeng.com/#docs/promise#Promise-%E7%9A%84%E5%90%AB%E4%B9%89 ES6 规定,Promise对象是一个 ...

  3. webpack使用雪碧图插件

    1.先安装插件 npm install --save-dev webpack-spritesmith 2.配置webpack 配置之前 先引入var SpritesmithPlugin = requi ...

  4. python selenium截取指定元素图片

    1.截取当前屏幕 @property def getImage(self): ''' 截取图片,并保存在images文件夹 :return: 无 ''' timestrmap = time.strft ...

  5. undefined reference 问题各种情况分析

    扒自网友文章 关于undefined reference这样的问题,大家其实经常会遇到,在此,我以详细地示例给出常见错误的各种原因以及解决方法,希望对初学者有所帮助. 1.  链接时缺失了相关目标文件 ...

  6. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  7. Web Audio初步介绍和实践

    Web Audio还是一个比较新的JavaScript API,它和HTML5中的<audio>是不同的,简单来说,<audio>标签是为了能在网页中嵌入音频文件,和播放器一样 ...

  8. (1)打造简单OS-汇编写入引导区,虚拟机启动步骤

    首先需要您在网上下载NASM编译器,可以将汇编编译为二进制文件 1.写一段汇编代码在屏幕上打印一段字符,可以运行的!并进行nasm为二进制文件,如下"test.asm" 该段汇编主 ...

  9. 学习笔记<3>View接触

    一.View基本概念 1.界面上显示所有的控件都是用对象表示的,即有类,这些类都是View的子类. 2.View的种类 二.在Activity当中获取代表View的对象 1.根据ID可以用方法获取到对 ...

  10. MyEclipse 黑色主题 jsp 页面 js背景色修改

    Windows-->Preferences-->Myeclipse-->Files and Editors-->JavaScript-->Editor-->Synt ...