(Hdu step 2.1.6) find new friends (simple use of the Euler's function: calculate the number of elements that are mutually compatible with n), hdu2.1.6
Question:
Find new friends |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission (s): 2788 Accepted Submission (s): 1307 |
|
Problem Description the New Year is approaching. The pig Association is preparing to have a party. We already know that there are N members and the number of members ranges from 1 to N. The number of the President is N, if you are an old friend of the president, the member's number must have an appointment with N greater than 1. Otherwise, they will all be new friends. Now the president wants to know how many new friends there are? Compile a program to help the President calculate it. |
The first line of Input is the number of test data groups CN (Case number, 1 <CN <10000), followed by a positive integer N (1 <n <32768) in CN, indicating the number of members. |
Output outputs the number of new friends in a row for each N, so that a total of CN rows are Output. |
Sample Input22560824027 |
Sample Output768016016 |
AuthorSmallBeer (CRF) |
Source hangdian ACM training team training competition (VII) |
Recommendlcy |
Question Analysis:
If this question is purely violent (it is to use two for loops to find the data scale from 1 ~ The number of elements in I that interact with I), the timeout is very high (n itself is large, and the brute force algorithm> O (n ^ 2) is also used ). This question is actually "finding from 1 ~ The number of elements in n that interact with n. This problem has a special knowledge in number theory to solve-Euler's function.
The following describes the basic knowledge used in this question:
1) product functions: functions that meet f (AB) = f (a) * f (B) are product functions, where a and B are any two numbers of mutual quality. If a and B are any two integers, then f becomes a fully product function. For product functions, f (1) = 1.
If n is a prime number (prime number), f (n) = n-1;
If n is the sum, f (n) <n-1;
The Code is as follows:
/** F1.cpp ** Created on: January 31, 2015 * Author: Administrator */# include <iostream> # include <cstdio> # include <cstring> const int maxn = 32770; bool u [maxn]; int su [maxn]; int f [maxn];/*** Euler function: * used for solving: * obtain from 1 ~ Number of elements in n to n */void oula () {memset (u, true, sizeof (u); f [1] = 1; int I; int j; int num = 1; for (I = 2; I <maxn; ++ I) {if (u [I] = true) {su [num ++] = I; f [I] = I-1;} for (j = 1; j <num; ++ j) {if (I * su [j]> maxn) {break;} u [I * su [j] = false; if (I % su [j]) {f [I * su [j] = f [I] * (su [j]-1 );} else {f [I * su [j] = f [I] * su [j]; break ;}}} int main () {oula (); int t; scanf ("% d", & t); while (t --) {int n; scanf ("% d", & n); printf ("% d \ n ", f [n]);} return 0 ;}