[UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3651

题意:问一个数n能拆成多少种连续的素数和。

筛出所有素数发现只有1200个,维护素数的前缀和,再打出所有区间的前缀和的表,统计出来,直接查询。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn = 10010;
 5 bool isprime[maxn];
 6 int prime[maxn];
 7 int sum[maxn];
 8 int pcnt;
 9 int n;
10 int mp[5800000];
11 void init() {
12   memset(isprime, true, sizeof(isprime));
13   memset(prime, 0, sizeof(prime));
14   pcnt = 0;
15 }
16 void getPrime() {
17     init();
18   prime[0] = prime[1] = 0;
19   for(int i = 2; i <= maxn; i++) {
20     if(isprime[i]) prime[++pcnt] = i;
21     for(int j = 1; j <= pcnt; j++) {
22           if(i * prime[j] > maxn) break;
23         isprime[i*prime[j]] = 0;
24         if(i % prime[j] == 0) break;
25     }
26   }
27 }
28
29 int main() {
30     // freopen("in", "r", stdin);
31     // freopen("out", "w", stdout);
32     getPrime();
33     memset(sum, 0, sizeof(sum));
34     sum[0] = prime[0];
35     for(int i = 1; i < pcnt; i++) sum[i] = sum[i-1] + prime[i];
36     memset(mp, 0, sizeof(mp));
37     for(int i = 0; i < pcnt; i++) {
38         for(int j = i; j < pcnt; j++) {
39             mp[sum[j]-sum[i]]++;
40         }
41     }
42     while(~scanf("%d", &n) && n) {
43         printf("%d\n", mp[n]);
44     }
45     return 0;
46 }
时间: 2024-12-17 19:35:10

[UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)的相关文章

POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20020   Accepted: 10966 Description Some positive integers can be represented by a sum of one or more consecutive

POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895   Accepted: 10906 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj2739 Sum of Consecutive Prime Numbers (素数打表)

D - Sum of Consecutive Prime Numbers Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2739 Appoint description: System Crawler (2016-05-05) Description Some

Sum of Consecutive Prime Numbers(素数打表+尺取)

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

POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)

[题意简述]:题意很简单,就是用连续的素数加和,计算有多少个这样的连续的素数数列可以使这个序列的和等于输入的数. [分析]:很经典的素数模板,基本所有有关素数的题,我都会使用这个模板. // 268K 16Ms #include<iostream> using namespace std; #define N 10000 bool isprime[N]; long long prime[1300],nprime; // 注意long long void doprime() { long lon

UVA 1210 Sum of Consecutive Prime Numbers(数论)

UVA - 1210 Sum of Consecutive Prime Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such

POJ 2739 Sum of Consecutive Prime Numbers(水题)

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20560   Accepted: 11243 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

poj 2379 Sum of Consecutive Prime Numbers

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24498   Accepted: 13326 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio