传送门: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. CC3100BoosterPack和CC31XXEMUBOOST板子的测试

    1. 先测试右边的CC3100BoosterPack,测试发现LDO坏了,无法输出3.3V,所以只能用左边的板子供电. 2. 插上CC31XXEMUBOOST板子的J1,两个板子插在一起,等待驱动安装 ...

  2. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  3. 【个人训练】(UVa11129)An antiarithmetic permutation

    题意与解析 一条非常有趣的二分题.一开始没有懂解法,去网上看了半天全是做法没有这样做为什么是对的(或者说的很含糊).一做完回顾一下立刻有点开朗的感觉. 题意很简单,维护一个0-n-1的数列,使其选出长 ...

  4. Spotlight on MySQL

    聚光灯在MySQL 1.Sessios会话Total Users:总用户数前连接到MySQL服务器的用户会话总数Active Users:活跃用户此控件表示连接到当前正在执行SQL语句或其他数据库请求 ...

  5. Python 基本文件操作

    文件模式 'r' 读模式 'w' 写模式 (清除掉旧有数据并重新开始) 'a' 追加模式 'b' 二进制模式 '+' 读/写模式 注意: 'b'   : 二进制模式 可添加到其他模式中使用 '+'  ...

  6. spring-boot分页插件

    1.分页插件,spring-boot.,第一次调用时,存值到 model.addAttribute("status", id);页面获取2.页面获取 后台存入的值,放在input ...

  7. Python 3.6 爬取BD电影网

    2018-07-10 #coding:utf-8 #coding:utf-8 from lxml import etree import requests import pandas import t ...

  8. 软件工程项目组Z.XML会议记录 2013/11/20

    软件工程项目组Z.XML会议记录 [例会时间]2013年11月20日星期三21:00-22:00 [例会形式]小组讨论 [例会地点]学生公寓3号楼会客厅 [例会主持]李孟 [会议记录]李孟 会议整体流 ...

  9. 【转】H5 - HTML5新增标签

    下面分别是传统的div+css的页面布局方式 下面是HTML5布局方式: 是不是精简了很多呢  现在来说说图片中出现的标签: 结构标签:(块状元素) 有意义的div artical 标记定义一篇文章  ...

  10. 【电影影评】梦之安魂曲-败给了BGM和豆瓣影评

    首先,这部电影豆瓣8.7分,一般来说,豆瓣的打分是比较准确的.能反映一个片子的质量,而较少受到环境的影响.但是这种关系当然也不全对,比如某些片子可能特别让某一种人喜欢(如退役军人和军旅题材),而在某些 ...