首页 技术 正文
技术 2022年11月16日
0 收藏 534 点赞 2,425 浏览 985 个字

传送门

解题思路

  \(BSGS\)裸题??要求的是\(g^a =A (mod\) \(p)\),设\(m\)为\(\sqrt p\),那么可以设\(a=i*m-j\),式子变成

  $$ g^{i*m-j}=A\mod p$$

  然后把\(j\)移过去,

  $$g{i*m}=A*gj\mod p$$

  然后可以预处理枚举\(j\)的值用哈希存下来,每次直接\(O(m)\)询问,总的时间复杂度为\(O(T\sqrt p \log)\)

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#define int long longusing namespace std;
typedef long long LL;int g,MOD,T,A,B,a,b,m;
map<int,int> mp;inline int fast_pow(int x,int y){
int ret=1;
for(;y;y>>=1){
if(y&1) ret=1ll*ret*x%MOD;
x=1ll*x*x%MOD;
}
return ret;
}inline void prework(){
m=ceil(sqrt(MOD));
int base=fast_pow(g,m),now=1;
mp[now]=0;
for(int i=1;i<=m;i++){
now=1ll*now*base%MOD;
mp[now]=i;
}
}inline int BSGS(int x){
int now=x;
if(mp.count(now)) return mp[now]*m;
for(int i=1;i<=m;i++){
now=1ll*now*g%MOD;
if(mp.count(now)) return mp[now]*m-i;
}
}signed main(){
scanf("%lld%lld%lld",&g,&MOD,&T);
prework();
while(T--){
scanf("%lld%lld",&A,&B);
a=BSGS(A); b=BSGS(B);
a=(a%(MOD-1)+MOD-1)%(MOD-1);
b=(b%(MOD-1)+MOD-1)%(MOD-1);
printf("%lld\n",fast_pow(g,1ll*a*b%(MOD-1)));
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906