zoukankan      html  css  js  c++  java
  • Sphenic numbers(素数筛)

    Schoolboy Vasya is interested in the problem of distinguishing prime numbers. He has decided to develop his own testing method for it. Unfortunately, the new algorithm has one deficiency – it yields false positive outputs in case with so-called sphenic numbers. For those who does not know: sphenic 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 “YES” (without quotation marks) if number n is sphenic or “NO” if it is not. Examples

    Examples

    Input.txt

    30

    40

    10467397 

    Output.txt

    YES

    NO

    YES

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 
     5 #define maxx 10467397/6
     6 
     7 int cur, prime[maxx+10];
     8 int vis[maxx+10];
     9 void find_prime()
    10 {
    11     int i, j;
    12     memset(vis, 0, sizeof(vis));
    13     cur = 0;
    14     for(i=2; i<maxx; i++)
    15     {
    16         if(vis[i]==0)
    17         {
    18             prime[cur++] = i;
    19             for(j=1; j*i<=maxx; j++)
    20             {
    21                 vis[j*i] = 1;
    22             }
    23         }
    24 
    25     }
    26 }
    27 
    28 int solve(int n)
    29 {
    30     int num = 0, i;
    31     for(i=0; i<cur&&n>1; i++)
    32     {
    33         if(n % prime[i]==0)
    34         {
    35             num++;
    36             n/=prime[i];
    37         }
    38         if(n % prime[i]==0) break;
    39     }  
    40     if(num==3&&n==1&&!(i<cur&&n>1)) return 1;
    41     else return 0;
    42 }
    43 
    44 int main()
    45 {
    46     freopen("input.txt", "r", stdin);
    47     freopen("output.txt", "w", stdout);
    48     int n;
    49     find_prime();
    50     scanf("%d", &n);
    51     if(solve(n)) printf("YES
    ");
    52     else printf("NO
    ");
    53     return 0;
    54 }
  • 相关阅读:
    ubuntu 安装 redis desktop manager
    ubuntu 升级内核
    Ubuntu 内核升级,导致无法正常启动
    spring mvc 上传文件,但是接收到文件后发现文件变大,且文件打不开(multipartfile)
    angular5 open modal
    POJ 1426 Find the Multiple(二维DP)
    POJ 3093 Margritas
    POJ 3260 The Fewest Coins
    POJ 1837 Balance(二维DP)
    POJ 1337 A Lazy Worker
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11342504.html
Copyright © 2011-2022 走看看