BSGS模版 a^x=b ( mod c)
kuangbin的BSGS:
c为素数;
#define MOD 76543
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 != -; i = next[i])
if(hs[i] == x)
return id[i];
return -;
}
int BSGS(int a,int b,int n)
{
memset(head,-,sizeof(head));
top = ;
if(b == )return ;
int m = sqrt(n*1.0), j;
long long x = , p = ;
for(int i = ; 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)) != - )return i-j;
if(i > n)break;
}
return -;
}
扩展BSGS:
const int MAXN= ;
struct LINK{
ll data;
ll j;
ll next;
}HASH_LINK[];
ll ad, head[MAXN]; ll Gcd(ll a, ll b){
return b ? Gcd(b, a % b) : a;
} ll Ext_Gcd(ll a, ll b, ll &x, ll &y){
if(!b){
x = ; y = ;
return a;
}
ll r = Ext_Gcd(b, a % b, x, y);
ll t = x; x = y; y = t - a / b * y;
return r;
} ll POWER(ll a, ll b, ll c){
ll ans = ;
while(b){
if(b & ) ans = ans * a % c;
a = a * a % c;
b >>= ;
}
return ans;
} void init(){
memset(head, -, sizeof(head));
ad = ;
} ll Hash(ll a){
return a % MAXN;
} void INSERT_HASH(ll i, ll buf){
ll hs = Hash(buf), tail;
for(tail = head[hs]; ~tail; tail = HASH_LINK[tail]. next)
if(buf == HASH_LINK[tail]. data) return;
HASH_LINK[ad]. data = buf;
HASH_LINK[ad]. j = i;
HASH_LINK[ad]. next = head[hs];
head[hs] = ad ++;
} ll BSGS(ll a, ll b, ll c){
ll i, buf, m, temp, g, D, x, y, n = ;
for(i = , buf = ; i < ; i ++, buf = buf * a % c)
if(buf == b) return i;
D = ;
while((g = Gcd(a, c)) != ){
if(b % g) return -; // g | b 不满足,则说明无解
b /= g;
c /= g;
D = D * a / g % c;
++ n;
}
init();
m = ceil(sqrt((long double) c));
for(i = , buf = ; i <= m; buf = buf * a % c, i ++) INSERT_HASH(i, buf);
for(i = , temp = POWER(a, m, c), buf = D; i <= m; i ++, buf = temp * buf % c){
Ext_Gcd(buf, c, x, y);
x = ((x * b) % c + c) % c;
for(ll tail = head[Hash(x)]; ~tail; tail = HASH_LINK[tail].next)
if(HASH_LINK[tail]. data == x) return HASH_LINK[tail].j + n + i * m;
}
return -;
}
BSGS模版 a^x=b ( mod c)的更多相关文章
- [SDOI2011]计算器(exgcd&BSGS)
k=1:裸的快速幂k=2:xy=z+kp,直接exgcd,这个可以不用解释了,不懂的同学可以看代码 k=3:裸的BSGS 重点是k=3(BSGS学习)ax=b(mod p)求解这个同余方程只能求gcd ...
- BSGS&EXBSGS 大手拉小手,大步小步走
大步小步走算法处理这样的问题: A^x = B (mod C) 求满足条件的最小的x(可能无解) 其中,A/B/C都可以是很大的数(long long以内) 先分类考虑一下: 当(A,C)==1 即A ...
- 扩展BSGS算法
求解A^x ≡ B mod P (P不一定是质数)的最小非负正整数解 先放几个同余定理: 一.判断如果B==1,那么x=0,算法结束 二.若gcd(A,P)不能整除 B,则 无解,算法结束 三.若gc ...
- 【洛谷4884】多少个1?(BSGS)
点此看题面 大致题意: 求满足\(个111...111(N\text{个}1)\equiv K(mod\ m)\)的最小\(N\). 题目来源 这题是洛谷某次极不良心的月赛的\(T1\),当时不会\( ...
- 扩展BSGS求解离散对数问题
扩展BSGS用于求解axΞb mod(n) 同余方程中gcd(a,n)≠1的情况 基本思路,将原方程转化为a与n互质的情况后再套用普通的BSGS求解即可 const int maxint=((1< ...
- BSGS && EXBSGS
基础BSGS 用处是什么呢w 大步小步发(Baby-Step-Giant-Step,简称BSGS),可以用来高效求解形如\(A^x≡B(mod C)\)(C为素数)的同余方程. 常用于求解离散对数问题 ...
- luoguP3306 [SDOI2013]随机数生成器
题意 将\(x_1,x_2,x_3...x_n\)写出来可以发现通项为\(a^{i-1}*x_1+b*\sum\limits_{j=0}^{i-2}a^j=a^{i-1}*x_1+b*\frac{1- ...
- 【HDU2815】【拓展BSGS】Mod Tree
Problem Description The picture indicates a tree, every node has 2 children. The depth of the nod ...
- 51Nod1123 X^A Mod B 数论 中国剩余定理 原根 BSGS
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1123.html 题目传送门 - 51Nod1123 题意 $T$ 组数据. 给定 $A,B,C$,求 ...
随机推荐
- Java程序编译和运行的过程【转】
转自:http://www.360doc.com/content/14/0218/23/9440338_353675002.shtml Java整个编译以及运行的过程相当繁琐,本文通过一个简单的程序来 ...
- <mvc:annotation-driven />与<context:annotation-config />
Spring家族的配置中这两个配置的意义,说具体点其实根据标签的shecma就能看出来,mvc,主要就是为了Spring MVC来用的,提供Controller请求转发,json自动转换等功能,而co ...
- 创建sh文件
创建sh文件 #/bin/bash v_file=$ v_type=$ v_desc=$ touch $v_file echo '#================================== ...
- Educational Codeforces Round 13 D:Iterated Linear Function(数论)
http://codeforces.com/contest/678/problem/D D. Iterated Linear Function Consider a linear function f ...
- html状态码与缓存学习
当浏览器访问一个页面时,浏览者的浏览器会向网页所在的服务器发送请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求. ...
- c# web 缓存管理
using System; using System.Collections; using System.Text.RegularExpressions; using System.Web; usin ...
- 使用 Filter 完成一个简单的权限模型
****对访问进行权限控制: 有权限则可以访问, 否则提示: 没有对应的权限, 请 返回其访问者的权限可以在manager那进行设置:
- Distinct<TSource>(IEqualityComparer<TSource> comparer) 根据列名来Distinct
1. DistinctEqualityComparer.cs public class DistinctEqualityComparer<T, V> : IEqualityComparer ...
- poj3372 Candy Distribution
可以证明: f(k) = k *(k - 1)/ 2 (1 ≤ k ≤ n)是n的完全剩余系当且仅当n = 2 ^ t. http://poj.org/problem?id=3372
- https协议操作
在伪静态中加入下列代码 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{SERVER_PORT} 80 RewriteRul ...