博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[学习笔记]BSGS
阅读量:4549 次
发布时间:2019-06-08

本文共 1913 字,大约阅读时间需要 6 分钟。

\(\%\%\% Fading\) 早就会了,我最近才理解,当时颓废太多忘学了

1、[SDOI2013]随机数生成器

当天正好在学数列,回来发现用必修五的知识就没了……

不过特判好烦啊。

\(Code\ Below:\)

#include 
#define int long longusing namespace std;int p,a,b,x,t;int gcd(int a,int b){ return (b==0)?a:gcd(b,a%b);}int fast_pow(int a,int b,int p){ int ret=1; for(;b;b>>=1,a=1ll*a*a%p) if(b&1) ret=1ll*ret*a%p; return ret;}int bsgs(int a,int b,int p){ map
mp; mp.clear(); int t=sqrt(p)+1,val=b,i,j; for(i=0;i
=0&&i*t-j>=0) return i*t-j; val=val*a%p; } return -1;}signed main(){ int T; scanf("%lld",&T); while(T--){ scanf("%lld%lld%lld%lld%lld",&p,&a,&b,&x,&t); if(x==t) puts("1"); else if(a==1){ t=(t-x+p)%p; if(t%gcd(b,p)) puts("-1"); else { int inv=fast_pow(b,p-2,p); printf("%lld\n",(t*inv+1==p)?p:(t*inv+1)%p); } } else if(a==0){ if(b==t) puts("2"); else puts("-1"); } else { int val=b*fast_pow(a-1,p-2,p)%p; int ans=bsgs(a,(t+val)*fast_pow(x+val,p-2,p)%p,p); if(ans==-1) puts("-1"); else printf("%lld\n",ans+1); } } return 0;}

2、[CQOI2018]破解D-H协议

裸题。

\(Code\ Below:\)

#include 
#define int long longusing namespace std;int a,b,g,p;int fast_pow(int a,int b){ int ret=1; for(;b;b>>=1,a=a*a%p) if(b&1) ret=ret*a%p; return ret;}int bsgs(int a,int b){ map
mp; mp.clear(); int i,j,val=b,t=sqrt(p)+1; for(i=0;i
=0&&i*t-j>=0) return i*t-j; val=val*a%p; } return -1;}signed main(){ int T; scanf("%lld%lld%lld",&g,&p,&T); while(T--){ scanf("%lld%lld",&a,&b); printf("%lld\n",fast_pow(b,bsgs(g,a))); } return 0;}

转载于:https://www.cnblogs.com/owencodeisking/p/10227583.html

你可能感兴趣的文章
redhat6.5安装oracle 11g
查看>>
Using View and Data API with Meteor
查看>>
python cmd模块练习
查看>>
前端知识点
查看>>
Java的访问权限
查看>>
HTML5 1.5 表格元素
查看>>
SMT(SF)
查看>>
Android系列--DOM、SAX、Pull解析XML
查看>>
关于64位 MS SQL 导入导出 Oracle 引发 ORA-06413 的解决方法
查看>>
java.io.UnsupportedEncodingException
查看>>
浅析手机抓包方法实践(zt)
查看>>
记一次MySQl 安装1067错误
查看>>
DirectSound的应用
查看>>
MessageDigest简单介绍
查看>>
python基础学习笔记(十一)
查看>>
HTMl5的sessionStorage和localStorage(转)
查看>>
网络是怎样连接的-路由器的包转发操作(上)
查看>>
WPF - EventSetter
查看>>
Superblock mount time is in the future(转载)
查看>>
.Net开源框架列表
查看>>