Sphenic numbers

Schoolboy Vasya is interested in the problem of distinguishing
has decided to develop his own testing method for it. 
Unfortunately, the new algorithm has one deficiency – it yield
in case with so-called sphenic numbers. For those who does n
number is the product of exactly three distinct prime numbers
Help Vasya write a program to distinguish sphenic numbers.
Inut
The input file contains a single number – integer n.
Limitations
30 ≤ n ≤ 10467397.
Output
The output file must contain one single line with the value “Y
marks) if number n is sphenic or “NO” if it is not.
Examples
Input.txt Output.txt
30 YES
40 NO

10467397 YES

题意:一个数让你判断一下是不是一种数,这种数是可以有三个素数相乘得到

思路:

遍历统计能被该数整除且只能整除一次的素数

ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int prim[1000000];
int t;
bool vis[10467400];
void getprim()
{
    memset(vis,true,sizeof(vis));
    t=0;
    for(int i=2;i<=10467397;i++)
    {
        if(vis[i])
          prim[t++]=i;
          for(int j=0;j<t&&prim[j]*i<=10467397;j++)
          {
              vis[i*prim[j]]=false;
              if(i%prim[j]==0)
              break;
          }
    }
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int x;
    getprim();
    while(scanf("%d",&x)!=-1)
    {
        int sum=0;
       for(int i=0;i<t;i++)
       {
         if(prim[i]<=x)
         {
             if(x%prim[i]==0&&(x/prim[i])%prim[i]!=0)
             {
                 sum++;
                 x=x/prim[i];
             }
         }
         else
         break;
       }
       if(sum==3)
       printf("YES\n");
       else
       printf("NO\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值