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];
int lenPrimes;

void initTblOfPrime()
{
	memset(isPrime, 1, sizeof(isPrime));
	isPrime[0] = isPrime[1] = 0;
	for(int i = 2; i < sqrt(Max*1.0); i ++)
	{
		if(isPrime[i]){
			for(int j = i*i; j < Max; j += i)
			{
				isPrime[j] = 0;
			}
		}
	}
	lenPrimes = 0;
	for(int i = 2; i < Max; i ++)
	{
		if(isPrime[i])	tblPrime[lenPrimes++] = i;
	}
}

int main()
{
	int x;
	initTblOfPrime();
	//for(int i = 0; i < 10; i ++) cout<<tblPrime[i]<<' ';

	scanf("%d", &n);
	while(n --)
	{
		scanf("%d", &x);
		if(x == 0)
		{
			printf("%d %d\n", 2, 2);
		}
		else
		{
			int tmp = 0, tag = 0;
			if(x > 0) tmp = 1;
			else{
				tmp = -1;
				x = -1 * x;
			}
			for(int i = 0; i < lenPrimes; i ++)
			{
				 if(x+tblPrime[i] < Max && isPrime[x+tblPrime[i]]){
				 	tag = 1;
				 	if(tmp == 1)
					 	printf("%d %d\n", x+tblPrime[i], tblPrime[i]);
					else printf("%d %d\n", tblPrime[i], x+tblPrime[i]);
				 }
				 if(tag) break;
			}
			if(!tag) printf("FALT\n");
		}
	} 

	return 0;
} 

hdu 4715 素数打表

时间: 2024-10-22 21:20:06

hdu 4715 素数打表的相关文章

HDU 3792 素数打表

Description If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 = 1 and dn=even for n>1. Twin Prime Conjecture states that "There are infinite consecutive primes differing by 2". Now given any positive inte

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

HDU 1397 Goldbach&#39;s Conjecture【素数打表】

题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<algorithm> 9 #define mod=

hdu 2161 Primes 素数打表

在kuangbin带你飞专题看到的,水了一发,但是wa了一次,T了一次,竟然连素数打表都快不会写了. 而且连求素数时候只需到根号n就可以都忘了,假设有因子m大于√n,那么n/m一定小于√n,所以它在√n前面已经被选出来了. 代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<vector>

[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

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

hdu 1012 素数判定

这道题~以前判定prime是一个个去试着整除再去存储,上次弄过欧拉函数那题目之后就知道了,这样会更快捷: 1 prime[0] = prime[1] = 1; 2 for(int i = 2; i <maxn; i++) 3 { 4 if(!prime[i]) 5 { 6 for(int j = i * 2; j < maxn; j += i) 7 prime[j] = 1; 8 } 9 } 以下是AC代码~~~水水题~ 1 #include<iostream> 2 #includ

hdu 2012 素数判定 Miller_Rabbin

素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 71785    Accepted Submission(s): 24969 Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数. Input 输入数据

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

#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, 即