素数筛选 HDU 4715

打表,把所有的素数找出来,并且还要把那些数是素数标记下

Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2281    Accepted Submission(s): 642

Problem Description

All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two primes.
To validate this conjecture, you are asked to write a program.

Input

The first line of input is a number nidentified the count of test cases(n<10^5). There is a even number xat the next nlines. The absolute value of xis not greater than 10^6.

Output

For each number xtested, outputstwo primes aand bat one line separatedwith one space where a-b=x. If more than one group can meet it, output the minimum group. If no primes can satisfy it, output ‘FAIL‘.

Sample Input

3
6
10
20

Sample Output

11 5
13 3
23 3

Source

2013 ACM/ICPC Asia Regional Online —— Warmup

#include <iostream>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include<string>
#include <queue>
#include<map>
#define ll long long
#define N 1000010
#define eps 1e-6
#define pi acos(-1.0)
using namespace std;
const int INF = (1 << 30);

bool isprime[N];
int prime[N], primenum;//有primenum个素数 math.h
void PRIME(){
	primenum = 0;
	memset(isprime, false, sizeof(isprime));
	isprime[2] = true;
	prime[primenum++] = 2;
	for (int i = 3; i < N; i += 2)
	for (int j = 0; j<primenum; j++)
	if (i%prime[j] == 0)break;
	else if (prime[j]>sqrt((double)i) || j == primenum - 1)
	{
		prime[primenum++] = i;
		isprime[i] = true;
		break;
	}
}
int main()
{
	PRIME();
	int t, n;
	cin >> t;
	while (t--)
	{
		cin >> n;
		int flag = 0;
		for (int i = 0; i < primenum; i++)
		{
			if (prime[i] > n && isprime[prime[i] - n])
			{
				flag = 1;
				printf("%d %d\n", prime[i], prime[i] - n);
				break;
			}
		}
		if (!flag)
			puts("FAIL");
	}
	return 0;
}
时间: 2024-10-13 01:42:50

素数筛选 HDU 4715的相关文章

hdu 5407 CRB and Candies(素数筛选法,除法取模(乘法逆元))

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5407 解题思路: 官方题解: The problem is just to calculate g(N) =\ LCM(C(N,0), C(N,1), ..., C(N, N))g(N) = LCM(C(N,0),C(N,1),...,C(N,N)). Introducing function f(n) =\ LCM(1, 2, ..., n)f(n) = LCM(1,2,...,n), the

HDU 1164 Eddy&#39;s research I【素数筛选法】

思路:将输入的这个数分成n个素数的相乘的结果,用一个数组存储起来.之后再输出就可以了 Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6633    Accepted Submission(s): 3971 Problem Description Eddy's interest is very ext

hdu 4715 素数打表

先利用筛法完成素数打表 再从小到大判断即可 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int Max = 1e6 + 50; int n; int isPrime[Max]; int tblPrime[Max];

HDU 2161 Primes (素数筛选法)

题意:输入一个数判断是不是素数,并规定2不是素数. 析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了... 不说了,直接上代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; typedef long long LL; const int maxn = 16000 + 10; int p

O(N)的素数筛选法和欧拉函数

首先,在谈到素数筛选法时,先涉及几个小知识点. 1.一个数是否为质数的判定. 质数,只有1和其本身才是其约数,所以我们判定一个数是否为质数,只需要判定2~(N - 1)中是否存在其约数即可,此种方法的时间复杂度为O(N),随着N的增加,效率依然很慢.这里有个O()的方法:对于一个合数,其必用一个约数(除1外)小于等于其平方根(可用反证法证明),所以我们只需要判断2-之间的数即可. bool is_prime(int num) { const int border = sqrt(num); for

[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,

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

Light OJ 1197 1197 - Help Hanzo(大区间素数筛选)

Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he i

素数筛选(模板)

#include <stdio.h> int main() { int i,j,a[505]={0}; for(i=1;i<=500;i++) a[i]=1; for(i=2;i<=500;i++) if(a[i]) for(j=i+i;j<=500;j+=i) a[j]=0; for(i=2;i<=500;i++) if(a[i]) printf("%d ",i); printf("\n"); return 0; } 素数筛选(