BZOJ 1420 Discrete Root
思路:数学大汇总
提交:\(3\)次
错因:有一个\(j\)写成\(i\)
题解:
求:\(x^k \equiv a \mod p\)
我们先转化一下:求出\(p\)的原根\(g\)
然后我们用\(BSGS\)可以求出 \(g^b \equiv a \mod p\),即\(a\)的指标\(b\).然后因为原根的幂可以表示\([0,p-1]\)中的任何一个数,所以设\(x=g^y\),原式可以转化成 \((g^y)^k \equiv a \mod p\),即\(g^{y*k} \equiv g^b\mod P\),所以指数满足 \(y*k \equiv b \mod \varphi(p)\),可以用\(exgcd\)解出这个方程的解。
代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
#include<algorithm>
#define ll long long
#define RR register ll
#define int ll
#define R register int
using namespace std;
namespace Luitaryi {
template<class I> inline I g(I& x) { x=0; register I f=1;
register char ch; while(!isdigit(ch=getchar())) f=ch=='-'?-1:f;
do x=x*10+(ch^48); while(isdigit(ch=getchar())); return x*=f;
} ll a,n,k,m,p,G,cnt,mem[100010],ind,ans[100010];
map<ll,ll> mp;
inline ll qpow(ll a,ll b) { RR ret=1;
for(;b;b>>=1,(a*=a)%=p) if(b&1) (ret*=a)%=p; return ret;
}
inline void exgcd(ll a,ll b,ll& x,ll& y) {
if(!b) {x=1,y=0; return ;}
exgcd(b,a%b,y,x); y-=a/b*x;
}
inline ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
inline int BSGS() {
R t=sqrt(p)+1; RR sum=a;//BSGS求a的指标
for(R i=1;i<=t;++i) (sum*=G)%=p,mp[sum]=i;
sum=qpow(G,t); if(sum==0) return a==0?1:-1;
for(R i=1;i<=t;++i) {
RR tmp=qpow(sum,i);
if(mp.count(tmp)&&i*t-mp[tmp]>=0) return i*t-mp[tmp];
} return -1;
}
inline void main() {
m=g(p)-1,g(k),g(a); if(!a) return (void) printf("1\n0\n"); RR x=m;
for(R i=2,lim=sqrt(m);i<=x&&i<=lim;++i) {
if(x%i==0) mem[++cnt]=i;
while(x%i==0) x/=i;
} if(x>1) mem[++cnt]=x;
//求原根
for(R i=2;i<=m;++i) { register bool flg=true;
for(R j=1;j<=cnt;++j) {
if(qpow(i,m/mem[j])==1) {
flg=false; break;
}
} if(flg) {G=i; break;}
}
//求指标
ind=BSGS(); if(ind==-1) return (void) puts("0");
//解同余方程
RR y; RR d=gcd(k,m);
if(ind%d) return (void) puts("0");
R A=k/d,B=m/d; ind/=d;
exgcd(A,B,x,y); (x*=ind)%=m; x=(x%B+B)%B;
cnt=0;
while(x<=m) ans[++cnt]=qpow(G,x),
x+=B; sort(ans+1,ans+cnt+1);
printf("%lld\n",cnt); for(R i=1;i<=cnt;++i) printf("%lld ",ans[i]);
}
} signed main() {Luitaryi::main(); return 0;}
2019.08.22
78
BZOJ 1420 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 (原根+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]. ...
- BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)
我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...
- bzoj1420/1319 Discrete Root
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1420 http://www.lydsy.com/JudgeOnline/problem.ph ...
- BZOJ 3239: Discrete Logging [BGSG]
裸题 求\(ind_{n,a}b\),也就是\(a^x \equiv b \pmod n\) 注意这里开根不能直接下取整 这个题少了一些特判也可以过... #include <iostream& ...
- BZOJ 3239 Discrete Logging(BSGS)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...
- bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】
都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ 4408 主席数+找规律
#include <cstdio> ; inline void Get_Int(int &x) { ; ') ch=getchar(); +ch-'; ch=getchar();} ...
随机推荐
- 人机交互技术 Week 1_人机交互概述
HCI Week 1_Introduction 18-19学年春夏学期选修了计院万华根老师的人机交互技术课程,老师由于知识产权相关原因不能提供课件,故尽可能对课程内容做详尽的课堂笔记以供复习,如有不妥 ...
- LC 21. Merge Two Sorted Lists
题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splic ...
- 自定义模块,time,datetime以及random
自定义模块,time,datetime以及random 1.自定义模块 自定义一个模块 import #导入 (拿工具箱) 模块分类 1.内置模块(标准库) --python解释器自带的.py文件(模 ...
- Oracle数据库——ROWNUM
Oracle数据库--ROWNUM 前言 刚学到了ROWNUM的用法,网上一搜,结果发现了有很多帖子,写的都很全.本着好记性不如烂笔头的原则,我还是决定自己手打一遍,当然下面也附上了我参考的链接. ...
- 让Sublime Text3支持新建.vue高亮显示模板
首先要使用Package Control,安装要好 Vue Syntax Highlight和sublimetmpl插件. 1, 在Packages\SublimeTmpl\templates目录下新 ...
- 学习嵌入式为什么要有uboot(深度解析)
ref:http://www.elecfans.com/d/617674.html 为什么要有uboot 1.1.计算机系统的主要部件 (1)计算机系统就是以CPU为核心来运行的系统. 典型的 ...
- js:把字符串转为变量使用; js下将字符串当函数去执行的方法
1 把字符串当变量使用 通过计算 string 得到的值(如果有的话).该方法只接受原始字符串作为参数 demo: var type = "car"; var newStr = & ...
- [yarn]yarn和npm的对比
一.简介 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服 ...
- 洛谷 P1540 机器翻译
链接:https://www.luogu.org/problemnew/show/p1540 题目: 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译 ...
- Linux Centos7配置ftp服务器
一.安装 1.安装 yum install -y vsftpd 2.设置开机启动 systemctl enable vsftpd.service 3.启动 systemctl start vsftp ...