BSGS (Baby-step-giant-step) algorithm and its application

Source: Internet
Author: User
Tags modulus

Let's start with a little recap, we're already going to find a linear equation (including its special case multiplication inverse)

We will also perform a fast algorithm for power modulus (modulo is the Fermat theorem for prime numbers with Euler's theorem for general conditions)

We also extend the expansion of Euler's theorem to solve the situation where the exponent is particularly large.

For the modular linear equations, the modulus coprime is used directly with Sun Tzu's theorem

When modulus is not coprime, the idea of combining equations is extended to expand the Chinese remainder theorem.

The next thing to learn is the Chenmo equation, but the transcendental equation.

How to surpass?

method is not flexible, directly copied over

Turn from 73162229

The typical example is POJ2417, which is to beg for this.

Requires a minimum integer solution

Not enough brains. Qaq Sleep Qaq

1#include <cmath>2#include <cstdio>3#include <algorithm>4 using namespacestd;5 structHashmap6 {7     Static Const intHa=999917, maxe=46340;8     intE,lnk[ha],son[maxe+5],nxt[maxe+5],w[maxe+5];9     intTop,stk[maxe+5];Ten     voidClear () {e=0; while(top) lnk[stk[top--]]=0;} One     voidADD (intXintY) {son[++e]=y;nxt[e]=lnk[x];w[e]=0x7fffffff; lnk[x]=E;} A     BOOLCountinty) -     { -         intx=y%Ha; the          for(intj=lnk[x];j;j=Nxt[j]) -             if(Y==son[j])return true; -         return false; -     } +     int&operator[](inty) -     { +         intx=y%Ha; A          for(intj=lnk[x];j;j=Nxt[j]) at             if(Y==son[j])returnW[j]; -ADD (x, y); stk[++top]=x;returnW[e];  -     } - }f; - intEXGCD (intAintBint&x,int&y) - { in     if(b==0) {x=1; y=0;returnA;} -     intR=EXGCD (b,a%b,x,y); to     intt=x;x=y;y=t-a/b*y; +     returnR; - } the intBSGS (intAintBintC) * { $     if(c==1)if(b==0)returna!=1;Else return-1;Panax Notoginseng     if(b==1)if(a!=0)return 0;Else return-1; -     if(a%c==0)if(b==0)return 1;Else return-1; the     intM=ceil (sqrt (C)), d=1, base=1; F.clear (); +      for(intI=0; i<=m-1; i++) A     { thef[base]=min (f[base],i); +Base= ((Long Long) base*a)%C; -     } $      for(intI=0; i<=m-1; i++) $     { -         intX,y,r=EXGCD (d,c,x,y); -X= ((Long Long) x*b%c+c)%C; the         if(F.count (x))returni*m+F[x]; -D= ((Long Long) d*base)%C;Wuyi     } the     return-1; - } Wu intMain () - { About     inta,b,c; $      while(SCANF ("%d%d%d", &c,&a,&b) = =3) -     { -         intans=bsgs (a,b,c); -         if(ans==-1) printf ("No solution\n"); A         Elseprintf"%d\n", ans); +     } the     return 0; -}

BSGS (Baby-step-giant-step) algorithm and its application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.