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 ...
随机推荐
- 【转】ASP.NET Core 快速入门(环境篇)
原文链接:http://www.cnblogs.com/zhaopei/p/netcore.html [申明]:本人.NET Core小白.Linux小白.MySql小白.nginx小白.而今天要说是 ...
- CodeForces-1121C System Testing
题目链接 https://vjudge.net/problem/CodeForces-1121C 题面 Description Vasya likes taking part in Codeforce ...
- pandas DataFrame的查询方法(loc,iloc,at,iat,ix的用法和区别)
pandas DataFrame的增删查改总结系列文章: pandas DaFrame的创建方法 pandas DataFrame的查询方法 pandas DataFrame行或列的删除方法 pand ...
- deeplearning.ai课程学习(3)
第三周:浅层神经网络(Shallow neural networks) 1.激活函数(Activation functions) sigmoid函数和tanh函数两者共同的缺点是,在z特别大或者特别小 ...
- BZOJ 3925 ZJOI2015 地震后的幻想乡 状压dp+期望
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3925 题意概述: 给出一张N点M边的最小生成树,其中每条边的长度为[0,1]的实数,求最小 ...
- 子组件通过$emit触发父组件的事件时,参数的传递
子组件.vue <template> <div> <el-table :data="comSchemaData" highlight-current- ...
- asp.net中的cookie
一.cookie导读,理解什么是cookie 1.什么是cookie:cookie是一种能够让网站服务器把少量数据(4kb左右)存储到客户端的硬盘或内存.并且读可以取出来的一种技术. 2.当你浏览某网 ...
- IE6中png背景图片透明的最好处理方法
在IE6浏览器下png(24位)的图片显示是不能透明的. 1.处理办法就是用DDPngMin.js <!--[if IE 6]> <script src="js/DDPng ...
- 【算法】最小乘积生成树 & 最小乘积匹配 (HNOI2014画框)
今天考试的时候果然题目太难于是我就放弃了……转而学习了一下最小乘积生成树. 最小乘积生成树定义: (摘自网上一篇博文). 我们主要解决的问题就是当k = 2时,如何获得最小的权值乘积.我们注意到一张图 ...
- C++——派生类中的访问——可见性问题
C++中派生类对基类成员的访问形式主要有以下两种: 1.内部访问:由派生类中新增成员对基类继承来的成员的访问. 2.对象访问:在派生类外部,通过派生类的对象对从基类继承来的成员的访问.今天给大家介绍在 ...