BSGS and extended bsgs templates

Source: Internet
Author: User
Tags cmath

Bsgs

#include <iostream>//poj2417#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#definell Long Longusing namespacestd;Const intMaxint= ((1<< -)-1)*2+1;inta,b,c;structhashmap{Static Const intHa=999917, maxe=46340; intE,lnk[ha],son[maxe+5],nxt[maxe+5],w[maxe+5]; intTop,stk[maxe+5]; voidClear () {e=0; while(top) lnk[stk[top--]]=0;} voidADD (intXintY) {son[++e]=y;nxt[e]=lnk[x];w[e]=maxint;lnk[x]=E;} BOOLCountinty) {intx=y%Ha;  for(intj=lnk[x];j;j=Nxt[j])if(Y==son[j])return true; return false; }    int&operator[] (inty) {intx=y%Ha;  for(intj=lnk[x];j;j=Nxt[j])if(Y==son[j])returnW[j]; ADD (x, y); stk[++top]=x;returnW[e]; }}; Hashmap F;intEXGCD (intAintBint&x,int&y) {    if(!B) {x=1; y=0;returnA;} intR=EXGCD (B,a%b,x,y), t=x;x=y;y=t-(A/b) *y; returnR;}intBSGS (intAintBintC) {    if(c==1)if(! Breturna!=1;Else return-1; if(b==1)ifAreturn 0;Else return-1; if(a%c==0)if(! Breturn 1;Else return-1; intM=ceil (sqrt (C)), d=1, base=1; F.clear ();  for(intI=0; i<=m-1; i++)//Save the A^j in a hash table first{F[base]=min (f[base],i); Base= (ll) base*a)%C; }     for(intI=0; i<=m-1; i++)    {        intX,y,r=EXGCD (d,c,x,y); X= (ll) x*b%c+c)%c;//expanding Europe for A^j        if(F.count (x))returnI*M+F[X];//Find the solutionD= (LL) d*base)%C; }    return-1;}intMain () { while(~SCANF ("%d%d%d",&c,&a,&B)) {intans=bsgs (A,B,C); if(ans==-1) printf ("No solution\n"); Elseprintf"%d\n", ans); }    return 0;}

Extended BSGS

#include <iostream>//poj3243#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#definell Long Longusing namespacestd;Const intMaxint= ((1<< -)-1)*2+1;inta,b,c;structhashmap{Static Const intHa=999917, maxe=46340; intE,lnk[ha],son[maxe+5],nxt[maxe+5],w[maxe+5]; intTop,stk[maxe+5]; voidClear () {e=0; while(top) lnk[stk[top--]]=0;} voidADD (intXintY) {son[++e]=y;nxt[e]=lnk[x];w[e]=maxint;lnk[x]=E;} BOOLCountinty) {intx=y%Ha;  for(intj=lnk[x];j;j=Nxt[j])if(Y==son[j])return true; return false; }    int&operator[] (inty) {intx=y%Ha;  for(intj=lnk[x];j;j=Nxt[j])if(Y==son[j])returnW[j]; ADD (x, y); stk[++top]=x;returnW[e]; }}; Hashmap F;intgcdintAintBB) {    if(!B)returnA; Else returnGCD (b,a%b);}intEXGCD (intAintBint&x,int&y) {    if(!B) {x=1; y=0;returnA;} intR=EXGCD (b,a%b,x,y), t=x; X=y;y=t-(A/b) *y; returnR;}intEXBSGS (intAintBintC) {    if(c==1)if(! Breturna!=1;Else return-1; if(b==1)ifAreturn 0;Else return-1; if(a%c==0)if(! Breturn 1;Else return-1; intR,d=1, num=0;  while((R=GCD (a,c)) >1)    {        if(b%r)return-1; num++; B/=r; C/=r;d= (LL) d*a/r)%C; }     for(intI=0, now=1; i<num;i++,now= (ll) now*a)%C) {if(now==b)returni; }    intM=ceil (sqrt (C)), base=1; F.clear ();  for(intI=0; i<=m-1; i++) {F[base]=min (f[base],i); Base= (ll) base*a)%C; }     for(intI=0; i<=m-1; i++)    {        intX,y,r=EXGCD (d,c,x,y); X= (ll) x*b%c+c)%b; if(F.count (x))returni*m+f[x]+num; D= (ll) d*base)%C; }    return-1;}intMain () { while(~SCANF ("%d%d%d", &a,&c,&b))//a^y MoD c=b    {        if(! a&&! b&&! C Break; intans=exbsgs (A,B,C); if(ans==-1) printf ("No solution\n"); Elseprintf"%d\n", ans); }    return 0;}

BSGS and extended bsgs templates

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.