题目链接:http://poj.org/problem?id=2417

题目:

题意:

  求一个最小的x满足a^x==b(mod p),p为质数。

思路:

  BSGS板子题,推荐一篇好的BSGS和扩展BSGS的讲解博客:http://blog.miskcoo.com/2015/05/discrete-logarithm-problem

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pli;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson i<<1
#define rson i<<1|1
#define lowbit(x) x&(-x)
#define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define FIN freopen("D://code//in.txt", "r", stdin);
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f; int a, b, p; struct Hashmap { //哈希表
static const int Ha=, maxe=;
int E,lnk[Ha], son[maxe+], nxt[maxe+], w[maxe+];
int top, stk[maxe+];
void clear() {
E=;
while(top) lnk[stk[top--]]=;
}
void Add(int x,int y) {
son[++E]=y;
nxt[E]=lnk[x];
w[E]=((<<) - ) * + ;
lnk[x]=E;
}
bool count(int y) {
int x=y % Ha;
for (int j = lnk[x]; j; j=nxt[j])
if (y == son[j]) return true;
return false;
}
int& operator [] (int y) {
int x=y % Ha;
for (int j = lnk[x]; j; j = nxt[j])
if (y == son[j]) return w[j];
Add(x,y);
stk[++top]=x;
return w[E];
}
}mp; int exgcd(int a, int b, int& x, int& y) {
if(b == ) {
x = , y = ;
return a;
}
int d = exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return d;
} int BSGS(int A, int B, int C) {
if(C == ) {
if(!B) return A != ;
else return -;
}
if(B == ) {
if(A) return ;
else return -;
}
if(A % C == ) {
if(!B) return ;
else return -;
}
int m = ceil(sqrt(C)); //分块
int D = , base = ;
mp.clear();
for(int i = ; i <= m - ; i++) {
if(mp[base] == ) mp[base] = i;
else mp[base] = min(mp[base], i);
base = ((LL)base * A) % C;
}
for(int i = ; i <= m - ; i++) {
int x, y, d = exgcd(D, C, x, y);
x = ((LL)x * B % C + C) % C;
if(mp.count(x)) return i * m + mp[x];
D = ((LL)D * base) % C;
}
return -;
} int main() {
//FIN;
while(~scanf("%d%d%d", &p, &a, &b)) {
int ans = BSGS(a, b, p);
if(ans == -) printf("no solution\n");
else printf("%d\n", ans);
}
return ;
}

Discrete Logging(POJ2417 + BSGS)的更多相关文章

  1. POJ2417 Discrete Logging【BSGS】

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 2494 ...

  2. Discrete Logging(poj2417)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5120   Accepted: 2319 ...

  3. POJ2417 Discrete Logging【BSGS】(模板题)

    <题目链接> 题目大意: P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P). 解题分析: 这题是bsgs算法的模板题. #incl ...

  4. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

  5. 【BSGS】BZOJ3239 Discrete Logging

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 729  Solved: 485[Submit][Statu ...

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

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

  7. 【BZOJ3239】Discrete Logging BSGS

    [BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...

  8. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

  9. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

随机推荐

  1. PHP中类型约束

    类型约束 什么叫类型约束? 就是要求某个变量只能使用(接收,存储)某种指定的数据类型: php属于“弱类型语言”,通常不支持类型约束: 相应的,强类型语言,类型约束却是其“基本特征”. php中,只支 ...

  2. C#和Java访问修饰符的比较

    访问修饰符对于C#:类 的默认修饰符是 internal(外部类只能被public / internal 修饰)枚举 的默认修饰符是 public 且此类型不允许其它访问修饰符接口 的默认修饰符是 i ...

  3. Java Servlet简介

     一.了解Servlet的概念 Servlet定义 Servlet是基于Java技术的Web组件,由容器管理并产生动态的内容.Servlet引擎作为WEB服务器的扩展提供支持Servlet的功能.Se ...

  4. zepto中$.proxy()的到底有多强大?

    好吧,其实是标题党了,哈哈,只是想总结一下工作中遇到$.proxy()的用法而已. 一.语法: $.proxy()有两种使用语法 1)$.proxy(fn,context),fn是一个函数,conte ...

  5. CF486D-Valid Sets

    题目 给出一个\(n\)个点的树,每个点有权值\(a_i\),再给出一个\(d\),问有多少个非空点集满足: 点集在树上构成联通子图 \[\max _{v\in S}a_v -\min _{v\in ...

  6. XML格式化加载的时候提示Content is not allowed in prolog. Nested exception: Content is not allowed in prolog

    原因:原本是.xml文件格式的内容,被你用右键,文本编辑,保存,导致格式不认了. 解决方法:下载个notepad+ 工具,用这工具打开,修改,编辑,保存,即可被继续认作xml格式.

  7. python打造12306余票实时监控

    # encoding=utf-8from Tkinter import *from ScrolledText import ScrolledTextimport urllib2import jsoni ...

  8. 【CodeChef】ForbiddenSum

    Portal --> CC ForbiddenSum Solution 场上想到了\(O(NM)\)的做法..然而并没有什么用 首先考虑比较简单的一个问题,给定一个数组\(A\),问这些数不能凑 ...

  9. 学习 opencv---(11)OpenC 边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器

    本篇文章中,我们将一起学习OpenCV中边缘检测的各种算子和滤波器——Canny算子,Sobel算子,Laplace算子以及Scharr滤波器.文章中包含了五个浅墨为大家准备的详细注释的博文配套源代码 ...

  10. Leetcode 7. 整数反转(待整理)

    1.题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: ...