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 }
  • 相关阅读:
    java实现第六届蓝桥杯密文搜索
    java实现第六届蓝桥杯奇怪的数列
    jquery input 赋值和取值
    jQuery对html元素的取值与赋值实例详解
    Jmeter接口测试图文示例
    Jmeter接口测试案例实践(一)
    消息队列-推/拉模式学习 & ActiveMQ及JMS学习
    IDEA和Pycharm 等系列产品激活激活方法和激活码
    Mybatis中的association用法
    设置 Tomcat 的JVM运行内存
  • 原文地址:https://www.cnblogs.com/0xiaoyu/p/11342504.html
Copyright © 2011-2022 走看看