思路:数学大汇总

提交:\(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的更多相关文章

  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 (原根+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]. ...

  3. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

  4. bzoj1420/1319 Discrete Root

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1420 http://www.lydsy.com/JudgeOnline/problem.ph ...

  5. BZOJ 3239: Discrete Logging [BGSG]

    裸题 求\(ind_{n,a}b\),也就是\(a^x \equiv b \pmod n\) 注意这里开根不能直接下取整 这个题少了一些特判也可以过... #include <iostream& ...

  6. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

  7. 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 \)这里-的作用是避 ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. BZOJ 4408 主席数+找规律

    #include <cstdio> ; inline void Get_Int(int &x) { ; ') ch=getchar(); +ch-'; ch=getchar();} ...

随机推荐

  1. Win10 自定义鼠标右键菜单

    1. 点击文件鼠标右键显示软件 1.1 步骤 win+R输入regedit进入注册表 定位到HKEY_CLASSES_ROOT\*\shell下 在shell创建一个你想要的右键文件 例如:Kinok ...

  2. mysql 库表的操作

    MySQL的库.表的详细操作 MySQL数据库 本节目录 一 库操作 二 表操作 三 行操作 一 库操作 1.创建数据库 1.1 语法 CREATE DATABASE 数据库名 charset utf ...

  3. S03_CH06_AXI_VDMA_OV7725摄像头采集系统

    S03_CH06_AXI_VDMA_OV7725摄像头采集系统 本课程将对Xilinx提供的一款IP核--AXI VDMA(Video Direct Memory Access) 进行详细讲解,为后续 ...

  4. [Vue]vue中路由重定向redirect

    1.重定向的地址不需要接收参数 const routes = [ { path: '/', redirect: '/index'}, { path: '/index', component: inde ...

  5. vue学习(4)-组件的创建,父子组件传值,$refs

    模块化:代码逻辑 组件化:UI 组件的创建:1.

  6. spring data 入门

    提出问题 我是Sping Data,是程序员的春天,因为我提供很多接口给开发人员, 减少程序员重复的写CRUD和分页等方法,你们也可以叫我春D,或者春帝,因为我很酷 解决问题 在Spring Data ...

  7. mongodb聚合查询-aggregate

    Mongodb-aggregate 在工作中经常遇到一些mongodb的聚合操作,和mysql对比起来,mongo存储的可以是复杂的类型,比如数组,字典等mysql不善于处理的文档型结构,但是mong ...

  8. URL - Fiddler - IIS

    URL和URI URI:Uniform Resource Identifier,唯一标识一个网络资源 URL:Uniform Resource Locator,指向网络资源地址 URL是URI的子集, ...

  9. python使得文件不包含重复行

    set函数去重 # -*- coding:utf-8 -*- srcTxt=open('1.txt','r').readlines() noRepeat=open('2.txt','w') st=se ...

  10. RHEL7启动到命令模式

    打开/etc/inittab 文件会看到以下信息 从中知道想要启动后就进入完整的多用户文本模式(命令行模式) 以root权限执行: ln -sf /lib/systemd/system/multi-u ...