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

题目大意:

  这道题是说,我给你一个数字n,然后你求出n==prime[i]+prime[i+1]+prime[i+2]+...+prime[i+tt]的方案数。

解题思路:

  很水的一道题,上来后先打一个10000以内的素数表,然后你只需要处理好素数串的头和尾就行了,什么是头和尾呢?

就是for( int i = 1;prime[i] <= n;i++) ,因为我们这样想啊,如果prime[i] > n 的话,肯定是直接退出的,不用在判断了

然后内部的判断用一个tt变量边走边标记就好

代码:

  1 # include<cstdio>
  2 # include<iostream>
  3 # include<fstream>
  4 # include<algorithm>
  5 # include<functional>
  6 # include<cstring>
  7 # include<string>
  8 # include<cstdlib>
  9 # include<iomanip>
 10 # include<numeric>
 11 # include<cctype>
 12 # include<cmath>
 13 # include<ctime>
 14 # include<queue>
 15 # include<stack>
 16 # include<list>
 17 # include<set>
 18 # include<map>
 19
 20 using namespace std;
 21
 22 const double PI=4.0*atan(1.0);
 23
 24 typedef long long LL;
 25 typedef unsigned long long ULL;
 26
 27 # define inf 999999999
 28 # define MAX 10000+4
 29
 30 int prime[MAX];
 31 int book[MAX];
 32 int len = 0;
 33
 34 void init()
 35 {
 36     for ( int i = 2;i <= MAX;i++ )
 37     {
 38         book[i] = 1;
 39     }
 40     for ( int i = 2;i <= MAX;i++ )
 41     {
 42         if ( book[i]==1 )
 43         {
 44             for ( int j = 2*i;j <= MAX;j+=i )
 45             {
 46                 book[j] = 0;
 47             }
 48         }
 49     }
 50     len = 0;
 51     for ( int i = 2;i <= MAX;i++ )
 52     {
 53         if ( book[i]==1 )
 54         {
 55             prime[++len] = i;
 56         }
 57     }
 58    // for ( int i = 1;i <= len;i++ )
 59   //  {
 60   ////      cout<<prime[i]<<" ";
 61   //  }
 62   //  cout<<endl;
 63
 64 }
 65
 66
 67 int main(void)
 68 {
 69     init();
 70    // cout<<prime[1]<<endl;
 71     int n;
 72     while ( cin>>n )
 73     {
 74         if ( n==0 )
 75             break;
 76
 77         int tot = 0;
 78         int temp = 0;
 79         int j = 1;
 80
 81         for ( int i = 1;prime[i] <= n;i++ )
 82         {
 83             temp = 0;
 84             for ( int tt = i;tt <= len;tt++ )
 85             {
 86                 temp+=prime[tt];
 87                 if ( temp==n )
 88                 {
 89                     tot++;
 90                     break;
 91                 }
 92                 if ( temp > n )
 93                 {
 94                     break;
 95                 }
 96             }
 97         }
 98
 99         cout<<tot<<endl;
100
101     }
102
103
104
105     return 0;
106 }
时间: 2024-12-25 16:39:59

POJ 2739 Sum of Consecutive Prime Numbers(水题)的相关文章

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

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

poj 2739 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

[水] poj 2739 Sum of Consecutive Prime Numbers

题意: 给一个n,有多少种连续的素数和加起来等于n. 思路: 打素数表,然后直接暴力. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"queue" #include"algorithm" #include"iostream" #include"

POJ 2739 Sum of Consecutive Prime Numbers(素数)

http://poj.org/problem?id=2739 题意: 给你一个10000以内的自然数X,然后问你这个数x有多少种方式能由连续的素数相加得来? 分析: 首先用素数筛选法把10000以内的素数都找出来按从小到大保存到prime数组中. 然后找到数X在prime中的上界, 如果存在连续的素数之和==X, 那么一定是从一个比X小的素数开始求和(不会超过X的上界),直到和sum的值>=X为止. 所以我们暴力枚举10000以内的所有可能的素数相加和的起始点i,然后求连续素数的和,看看当前以p

POJ 2739 Sum of Consecutive Prime Numbers 难度:0

题目链接:http://poj.org/problem?id=2739 #include <cstdio> #include <cstring> using namespace std; int method[10001][1300]; int dp[10001]; bool isntprime[10001]; int heap[1300],cnt; void calprime(){ method[2][0]=1; dp[2]++; heap[cnt++]=2; for(int i