POJ - 2739 - Sum of Consecutive Prime Numbers = 水题

时间:2019-10-23
本文章向大家介绍POJ - 2739 - Sum of Consecutive Prime Numbers = 水题,主要包括POJ - 2739 - Sum of Consecutive Prime Numbers = 水题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

http://poj.org/problem?id=2739

题意:给1~10000的数字,求他有多少种连续质数分解,连续质数分解就是指连续的某些质数的和,比如2+3+5+7,3+5+7,这样的。

预处理直接暴力。

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

const int MAXN = 1e4;
int p[MAXN + 5], ptop;
bool np[MAXN + 5];

void sieve(int n) {
    np[1] = 1;
    for(int i = 2; i <= n; ++i) {
        if(np[i])
            continue;
        else {
            p[++ptop] = i;
            for(int j = i + i; j <= n; j += i)
                np[j] = 1;
        }
    }
}

int ans[MAXN + 5];

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    sieve(MAXN);
    for(int i = 1; i <= ptop; ++i) {
        int sum = 0;
        for(int j = i; j <= ptop; ++j) {
            sum += p[j];
            if(sum > MAXN)
                break;
            ans[sum]++;
        }
    }
    int n;
    while(~scanf("%d", &n)) {
        if(n == 0)
            break;
        printf("%d\n", ans[n]);
    }
}

原文地址:https://www.cnblogs.com/Inko/p/11728527.html