POJ 2417 Discrete Logging 离散对数
链接:http://poj.org/problem?id=2417
题意:
思路:求离散对数,Baby Step Giant Step算法基本应用。
下面转载自:AekdyCoin
【普通Baby Step Giant Step】
【问题模型】
求解
A^x = B (mod C) 中 0 <= x < C 的解,C 为素数
【思路】
我们能够做一个等价
x = i * m + j ( 0 <= i < m, 0 <=j < m) m = Ceil ( sqrt( C) )
而这么分解的目的无非是为了转化为:
(A^i)^m * A^j = B ( mod C)
之后做少许暴力的工作就能够解决这个问题:
(1) for i = 0 -> m, 插入Hash (i, A^i mod C)
(2) 枚举 i ,对于每个枚举到的i,令 AA = (A^m)^i mod C
我们有
AA * A^j = B (mod C)
显然AA,B,C均已知,而因为C为素数,那么(AA,C)无条件为1
于是对于这个模方程解的个数唯一(能够利用扩展欧几里得或 欧拉定理来求解)
那么对于得到的唯一解X,在Hash表中寻找,假设找到,则返回 i * m + j
注意:因为i从小到大的枚举,而Hash表中存在的j必定是对于某个剩余系内的元素X 是最小的(就是指标)
所以显然此时就能够得到最小解
假设须要得到 x > 0的解,那么仅仅须要在上面的步骤中推断 当 i * m + j > 0 的时候才返回
(转载结束)
本题仅仅是最基础的应用,复杂度是
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <set>
#define PI acos(-1.0)
#define maxn 10005
#define INF 0x7fffffff
#define eps 1e-8
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
LL pow_mod(LL aa,LL ii,LL nn)
{
if(ii==0)
return 1%nn;
LL temp=pow_mod(aa,ii>>1,nn);
temp=temp*temp%nn;
if(ii&1)
temp=temp*aa%nn;
return temp;
}
struct b_step
{
int i,m;
} bb[100005];
bool cmp(b_step a,b_step b)
{
return a.m==b.m?a.i<b.i:a.m<b.m;
}
int BiSearch(int m,LL num)
{
int low=0,high=m,mid;
while(low<=high)
{
mid=(low+high)>>1;
if(bb[mid].m==num)
return bb[mid].i;
if(bb[mid].m<num)
low=mid+1;
else
high=mid-1;
}
return -1;
}
void giant_step_baby_step(LL b,LL n,LL p)
{
int m=(int)ceil(sqrt((double)p));
bb[0].i=0,bb[0].m=1;
for(int i=1; i<m; i++)
{
bb[i].i=i;
bb[i].m=bb[i-1].m*b%p;
}
sort(bb,bb+m,cmp);
int top=0;
for(int i=1; i<m; i++)
if(bb[i].m!=bb[top].m)
bb[++top]=bb[i];
LL bm=pow_mod(pow_mod(b,p-2,p),m,p);
LL ans=-1;
LL tmp=n;
for(int i=0; i<m; i++)
{
int pos=BiSearch(top,tmp);
if(~pos)
{
ans=m*i+pos;
break;
}
tmp=((LL)tmp*bm)%p;
}
if(!~ans)
puts("no solution");
else
printf("%d\n",ans);
}
int main()
{
LL p,b,n;
while(~scanf("%lld%lld%lld",&p,&b,&n))
{
giant_step_baby_step(b,n,p);
}
return 0;
}
POJ 2417 Discrete Logging 离散对数的更多相关文章
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- 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(离散对数-小步大步算法)
Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...
- 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
http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ...
随机推荐
- 《转》如何成为一个牛逼的C/C++程序员?
原地址:http://blog.csdn.net/langeldep/article/details/6333562 这个题目的噱头太大,要真的写起来, 足够写一本书了. 本人是过来人, 结合自身的体 ...
- dvtm: 平铺式终端管理器 — LinuxTOY
dvtm: 平铺式终端管理器 — LinuxTOY LinuxTOY 是一个致力于提供 Linux 相关资讯的专题站点.如果您发现了好用好玩的 Linux 东东并愿意发扬自由.分享的精神,可以点击顶部 ...
- C语言内存对齐
转:http://blog.csdn.net/embeddedman/article/details/7429976 首先由一个程序引入话题: 1 //环境:vc6 + windows sp2 2 ...
- hdu4635(强连通缩点)
传送门:Strongly connected 题意:求最多可以加多少边,使得最新的图还不是强连通图. 分析:最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边,那么要使得边数 ...
- hdu5046(重复覆盖+二分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:要在n个城市里建造不超过k个机场覆盖所有城市,问机场城市之间最大距离最小为多少. 分析:二 ...
- 【转向Javascript系列】深入理解Web Worker
本文首发在alloyteam团队博客,链接地址http://www.alloyteam.com/2015/11/deep-in-web-worker/ 上一篇文章<从setTimeout说事件循 ...
- 在web浏览器中判断app是否安装并直接打开
最近公司App产品在运营推广上有一个需求,就是要求可以让用户在访问我们的推广网页时,就可以判断出这个用户手机上是否安装了我们的App,如果安装了则可以直接在网页上打开,否则就引导用户前往下载.从而形成 ...
- JavaScript采用append添加的元素错误
1.错误叙述性说明 于IE览器上: Uncaught HierarchyRequestError:Failed to excute 'appendChild' on 'Node':The new ch ...
- 理解cookie的path和domain属性(转)
今天在做验证码时发现一个问题:A.B窗口都打开同一个页面,A先生成一个验证码,B再生成验证码,这时A所生成的验证码被B覆盖掉了.原因是使用了同名的cookie来存储验证码.一时找不到解决方法就参考了W ...
- TCP header
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vzc2VyNDM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...