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>
#include<set>
#include<string>
#include<algorithm>
#define MAX 0x7fffffff

using namespace std;
int primer[16000];
int main()
{
	int i ,j;
	memset(primer,0,sizeof(primer));
	primer[1] = primer[2] = 1;
	int en = sqrt(16000.0);
	for(i=2; i <= en; i++)
		if(!primer[i] || i==2)
		{
			for(j=i+i; j<=16000; j+=i)
				primer[j] = 1;
		}
	int n,cnt = 0;
	while(cin >> n, n > 0)
	{
		cnt ++;
		printf("%d: ",cnt);
		if(!primer[n])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 11:30:42

hdu 2161 Primes 素数打表的相关文章

HDOJ(HDU) 2161 Primes(素数打表)

Problem Description Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes. Input Each i

HDU 2161 Primes(打表)

Primes Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9888    Accepted Submission(s): 4136 Problem Description Write a program to read in a list of integers and determine whether or not each n

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

hdu 2161 Primes 筛法求素数 大水题

Primes Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7659    Accepted Submission(s): 3130 Problem Description Write a program to read in a list of integers and determine whether or not each n

HDU 2161: Primes

Primes ///@author Sycamore, ZJNU ///@date 8/2/2017 #include<iostream> #include <cmath> #include<set> #include<algorithm> using namespace std; set<int>s = {3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,1

hdu 2521 反素数(打表)

反素数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5723    Accepted Submission(s): 3355 Problem Description 反素数就是满足对于任意i(0<i<x),都有g(i)<g(x),(g(x)是x的因子个数),则x为一个反素数.现在给你一个整数区间[a,b],请你求出该区间的x使

UVA1213Sum of Different Primes(素数打表 + DP)

题目链接 题意:选择k个素数,使得和为N(1120)的方案数: 筛选出 <= N 的素数,然后就背包 写的时候没初始dp[0][0] = 1;而且方案数也没相加,真是弱逼 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int Max = 1120; int prime[Max + 5], t

HDU - 2161 - Primes (质数)

Primes Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8959    Accepted Submission(s): 3754 Problem Description Write a program to read in a list of integers and determine whether or not each n

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