题目链接: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. 【final】评价①

    飞天小女警添加猜你喜欢功能,个人很喜欢.当推荐产品不喜欢的时候还有其他的选择,很人性化. 金州勇士将管理人员的角色分开,使整个系统的分工更明确,也更清晰. 新蜂的俄罗斯方块随着等级的提升有直观的颜色变 ...

  2. Fetch POST All in One

    Fetch POST All in One FPAIO "use strict"; /** * * @author xgqfrms * @license MIT * @copyri ...

  3. Catalan数,括号序列和栈

    全是入门的一些东西.基本全是从别处抄的. 栈: 支持单端插入删除的线性容器. 也就是说,仅允许在其一端加入一个新元素或删除一个元素. 允许操作的一端也叫栈顶,不允许操作的一端也叫栈底. 数个箱子相叠就 ...

  4. Appium自动化测试框架

    1.在utils包中创建一个AppiumUtil类,这个类是对appium api进行封装的. 代码如下: package utils; import java.net.MalformedURLExc ...

  5. Contest 5

    A:这我怎么没学傻了啊.整个一傻逼题一眼容斥我连暴力都写不出来啊.显然序列是没有什么用的,考虑求众数小于x的概率,显然可以枚举有几个超过容斥一发.虽然要算的组合数非常大,发现可以抵消很大一部分,最后算 ...

  6. 线段树之Sum

    题面: 给定一数列,规定有两种操作,一是修改某个元素,二是求区间的连续和. Input: 输入数据第一行包含两个正整数n,m(n<=100000,m<=500000),以下是m行, 每行有 ...

  7. [BJWC2011]元素 线性基

    题面 题面 题解 一个方案合法,当且仅当选取的01串凑不出0. 因此就是要使得选取的01串全在线性基内,具体原因可以看这道题:[CQOI2013]新Nim游戏 线性基 要使得魔力值最大,只需要按法力值 ...

  8. NAT网络地址转换模拟过程

    原理图,如图1 图1 以下为配置NAT网络地址转换的实验: eNSP模拟图,如图2 图2 Step1.给路由器的每个接口赋予一个地址,如图3,图4 图3 图4 AR1和AR2中添加路由表项,如图5,图 ...

  9. 代码收藏系列--jquery--筛选器、事件绑定技巧

    Jquery筛选器的一些常用技巧,比如过滤属性等 /* 过滤获取没有含data-xsui-grid-colspan的节点 */$(this).find('.xsui-grid-cell:not([da ...

  10. bzoj3251: 树上三角形(思维题)

    神tmWA了8发调了20min才发现输出没回车T T... 首先考虑一段什么样的序列才会是N... 显然最长的形式就是斐波那契,前两数之和等于第三数之和,这样就无法组成三角形并且序列最长.可以发现在i ...