05: Moon Landing program, 05 moon landing Program

Source: Internet
Author: User

05: Moon Landing program, 05 moon landing Program

Total time limit:
40000 ms
 
Time limit for a single test point:
4000 ms
 
Memory limit:
256000kB
Description

HJA is learning mathematics with his students, so he has a very simple mathematical problem: finding the minimum Natural Number x that satisfies ax Limit = B (mod p.

 

 

For 30% of data, 1 ≤ p ≤ 1000.

For 100% of data, 1 ≤ a, B <p ≤ 1e12.

Input
Three integers a, B, and p in the input data row. We ensure that p is a prime number.
Output
An integer in a row represents the smallest natural number x. If such an integer does not exist, the output is-1.
Sample Input
2 1 3
Sample output
0
Source
Zhonghaoxi
  • View
  •  
  • Submit
  •  
  • Statistics
  •  
  • Question

 

After doing this, we can find the greatness of pb_ds ....

Pb_ds 80 20000 + ms not required

AC 10000 + ms is used ..

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <map> 6 # define LL long 7 using namespace std; 8 LL a, B, c; 9 map <LL, LL> mp; 10 LL fastmul (LL x, LL p) 11 {12 LL now = x; 13 LL ans = 0; 14 while (p) 15 {16 if (p & 1) 17 {18 -- p; 19 ans = (ans + now) % c; 20} 21 p >>= 1; 22 now = (now + now) % c; 23} 24 return (ans) % c; 25} 26 LL fastpow (LL, LL p, LL c) 27 {28 LL base = a; LL ans = 1; 29 while (p! = 0) 30 {31 if (p % 2 = 1) ans = (fastmul (ans, base) % c; 32 base = (fastmul (base, base )) % c; 33 p = p> 1; 34} 35 return ans; 36} 37 int main () 38 {39 // a ^ x = B (mod c) 40/* LL x, y, mod; 41 cin> x> y> mod; 42 c = mod; 43 a = x; 44 B = y; */45 cin> a> B> c; 46 LL m = ceil (sqrt (c); // You must round up 47 mp. clear (); 48 if (a % c = 0) 49 {50 printf ("-1 \ n"); 51 return 0; 52} 53 // solution condition 54 LL ans of the ferma small theorem; // store the result of each enumeration B * a ^ j55 for (LL j = 0; j <= m; j ++) // a ^ (I * m) = B * a ^ j56 {57 if (j = 0) 58 {59 ans = B % c; 60 mp [ans] = j; // process a ^ 0 = 1 61 continue; 62} 63 ans = (fastmul (ans, a) % c; // a ^ j 64 mp [ans] = j; // store the result of each enumeration 65} 66 LL t = fastpow (a, m, c); 67 ans = 1; // a ^ (I * m) 68 LL flag = 0; 69 for (LL I = 1; I <= m; I ++) 70 {71 ans = (fastmul (ans, t) % c; 72 if (mp [ans]) 73 {74 LL out = fastmul (I, m)-mp [ans]; // x = I * m-j75 printf ("% lld \ n", (out % c + c) % c); 76 flag = 1; 77 break; 78} 79 80} 81 if (! Flag) 82 printf ("-1 \ n"); 83 return 0; 84}80 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <map> 6 # include <ext/pb_ds/ assoc_container.hpp> 7 # include <ext/pb_ds/hash_policy.hpp> 8 # define LL long 9 using namespace std; 10 using namespace _ gnu_pbds; 11 LL a, B, c; 12 gp_hash_table <LL, LL> mp; 13 LL fastmul (LL x, LL p) 14 {15 LL now = x; 16 LL ans = 0; 17 while (p) 18 {19 if (p & 1) 20 {21 -- p; 22 ans = (ans + now) % c; 23} 24 p >>=1; 25 now = (now + now) % c; 26} 27 return (ans) % c; 28} 29 LL fastpow (LL, LL p, LL c) 30 {31 LL base = a; LL ans = 1; 32 while (p! = 0) 33 {34 if (p % 2 = 1) ans = (fastmul (ans, base) % c; 35 base = (fastmul (base, base )) % c; 36 p = p> 1; 37} 38 return ans; 39} 40 int main () 41 {42 // a ^ x = B (mod c) 43/* LL x, y, mod; 44 cin> x> y> mod; 45 c = mod; 46 a = x; 47 B = y; */48 cin> a> B> c; 49 LL m = ceil (sqrt (c); // You must round up to 50 mp. clear (); 51 if (a % c = 0) 52 {53 printf ("-1 \ n"); 54 return 0; 55} 56 // solution condition 57 LL ans of the ferma small theorem; // store the result of each enumeration B * a ^ j58 for (LL j = 0; j <= m; j ++) // a ^ (I * m) = B * a ^ j59 {60 if (j = 0) 61 {62 ans = B % c; 63 mp [ans] = j; // process a ^ 0 = 1 64 continue; 65} 66 ans = (fastmul (ans, a) % c; // a ^ j 67 mp [ans] = j; // store the result of each enumeration 68} 69 LL t = fastpow (a, m, c); 70 ans = 1; // a ^ (I * m) 71 LL flag = 0; 72 for (LL I = 1; I <= m; I ++) 73 {74 ans = (fastmul (ans, t) % c; 75 if (mp [ans]) 76 {77 LL out = fastmul (I, m)-mp [ans]; // x = I * m-j78 printf ("% lld \ n", (out % c + c) % c); 79 flag = 1; 80 break; 81} 82 83} 84 if (! Flag) 85 printf ("-1 \ n"); 86 return 0; 87}AC used

 

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.