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 ...
随机推荐
- 41、用Python实现一个二分查找的函数
data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35] def binary_search(dataset ...
- php遍历路径——php经典实例
php遍历路径——php经典实例 代码: <html> <head> <title>遍历目录</title> <meta charset=&quo ...
- 巅峰极客CTF writeup[上]
经验教训 1.CTF不比实战,最好不要死磕.死磕就输了.我就是死磕在缓存文件死的.真的惭愧: 2.对于flag的位置不要太局限于web目录下,如果是命令执行直接上find / -name flag*: ...
- Linux下如何打开img镜像文件
有些镜像文件为IMG格式,在Linux如何打开呢?例如从微软dreampark下载的Windows Server 2008 R2镜像文件,使用file命令查看: $ file chs_windows_ ...
- 【转载】selenium with PhantomJs wait till page fully loaded?
I use Selenium with Phantomjs, and want to get the page content after the page fully loaded. I tried ...
- juery获取元素的方法
1 从集合中通过指定的序号获取元素 html: 复制代码 代码如下: <div> <p>0</p> <p>1</p> <p>2& ...
- 012 public等关键字可见性
public: 具有最大的访问权限,可以访问任何一个在classpath下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. protected: 主要的作用就是用来保护子 ...
- (总结)MySQL自带的性能压力测试工具mysqlslap详解
PS:今天一同事问我有木有比较靠谱的mysql压力测试工具可用.其实mysql自带就有一个叫mysqlslap的压力测试工具,还是模拟的不错的.下面举例说说.mysqlslap是从5.1.4版开始的一 ...
- Linux/Unix 怎样找出并删除某一时间点的文件
Linux/Unix 怎样找出并删除某一时间点的文件 在Linux/Unix系统中,我们的应用每天会产生日志文件,每天也会备份应用程序和数据库,日志文件和备份文件长时间积累会占用大量的存储空间,而有些 ...
- 二、python框架相关知识体系
Django框架 1.django框架.flask框架和Tornado框架的区别? django框架,内置组件多,自身功能强大,是一个大而全的框架,ORM.Admin.中间件.Form.ModelFr ...