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 }
  • 相关阅读:
    模块(module)
    编译和解释性语言和python运行方式
    管道通信初级
    Page.TryUpdateModel 方法
    运用模型绑定和web窗体显示和检索数据(Retrieving and displaying data with model binding and web forms)
    HttpResponse 类
    ASP.NET中IsPostBack详解
    3.4.1 使用过滤式扩展方法(P43-44)
    使用扩展方法(Chapter3 P39-41)
    C#对象初始或器-Chapter3 P38
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11342504.html
Copyright © 2011-2022 走看看