Sum of Consecutive Prime Numbers(poj2739)

Sum of Consecutive Prime Numbers

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22019   Accepted: 12051

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

思路:先筛选出0到10000的素数,再尺取法跑一遍就可以了

 1 import java.util.*;
 2 import java.lang.*;
 3 import java.io.*;
 4
 5 public class Main {
 6     public static void main(final String[] args) {
 7         Scanner in = new Scanner(System.in);
 8         int n, i, j, k, p, q;
 9         int aa[] = new int[20000];
10         int bb[] = new int[20000];
11         Arrays.fill(aa, 0);
12         aa[1] = 1;
13         aa[0] = 1;
14         int cnt = 0;
15         for (i = 2; i <= 100; i++) {
16             if (aa[i] == 0) {
17                 for (j = i; i * j <= 10000; j++) {
18                     aa[i * j] = 1;
19                 }
20             }
21
22         }
23         for (i = 0; i <= 10000; i++) {
24             if (aa[i] == 0) {
25                 bb[cnt++] = i;
26             }
27         }
28         int ss = 1;
29         while (ss == 1) {
30             k = in.nextInt();
31             int ans = 0;
32             if (k == 0) {
33                 break;
34             } else {
35                 int sum = 0;
36                 int ll = 0;
37                 int rr = 0;
38                 while (ss == 1) {
39                     if (rr > cnt) {
40                         break;
41                     }
42                     if (bb[rr] > k) {
43                         break;
44                     }
45                     if (ll > rr) {
46                         break;
47                     }
48                     while (ss == 1) {
49                         sum += bb[rr];
50                         if (sum >= k) {
51                             break;
52                         } else {
53                             rr++;
54                         }
55                         if (rr > cnt) {
56                             break;
57                         }
58                     }
59                     if (sum == k) {
60                         ans++;
61                     }
62                     sum -= bb[ll];
63                     sum -= bb[rr];
64                     ll++;
65                 }
66             }
67             System.out.println(ans);
68         }
69         return;
70     }
71 }

时间: 2024-12-23 03:17:48

Sum of Consecutive Prime Numbers(poj2739)的相关文章

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

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

poj 2739 Sum of Consecutive Prime Numbers 尺取法

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

【UVA】1210 - Sum of Consecutive Prime Numbers

普通的求区间连续和的问题,一开始以为是区间移动,但是怕UVA数据太严,直接打表,后来发现自己的担心是多余的. 14044972 1210 Sum of Consecutive Prime Numbers Accepted C++ 0.049 2014-08-15 10:30:11 打表的话效率可能不是很高. AC代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm&

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: 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