poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 2831 | Accepted: 1391 |
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
/* 模板题baby_step.
Hash + 扩展欧几里得 +拆分思想 题意:
求A^x = B( mod C )的最小x,其中C是一个质数。
思路:
用普通的babystep_gaint_step即可,具体的做法是这样的,我们把x写成下面这样
的形式:x = i * m + j , 这样上式就可以变成:A^m^i * A^j = B( mod C ),其中m=
ceil( sqrt(C) ),0<=i<m , 0<=j <m,接下去我们先求出所有的A^j % C的值,并且把
它们存到一个hash表中去,接下去就是先处理出A^m%C的值,记作D,现在上式就
变成了这样:D^i * A^j = B( mod C ), 现在我们从0-m-1枚举i,这样D^i的值就
已经知道了,记为DD,下面我们先令A^j 为x,式子就变成了:DD*x = B( mod C )
这个式子是可以通过普通的扩展欧几里得算法求出x的解的(这个方程的x在0-C
内一定会只有一个唯一的解,因为gcd(DD, C) == 1, C是质数),然后在建好的hash
表中查找这个x是否存在,若是存在,则输出此时的i*m+j,就是答案。下面说明一下,
为什么x只需要考虑0-C的解就可以了,如果解存在,则上面已经说明,一定会在0-
C范围内存在一个解,因为是找最小的解,因此这时候的解就是答案;如果不存在
解,我们下面将要说明只需要考虑0-C范围内无解,方程就不会有解了。证明的过程
大致是这样的,首先如果方程在0 -- C内都没有解, 考虑A^x%C的值,由鸽笼原理可
知,余数中势必要出现循环节,而且循环节的长度是C的欧拉函数值,也就是说接下
去的x的余数将进入一个循环,从而将不会得出解了。 */ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<math.h>
using namespace std; typedef __int64 LL;
const int MAX=;
LL A,B,C;
bool Hash[MAX];
LL idx[MAX];
LL val[MAX]; void Ex_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==)
{
x=;
y=;
return ;
}
Ex_gcd(b,a%b,x,y);
LL hxl=x-(a/b)*y;
x=y;
y=hxl;
} LL Euler(LL n)
{
LL i,temp=n;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
while(n%i==)
n=n/i;
temp=temp/i*(i-);
}
}
if(n!=)
temp=temp/n*(n-);
return temp;
} void Insert(LL id,LL num)
{
LL k=num%MAX;
while(Hash[k] && val[k]!=num)
{
k++;
if(k==MAX) k=k-MAX;
}
if(!Hash[k])
{
Hash[k]=;
idx[k]=id;
val[k]=num;
}
}// Hash make LL found(LL num)
{
LL k=num%MAX;
while(Hash[k] && val[k]!=num)
{
k++;
if(k==MAX) k=k-MAX;
}
if(!Hash[k])
{
return -;
}
return idx[k];
}// Hash find LL baby_step(LL a,LL b,LL c)
{
LL M=ceil(sqrt(Euler(c)*1.0));
memset(Hash,false,sizeof(Hash));
memset(idx,-,sizeof(idx));
memset(val,-,sizeof(val));
LL D=;
for(LL i=;i<M;i++)
{
Insert(i,D);
D=D*a%c;
}//maek D; LL res=;
LL x,y;
for(LL i=;i<M;i++)
{
Ex_gcd(res,c,x,y);
LL tmp=x*b%c;
tmp=(tmp%c +c)%c;
LL k=found(tmp);
if(k!=-)
{
return LL(i)*M+k;
}
res=res*D%c;
}
return -;
} int main()
{
while(scanf("%I64d%I64d%I64d",&C,&A,&B)>)
{
LL res=baby_step(A,B,C);
if(res==-)
{
printf("no solution\n");
}
else printf("%I64d\n",res);
}
return ;
}
poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step的更多相关文章
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- 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(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 离散对数
链接: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 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://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ...
随机推荐
- android 开发 简单的小计算器
↑大致效果 项目构成: 随便写的,用的线性布局 activity_main.xml <?xml version="1.0" encoding="utf-8" ...
- 如何检查 IP是否冲突了
[root@TEST_192_168_1_252 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 44:A8:42:00:1A:B5 ...
- 编程大牛 Bruce Eckel 对新程序员的忠告
简评:作者 Bruce Eckel 是编程界的大牛,著有大名鼎鼎的<Thinking in C++>和<Thinking in Java>.本文是他对程序员(尤其是新手)的忠告 ...
- 安装配置python、beautifulsoup4、pip的心酸总结
1.python下载安装不纠结,但如果要加入到eclipse里面就要注意一下版本,版本不匹配会造成,要不python降级,要不eclipse升级的情况 2.在稍新版本的python立面就附带下载在了p ...
- ReactNative 打包 APK
ReactNative打包步骤: 1.在项目的根目录执行下面这行命令: keytool -genkey -v -keystore my-release-key.keystore -alias my-k ...
- redis-server.exe双击闪退 win10系统
博客 解决方法: 1-win+R 打开命令行 2-cd至redis目录,例如 D:\redis> 3-输入 redis-server.exe redis.windows.conf 4-若 ...
- #PHP# 华为云 API 方式发送短信
使用给华为云 消息 服务 API 方式发送短信 代码来自华为云,已通过测试 <?php /** * 华为云发送短信示例代码 * 本段代码需要使用自己的配置信息才能正常运行,出配置信息外,不需要改 ...
- MVC3权限验证,诡异的OnAuthorization
mvc3权限验证 protected override void OnAuthorization(AuthorizationContext filterContext) { if (//开始权限验证返 ...
- (转)zabbix3.4使用percona-monitoring-plugins监控mysql
原文:https://blog.csdn.net/yanggd1987/article/details/79656771 简介 之前主要使用nagios监控mysql,本文主要介绍使用percona- ...
- springboot入门神器 -http://start.spring.io/(在线项目构建)
参考并直接引用:http://www.sousou.io/article/1506656459859 最近在学习spring boot,看的书是<JavaEE开发的颠覆者 Spring Boot ...