G - G ZOJ - 2723 (素数打表+set)

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 }

原文地址:https://www.cnblogs.com/ruruozhenhao/p/8613168.html

时间: 2024-11-08 16:51:36

G - G ZOJ - 2723 (素数打表+set)的相关文章

[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   Accepted: 3194 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of

linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o =&gt; o).Where(g =&gt; g.Count() == 1) .Select(g =&gt; g.ElementAt(0));

static void Main(string[] args) { int[] arr = { 1, 3, 3, 3, 3, 4, 5, 4, 5, 8, 9, 3 }; //不重复 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0)); var uniqueList = arr.Distinct<int>().ToList(); foreach(var

POJ 1365(质因数分级+素数打表)

Prime Land Time Limit: 1000ms                                Memory Limit: 10000KB Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,... denote the incr

标记素数法(模板)+素数打表

#include <stdio.h> #include <string.h> #define N 3000000 int f[3000000]; int main() { memset(f, 0, sizeof(f)); int i, j; f[0]=1; f[1]=1; for(i=2; i<=N; i++) { if(f[i]==0) { for(j=i*2; j<=N; j+=i) { f[j]=1; //不是素数 } } } // 打印所有发f[i]==0, 即

Fermat’s Chirstmas Theorem (素数打表的)

Fermat’s Chirstmas Theorem Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice SDUTOJ 2093 Description In a letter dated December 25, 1640; the great mathematician Pierre de Fermat wrote to Marin Mersenne

Ligh OJ 1370 Party All the Time (欧拉函数 +素数打表)

1370 - Bi-shoe and Phi-shoe PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for

CodeForces 385C Bear and Prime Numbers 素数打表

第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. The second query comes

var genreModel =storeDB.Genres.Include(&quot;Albums&quot;).Single(g =&gt; g.Name == genre);是什么意思?

g => g.Name == genre代表一个匿名函数.即这里向Single方法传入了一个方法类型的参数. =>左边的g代表方法的参数,可以有多个,如(g,f) => ...,=>右边代表方法的返回值,写全了是这样的:g => { return g.Name }.里之所以能点出Name属性,是因为Single方法声明了这个方法类型参数的格式,比如:Func<Student, bool>这个方法类型指示输入参数是Student类型,返回值是bool类型.对应于上面

HDU 1397 Goldbach&#39;s Conjecture(素数打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1397 Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not