zoj 2723 Semi-Prime(素筛打表+搜索优化)

题目链接:

  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2723

题目描述:

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.

Input

There are several test cases in the input. Each case contains a single integer 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 Output

No
Yes
Yes
No

 /*问题 判断输入的n(n的范围是2到100 0000)是否为半素数
解题思路 先用素筛法建立一张[2,50 0000]的素数表放在数组里,再将素数存进向量里,然后计算出2到100 0000的半素
数表放在set集合里后查表即可*/
#include <cstdio>
#include <set>
#include <cmath>
#include <vector>
using namespace std; int isPrim[]; void prim(int n); int main()
{
prim(); vector<int> prim_vec;//对于未知大小的数据来说非常方便
int i;
for(i=;i<=;i++){
if(isPrim[i])
prim_vec.push_back(i);
} set<int> prim_set;//查询起来非常快速、方便
int j,p;
for(i=;i<prim_vec.size();i++){
for(j=;j<prim_vec.size();j++){
if( (p = prim_vec[i]*prim_vec[j]) <=)
prim_set.insert(p);
else
break;//一种优化方法,对于有顺序的数据
}
} int n;
set<int>::iterator it;
while(scanf("%d",&n) != EOF)
{
it=prim_set.find(n);
if(it != prim_set.end())
printf("Yes\n");
else
printf("No\n");
}
return ;
} void prim(int n)
{
int i,j,k; for(i=;i<=n;i++)
isPrim[i]=;//全部初始化为1,假定全是素数
isPrim[]=isPrim[]=;//0和1非素数 k=(int)sqrt(n);
for(i=;i<=k;i++){
if(isPrim[i]){
for(j=*i;j<=n;j+=i)
isPrim[j]=;
}
}
/*
int count=0;
for(i=2;i<=n;i++){
if(isPrim[i])
count++;//记录50 0000以内的素数有41538个,验证素筛写的对不对
}
printf("%d\n",count);
*/
}
上一篇:利用openssl构建根证书-服务器证书-客户证书


下一篇:apk签名方法