Hdu4335 -- What is n? -- Number Theory

Source: Internet
Author: User

Question address: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4335

Question meaning:

Here are three numbers: B, P, M.

Ask if you meet 0 <= n <= m and n ^ n! Number of N in % P = B

Solution:

Main Research

A ^ X % C = a ^ (X % PHI (c) + PHI (c) % C where x> = PHI (c)

Phi (c) is the Euler's function.

Part 1 n! <PHI (c) Then you need to calculate n directly! Fortunately, Phi (c) is not very large.

Part 2 n! > = PHI (c) but n! % PHI (c )! = 0 this part also requires brute-force computation, but the modulo operation can be performed without a very large number.

Part 3 n! > = PHI (C) and n! % PHI (c) = 0 is converted to N ^ PHI (c) % C and then (N % C) ^ PHI (c) % C is a loop section with the length of C.

For details, see the code:

# Include <cstdio> # include <iostream> # include <cstring> # include <cmath> using namespace STD; # define ull unsigned long // calculate the Euler's function const int maxp = 100000 + 20; ull Phi [maxp]; void phi_table () {memset (PHI, 0, sizeof (PHI); Phi [1] = 1; for (INT I = 2; I <maxp; I ++) {If (! Phi [I]) {for (Int J = I; j <maxp; j ++ = I) {If (! Phi [J]) Phi [J] = J; Phi [J] = Phi [J]/I * (I-1); }}} ull euler_phi (ull P) {ull M = (ull) SQRT (p + 0.5); ull ans = P; For (ull I = 2; I <= m; I ++) {If (P % I = 0) {ans = ANS/I * (I-1); While (P % I = 0) P/= I ;}} if (p> 1) ans = ANS/P * (p-1); Return ans;} ull power (ull A, ull X, ull p) {ull ret = 1; A = A % P; while (x> 0) {If (X & 1) ret = RET * A % P; A = A * A % P; X = X/2;} return ret;} int main () {// phi_table (); int t; int CA = 1; scanf ("% D", & T); While (t --) {ull B, P, M; CIN> B> P> m; // If (B = 0 & P = 1 & M = 18446731673709551615ull) {printf ("case # % d: 18446744073709551616 \ n", CA ++); continue;} ull _ Phi = euler_phi (p); ull ans = 0; if (B = 0) ans ++; ull sum = 1; // first look at n! % Phi! = 0, when n! <Phi, MOD is not 0 for (ull I = 1; I <_ Phi & I <= m; I ++) {sum = sum * I % _ Phi + _ Phi; If (Power (I, sum, P) = B) ans ++ ;} // and then from Phi to m, but we noticed n next! % Phi = 0, then the equation is I ^ Phi % P = B // then (I + KP) ^ Phi % P = B, from the beginning of I to m, we can see several P lengths for (ull I = _ Phi; I <_ Phi + P & I <= m; I ++) {If (Power (I, _ Phi, P) = B) {ans + = (M-I)/p + 1 ;}} cout <"case #" <ca ++ <":" <ans <Endl;} return 0 ;}

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.