链接: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. extern 使用方法具体解释

    在C语言中,修饰符extern用在变量或者函数的声明前,用来说明"此变量/函数是在别处定义的.要在此处引用".(extern能够置于变量或者函数前,以标示变量或者函数的定义在别的文 ...

  2. VCL改变主窗体的方法

    使用如下语句即可Pointer((@Application.MainForm)^) := Form1; 仔细想想和Pointer((Application.MainForm)) := Form1;有什 ...

  3. SilkTest高级进阶系列8 – 放下榔头,立地成佛

    在地球尚未毁灭的某天,手动测试的同事找到你所在的自动组,问是否可以帮助他们自动化一个需要连续添加100个条目的测试用例,因为他们觉得这是一个噩梦.你口头上说要先烟酒烟酒才能知道,但是内里早就满心欢喜: ...

  4. js技术要点---JS 获取网页源代码

    JS 获取网页源代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html& ...

  5. 一个通用onReady函数的实现

    define([], function(){ function onReady(fn) { var DOC = document, html = DOC.documentElement, W3C = ...

  6. Android开发技术周报

    Android开发技术周报 原文  http://androidweekly.cn/android-dev-weekly-issue48/ 教程 深入理解Android之Gradle Gradle是当 ...

  7. UVA 10892 LCM Cardinality(数论 质因数分解)

    LCM Cardinality Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair of number ...

  8. Socket简介 (转)

    Socket小白篇-附加TCP/UDP简介 Socket 网络通信的要素 TCP和UDP Socket的通信流程图 1.Socket 什么是Socket Socket:又称作是套接字,网络上的两个程序 ...

  9. 《深入理解OSGi:Equinox原理、应用与最佳实践》笔记_1_运行最简单的bundlehelloworld

    <深入理解OSGi:Equinox原理.应用与最佳实践>笔记_1_运行最简单的bundlehelloworld 买了周大大的OSGI的书看 先前完全没有基础 就靠这本书看看学学 顺便记一些 ...

  10. ContentType ,charset和pageEncoding的区别(转)

    ========================说法一=========================== ContentType 属性指定响应的 HTTP 内容类型.如果未指定 ContentTy ...