SGU[113] Nearly prime numbers

Description

描述

Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if given number is nearly prime and “No” otherwise.

近似质数是由两个质数P1、P2组成的正整数P1 * P2。给定一系列的正整数N,你需要写一个程序来判断它是否是近似质数,如果是,输入出“Yes”,否则输出“No”。

Input

输入

Input file consists of N+1 numbers. First is positive integer N (1£N£10). Next N numbers followed by N. Each number is not greater than 109. All numbers separated by whitespace(s).

输入文件包含N + 1个数,第一个是正整数N(1 <= N <= 10),接下来N个数,每个数不超过109。所有的数字以空格隔开。

Output

输出

Write a line in output file for each number of given sequence. Write “Yes” in it if given number is nearly prime and “No” in other case.

对于给定的每个数字,如果是近似质数,则输出“Yes”,否则输出“No”。

Sample Input

样例输入

1
6

Sample Output

样例输出

Yes

Analysis

分析

考虑到数据范围不是很大,109以内仅有五千多万个质数,可以通过打表来解决。打表自然是筛法求素数。

每次读入数据以后,只要比较到 sqrt(X) 就可以了,这样我们就可以在O(√n)的时间内求出结果。

这里有个小小的注意点,我们在循环的时候,最好使用i * i <= X来代替i <= sqrt(X) + 1,因为后者容易产生浮点数误差。

Solution

解决方案

#include <iostream>
#include <string>
#include <vector>
#include <memory.h>
#include <math.h> 

using namespace std;

const int MAX = 32000;

bool pPrimes[MAX];
vector<int> pVec;

int main()
{
	unsigned long long N, x, y;
	memset(pPrimes, true, sizeof(pPrimes));
	for(int i = 2; i < MAX; i++)
	{
		if(pPrimes[i])
		{
			pVec.push_back(i);
			for(int j = i + i; j < MAX; j += i)
			{ pPrimes[j] = false; }
		}
	}
	cin >> N;
	for(int i = 1; i <= N; i++)
	{
		cin >> x;
		bool bFlag = false;
		for(int j = 0; j < pVec.size(); j++)
		{
			if(x % pVec[j] == 0)
			{
				y = x / pVec[j];
				bool bTmp = true;
				for(int k = 2; k * k <= y; k++)
				{
					if(y % k == 0)
					{ bTmp = false; break; }
				}
				if(y <= 1) { bTmp = false; }
				if(y == 2) { bTmp = true; }
				if(bTmp) { bFlag = true; break; }
			}
		}
		if(bFlag) { cout << "Yes" << endl; }
		else { cout << "No" << endl; }
	}
	return 0;
}

这道题目可以用来练习使用筛法求素数,题目本身并不具有很强的思考性。

时间: 2024-07-28 21:42:49

SGU[113] Nearly prime numbers的相关文章

Sum of Consecutive Prime Numbers POJ - 2739

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The intege

POJ 2739 Sum of Consecutive Prime Numbers(水题)

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20560   Accepted: 11243 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12955    Accepted Submission(s): 4490 Problem Description Give you a lot of positive integers, just to find out how many pr

hdu5108Alexandra and Prime Numbers(素数的性质)

题目链接: huangjing 思路:每一个数都可以表示成若干个素数的乘积,那么可以对N从2一直枚举到sqrt(N),然后对每个数都能除到不能取余为止,那么后面的合数就不会除了,所以最后得到的数就是最大的质因子,然后直接N/最大的质因子,还有就是N=1的时候没有存在的数  . 题目: Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Accepted: 10619 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations d

[email&#160;protected] Sieve of Eratosthenes (素数筛选算法) &amp; Related Problem (Return two prime numbers )

Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13,

HDU2138 How many prime numbers【水题】

How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12742    Accepted Submission(s): 4400 Problem Description Give you a lot of positive integers, just to find out how many p

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

HDu 2138 How many prime numbers 高效Miller素数测试

题目就是给出一组数,让我们测试其中有多少个是素数. 求素数有测试sqrt(n)个数的方法,有筛子方法,不过对于本题这样的题目来说就都不是高效的. 本题使用Miller Rabin素数测试法,效率奇高,对于不是极其大的整数测试都几乎是常数时间.令人神往的算法啊. 网上有个程序,好像是什么吉林的模板程序,不过我一直没看懂他是什么思路写的,是个AC的程序,不过却是错误的,呵呵,因为程序一直把9当做素数. 于是上网查找了其中原理,自己写了个程序,效率和他的差不多一样,通过时间基本无差别,不过我的思路是按