思路:数学大汇总

提交:\(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. shell习题第24题:杀进程

    [题目要求] 一台机器负载高,top查看到有很多sh的进程,然后top -c查看可以看到对应的进程命令是sh -c /bin/clear.sh 经分析后发现是因为该脚本执行时间太长,导致后续执行时,上 ...

  2. Linux自动运维工具Ansible的使用

    Linux自动运维工具Ansible的使用 我们熟悉这个工具后, 可以很轻松的安装k8s. 一.介绍 ansible - run a task on a target host(s) Ansible是 ...

  3. Nginx安装配置|Nginx反向代理|Nginx支持HTTPS|Nginx重定向

    Nginx安装配置 可以直接看到最下面的HTTPS. Nginx安装 我的系统如下: No LSB modules are available. Distributor ID: Ubuntu Desc ...

  4. (八)二进制文件在webservice中的处理(以byte[]字节数组方式)

    一.介绍 二进制文件在webservice中的处理. A:通过byte[]字节数组的方式来传递.比较适合小文件,字节数组不能太大的情况.(本章所用) B:通过DataHander的方式来进行传递. 1 ...

  5. Asp.Net Core 轻松学系列-4玩转配置文件

    目录 前言 另类方式使用 hosting.json 使程序运行于多个端口 结语 前言     在 .NET Core 项目中,配置文件有着举足轻重的地位:与.NetFramework 不同的是,.NE ...

  6. ArrayList和CopyOnWriteArrayList(转载)

    这篇文章的目的如下: 了解一下ArrayList和CopyOnWriteArrayList的增删改查实现原理 看看为什么说ArrayList查询快而增删慢? CopyOnWriteArrayList为 ...

  7. 服务器去掉IE浏览器网络安全限制

  8. 有选择性的启用SAP UI5调试版本的源代码

    在低版本的SAP UI5应用中,我们一旦切换成调试模式,那么应用程序源代码和UI5框架程序的源代码的调试版本都会重新加载,耗时很长. 我最近发现UI5新版本1.66.1提供了选择性加载调试版本的源代码 ...

  9. Python之定义默认参数

    定义函数的时候,还可以有默认参数. 例如Python自带的 int() 函数,其实就有两个参数,我们既可以传一个参数,又可以传两个参数: >>> int('123') 123 > ...

  10. AndroidStudio设置应用图标

    两种方法: 第一种:打开new -->Image Asset更换自己的图标一直保存即可. 第二种:在 res\drawable目录下 放置图标;修改 AndroidManifest.xml : ...