思路:数学大汇总

提交:\(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. python读写文件的操作

    编程语言中,我们经常会和文件和文件夹打交道,这篇文章主要讲的是Python中,读写文件的常用操作: 一.打开文件 openFile = open('../Files/exampleFile.txt', ...

  2. spring 框架的核心总结

    最近在学习Java语言,从而也学习了SpringFramework 这个大名鼎鼎的框架.从而做一些的记录. 题外话: 学习过几种不同的语言,后来知道所有的编程语言里所有的概念翻来覆去都是一样的事物,只 ...

  3. 关于Basic Latin踩到的一些坑

    目录 在wiki中,很多语言的字符集都包含了Basic Latin,一开始我没有细看,以为Basic Latin里面都是正常的字符集,后来在线上环境出现了问题 博主某天接到一个需求,需要过滤出某国语言 ...

  4. asp.net mvc抓取微信文章里面所有的图片

    /// <summary> /// 下载指定URL下的所有图片 /// </summary> public class WebPageImage { /// <summa ...

  5. (十八)SpringBoot之发送QQ邮件

    一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  6. ASP.NET Core 入门(1)(搭建环境CentOS)

    一.CentOS 7 安装 下载CentOS http://isoredirect.centos.org/centos/7/isos/x86_64/  选择其中下载即可. 下载完成后打开vmware准 ...

  7. 最快得到MYSQL两个表的差集

    Mysql里不外乎就是 子查询 和 连接 两种方式. 设第一个表为table1, 第二个为table2, table1包含table2. sql为: //子查询 select table1.id fr ...

  8. gitea configure

    gitea configure app.ini APP_NAME = Gitea: Git with a cup of tea RUN_USER = LSGX RUN_MODE = prod [oau ...

  9. 媲美pandas的数据分析工具包Datatable

    1 前言 data.table 是 R 中一个非常通用和高性能的包,使用简单.方便而且速度快,在 R 语言社区非常受欢迎,每个月的下载量超过 40 万,有近 650 个 CRAN 和 Biocondu ...

  10. 关于element ui滚动条使用

    element ui 自带的滚动条使用 在容器的直接外层添加 (需要展现滚动条的那一级) <el-scrollbar style="height:100%"></ ...