BSGS 模板
模板如下:
扩展版本:
求解a^k=b %p 求k,最小的k一定小于p,否则会重复,否则无解
***********************
gcd(a,p)=1时
设k=mi+v m=sqrt(p);
i,v<=m
a^v=b*(a^-m)^i %p
打表map for i=0~m-1 (a^i,i)
for i=0~m-1
check a^v存在
****************************
gcd(a,p)=t时
(A/t)^c*A^(x-c)=B/(t^c) %C/(t^c) c max
A^(x-c)=B/(t^c)*(A/t)^-c %C/(t^c)
const int NN = 99991 ; //sqrt(p)
int Hash[NN][2] ;
void insert(int id , LL vv){
LL v = vv % NN ;
while( Hash[v][0]!=-1 && Hash[v][1]!=vv){
v++ ; if(v == NN) v-=NN ;
}
if(Hash[v][0]==-1 ){
Hash[v][1] = vv ; Hash[v][0] = id ;
}
}
int find(LL vv){
LL v = vv % NN ;
while( Hash[v][0]!=-1 && Hash[v][1]!=vv){
v++ ;if(v == NN) v-=NN ;
}
if( Hash[v][0]==-1 ) return -1;
return Hash[v][0] ;
}
void ex_gcd(LL a , LL b , LL& x , LL& y){
if(b == 0){
x = 1 ; y = 0 ;
return ;
}
ex_gcd(b , a%b , x, y) ;
LL t = x ;
x = y;
y = t - a/b*y ;
}
LL baby_step(LL A, LL B , LL C){ //A^x=B %C 最小x,__gcd g++使用
LL D=1 % C ,d=0;
if(__gcd(A,C)!=1){
LL ans = 1 ;
for(LL i=0;i<=50;i++){
if(ans == B) return i ;
ans = ans * A % C ;
}
LL tmp ;
while( (tmp=__gcd(A,C)) != 1 ){
if(B % tmp) return -1 ;
d++ ;
B/=tmp ;
C/=tmp ;
D = D*A/tmp%C ;
} //D*A^(x-d)=B %C
} //printf("D=%lld A=%lld B=%lld C=%lld d=%lld\n",D,A,B,C,d);
memn(Hash);
LL M = ceil( sqrt(C*1.0) ) ;
LL rr = 1 ;
for(int i=0;i<M;i++){
insert(i, rr) ;
rr = rr * A % C ;
} //rr=A^M
LL jj,x,y;
for(int i=0;i<M;i++){
ex_gcd(D, C , x, y) ;
jj = find( (x * B % C+C)%C ) ; //printf("f %lld\n",r);
if(jj != -1){
return i*M+jj+d;
}
D = D * rr % C ;
}
return -1 ;
}
-1无解 sqrt(p)
=---------------------------------------
普通版本
//POJ 2417
//baby_step giant_step
// a^x = b (mod n) n为素数,a,b < n
// 求解上式 0 <= x < n的解
#include <cmath>
#include <cstdio>
#include <cstring>
#define MOD 76543
using namespace std;
int hs[MOD], head[MOD], next[MOD], id[MOD], top;
void insert(int x, int y)
{
int k = x % MOD;
hs[top] = x;
id[top] = y;
next[top] = head[k];
head[k] = top++;
}
int find(int x)
{
int k = x % MOD;
for (int i = head[k]; i != -1; i = next[i])
if (hs[i] == x)
return id[i];
return -1;
}
int BSGS(int a, int b, int n)
{
memset(head, -1, sizeof(head));
top = 1;
if (b == 1)
return 0;
int m = sqrt(n * 1.0), j;
long long x = 1, p = 1;
for (int i = 0; i < m; i++, p = p * a % n)
insert(p * b % n, i);
for (long long i = m; ; i += m)
{
if ((j = find(x = x * p % n)) != -1)
return i - j;
if (i > n)
break;
}
return -1;
}
int main()
{
int a, b, n;
while (~scanf("%d%d%d", &n, &a, &b))
{
int ans = BSGS(a, b, n);
if (ans == -1)
printf("no solution\n");
else
printf("%d\n", ans);
}
}
BSGS 模板的更多相关文章
- Bsgs模板
模板最主要的是自己看得舒服,不会给自己留隐患,调起来比较简单,板子有得是,最主要的是改造出适合你的那一套. ——mzz #include<bits/stdc++ ...
- bzoj2242,洛谷2485----SDOI2011计算器(exgcd,qsm,bsgs模板)
就是一道模板题! 这里再强调一下 BSGS 考虑方程\(a^x = b \pmod p\) 已知a,b,p\((2 \le p\le 10^9)\),其中p为质数,求x的最小正整数解 解法: 注意到如 ...
- BSGS模板(慢速)
//author Eterna #define Hello the_cruel_world! #pragma GCC optimize(2) #include<iostream> #inc ...
- bzoj 2242 [SDOI2011]计算器——BSGS模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
- 【Luogu】P2485计算器(快速幂,exgcd和Bsgs模板)
题目链接 题目描述非常直接,要求你用快速幂解决第一问,exgcd解决第二问,bsgs解决第三问. emmmm于是现学bsgs 第二问让求最小整数解好烦啊…… 假设我们要求得方程$ax+by=c(mod ...
- 2019牛客多校第五场C generator 2 hash,bsgs模板
generator 2 题意 给出\(x_0,a,b,p\),有方程\(x_i\equiv (a*x_{i-1}+b)(\% p)\),求最小的i,使得\(x_i=v\),不存在输出-1 分析 经过公 ...
- U9249 【模板】BSGS
题目描述 给定a,b,p,求最小的非负整数x 满足a^x≡b(mod p) 若无解 请输出“orz” 输入输出格式 输入格式: 三个整数,分别为a,b,p 输出格式: 满足条件的非负整数x 输入输出样 ...
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
- BZOJ3239Discrete Logging——BSGS
题目大意:给出$P,B,N$,求最小的正整数$L$,使$B^L\equiv N(mod\ P)$. $BSGS$模板题. #include<set> #include<map> ...
随机推荐
- 贪心算法_01背包问题_Java实现
原文地址:http://blog.csdn.net/ljmingcom304/article/details/50310789 本文出自:[梁敬明的博客] 1.贪心算法 什么是贪心算法?是指在对问题进 ...
- 浅析busybox如何集成到openwrt
背景 近日添加了一个包到openwrt中,在此过程中又对openwrt多了一些认识 这个包本身自带了kconfig,可直接在这个包里面执行make menuconfig进行配置,然后执行make 但要 ...
- PyQt: eg2
#coding:utf-8 from __future__ import division import sys from math import * from PyQt4 import QtCore ...
- Nginx服务安全设置和参数调优
1.添加参数隐藏Nginx版本号 vim /application/nginx/conf/nginx.conf #http标签下添加 server_tokens off; #测试 [root@cobb ...
- 转:google测试分享-GTA
原文: http://blog.sina.com.cn/s/blog_6cf812be0102viuh.html 上一次分享了google测试分享-分层测试,有很多自动化测试的策略和实施都要有一个重点 ...
- TeX Live & TeXstudio 安装手记
数据库课上又看到了那位用 beamer 做 slides 的师兄,想到自己一拖再拖的LaTeX入门,决定赶快动手装个环境再说~在经过一番搜索和研究之后决定先在 windows 底下试用,选择 TeX ...
- for循环练习题(共六道题)
第一题: 假设一个简单的ATM机的取款过程是这样的:首先提示用户输入密码(password),最多只能输入三次,超过3次则提示用户“密码错误,请取卡”结束交易.如果用户密码正确,再提示用户输入取款金额 ...
- 尝试 TFS Express 2012.3
之前一直使用SVN做版本管理,但是只能管理代码.之前的一份工作,只用了TFS来管理,可以将任务与代码集成管理,很是方便,只是安装太过于繁琐,现在的公司人少,不想费那么多事. 最关键的,就是安装TFS需 ...
- <<Javascript Patterns>>阅读笔记 -- 第2章 基本技巧(二)
关于for-in循环 循环数据时, 强烈不推荐使用for-in循环.因为当Array对象被扩展后, 再用for-in循环遍历数据会导致逻辑上的错误, 举例说明: var arr = ['a', 'b' ...
- 一个配置文件收集多个日志-if根据type类型判断
1.同时收集/var/log/messages日志和secure日志 #vim /etc/logstash/conf.d/system.conf input { file { path => & ...