Baby step gaint step

Source: Internet
Author: User

Given the same remainder, evaluate all the solutions including it, where there is always a prime number.

 

Analysis:The steps for resolving the same formula are as follows:

(1) An original root of the Modulus

(2) ExploitationBaby step giant stepFind one, so that, because it is a prime number, there is a unique solution.

(3) set, so that there is, where, then get.

(4) Find all, you can know that there is a total of solutions, we can find all, and then sort the order.

 

O (SQRT (N) time complexity

Bsgs is as follows (Forward star Version)

  
const maxn=200001;type node=record        data,next,id:longint;end;type LL=int64;var edge:array [0..maxn] of node;    head:array [0..maxn] of longint;    cnt:longint;    a,b,c:ll;procedure insert(data,id:longint);var i,k:longint;begin  k:=data mod maxn;  i:=head[k];  while i<>-1 do    begin      if edge[i].data=data then exit;      edge[cnt].data:=data;      edge[cnt].id:=id;      edge[cnt].next:=head[k];      head[k]:=cnt;      inc(cnt);      i:=edge[i].next;    end;end;function find(data:ll):longint;var i,k:longint;begin  k:=data mod maxn;  i:=head[k];  while i<>-1 do    begin      if edge[i].data=data then exit(edge[i].id);      i:=edge[i].next;    end;  exit(-1);end;procedure extend_gcd(a,b:ll;var x,y:ll);var t:ll;begin  if b=0 then    begin      x:=1;      y:=0;      exit;    end;  extend_gcd(b,a mod b,x,y);  t:=x;  x:=y;  y:=t-(a div b)*y;end;function gcd(x,y:ll):ll;begin  if x mod y=0 then exit(y)  else exit(gcd(y,x mod y));end;function modd(x,p:ll):ll;begin  if x>=p then exit(x mod p);  if x<0 then exit((x mod p+p) mod p);  exit(x);end;function quick_mod(a,n,p:ll):ll;var ans,t:ll;begin  ans:=1; t:=modd(a,p);  while n<>0 do    begin      if (n and 1)=1 then ans:=modd(ans*t,c);      n:=n>>1;      t:=modd(t*t,c);    end;  exit(ans);end;function bsgs(a,b,c:ll):ll;var x,y,k,t,d,len,m:ll; i:longint;begin   fillchar(head,sizeof(head),$ff);   cnt:=0;   b:=modd(b,c);   for i:=0 to 100 do      begin        if b=t then exit(i);        t:=modd(t*a,c);      end;   d:=1; len:=0;   while true do     begin       t:=gcd(a,c);       if t=1 then break;       if (b mod t)<>0 then exit(-1);       c:=c div t;       b:=b div t;       d:=modd(d*a div t,c);       inc(len);     end;   m:=trunc(sqrt(c));   t:=1;   for i:=0 to m do      begin        insert(t,i);        t:=modd(t*a,c);      end;    k:=quick_mod(a,m,c);    for i:=0 to m do      begin        extend_gcd(d,c,x,y);        t:=modd(b*x,c);        if (y=find(t)) and (y<>-1) then exit(i*m+y+len);        d:=modd(d*k,c);      end;    exit(-1);end;begin  readln(a,b,c);  writeln(bsgs(a,b,c));end.
View code

 

Baby step gaint step

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.