This question is a naked query of the Euler's function, but the result is a tragedy, TLE.
# Include <stdio. h> int ecluid (int n, int A) {// calculate the maximum approximate number of N and a if (N % A = 0) return a; else {n % =; ecluid (A, n) ;}} int main () {int test_case, N, friend_num; scanf ("% d", & test_case); While (test_case --) {scanf ("% d", & N); friend_num = 0; For (INT I = 2; I <n ;++ I) {If (ecluid (n, I )! = 1) {++ friend_num;} printf ("% d \ n", N-friend_num-1);} return 0 ;}
Then I typed a method for listing prime numbers, and determined their common divisor one by one. The result was TLE.
# Include <stdio. h> # include <string. h> int prime [2000], prime_top = 0; void build_prime () {int not_prime [20000]; memset (not_prime, 0, sizeof (not_prime )); for (INT I = 2; I <= 17000; ++ I) {If (! Not_prime [I]) {Prime [prime_top ++] = I ;}for (Int J = 0; I * prime [J] <= 17000 & J <prime_top; ++ J) {not_prime [I * prime [J] = 1; if (I % prime [J] = 0) {break ;}}} int main () {int test_case, N, not_new_friend_num; build_prime (); // printf ("% d \ n", prime [prime_top-1], prime_top ); scanf ("% d", & test_case); While (test_case --) {scanf ("% d", & N); not_new_friend_num = 0; For (INT I = 2; I <n; ++ I) {for (Int J = 0; j <prime_top; ++ J) {If (N % prime [J] = 0 & I % prime [J] = 0) {++ not_new_friend_num; break ;}}} printf ("% d \ n", N-not_new_friend_num-1);} return 0 ;}
Then I can't do Baidu. I thought of the Phi (Mn) = PHI (m) * PHI (n) method when I saw someone talking about using the Euler's function, but I didn't expect it to be a naked Euler's function. The result is still TLE.
# Include <stdio. h> int Eu (int n) {int ans = 1; for (INT I = 2; I * I <= N; ++ I) {If (! (N % I) {ans * = I-1; N/= I; while (! (N % I) {ans * = I; N/= I ;}}if (n> 1) {ans * = n-1 ;}return ans ;} int main () {int test_case, N, friend_num; scanf ("% d", & test_case); While (test_case --) {scanf ("% d", & N ); friend_num = 0; For (INT I = 2; I <n; ++ I) {If (Eu (I) * Eu (n) = Eu (N * I )) {// printf ("% d \ n", I); ++ friend_num;} printf ("% d \ n", friend_num + 1);} return 0 ;}
Finally, I had to think about it and found that it could be solved by the naked Euler function.
1049 relativesCodeI changed it. AC instantly! Wow.
# Include <stdio. h> int ans; void Eu (int n) {for (INT I = 2; I * I <= N; ++ I) {If (! (N % I) {ans * = I-1; N/= I; while (! (N % I) {ans * = I; N/= I ;}}if (n> 1) {ans * = n-1 ;}return ;} int main () {int N, test_case; scanf ("% d", & test_case); While (test_case --) {scanf ("% d", & N ); ans = 1; EU (n); printf ("% d \ n", ANS);} return 0 ;}