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 ...
随机推荐
- C++各种类型的简单排序大汇总~
啊,排序的技能点也太多了吧!!!LITTLESUN快要**在排序的技能场了啊!(划掉)经历了两天48小时2880分钟172800秒的艰苦奋斗,终于终于终于学的差不多了!明天就可以去打排序的小怪喽!(撒 ...
- I两种冒泡算法
两种冒泡算法: 第一个循环,I 定位当前坐标,第二个循环 把 I 之后的每个数都与 I 比较(比 I 小的都去坐标I),第二个循环之后 坐标 I 为数组里最小的数值. 效率比较高的冒泡算法: stat ...
- 深挖 NGUI 基础 之UICamera (二)
一.UI Camera作用 UICamera需要挂载在摄像机上才能发挥作用 UICamera仅负责 发送NGUI 事件 到 脚本所附加的摄像机中看得到的对象,比如我自定义了NGUI层(在Inspect ...
- Python简要标准库(3)
shelve 若只需要一个简单的存储方案,那么shelve模块可以满足你大部分的需要,你所需要的只是为它提供文件名.shelve中唯一有趣的函数是open,在调用的时候他会返回一个Shelf对象 注意 ...
- SQL - SELECT COUNT用法
SQL Server数据库 COUNT() 函数返回匹配指定条件的行数. 语法 SQL COUNT(column_name) 语法 COUNT(column_name) 函数返回指定列 ...
- 论文翻译--StarCraft Micromanagement with Reinforcement Learning and Curriculum Transfer Learning
(缺少一些公式的图或者效果图,评论区有惊喜) (个人学习这篇论文时进行的翻译[谷歌翻译,你懂的],如有侵权等,请告知) StarCraft Micromanagement with Reinforce ...
- CCS Font 知识整理总结
总是搞不懂 CCS 中如何正确的使用字体,这下明白了. 1.什么是 font-face font-face 顾名思义,就是文字的脸.字体是文字的外在形式,就是文字的风格,是文字的外衣.比如行书.楷书. ...
- HDU 4745 Two Rabbits(最长回文子序列)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Description Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon ...
- STL Allocator
从上面这个程序可以看出,我们这里手动使用了分配器,分配器有很多种类,有std::,还有非std::,也就是上面的__gnu_cxx下面的,我们在使用容器的时候不关心我们使用什么分配器,也不关心我们如何 ...
- Bjarne Stroustrup语录2
一.致读者 1. 在编程序时,你是在为你针对某个问题的解决方案中的思想建立起一种具体表示.让程序的结构尽可能地直接反映这些思想: ★.如果你能把“它”看成一个独立的概念,就把它做成一个类. ...