Bzoj 3239 Discrete Logging baby-step-giant-step

Source: Internet
Author: User

Title: Given P,b,n, find the smallest L make b^l≡n (mod p) (p is prime)

Naked bsgs ... Practice practiced hand--

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define M 100100#define INF 0x3f3f3f3fusing namespace std;typedef pair<long long,long long> Abcd;long Long a,b,c;namespace hash_set{struct list{list *next;int key,val; List (list *_,int __): Next (_), key (__), Val (INF) {}}*head[m];int tim[m],t;inline void Clear () {++t;} int& Hash (int key) {int x=key%m;if (tim[x]!=t) tim[x]=t,head[x]=0x0; List *temp;for (Temp=head[x];temp;temp=temp->next) if (temp->key==key) return temp->val;head[x]=new list ( Head[x],key); return head[x]->val;}} ABCD EXGCD (Long long X,long long y) {if (!y) return ABCD (1,0); ABCD TEMP=EXGCD (y,x%y); return ABCD (temp.second,temp.first-x /y*temp.second);} int Exbsgs () {using namespace Hash_set;int I;long long a_m,d,m= (int) ceil (sqrt (C) +1e-7); Clear (); for (i=0,a_m=1;i<m;i++, (a_m*=a)%=c) {int &temp=hash (a_m); Temp=min (temp,i);} For (i=0,d=1;i<=m;i++, (d*=a_m)%=c) {ABCD temp=exgcd (d,c); Long Long x= (temp.first*b%C+c)%c;if (Hash (x)!=inf) return I*m+hash (x);} return-1;} int main () {while (cin>>c>>a>>b) {int ans=exbsgs (), if (!~ans) puts ("no solution"), Else printf ("%d\n", ans);} return 0;}



Bzoj 3239 Discrete Logging baby-step-giant-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.