Discrete Logging (poj2417)

Source: Internet
Author: User
Tags ack gcd

Discrete Logging
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5120 Accepted: 2319

Description

Given a prime P, 2 <= p < 231, an integer B, 2 <= B < P, and an integer n, 1 <= n < P, compute the DISCR Ete logarithm of N, base B, modulo P. That's, find an integer L such that
    B
L
= = N (mod P)

Input

Read several lines of input, each containing p,b,n separated by a space.

Output

For each line print the logarithm to a separate line. If There is several, print the smallest; If there is none, print "no solution".

Sample Input

5 2 15 2 25 2 35 2 45 3 15 3 25 3 35 3 45 4 15 4 25 4 35 4 412345701 2 11111111111111121 65537 1111111111

Sample Output

013203120no Solutionno solution19584351462803587

Idea: Baby_step,giant_step algorithm template problem

1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <stdlib.h>5#include <queue>6#include <vector>7#include <math.h>8#include <string.h>9#include <map>Ten#include <Set> One using namespacestd; AtypedefLong LongLL; -LL mod[65539]; - BOOLjudge (LL N) the { -LL Ac=sqrt (1.0*n); -     if(ac*ac==N) -         return true; +     Else return false; - } +Map<ll,ll>my; A Set<LL>que; at Set<LL>:: iterator it; - ll Quick (ll n,ll m,ll p); - ll gcd (ll n,ll m) - { -     if(m==0) -         returnN; in     Else returnGCD (m,n%m); - } toPair<ll,ll>Pc (LL n,ll m) + { -     if(m==0) the         returnMake_pair (1,0); *     Else $     {Panax NotoginsengPAIR&LT;LL,LL&GT;N=PC (m,n%m); -         returnMake_pair (n.second,n.first-(n/m) *n.second); the     } + } Atypedefstructpp the { + LL x; - LL ID; $ } SS; $SS table[655390]; -SS tt[655390]; - BOOLCMP (pp p,pp q) the { -     if(p.x==q.x)Wuyi         returnp.id<q.id; the     returnp.x<q.x; - } Wu intMainvoid) - { About LL p,b,n; $      while(SCANF ("%lld%lld%lld", &p,&b,&n)! =EOF) -     { -         BOOLA=judge (P); -LL Ask=sqrt (1.0*P); A         if(!a) +ask+=1; the         inti,j; -mod[0]=1; $         intvv=0; thetable[0].id=0; thetable[0].x=1; the         intcn=1; the          for(i=1; i<=ask; i++) -         { inmod[i]=mod[i-1]*b%P; theTable[i].id=i; thetable[i].x=Mod[i]; About         } theSort (table,table+ask+1, CMP); thett[0].id=table[0].id; thett[0].x=table[0].x; +LL yy=tt[0].x; -          for(i=1; i<=ask;i++) the         {Bayi             if(table[i].x!=yy) the             { theyy=table[i].x; -tt[cn].x=yy; -Tt[cn].id=table[i].id; thecn++; the             } the         } the         intFl=0; - LL ack; theLL Nn=quick (b,p-2, P); thenn=Quick (nn,ask,p); theLL ni=1;94          for(i=0; i<=ask; i++) the         { theLL ap=ni*n%P; theni%=P;98             intL=0; About             intr=cn-1; -LL ic=-1;101              while(l<=R)102             {103                 intMid= (L+R)/2;104                 if(tt[mid].x>=AP) the                 {106Ic=mid;107r=mid-1;108ack=tt[ic].id;109                 } the                 ElseL=mid+1;111             } the             if(ic!=-1&&tt[ic].x==AP)113             { the                 //printf ("%lld\n", table[ic].x);p rintf ("%d\n", I); theFl=1; the                  Break;117             }118ni= (NI*NN)%P;119         } -         if(!FL)121printf"No solution\n");122         Elseprintf"%lld\n", ack+ (LL) i*ask);123     }124     return 0; the }126 ll Quick (ll n,ll m,ll p)127 { -n%=p;129LL ans=1; the      while(m)131     { the         if(m&1)133         {134ans=ans*n%p;135         }136n=n*n%p;137M/=2;138     }139     returnans; $}

Discrete Logging (poj2417)

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.