链接: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 离散对数的更多相关文章

  1. BSGS算法+逆元 POJ 2417 Discrete Logging

    POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accept ...

  2. poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2831   Accepted: 1391 ...

  3. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  4. 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 ...

  5. POJ 2417 Discrete Logging ( Baby step giant step )

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

  6. POJ 2417 Discrete Logging(离散对数-小步大步算法)

    Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 ...

  7. 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* ...

  8. POJ 2417 Discrete Logging BSGS

    http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...

  9. POJ 2417 Discrete Logging

    http://www.cnblogs.com/jianglangcaijin/archive/2013/04/26/3045795.html 给p,a,b求a^n==b%p #include<a ...

随机推荐

  1. Cocos2d-x 创建(create)动画对象CCAnimation报错分析

    本人在使用精灵表单创建动画的过程中突然遇到了一些个问题,下面进行一下分析总结. 根据在Cocos2d-iphone中的经验,我写出了如下的代码: CCSpriteFrameCache::sharedS ...

  2. NginX issues HTTP 499 error after 60 seconds despite config. (PHP and AWS)

    FROM: http://stackoverflow.com/questions/15613452/nginx-issues-http-499-error-after-60-seconds-despi ...

  3. Android 吸入动画效果详解

    1,背景 吸入(Inhale)效果,最初我是在iOS上面看到的,它是在Note程序中,用户可能添加了一页记录,在做删除时,它的删除效果是:这一页内容吸入到一个垃圾框的图标里面.请看下图所示: ==== ...

  4. 【linux驱动分析】之dm9000驱动分析(六):dm9000_init和dm9000_probe的实现

    一.dm9000_init 打印出驱动的版本,注冊dm9000_driver驱动,将驱动加入到总线上.运行match,假设匹配,将会运行probe函数. 1 static int __init 2 d ...

  5. Android仿iOS7的UISegmentedControl 分段

    效果图: 这里仅仅简单做了两个button的. 首先是两个button的背景: res/drawable/seg_left.xml <?xml version="1.0" e ...

  6. 搭建自己的XenServer+CloudStack云平台,提供IaaS服务(一)环境搭建

    目标 搭建一个完整的基于XenServer和CloudStack的虚拟化平台,提供IaaS服务. 搭建三台安装了XenServer的服务器 搭建一台安装了CloudStack的服务器用以管理云平台 搭 ...

  7. 真实世界里的钢铁侠-特斯拉汽车创始人埃隆&#183;马斯克(Elon Musk)

    真实世界里的钢铁侠--特斯拉汽车公司和SpaceX公司总裁马斯克(31岁).当我们得意于「站在山上踢几块石头下去」或是「站在风口上的猪」的成功理论的时候,我们真的成功了吗?我们到底创造了什么?改变了什 ...

  8. nginx fastcgi 自定义错误页面

    http{ fastcgi_intercept_errors on; error_page 404 /404.html; } fastcgi_intercept_errors on;必须设置 之后通过 ...

  9. OC -- 第一个类

    OC -- 第一个类 类名:Car 属性:轮胎个数.时速 行为:跑 完整写一个类:类的声明和实现 1.    类的声明 代码: // NSObject 再Foundation框架中 #import & ...

  10. 调用WCF的异步方法

    原文:调用WCF的异步方法 AsyncCallback aLoginCallback = delegate(IAsyncResult result) { var aSystemUser = WcfCl ...