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

http://www.lydsy.com/JudgeOnline/problem.php?id=1319

【题解】

求x^A=B(mod P),其中P是质数。

考虑对两边取log,设g为P的原根。

Alog(x) = log(B) (mod P-1)

log(x)表示以g为底的log

那么log(B) = y,其中g^y = B (mod P),用BSGS求出即可。

我们要求的是x,不妨先求log(x),设ans=log(x)

那么A*ans + (P-1)*k = y。这是一个exgcd的形式,所以我们可以求出ans的所有解(由于相当于指数,所以必须小于P-1)

然后快速幂即可。

# include <map>
# include <math.h>
# include <stdio.h>
# include <assert.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + ;
const int mod = 1e9+; # define RG register
# define ST static ll A, B, P, B0;
ll g, ans[M]; int ansn=; inline ll pwr(ll a, ll b, ll P) {
ll ret = ; a %= P;
while(b) {
if(b&) ret = ret * a % P;
a = a * a % P;
b >>= ;
}
return ret;
} ll y[M];
inline ll G(ll x) {
ll t = x; int nn = ;
for (int i=; i*i<=x; ++i) {
if(x%i) continue;
y[++nn] = i;
while(x%i == ) x/=i;
}
if(x != ) y[++nn] = x;
for (ll g=; ; ++g) {
bool flag = ;
for (int i=; i<=nn; ++i)
if(pwr(g, t/y[i], P) == ) {
flag = ;
break;
}
if(flag) return g;
}
} map<ll, int> mp;
inline ll BSGS(ll A, ll B, ll P) {
mp.clear();
int m = ceil(sqrt(1.0 * P));
ll t = B, g;
for (int i=; i<m; ++i) {
if(!mp[t]) mp[t] = i;
t = t * A % P;
}
g = pwr(A, m, P); t = g;
for (int i=, ps; i<=m+; ++i) {
if(mp.count(t)) return (ll)i*m - mp[t];
t = t * g % P;
}
return -;
} ll exgcd(ll a, ll b, ll &x, ll &y) {
if(b == ) {
x = , y = ;
return a;
}
ll ret = exgcd(b, a%b, x, y), t;
t = x;
x = y;
y = t - (a/b) * y;
return ret;
} int main() {
cin >> P >> A >> B;
g = G(P-);
// cout << g << endl;
// x^A = B (mod P)
// A log_g(x) = log_g(B) (mod P-1)
B0 = BSGS(g, B, P);
assert(B0 != -);
// cout << B0 << endl;
ll tx, ty, GCD;
GCD = exgcd(A, P-, tx, ty);
if(B0 % GCD) {
puts("");
return ;
} ty = (P-)/GCD;
tx = (tx % ty + ty) % ty;
tx = (tx * B0/GCD) % ty;
while(tx < P-) {
ans[++ansn] = pwr(g, tx, P);
tx += ty;
}
sort(ans+, ans+ansn+); cout << ansn << endl;
for (int i=; i<=ansn; ++i)
printf("%lld\n", ans[i]);
return ;
}

bzoj1420/1319 Discrete Root的更多相关文章

  1. bzoj 1420 Discrete Root - 原根 - exgcd - BSGS

    题目传送门 戳我来传送 题目大意 给定$k, p, a$,求$x^{k}\equiv a \pmod{p}$在模$p$意义下的所有根. 考虑模$p$下的某个原根$g$. 那么$x  = g^{ind_ ...

  2. BZOJ 1420 Discrete Root

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

  3. 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]. ...

  4. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  5. 主席树——求静态区间第k大

    例题:poj2104 http://poj.org/problem?id=2104 讲解:http://blog.sina.com.cn/s/blog_6022c4720102w03t.html ht ...

  6. 证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)

    离散对数问题,英文是Discrete logarithm Problem,有时候简写为Discrete log,该问题是十几个开放数学问题(Open Problems in Mathematics, ...

  7. PP: Deep r -th Root of Rank Supervised Joint Binary Embedding for Multivariate Time Series Retrieval

    from: Dacheng Tao 悉尼大学 PROBLEM: time series retrieval: given the current multivariate time series se ...

  8. Centos 下 mysql root 密码重置

    重置mysql密码的方法有很多,官网也提供了很方便的快捷操作办法,可参考资料 resetting permissions .本文重置密码的具体步骤如下: 一.停止MySQL(如果处于运行状态) #se ...

  9. CentOS7 重置root密码

    1- 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 4 - 现在按下 Con ...

随机推荐

  1. define 和 const常量有什么区别?

    define在预处理阶段进行替换,const常量在编译阶段使用 宏不做类型检查,仅仅进行替换,const常量有数据类型,会执行类型检查 define不能调试,const常量可以调试 define定义的 ...

  2. cachel-control

    nodejs: res.set('Cache-Control', 'public, max-age=31557600'); express全局设置:       app.use(express.sta ...

  3. springmvc基础篇—通过注解的方式去配置项目

    学习了通过xml方式去配置项目后,当然要掌握更简单更灵活的注解方式哟,这是官方推荐使用的方式. 一.修改配置文件,建议大家直接使用我的配置文件 <?xml version="1.0&q ...

  4. ES6 语法糖

    重新认识ES6中的语法糖:https://segmentfault.com/a/1190000010159725

  5. Qt Charts_Audio实践

    这里完全是照搬帮助文档中的代码生成的程序 上预览图 工程文件代码 #------------------------------------------------- # # Project crea ...

  6. zabbix 一些问题随记

    1. zabbix运行不了,显示被锁,去检查日志中的报错 2. 配置界面,连接不到数据库,检查server配置文件,mysql授权命令要准确,重启 3. 显示没有php文件,下载即可,或者修改网页访问 ...

  7. golang模拟新浪微博登录

    1.基于幽灵蛛pholcus开源项目的规则 直接贴代码,代码可以更改后用于其他爬虫项目 package pholcus_lib // 基础包 import ( // "github.com/ ...

  8. 【iOS开发】NSOperation简单介绍

    iOS开发多线程篇—NSOperation简单介绍 一.NSOperation简介 1.简单说明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现 ...

  9. org.json.Json Object的put和append方法比较

    json.append(key,value) 会把 value 包装成一个数组 JSONObject append = new JSONObject().append("a", & ...

  10. 201621044079《Java程序设计》第二周学习总结

    Week02-Java基本语法与类库 1.本周学习总结 记录本周学习中的重点 尝试使用 原则:少而精,自己写.即使不超过5行也可,但请一定不要简单的复制粘贴 1.学习了Java的数据类型 int ch ...