zoukankan      html  css  js  c++  java
  • POJ 2739

    Sum of Consecutive Prime Numbers
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 17965   Accepted: 9871

    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

     
    尺取法
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 typedef pair<int,int> pii;
     9 
    10 #define maxn 10005
    11 
    12 int len = 0,len1 = 0,n;
    13 int ele[maxn];
    14 bool prime[maxn];
    15 
    16 
    17 void init() {
    18         for(int i = 2; i <= maxn - 5; ++i) {
    19                 prime[i] = i % 2 || i == 2;
    20         }
    21 
    22         for(int i = 2; i * i <= maxn - 5; ++i) {
    23                 if(prime[i])
    24                 for(int j = i; j * i <= maxn - 5; ++j) prime[j * i] = 0;
    25         }
    26 
    27         for(int i = 2; i <= maxn - 5; ++i) if(prime[i]) ele[len++] = i;
    28         //for(int i = 0; i < 10; ++i) printf("%d ",ele[i]);
    29 
    30 
    31 }
    32 
    33 int main()
    34 {
    35    // freopen("sw.in","r",stdin);
    36     init();
    37 
    38     while(~scanf("%d",&n) && n) {
    39             int s = 0,pos = 0,sum = 0,ans = 0;
    40             for(; ele[s] <= n; ++s) {
    41                     while(sum < n && pos < len ) {
    42                             sum += ele[pos++];
    43                     }
    44                     if(sum < n) break;
    45                     if(sum == n) ++ans;
    46                     sum -= ele[s];
    47             }
    48             printf("%d
    ",ans);
    49 
    50     }
    51 
    52 
    53     return 0;
    54 }
    View Code
  • 相关阅读:
    Vue 02
    Vue 初识
    复杂数据类型之函数 对象
    Collections工具类
    遍历集合的方法总结
    使用Iterator迭代器遍历容器元素(List/Set/Map)
    TreeSet的使用和底层实现
    HashSet基本使用
    HashSet底层实现
    TreeMap的使用和底层实现
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3623581.html
Copyright © 2011-2022 走看看