加载中...

POJ 2739 - Sum of Consecutive Prime Numbers


0x00 POJ 原题

Description

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 integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime

numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.

Your mission is to write a program that reports the number of representations for the given positive integer.

Input

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2
3
17
41
20
666
12
53
0

Sample Output

1
1
2
3
0
0
1
2

Source

Japan 2005

0x10 解题思路

本题用一般的素数求法就可以做出来了,虽然可以 AC,不过时间复杂度很大。

因此建议优化算法,优化的过程可以参考 POJ-2262

0x11 题意分析

一个整数 n 有时可以被写成若干个连续素数之和的序列。

给定一个整数 n,求出这些序列的个数(若 n 自身也是素数,则也算其中一种序列),其中 n∈[2,10000]

0x12 求解步骤

主要分两步走:

① 求出 10000 以内所有素数

② 在素数表中找出可以通过连续求和得到 n 的若干种组合

第①步只需要打表做一次即可,如何快速找到 10000 内的所有素数。

关于素数的求解方法,不外乎用到:

定义:只能被 1 或者自身整除的自然数(不包括 1),称为素数

定理:如果一个数 k 是合数,那么它的最小质因数肯定<=sqrt(k)

由于一个自然数若不是合数则必是素数,这个定理可以反过来用于素数:

如果一个数 k 是素数, 那么 k 必不能被<=sqrt(k)的所有整数整除

算法:埃拉托斯特尼筛法,也简称筛法,是一种空间换时间算法.

筛法主要用于求出某一个范围内的所有素数,而不用于判断某个数是否为素数.

其主要思想是利用了合数定理, 剔除范围内所有合数,剩下的必是素数.

例如要求 (1, n] 以内的所有素数:

那么把 2 的所有倍数删掉(不包括 2);

在剩下的数中第一个是 3,把 3 的所有倍数删掉(不包括 3);

在剩下的书中第一个是 7,把 7 的所有倍数删掉(不包括 7)……

一直重复直到遍历完 (1, sqrt(n)] 范围内的所有数,那么剩下的就是这个范围内的素数

常规情况下,

使用定义+定理求解素数,时间复杂度约为 O(n*sqrt(n)),超过千万级的话短时间内跑不动

使用筛法求解素数,时间复杂度可达到 O(n),但空间复杂度也达到了 O(n)

0x13 连续素数求和与剪枝

其实这题与 POJ2262 是类似的,都是围绕素数展开的操作.

因此第①步纯粹是走走形式求出素数集,这题的难点在于第②步.

理论上,根据哥德巴赫猜想,任意一个整数 n 只要足够大,都可以写成若干个素数之和的形式.

但是这种组合数比较多, 因此这题对组合范围做出了限制,要求是【连续素数】,且范围也压缩到【10000】以内

因此第②步其实可以暴力测试可行解:

在素数表中从 2 开始,在[2,n]范围内一直往后相加,若和值为 n,则得到一个序列; 否则不用继续测试可行解;

在素数表中从 3 开始,在[3,n]范围内一直往后相加,若和值为 n,则得到一个序列; 否则不用继续测试可行解;

……

在素数表中从 k 开始,在[k,n]范围内一直往后相加,若和值为 n,则得到一个序列; 否则不用继续测试可行解.

最后若 n 自身也素数,则序列数+1.

当然,这个测试方式是可以剪枝的:范围[k,n]可缩减到 [k, >=n/2 的第一个素数]

这种剪枝原理是因为,若序列中出现 1 个以上>=n/2 的数,和值必定>n.

还有一种剪枝可基于奇数与偶数相加的结果的奇偶性质确定:

若 n 是偶数, 则其序列必定有 含 2 的奇数个素数 或 不含 2 的偶数个素数

若 n 是奇数, 则其序列必定有 含 2 的偶数个素数 或 不含 2 的奇数个素数

0x20 AC 源码

/*
    Author:     Exp
    Date:       2017-11-30
    Code:       POJ 2739
    Problem:    Sum of Consecutive Prime Numbers
    URL:        http://poj.org/problem?id=2739
*/



#include <memory.h>
#include <math.h>
#include <iostream>
using namespace std;

const static int LEN = 10001;                            // 自然数数组长度(求解素数范围)
const static int SQRT_NUM = ceil(sqrt((double) LEN));    // 根据合数定理得到的质因数范围

/* 
 * 使用筛法找出自然数范围内的所有素数
 * @param markPrimes 素数标记集
 * return 范围内的素数个数
 */
int findPrimes(bool* markPrimes);

/* 
 * 把素数标记集转换成素数表
 * @param markPrimes 素数标记集
 * @param primeCnt 素数个数
 * return 素数表
 */
int* toPrimes(bool* markPrimes, int primeCnt);

/* 
 * 求解可以构成n的连续素数序列的个数
 * @param n 自然数n
 * @param primes 素数集
 * @param primeCnt 素数个数
 * return 序列个数
 */
int solveSerialNum(int n, int* primes, int primeCnt);

int main(void) {
    bool markPrimes[LEN];                            // 素数标记集, 标记每个自然数是否为素数
    int primeCnt = findPrimes(markPrimes);            // 找出范围内所有素数
    int* primes = toPrimes(markPrimes, primeCnt);    // 素数表

    int n = 0;
    while(cin >> n && n > 0) {
        int serialCnt = solveSerialNum(n, primes, primeCnt);    // 求解可以构成n的连续素数序列的个数
        serialCnt += (markPrimes[n] ? 1 : 0);    // 若n自身是素数, 则序列数+1
        cout << serialCnt << endl;
    }

    delete[] primes;
    return 0;
}

int findPrimes(bool* markPrimes) {
    memset(markPrimes, true, sizeof(bool) * LEN);    // 注意memset是按字节覆写内存的
    markPrimes[0] = markPrimes[1] = false;
    int cnt = 2;    // 非素数个数

    for(int i = 2; i <= SQRT_NUM; i++) {
        if(markPrimes[i] == false) {
            continue;
        }

        // 筛掉最小素数的所有倍数
        int multiple = 2;    // i的倍率(因不包括自身, 从2倍开始)    
        while(true) {
            int mNum = i * multiple;    // i的倍数
            if(mNum >= LEN) {
                break;
            }

            if(markPrimes[mNum] == true) {    // 避免重复计数
                markPrimes[mNum] = false;
                cnt++;
            }
            multiple++;
        }
    }
    return LEN - cnt;
}

int* toPrimes(bool* markPrimes, int primeCnt) {
    int* primes = new int[primeCnt];
    for(int i = 0, j = 0; i < LEN && j < primeCnt; i++) {
        if(markPrimes[i] == true) {
            primes[j++] = i;
        }
    }
    return primes;
}

int solveSerialNum(int n, int* primes, int primeCnt) {
    const int HALF_N = n / 2;
    int cnt = 0;

    for(int i = 0; i < primeCnt && primes[i] < n; i++) {
        int sum = primes[i];

        for(int j = i + 1; j < primeCnt; j++) {
            sum += primes[j];

            if(sum == n) {
                cnt++;
                break;

            } else if(primes[j] >= HALF_N) {
                break;
            }
        }
    }
    return cnt;
}

相关资料


文章作者: EXP
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 EXP !
赏
  目录