新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有大于1的公约数,否则都是新朋友,现在会长想知道究竟有几个新朋友?请你编程序帮会长计算出来。
Input第一行是测试数据的组数CN(Case number,1<CN<10000),接着有CN行正整数N(1<n<32768),表示会员人数。Output对于每一个N,输出一行新朋友的人数,这样共有CN行输出。
Sample Input
2
25608
24027
Sample Output
7680 16016
解题思路:实际上就是查找与N互质的数的个数:还用到了欧拉函数;
1 #include <iostream>
2 #include <string.h>
3 #include <set>
4 #include <math.h>
5 using namespace std;
6
7 const int MAX = 10000 + 50;
8 int T[MAX];
9
10 int pangd(int n)
11 {
12 int i;
13 int k = sqrt(n);
14 for( i = 2;i <= k;i++)
15 {
16 if(n%i==0 )
17 break;
18 }
19 if(i <= k)
20 return 0;
21 else
22 return 1;
23
24 }
25
26 void DP(int n)
27 {
28 if(n==1||pangd(n))
29 {
30 cout<<n-1<<endl;
31 return;
32 }
33
34 int ti =0 ;
35 for(int i = 2;i < n;i++)
36 {
37 if(n%i==0&&pangd(i))
38 T[ti++] = i;
39 }
40 int s=(n/T[0])*(T[0]-1);
41 for(int j=1;j<ti;j++)//套公式
42 {
43 s=(s/T[j])*(T[j]-1);
44 }
45 cout<<s<<endl;
46
47 }
48
49 int main()
50 {
51 int N;
52 cin>>N;
53 while(N--)
54 {
55 int n;
56 cin>>n;
57 DP(n);
58 }
59
60 return 0;
61 }