http://poj.org/problem?id=2417

BSGS 大步小步法( baby step giant step )

sqrt( p )的复杂度求出 ( a^x ) % p = b % p中的x

https://www.cnblogs.com/mjtcn/p/6879074.html

我的代码中预处理a==b和b==1的部分其实是不必要的,因为w=sqrt(p)(向上取整),大步小步法所找的x包含从0到w^2。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define LL long long
LL p,a,b;
map<LL,LL>q;
int main(){
while(~scanf("%lld%lld%lld",&p,&a,&b)){
if(b==){printf("0\n");continue;}
if(a==b){printf("1\n");continue;}
LL w=(LL)sqrt((double)p),x=,y=;if(w*w!=p)++w;
q[b]=-;
for(int i=;i<=w;++i){x=(x*a)%p;LL z=(b*x)%p;if(q[z]==)q[z]=i;}
bool f=;
for(int i=;i<=w;++i){
y=(y*x)%p;
if(q[y]!=){
LL z=q[y];if(z==-) z=;
z=(LL)i*w-z;f=;
printf("%lld\n",z);
break;
}
}
q[b]=;x=;
for(int i=;i<=w;++i){x=(x*a)%p;LL z=(b*x)%p;q[z]=;}
if(!f)printf("no solution\n");
}
return ;
}

POJ 2417 Discrete Logging BSGS的更多相关文章

  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 (Baby-Step Giant-Step)

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

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

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

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

  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 离散对数

    链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用. 下面转载自:AekdyCoin [普通Baby S ...

  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(离散对数-小步大步算法)

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

  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. Dubbo高可用

    高可用:通过设计减少系统不能提供服务的时间 (1).zookeeper宕机 原因:zookeeper宕机 现象:zookeeper注册中心宕机,还可以消费dubbo暴露的服务. 健壮性: 监控中心宕掉 ...

  2. FarBox的建站过程

    FarBox的建站过程 本文转自:http://mosir.org/html/y2012/How-to-build-your-website-by-farbox.html 作者: mosir 时间:  ...

  3. PYTHON-面向对象 类 绑定方法

    面向对象三大特性(*****) 1继承 1. 什么是继承 继承是一种新建类的方式,新建的类称之为子类/派生类,被继承的类称之为父类/基类/超类 继承与现实生活中的继承一个意思 (例如 小明继承小明他爹 ...

  4. h5新API之WebStorage解决页面数据通信问题

    localStorage相信大家都不陌生,今天我们要讨论的不是怎么存储数据,获取数据.而是看看WebStorage的一些妙用,相信大家在开发中遇到过这样一个场景,一个页面中嵌套一个iframe,ifr ...

  5. SqlServer 2017 下载地址及密钥

    下载地址: ed2k://|file|cn_sql_server_2017_developer_x64_dvd_11296175.iso|1769777152|E21AE7C3576C0BDF1BC0 ...

  6. cf689d ST表RMQ+二分

    类似hdu5289,但是二分更复杂.本题枚举左端点,右端点是一个区间,需要二分找到区间的左端点和右端点(自己手动模拟一次),然后区间长度就是结果增加的次数 另外结果开long long 保存 /** ...

  7. ural1855 线段树区间更新+推公式维护一元二次式

    和威威猫系列故事差不多,都是根据条件推出公式 /* 操作c a b d:a到b道路上的所有边权值加d 操作e a b:问a到b中包含的道路的平均权值 区间平均值=所有可能路径权值/所有路径数, 而路径 ...

  8. java远程工具类

    package com.zdz.httpclient; import java.io.BufferedReader; import java.io.IOException; import java.i ...

  9. 【C语言】 二叉树的基本运算

    • 二叉树节点类型BTNode: typedef struct node { char data; struct node *lchild, *rchild; } BTNode; 创建二叉树 void ...

  10. hdu 1257 一共要多少套拦截系统 (LIS)

    给出导弹的高度 拦截的导弹会比上一次低 至少要几套拦截系统才能防御所有导弹 求一套系统能防御的最大导弹数: 反向LIS求一共要多少套:正向LIS Sample Input8 389 207 155 3 ...