hduoj 1286 找新朋友

http://acm.hdu.edu.cn/showproblem.php?pid=1286php

找新朋友

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8694    Accepted Submission(s): 4592


html

Problem Description
新年快到了,「豬頭幫協會」準備搞一個聚會,已經知道現有會員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 的全部的與n互素的整數(包括1 不包括n)。測試

篩法求素數orzspa

 

AC代碼:code

 1 #include<stdio.h>
 2 #include<string.h>
 3 int judge[40000];
 4 int main()
 5 {
 6     int n, m;
 7     int ans;
 8     scanf("%d", &n);
 9     while(n--)
10         {
11         int cnt = 0;
12         scanf("%d", &m);
13         memset(judge, 0, sizeof(judge));
14         for(int i = 2; i <= m / 2; i++)
15         {
16             if(m%i == 0)
17             {
18                 judge[i] = 1;
19                 for(int j = i; j <= m; j+=i)
20                     judge[j] = 1;
21             }
22         }
23         for(int i = 2; i < m; i++){
24             if(judge[i] == 0){
25                 cnt++;
26             }
27         }
28         printf("%d\n", cnt + 1);
29     }
30     
31     return 0;
32 }    
相關文章
相關標籤/搜索