"BZOJ2242" "SDoi2011" calculator fast power +EXGCD+BSGS

Source: Internet
Author: User

Description you are asked to design a calculator to complete the following three tasks: 1, given y,z,p, calculates the value of Y^z Mod p, 2, given Y,z,p, calculates the smallest nonnegative integer satisfying the xy≡z (mod p), 3, given y,z,p, the calculation satisfies y^x≡z (mod p ) is the smallest non-negative integer. Input

The input contains multiple sets of data.

The first line contains two positive integers t,k each representing the number of data groups and the type of inquiry (for all data within a test point, the same type of inquiry). The following lines contain three positive integers per line y,z,p, describing a query. Output for each query, outputs a line of answers. For query Types 2 and 3, if no condition exists, the output is "Orz, I cannot find x!", noting that there is a space between the comma and "I". Sample Input"Sample Input 1"
3 1
2 1 3
2 2 3
2 3 3
"Sample Input 2"
3 2
2 1 3
2 2 3
2 3 3
"Data size and conventions"
For 100% of the data, 1<=y,z,p<=10^9, for prime numbers, 1<=t<=10. Sample Output"Sample Output 1"
2
1
2
"Sample Output 2"
2
1
0
HINT Source

First round Day1

Solution: The first question fast power second asked EXGCD third asked Bsgsbsgs (Baby Step Giant Step) on-line introduction is very detailed, but I make x=im-j so do not have to seek inverse spicy, corresponding in order not to appear negative is I from 1 to M enumeration, then J from 1 to M enumeration, because can be equal to m,j does not need to start from 0. Another: At the beginning of the CV test, with puts try, forgot to bring the line to the CV over, however, in Bzoj PE 233
1  2#include <iostream>3#include <cstdio>4#include <cmath>5#include <map>6 #definell Long Long7 using namespacestd;8 ll Y,z,p;9  Ten ll Fast_pow (ll y,ll z,ll p) One { All ans=1; -      while(z) -     { the         if(z&1) ans=ans*y%p; -y=y*y%p; -z>>=1; -     } +     returnans; - } +   A ll gcd (ll A,ll b) at{returnb==0? A:GCD (b,a%b);} -   - voidEX_GCD (ll a,ll b,ll &x,ll &y) - { -     if(!B) {x=1; y=0;return;} -EX_GCD (b,a%b,x,y); inll T=x; X=y; y=t-a/b*y; - } to   + voidsolve1 () - { theprintf"%lld\n", Fast_pow (y,z,p)); * } $  Panax Notoginseng voidsolve2 () - { thell d=gcd (y,p); +     if(z%d) {printf ("Orz, I cannot find x!\n");return;} AY/=d; Z/=D; the ll A, B; + EX_GCD (y,p,a,b); -a=a*z%p; $      while(a<0) a+=p; $printf"%lld\n", a); - } -   the voidsolve3 () - {Wuyiy%=p,z%=p; the     if(!y &&!z) {Puts ("1");return;} -     if(!y) {printf ("Orz, I cannot find x!\n");return;} WuMap<ll,ll>MP; - mp.clear (); Aboutll m=ceil (sqrt (p)); $ll T=fast_pow (y,m,p), k=z%p;//Direct M can -      for(intI=0; i<m;i++) -     { -         if(!mp[k])if(i) mp[k]=i;//pay attention to variables and revise them in time.  A                     Elsemp[k]=-1; +k=k*y%p; the     } -k=1; $      for(intI=0; i<m;i++) the     { the         if(Mp[k])//Watch out! MP and MP judgment the         { the             if(mp[k]==-1) mp[k]=0; -printf"%lld\n", i*m-mp[k]); in             return; the         } thek=k*t%p; About     } theprintf"Orz, I cannot find x!\n");//all kinds of silly the } the   + intMain () - { the     intt,t;Bayiscanf"%d%d",&t,&t); the      while(t--) the     { -scanf"%lld%lld%lld",&y,&z,&p); -         if(t==1) solve1 (); the         if(t==2) solve2 (); the         if(t==3) Solve3 (); the     } the}
View Code

"BZOJ2242" "SDoi2011" calculator fast power +EXGCD+BSGS

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.