zoukankan      html  css  js  c++  java
  • Sum of Consecutive Prime Numbers POJ

        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
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<string>
     5 #include<cstring>
     6 #define MAX 10005
     7 using namespace std;
     8 
     9 int prime[MAX],b[MAX];
    10 int k;
    11 
    12 void get_p()
    13 {   memset(prime,true,sizeof(prime));
    14     prime[1]=false,prime[2]=true;
    15     k=1; 
    16     for(int i=2;i<=MAX;i++){
    17        if(prime[i]){
    18               b[k++]=i;
    19            int j=i+i;
    20            while(j<=MAX)
    21            {  prime[j]=false;
    22               j=j+i;
    23            }  
    24        }
    25     }
    26 }
    27 int main()
    28 {  int n;
    29    while(~scanf("%d",&n)&&n){
    30           get_p();
    31          // for(int i=1;i<k;i++) printf("%d ",b[i]);
    32           int l=1,r=1;
    33           int ans=0,sum=0,t=0;
    34           while(r<=k){
    35                 if(b[r]>n) break;
    36                 sum+=b[r];
    37                 r++;
    38                 while(sum>n){
    39                      sum-=b[l];
    40                      l++;
    41                 }
    42                 if(sum==n) t++;
    43           }
    44           printf("%d
    ",t);
    45    }
    46 } 
  • 相关阅读:
    出栈序列的可能性判定
    阿里离线数据处理平台2013暑期学校
    终于算完了这道『1+1』
    数据结构学习笔记之一 链表
    经典解释监视器和对象锁
    Sybase中字符串替换函数 STR REPLACE
    使用Arrays sort 方法進行排序
    clustered和nonclustered索引的区别
    Java序列化的作用和反序列化
    Java多线程下载并具断点续传功能JAR
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6754169.html
Copyright © 2011-2022 走看看