HDU 1286 筛选

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286

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

题目意思很明白,这里用筛选就可以搞定。当然还更快的做法。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=32769;
bool if_f[maxn];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int ans=0;
        fill(if_f,if_f+n,true);
        for(int i=2;i<n;i++)
        {
            if(n%i==0)
           {
            for(int j=i;j<n;j=j+i)
            {
                if_f[j]=false;
            }
           }
        }
        for(int i=1;i<n;i++)
        {
            if(if_f[i])
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}



posted @ 2015-08-20 22:35  Zeroinger  阅读(117)  评论(0编辑  收藏  举报
无觅关联推荐,快速提升流量