求解A^x ≡ B mod P (P不一定是质数)的最小非负正整数解

先放几个同余定理:

一、判断如果B==1,那么x=0,算法结束

二、若gcd(A,P)不能整除 B,则 无解,算法结束

三、若gcd(A,P)!=1,令d=gcd(A,P),若d不能整除B,则无解,算法结束。

四、持续步骤三,直至 gcd(A,)=1

有 

五、枚举 0<x<k,若有解,输出x,算法结束

六、对于x>=k,

A=,B=,P=

A,P 互素 ,

直接用BSGS 求    * A ^ x ≡ B mod P

所得结果再+k即可

#include<map>
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; typedef long long LL; map<int,int>mp; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} int get_gcd(int a,int b) { return !b ? a : get_gcd(b,a%b); } int Pow(int a,int b,int mod)
{
int res=;
for(;b;a=1LL*a*a%mod,b>>=)
if(b&) res=1LL*res*a%mod;
return res;
} int ex_BSGS(int A,int B,int C)
{
if(B==) return ;
int k=,tmp=,d;
while()
{
d=get_gcd(A,C);
if(d==) break;
if(B%d) return -;
B/=d; C/=d;
tmp=1LL*tmp*(A/d)%C;
k++;
if(tmp==B) return k;
}
mp.clear();
int mul=B;
mp[B]=;
int m=ceil(sqrt(1.0*C));
for(int j=;j<=m;++j)
{
mul=1LL*mul*A%C;
mp[mul]=j;
}
int am=Pow(A,m,C);
mul=tmp;
for(int j=;j<=m;++j)
{
mul=1LL*mul*am%C;
if(mp.count(mul)) return j*m-mp[mul]+k;
}
return -;
} int main()
{
int A,C,B;
int ans;
while()
{
read(A); read(B); read(C);
if(!A) return ;
ans=ex_BSGS(A,B,C);
if(ans==-) puts("No Solution");
else cout<<ans<<'\n';
}
}

扩展BSGS算法的更多相关文章

  1. BSGS及扩展BSGS算法及例题

    \(BSGS(baby-step-giant-step)\)算法是用来解高次同余方程的最小非负整数解的算法,即形如这个的方程: \(a^x\equiv b(mod\ p)\) 其中\(p\)为质数(其 ...

  2. BSGS算法学习笔记

    从这里开始 离散对数和BSGS算法 扩展BSGS算法 离散对数和BSGS算法 设$x$是最小的非负整数使得$a^{x}\equiv b\ \ \ \pmod{m}$,则$x$是$b$以$a$为底的离散 ...

  3. BSGS算法及扩展

    BSGS算法 \(Baby Step Giant Step\)算法,即大步小步算法,缩写为\(BSGS\) 拔山盖世算法 它是用来解决这样一类问题 \(y^x = z (mod\ p)\),给定\(y ...

  4. BSGS算法及其扩展

    bsgs算法: 我们在逆元里曾经讲到过如何用殴几里得求一个同余方程的整数解.而\(bsgs\)就是用来求一个指数同余方程的最小整数解的:也就是对于\(a^x\equiv b \mod p\) 我们可以 ...

  5. BSGS算法_Baby steps giant steps算法(无扩展)详解

    Baby Steps-Varsity Giant Step-Astronauts(May'n・椎名慶治) 阅读时可以听听这两首歌,加深对这个算法的理解.(Baby steps少女时代翻唱过,这个原唱反 ...

  6. 【数论】【快速幂】【扩展欧几里得】【BSGS算法】bzoj2242 [SDOI2011]计算器

    说是BSGS……但是跟前面那题的扩展BSGS其实是一样的……因为模数虽然是质数,但是其可能可以整除a!!所以这两者其实是一样的…… 第一二种操作不赘述. #include<cstdio> ...

  7. BSGS与扩展BSGS

    BSGS \(BSGS\)算法又称大步小步\((Baby-Step-Giant-Step)\)算法 \(BSGS\)算法主要用于解以下同余方程 \[A^x\equiv B(mod\ p)\]其中\(( ...

  8. BSGS算法总结

    BSGS算法总结 \(BSGS\)算法(Baby Step Giant Step),即大步小步算法,用于解决这样一个问题: 求\(y^x\equiv z\ (mod\ p)\)的最小正整数解. 前提条 ...

  9. BSGS和扩展BSGS

    BSGS: 求合法的\(x\)使得\(a ^ x \quad mod \quad p = b\) 先暴力预处理出\(a^0,a^1,a^2.....a^{\sqrt{p}}\) 然后把这些都存在map ...

随机推荐

  1. 单源最短路径问题(dijkstra算法 及其 优化算法(优先队列实现))

    #define _CRT_SECURE_NO_WARNINGS /* 7 10 0 1 5 0 2 2 1 2 4 1 3 2 2 3 6 2 4 10 3 5 1 4 5 3 4 6 5 5 6 9 ...

  2. PHP7 学习笔记(九)phpsize动态编译openssl扩展 (微信公众平台)

    先吐槽,微信公众平台授权出问题了,尽然访问不了 一.问题描述: 使用PHP中的库函数file_get_contents时出现Unable to find the wrapper "https ...

  3. 【C++ STL】Set和Multiset

    1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. 只要是assignable.copyable.comparab ...

  4. cdh-5.10.0搭建安装

    1.修改主机名为master, slave1, slave2 vim /etc/sysconfig/network HOSTNAME = master HOSTNAME = slave1 HOSTNA ...

  5. 7.SpringBoot 之 Web

    添加资源处理 package org.springframework.boot.autoconfigure.web.servlet. public class WebMvcAutoConfigurat ...

  6. ubuntu18.04使用sudo时反应时间长

    一.查看/etc/sudoer这个文件,是否有当前用户,若无,请联系 管理员或者是通过root用户添加 二.用hostname命令查看自己的主机名 三.添加自己的主机名到/etc/hosts文件中

  7. 第一节 简单的jsp实例

    1.打开Eclipse,依次点击“File” .“New” .“Other” ,选择生成动态Web项目. 2.输入项目名字,点击Finish 生成项目结构如下: 3.在WebContent目录上,单击 ...

  8. django错误笔记——TypeError: view must be a callable or a list/tuple in the case of include().解决办法

    django增加用户认证模块时,总是提醒模块的url.py中 url(r'^login/$', 'django.contrib.auth.views.login', name='login'),出错: ...

  9. 第16月第24天 find iconv sublime utf-8

    1. find . -type f -exec echo {} \; find src -type f -exec sh -c "iconv -f GB18030 -t UTF8 {} &g ...

  10. Androidstudio中jar包重复或jar包里的类重复问题

    https://www.jianshu.com/p/dd5d4fda1df8 http://blog.csdn.net/hqb112233/article/details/51514208