bzoj1420/1319 Discrete Root
传送门: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的更多相关文章
- bzoj 1420 Discrete Root - 原根 - exgcd - BSGS
题目传送门 戳我来传送 题目大意 给定$k, p, a$,求$x^{k}\equiv a \pmod{p}$在模$p$意义下的所有根. 考虑模$p$下的某个原根$g$. 那么$x = g^{ind_ ...
- BZOJ 1420 Discrete Root
思路:数学大汇总 提交:\(3\)次 错因:有一个\(j\)写成\(i\) 题解: 求:\(x^k \equiv a \mod p\) 我们先转化一下:求出\(p\)的原根\(g\) 然后我们用\(B ...
- 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]. ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 主席树——求静态区间第k大
例题:poj2104 http://poj.org/problem?id=2104 讲解:http://blog.sina.com.cn/s/blog_6022c4720102w03t.html ht ...
- 证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)
离散对数问题,英文是Discrete logarithm Problem,有时候简写为Discrete log,该问题是十几个开放数学问题(Open Problems in Mathematics, ...
- 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 ...
- Centos 下 mysql root 密码重置
重置mysql密码的方法有很多,官网也提供了很方便的快捷操作办法,可参考资料 resetting permissions .本文重置密码的具体步骤如下: 一.停止MySQL(如果处于运行状态) #se ...
- CentOS7 重置root密码
1- 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 4 - 现在按下 Con ...
随机推荐
- Can’t delete list item in Sharepoint2013
Today,I have meet a very strange error.When I attempt to delete a item from a list,I recieve an ...
- c++ list_iterator demo
#include <iostream> #include <list> using namespace std; typedef list<int> Integer ...
- JavaScript序列化对象成URL格式
http://access911.net/fixhtm/72FABF1E15DCEAF3.htm?tt=
- MD5、SHA校验命令
linux系统的软件很多时候都以境像的方式提供下载,但我们如何确实下载的文件是没有被篡改过的呢?Linux中一般用对下载的文件进行MD5和SHA校验来确认. MD5 我们拿iptraf软件来试验: 我 ...
- iOS笔记054 - 核心动画
注意事项 :locationInView和translationInView // 返回相对于控件自身内部触摸点的位置 [pan locationInView:self]; // 返回两个触摸点之间的 ...
- hdu1505City Game(动态规划)
City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- LeetCode - 20. Valid Parentheses(0ms)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 感知机学习算法(PLA)
Perception Learning Algorithm, PLA 1.感知机 感知机是一种线性分类模型,属于判别模型. 感知机模型给出了由输入空间到输出空间的映射: f(X) = sign(WTX ...
- CentOS7安装Jenkins Master
一.安装java环境 1.查看服务器版本 cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 升级操作系统 yum update ...
- HDU 1271 整数对(思路题)
假设删除第k位,把整数A表示成如下形式: A = a * 10^(k+1) + b * 10 ^k + c; 则: B = a * 10^k + c; N = A + B = (11*a+b)*10^ ...