zoukankan      html  css  js  c++  java
  • G

    Prime Number Definition 
    An integer greater than one is called a prime number if its only positive divisors (factors) are one and itself. For instance, 2, 11, 67, 89 are prime numbers but 8, 20, 27 are not.

    Semi-Prime Number Definition 
    An integer greater than one is called a semi-prime number if it can be decompounded to TWO prime numbers. For example, 6 is a semi-prime number but 12 is not.

    Your task is just to determinate whether a given number is a semi-prime number.

    素数定义

    如果一个大于1的整数只有一个正整数(因子)是一个整数,那么它就称为素数。 例如,2,11,67,89是素数,但8,20,27不是。

    半素数定义

    如果一个大于1的整数可以分解为两个素数,则称其为一个半素数。 例如,6是一个半素数,但12不是。

    你的任务只是确定一个给定的数字是否是一个半素数。

    Input

    There are several test cases in the input. Each case contains a single integer N (2 <= N <= 1,000,000)

    输入中有几个测试用例。 每个案例包含一个整数N(2 <= N <= 1,000,000)

    Output

    One line with a single integer for each case. If the number is a semi-prime number, then output "Yes", otherwise "No".

    一行每个案件都有一个整数。 如果数字是半素数,则输出“是”,否则输出“否”。

    Sample Input

    3
    4
    6
    12

    Sample OutputNo 
    Yes 
    Yes 
    No 

    题解: 菜鸟用最朴素的算法,结果超时——正解使用set来储存(不会数据结果的ε=ε=ε=┏(゜ロ゜;)┛逃)

    代码来源

    AC代码

     1 #include <iostream>
     2 #include <vector>
     3 #include <set>
     4 #include <cmath>
     5 using namespace std;
     6 //建立全局向量,用来保存素数
     7 vector<int> v;
     8 //在全局内存中定义全局集合容器,用来保存半素数
     9 //集合是平衡二叉检索树,搜索速度最快
    10 set<int> s;
    11 //建立[a, b]范围内素数表
    12 void pt(int a, int b){
    13     for(int i = a; i <= b; i++){
    14         //2是素数,清除2的倍数
    15         if(i != 2 && i % 2 == 0) continue;
    16         //消除素数的倍数 
    17         for(int j = 3; j * j <= i; j += 2){
    18             if(i % j == 0)
    19                 goto RL;
    20         }
    21         v.push_back(i);
    22         RL: continue;
    23     }
    24 }
    25 
    26 int main(){
    27     pt(2, 500000);
    28     int i, j, p;
    29     for(i = 0; i < v.size(); i++){
    30         for(j = 0; j < v.size(); j++){
    31             p = v[i] * v[j];
    32             if(p < 1000000)
    33                 s.insert(p);
    34             else
    35                 break;
    36         }
    37     }
    38     //读入数据,在半素数表中查找,看是否在该表
    39     int n;
    40     set<int>::iterator it;
    41     while(cin >> n){
    42         it = s.find(n);
    43         if(it != s.end())
    44             cout << "Yes" << endl;
    45         else
    46             cout << "No" << endl;
    47     }
    48     return 0;
    49 }
    View Code
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    最小生成树(Prim和Kruscal)
    SPFA(还是稍微写写吧,虽然没什么用)
    最短路径(随便写写)(Floyd,Bellman-Ford,Dijkstra)
    Just a Hook HDU
    数论逆元
    最长上升子序列(LIS)算法(附Codeforces Round #641 (Div. 2),B题题解)
    Educational Codeforces Round 86 (Rated for Div. 2)
    Codeforces Round #633 (Div. 2)
    Codeforces Round #631 (Div. 2)
    Mayor's posters(线段树离散化)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8613168.html
Copyright © 2011-2022 走看看