BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 4860 | Accepted: 2211 |
Description
B^l==N(mod p)
Input
Output
Sample Input
5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111
Sample Output
0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587
/*BSGS算法+逆元*/
这个主要是用来解决这个题: A^x=B(mod C)(C是质数),都是整数,已知A、B、C求x。 我在网上看了好多介绍,觉得他们写得都不够碉,我看不懂…于是我也来写一发。 先把x=i*m+j,其中m=ceil(sqrt(C)),(ceil是向上取整)。 这样原式就变为A^(i*m+j)=B(mod C), 再变为A^j=B*A^(-m*i) (mod C), 先循环j=~(C-),把(A^j,j)加入hash表中,这个就是Baby Steps 下面我们要做的是枚举等号右边,从hash表中找看看有没有,有的话就得到了一组i j,x=i*m+j,得到的这个就是正确解。 所以,接下来要解决的就是枚举B*A^(-m*i) (mod C)这一步(这就是Giant Step A^(-m*i)相当于1/(A^(m*i)),里面有除法,在mod里不能直接用除法,这时候我们就要求逆元。 /*百度百科: 若ax≡1 mod f, 则称a关于模f的乘法逆元为x。也可表示为ax≡1(mod f)。
当a与f互素时,a关于模f的乘法逆元有唯一解。如果不互素,则无解。如果f为素数,则从1到f-1的任意数都与f互素,即在1到f-1之间都恰好有一个关于模f的乘法逆元。
*/ 然后我们用超碉的exgcd求逆元,exgcd(扩展欧几里德算法)就是在求AB的最大公约数z的同时,求出整数x和y,使xA+yB=z。算法实现就是gcd加几个语句。
然后我们再来看一下exgcd怎么求逆元:
对xA+yB=z, 变成这样xA = z - yB,取B=C(C就是我们要mod的那个) 推导出 xA % C = z %C 只要 z%C== 时,就可以求出A的逆元x 但用exgcd求完,x可能是负数,还需要这样一下:x=(x%C+C)%C //--exgcd介绍完毕-- 再看我们的题目, exgcd(A^(m*i) , C)=z,当C是质数的时候z肯定为1,这样exgcd求得的x就是逆元了。 因为x就是A^(m*i)的逆元,P/(A^(m*i))=P*x,所以 B*A^(-m*i) = B/(A^(m*i)) = B*x(mod C) 这样我们的式子A^j=B*A^(-m*i) (mod C)的等号右边就有了,就是B*x,就问你怕不怕! 枚举i,求出右边在hash里找,找到了就返回,无敌! /*---------分割线-----------------------*/
#include<cmath>
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define mod 100007
#define ll long long
struct hash
{
ll a[mod+],v[mod+];
hash(){memset(a,-,sizeof(a));}
int locate(ll x)
{
ll l=x%mod;
while(a[l]!=x&&a[l]!=-) l=(l+)%mod;
return l;
}
void insert(ll x,int i)
{
ll l=locate(x);
if(a[l]==-)
{
a[l]=x;
v[l]=i;
}
}
int get(ll x)
{
ll l=locate(x);
return (a[l]==x)?v[l]:-;
}
void clear()
{
memset(a,-,sizeof(a));
}
}s;
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=;
y=;
return ;
}
exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*y;
}
int main()
{
ll p,b,n;
while(scanf("%I64d%I64d%I64d",&p,&b,&n)==)
{
s.clear();
ll m=ceil(sqrt(p));
ll t=;
for(int i=;i<m;++i)
{
s.insert(t,i);
t=(t*b)%p;
}
ll d=,ans=-;
ll x,y;
for(int i=;i<m;++i)
{
exgcd(d,p,x,y);
x=((x*n)%p+p)%p;
y=s.get(x);
if(y!=-)
{
ans=i*m+y;
break;
}
d=(d*t)%p;
}
if(ans==-)
printf("no solution\n");
else printf("%I64d\n",ans);
} return ;
}
BSGS算法+逆元 POJ 2417 Discrete Logging的更多相关文章
- POJ - 2417 Discrete Logging(Baby-Step Giant-Step)
d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...
- POJ 2417 Discrete Logging ( Baby step giant step )
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3696 Accepted: 1727 ...
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2819 Accepted: 1386 ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- POJ 2417 Discrete Logging BSGS
http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...
- POJ 2417 Discrete Logging(离散对数-小步大步算法)
Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...
- POJ 2417 Discrete Logging 离散对数
链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...
- poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)
http://poj.org/problem?id=2417 A^x = B(mod C),已知A,B.C.求x. 这里C是素数,能够用普通的baby_step. 在寻找最小的x的过程中,将x设为i* ...
- POJ 2417 Discrete Logging
http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ...
随机推荐
- 固定bottom,页面其它可滑动实现方案
利用flex布局, <html> <body> <div class='container'> <div class='content'></di ...
- docker之设置开机自启动(二)
docker的自启动 通过sysv-rc-conf等管理 启动脚本 # docker.service #!/bin/sh sudo systemctl enable docker sudo syste ...
- Mysql存储之ORM框架SQLAlchemy(一)
上一篇我们说了mysql存储的原生语句方式,因为原生语句每次写都比较的复杂,所以这里我们说一种引用实体类的方式来操作数据库. 什么是ORM ORM技术:Object-Relational Mappin ...
- 图解IIS8上解决ASP.Net第一次访问慢的处理
- gan对抗式网络
感觉好厉害,由上图噪声,生成左图噪声生成右图以假乱真的图片, gan网络原理: 本弱又盗了一坨博文,不是我写的,如下:(跪膜各路大神) 前面我们已经讲完了一般的深层网络,适用于图像的卷积神经网络,适用 ...
- Kail Linux渗透测试之测试工具Armitage
Kali Linux下的Armitage是一个很强大的渗透工具,图形化操作页面,但我们把kali linux装在虚拟机里面,然后再启动armitage就会出现一个error,他会给你一个message ...
- Python抓取微博评论
本人是张杰的小迷妹,所以用杰哥的微博为例,之前一直看的是网页版,然后在知乎上看了一个抓取沈梦辰的微博评论的帖子,然后得到了这样的网址 然后就用m.weibo.cn进行网站的爬取,里面的微博和每一条微博 ...
- Regular Expression Matching——没理解的动态规划
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Linux命令之dig命令实例讲解
1.查看域名的A记录 # dig yahoo.com; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <<> ...
- IIS 部署nodejs
1.Node.js 2.IIS的URL Rewrite模块 3.iisnode