BSGS是一种解决一类专门的问题的解法,主要是解决已知A, B, C,求X使得A^x = B (mod p)这一类问题。

解法很简单,先设x = i*m-j(m=ceil(sqrt(p))),然后进行变形,得到ai*m = b*aj (mod p)。

先枚举j (范围0-m) ,将 b*aj  存入hash表。再枚举i (范围1-m) ,从hash表中寻找第一个满足ai*m = b*aj  (mod p)。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(register int i = a;i <= n;i++)
#define lv(i,a,n) for(register int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
map <ll,int> mp;
ll t,m,n,ans,now;
ll p,a,b;
bool flag;
ll qpow(ll a,ll b)
{
ll sum = ;
while(b)
{
if(b % == )
{
sum *= a;
}
a *= a;
sum %= p;
a %= p;
b >>= ;
}
return sum;
}
int main()
{
while(~scanf("%lld%lld%lld",&p,&a,&b))
{
if(a % p == )
{
printf("no solution\n");
continue;
}
mp.clear();
m = ceil(sqrt(p));
flag = false;
now = b % p;
mp[now] = ;
for(int i = ;i <= m;i++)
{
now = (now * a) % p;
mp[now] = i;
}
t = qpow(a,m);
now = ;
duke(i,,m)
{
now = (now * t) % p;
if(mp[now])
{
flag = true;
ans = i * m - mp[now];
printf("%lld\n",(ans % p + p) % p);
break;
}
}
if(!flag)
printf("no solution\n");
}
return ;
}

[模板] BSGS的更多相关文章

  1. [模板] BSGS/扩展BSGS

    简介 前置知识: 快速幂&&O(1)快速乘 [模板] 数学基础:快速幂/乘/逆元/exGCD/(ex)CRT/(ex)Lucas定理

  2. 模板BSGS(SDOI2011计算器) 模板EXBSGS

    BSGS和EXBSGS是OI中用于解决A^xΞB(mod C)的常用算法. 1.BSGS BSGS用于A,C互质的情况. 令m=sqrt(C),此时x可表示为i*m+j. 式中i和j都<=sqr ...

  3. 模板—BSGS

    #include<iostream> #include<cstdio> #include<cmath> #include<map> #define LL ...

  4. noip考前抱佛脚 数论小总结

    exCRT 求解韩信点兵问题,常见的就是合并不同\(mod\). 先mo一发高神的板子 for(R i=2;i<=n;++i){ ll Y1,Yi,lcm=Lcm(p[i],p[1]); exg ...

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

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

  6. BSGS 模板

    模板如下: 扩展版本: 求解a^k=b %p 求k,最小的k一定小于p,否则会重复,否则无解 *********************** gcd(a,p)=1时 设k=mi+v m=sqrt(p) ...

  7. U9249 【模板】BSGS

    题目描述 给定a,b,p,求最小的非负整数x 满足a^x≡b(mod p) 若无解 请输出“orz” 输入输出格式 输入格式: 三个整数,分别为a,b,p 输出格式: 满足条件的非负整数x 输入输出样 ...

  8. Bsgs模板

    模板最主要的是自己看得舒服,不会给自己留隐患,调起来比较简单,板子有得是,最主要的是改造出适合你的那一套.                  ——mzz #include<bits/stdc++ ...

  9. 算法笔记--BSGS && exBSGS 模板

    https://www.cnblogs.com/sdzwyq/p/9900650.html 模板: unordered_map<int, int> mp; LL q_pow(LL n, L ...

随机推荐

  1. Queueingconsumer 找不到

    springboot从1.5.9升级到2.0.0,queueingconsumer报错没有这个类,改为使用 DefaultConsumer

  2. 我的第一次"闭包"应用

    结论: 闭包可以当作强类型语言如C++.Java的全局变量使用,非常巧妙 需求: ssm项目,使用pagehelper分页,在写前一页.后一页.第一页.最后一页等页面跳转时,遇到了问题,如果查询全部的 ...

  3. cout 按进制数出

    头文件: #include<iostream> #include<iomanip> //setbase() #include<bitset> //bitset< ...

  4. Gym - 101670A Amusement Anticipation(CTU Open Contest 2017 签到题)

    题目&题意: 倒着找处于最后位置的等差数列的开头的位置. 例: 1 5 3 4 5 6 3 4 5 6是等差数列,它的开头的位置是3 PS: 读题真的很重要!!!!多组输入,上来就读错了!! ...

  5. Intellij idea 生成for循环代码块

    itar 生成array for代码块 for (int i = 0; i < array.length; i++) { = array[i]; } itco 生成Collection迭代 fo ...

  6. Linux 服务器 U盘安装(避免U盘启动)以及拔除U盘后无法引导系统

    一.U盘制作 首先下载两个文件: ·         rhel-server-6.3-i386-boot.iso    启动镜像 ·         rhel-server-6.3-i386-dvd. ...

  7. Python数据库连接池DBUtils(基于pymysql模块连接数据库)

    安装 pip3 install DBUtils DBUtils是Python的一个用于实现数据库连接池的模块. 此连接池有两种连接模式: # BDUtils数据库链接池: 模式一:基于threaing ...

  8. C++ <queue>用法

    C++队列可以不需要自己写,有现成的模版类 头文件: #include <queue> #include <iostream> using namespace std; (之前 ...

  9. eshing wind/tidal turbine using Turbogrid

    Table of Contents 1. meshing wind turbine using Turbogrid 1.1. ref 1 meshing wind turbine using Turb ...

  10. 【Codeforces 979B】Treasure Hunt

    [链接] 我是链接,点我呀:) [题意] 每次你可以将一个字符变成一个不同于本身的字符. 每个人需要改变n次(且不能不改变) 设每个人的字符串中出现次数最多的字符出现的次数为cnt[0~2] 问你谁的 ...