北邮acm2017热身赛 Saber's Conjecture 素数筛

题目

In a parallel universe, young Saber-chan does not have a humongous appetite, instead, she likes studying math in her spare time. This afternoon when Saber-chan is plunged in her study, she came across an interesting 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 is actually known as the Goldbach’s conjecture. It is has not been proved nor refused yet. However in this universe, there are more programmers than mathematicians, people tend to prove math findings by programs. As the King of Knights and King of Britain, Saber-chan orders you to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that Saber-chan is interested in the number of essentially different pairs and therefore you should not count (p1,p2) and (p2,p1) separately as two different pairs.

input

An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 215. The end of the input is indicated by a number 0.

output

Each output line should contain an integer number. No other characters should appear in the output.

sample

input:

6

10

12

0

output:

1

2

1

分析

给定一个数$p$,$p=q_1+q_2$其中$q_1$和$q_2$都是素数,输出这样的$(q_1,q_2)$对数

思路

step1:
利用素数筛,先枚举出一定范围内的素数

step2:
给定一个数p,遍历(2,p/2)中的素数t,如果p-t也是素数,则结果+1。

代码

#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
//bool f[1024*32];
int main()
{
//素数表
bool sushu[1024 * 32 + 1];
int n = 1024 * 32;
for (int i = 0; i <= n; i++)
sushu[i] = true;
sushu[1] = false;
for (int i = 2; i <= n; i++)
{
if (sushu[i] == true)
{
for (int j = 2; j*i <= n; j++)
sushu[i*j] = false;
}
}
vector<int> result;
while(cin){
int aaa;
cin >> aaa;
if (aaa ==0)
{
break;
}
int sum = 0;
for (int i = 2; i <= aaa / 2; i++) {
if (sushu[i] == 1) {
if (sushu[aaa - i] == 1) {
sum++;
}
}
}
result.push_back(sum);
}
for (int i = 0; i < result.size(); i++) {
cout << result[i] << endl;
}
//system("pause");
return 0;
}

素数筛

关于一定范围内素数的枚举,有下面两种素数筛的方法

1. 普通筛选法—埃拉托斯特尼筛法

基本思想:素数的倍数一定不是素数

步骤:

用一个长度为N+1的数组保存信息(0表示素数,1表示非素数),初始值都置为0(都默认为素数)

  1. 从第一个素数2开始遍历,把2的倍数都标记为非素数(置为1)
  2. 然后向后遍历,找到2后面的下一个素数3,把3的倍数都标记为非素数(置为1)
  3. 继续向后遍历,找到素数k,对k进行同样的处理
  4. 直到最后,数组中依然为0的数即为素数。

代码

bool sushu[1024 * 32 + 1];
int n = 1024 * 32;
for (int i = 0; i <= n; i++)
sushu[i] = true;
sushu[1] = false;
for (int i = 2; i <= n; i++)
{
if (sushu[i] == true)
{
for (int j = 2; j*i <= n; j++)
sushu[i*j] = false;
}
}

时间复杂度:$O(nlogn)$

2. 线性筛法—欧拉筛法

基本思想:保证每个合数只会被它的最小质因数筛去,因此每个数只会被标记一次

代码

#include<cstdio>
#include<cstring>
#define MAXN 100005
#define MAXL 1299710
int prime[MAXN];//最小质因数
int check[MAXL];//是否为素数
int tot = 0;
memset(check, 0, sizeof(check));
for (int i = 2; i < MAXL; ++i)
{
if (!check[i])
{
prime[tot++] = i;
}
for (int j = 0; j < tot; ++j)
{
if (i * prime[j] > MAXL)
{
break;
}
check[i*prime[j]] = 1;
if (i % prime[j] == 0)
{
break;
}
}
}

时间复杂度:$O(n)$

参考

Grubbyskyer的博客——线性筛法求素数