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
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;
}