HDU-4335-What is n? -Number Theory

Source: Internet
Author: User

Number theory. Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4335

Given bpm, several N conditions are required.

Proof see http://hi.baidu.com/aekdycoin/item/e493adc9a7c0870bad092fd9

The number of B, P, and m in the given question 0 <= n <= m must be n ^ (N !) Partition B (mod P), and the bad thing is that we didn't mention any special properties of the number here. Given is only limited to a positive integer and P> 0.

With the Euler's theorem, we know that N ^ (PHI (p) has 1 (mod P), but gcd (n, p) = 1 is required here. Obviously, the question does not have such data, if the given question is N or P, then we can know
N ^ (x) mod P has a circular section. This circular section is n ^ (PHI (p). If N and P are not of mutual quality, so we can prove that this cycle Section T | PHI (P), so we can still choose PHI (p) as the cycle section, so
Next we can divide the calculation into four parts.

Part_1: calculate n first! <PHI (p) has eight points at most, and the for loop is directly violent.

Part_2: if there are still intervals left, calculate PHI (p) <= n! <S !, S = min {x | X! MoD PHI (p) = 0} // n at this time! The power already has a circular section, but n is still changing.

Part_3: The next interval is n! % PHI (p) = 0, which indicates that it is completely dependent on the Phi (p) value, so as long as the cyclic nature of N is taken into account, simply count the number of occurrences in a loop section of N.

Part_4: Count the remaining n.

# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # define maxn 100000 using namespace STD; typedef unsigned long int int64; int P [maxn + 5], PM [10000], Phi [maxn + 5], idx =-1, Mod, B; int FAC [maxn + 5]; int64 m, ans; void getprime () {for (INT I = 2; I <= maxn; ++ I) {If (! P [I]) {PM [++ idx] = I;} For (Int J = 0; PM [J] * I <= maxn; ++ J) {P [PM [J] * I] = 1; if (I % PM [J] = 0) {break ;}}} /* printf ("% d \ n", idx); For (INT I = 0; I <= 100; ++ I) {printf ("% d ", PM [I]);} */} void Eular () {Phi [1] = 1; for (INT I = 2; I <= maxn; ++ I) {// printf ("Phi [% d] = % d \ n", I-1, Phi [I-1]); // getchar (); If (! P [I]) {Phi [I] = I-1; continue;} For (Int J = 0; PM [J] * PM [J] <= I; ++ J) {if (I % PM [J] = 0) {If (I/PM [J] % PM [J] = 0) {Phi [I] = PM [J] * Phi [I/PM [J];} else {Phi [I] = Phi [PM [J] * Phi [I/PM [J];} break ;}}} bool _ POW (int64, int64 B) {int64 ret = 1; while (B) {If (B & 1) {RET * = A; RET % = MOD;} A * =; A % = MOD; B >>=1;} return ret = B;} void deal () {int Lim =-1, PTR; For (PTR = 0; PTR <= m; ++ PTR) {// the first segment, less than a part of Phi [mod] If (! PTR) {FAC [PTR] = 1;} else {FAC [PTR] = FAC [ptr-1] * PTR;} If (FAC [PTR]> = Phi [mod]) {break;} If (_ POW (PTR, FAC [PTR]) {++ ans ;}} if (PTR> m) {return ;} // If M is exceeded, for (INT I = PTR; I <= m; ++ I) {If (! I) {FAC [I] = 1% Phi [mod];} else {FAC [I] = (FAC [I-1] * I) % Phi [mod];} if (FAC [I] = 0) {// If This vertex is found Lim = I; break;} If (_ POW (I, FAC [I] + Phi [mod]) {++ ans ;}}if (lim =-1) {return ;} // if no vertex int T = 0 that is multiples of Phi [mod] is found; If (m-lim + 1)> = mod) {// Lim this point is not calculated, so the length is M-LIM + 1, Here determine whether the remaining is greater than mod for (INT I = lim; I <Lim + MOD; ++ I) {If (_ POW (I % mod, Phi [mod]) {// because it is already a multiple of Phi [mod, therefore, the Phi [mod] index is directly substituted into ++ t;} ans + = (m-lim + 1)/(1llu * mod) * t ;} int left = (M-LIM + 1) % MOD; // residual remainder for (INT I = lim; I <Lim + left; ++ I) {If (_ POW (I % mod, Phi [mod]) {++ ans ;}} int main () {getprime (); Eular (); int t, CA = 0; scanf ("% d", & T); While (t --) {ans = 0; scanf ("% d % i64u", & B, & mod, & M); If (M = 18446731673709551615llu & mod = 1 & B = 0) {// because B = 0, P = 1, therefore, you can obtain m + 1 directly. Therefore, printf ("case # % d: 18446744073709551616 \ n", ++ CA);} else {deal (); printf ("case # % d: % i64u \ n", ++ ca, ANS) ;}} 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.