http://acm.hdu.edu.cn/showproblem.php?pid=1286
Analysis: With the president's number (N) has more than 1 of the convention number is old friends, let you novelty friends number. Then the new friend must be the number of the president's number (N) coprime.
Note: The general practice will time out. Euler's function is needed at this time.
Euler functions:
(Find the number of coprime between a number N and 1~n): found a mass factor of n I, then the number of conventions in 1~n and N is a multiple of the probability pi = 1/i, then the number of conventions with N is not a multiple of I (these numbers may be the number of n coprime) is 1-pi, that is (i-1)/I Then, when we find all the mass factors of n, the probability between 1~n and n coprime is the product of all (1-PI), then the number of n coprime is: eular (n) =n* (1-P1) * (1-P2) .... (1-PI);
Simply put, a positive integer n, the Euler function is less than or equal to the number of n coprime number in the number. For example Euler (8) = 4, because 1,3,5,7 are both and 8 coprime.
Euler function extension: the sum of all the mass factors of a number is Euler (n) *n/2.
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<queue>using namespacestd;#defineMAXN 32768#defineOO 0x3f3f3f3fintEulerintN) { intRes=n,a=N; for(intI=2; i*i<=a; i++) { if(a%i==0) {res=res/i* (I-1); while(a%i==0) a/=i; } } if(a>1) res=res/a* (A-1); returnRes;}intMain () {intT, N; scanf ("%d", &T); while(T--) {scanf ("%d", &N); printf ("%d\n", Euler (n)); } return 0;}
View Code
Find new friends Hdu 1286