BSGS(Baby Steps,Giant Steps)
Posted accpted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BSGS(Baby Steps,Giant Steps)相关的知识,希望对你有一定的参考价值。
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,m,y,z,p,x,ans,block; map<ll,int>mp; ll quick(ll a,ll b,ll p) ll res=1%p; while (b) if (b&1) res=res*a%p; a=a*a%p; b=b>>1; return res; void solve1() for (int i=1; i<=n; i++) scanf("%lld%lld%lld",&y,&z,&p); y=y%p; printf("%lld\\n",quick(y,z,p)); void solve2() for (int i=1; i<=n; i++) scanf("%lld%lld%lld",&y,&z,&p); y%=p; z%=p; if (y==0&&z!=0) printf("Orz, I cannot find x!\\n"); else printf("%lld\\n",z*quick(y,p-2,p)%p); void solve3() for (int i=1; i<=n; i++) scanf("%lld%lld%lld",&y,&z,&p); ans=-1; mp.clear(); y=y%p; if (y==0&&z==0) printf("1\\n"); continue; if (y==0) printf("Orz, I cannot find x!\\n"); continue; block=ceil(sqrt(p)); ll num=z; for (int j=0; j<=block; j++) mp[num]=j+1; num=num*y%p; ll sum=quick(y,block,p); num=sum; for (int j=1; j<=block; j++) if (mp[num%p]) if (mp[num%p]==-1) ans=0; else ans=j*block-mp[num]+1; break; num=num*sum%p; if (ans==-1) printf("Orz, I cannot find x!\\n"); else printf("%lld\\n",ans); int main() scanf("%lld%lld",&n,&m); if (m==1) solve1(); if (m==2) solve2(); if (m==3) solve3();
以上是关于BSGS(Baby Steps,Giant Steps)的主要内容,如果未能解决你的问题,请参考以下文章
BSGS离散对数(Baby-Step-Giant-Step)
数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)
BSGS(Baby-Step-Giant-Step)算法及其应用