[Note] bsgs & exbsgs

Source: Internet
Author: User
Tags mul
Bsgs

(I think this is something to write)
Bsgs is mainly used to solve the problem of the minimum positive integer solution of the equation, for example, \ (x ^ K = Y \ pmod p \) (note the interconnectivity between P and X ).
Set \ (M = \ lceil \ SQRT p \ rceil, K = Am-B, A \ In [1, m], B \ in [0, m )\)
Then the above equation can be transformed into \ (x ^ {Am} = Yx ^ B \ pmod p \)
Enumerate \ (B \), calculate the value on the right and save it to \ (MAP \). Just enumerate \ (A \) The table.
Q: Can I enumerate tables stored on the left and query on the right?
A: Yes, but the table on the left can be directly output...
By the way, the maximum value must be stored in the map, so that the answer you calculated is the smallest, so that the values can be updated.
Complexity: \ (O (\ SQRT plogp )\)
Template question [tjoi2007] Cute Prime Number

# Include <bits/stdc ++. h> using namespace STD; int P; Map <int, int> m; int KSM (int x, int y) {int S = 1; while (y) {If (Y & 1) S = 1ll * S * x % P; X = 1ll * x % P; y >>=1;} return s ;} int main () {int X, Y; CIN> P> x> Y; int M = SQRT (p) + 1; int S = y; for (INT I = 0; I <m; I ++) {M [s] = I; // update S = 1ll * S * x % P ;} int T = KSM (x, m); S = 1; for (INT I = 1; I <= m; I ++) {S = 1ll * S * T % P; If (M. count (s) {printf ("% d \ n", I * m-m [s]); Return 0 ;}} puts ("no solution "); return 0 ;}
Extended bsgs

When P is not a prime number (at this time, X and P are not necessarily mutually qualitative ),
Set d = gcd (x, P ),
If D is not divisible by Y, only y = 1, x = 0, and no solution is available in other cases.
If D is divisible by Y, if D is equal to 1, bsgs is used directly.
Otherwise \ [x ^ K = Y \ pmod p \]
\ [X ^ {k-1} × \ frac {x} {d} = \ frac {y} {d} \ pmod {\ frac {p} {d} \]
Continue until D = 1.
\ [X ^ {k-t} × \ frac {x ^ t} {\ prod d_ I} =\ frac {y} {\ prod d_ I} \ pmod {\ frac {P }{\ prod d_ I }}\]
First, check whether x = [0, T) is the solution. Obviously, t is log-level.
If neither [0, T) is a solution, the bsgs can solve the problem due to the mutual quality of \ (x, \ frac {p} {\ prod d_ I} \).
Remember to add T to the answer.
Template question [spoj3105] mod

#include<bits/stdc++.h>using namespace std;int re(){    int x=0,w=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();    return x*w;}int p;map<int,int>M;void mul(int&x,int y){x=1ll*x*y%p;}int ksm(int x,int y){    int s=1;    while(y){if(y&1)mul(s,x);mul(x,x);y>>=1;}    return s;}void exbsgs(int x,int y){    if(y==1){puts("0");return;}    int d=__gcd(x,p),k=1,t=0;    while(d^1){        if(y%d){puts("No Solution");return;}        ++t;y/=d;p/=d;mul(k,x/d);        if(y==k){printf("%d\n",t);return;}        d=__gcd(x,p);    }    int s=y;M.clear();int m=sqrt(p)+1;    for(int i=0;i<m;i++){        M[s]=i;mul(s,x);    }    s=k;k=ksm(x,m);    for(int i=1;i<=m;i++){        mul(s,k);        if(M[s]){printf("%d\n",i*m-M[s]+t);return;}    }    puts("No Solution");}int main(){    int x,y;    while(1){        x=re(),p=re(),y=re();        if(!x&&!p&&!y)break;        x%=p;y%=p;        exbsgs(x,y);    }    return 0;}

[Note] bsgs & exbsgs

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.