zoukankan      html  css  js  c++  java
  • 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. 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、
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int prime[100006];
     6 int num[100006];
     7 int sum[100006];
     8 int tot;
     9 void init(){
    10     sum[0]=0;
    11     for(int i=2;i<100006;i++){
    12         if(!num[i]){
    13             prime[tot]=i;
    14             sum[tot]=sum[tot-1]+prime[tot];
    15             tot++;
    16             for(int j=i;j<100006;j+=i){
    17                 num[j]=1;
    18             }
    19         }
    20     }
    21     tot--;
    22 }
    23 int main()
    24 {
    25     tot=1;
    26     init();
    27 
    28     int n;
    29     while(scanf("%d",&n)==1 && n){
    30         int ans=0;
    31         int s=0,t=1;
    32         //int w=0;
    33 
    34         for(;;){
    35             if(t>tot)
    36               break;
    37               
    38             int w=sum[t]-sum[s];
    39             if(w==n)
    40                 ans++;
    41 
    42             if(prime[t]>n)
    43                 break;
    44             if(w<=n)
    45               t++;
    46             if(w>n)
    47               s++;
    48             if(s==t)
    49               t++;
    50         }
    51 
    52         printf("%d
    ",ans);
    53     }
    54     return 0;
    55 }
    View Code

    2、

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 const int MAX = 100005;
     7 int pri[MAX], vis[MAX];
     8 int n, p;
     9 
    10 void ready()
    11 {
    12     p = 0;
    13     memset(vis, 0, sizeof(vis));
    14     for(int i = 2; i < MAX; ++i) {
    15         if(vis[i]) continue;
    16         pri[p++] = i;
    17         for(int j = i*2; j < MAX; j += i) vis[j] = 1;
    18     }
    19 }
    20 void solve()
    21 {
    22     int sum = 0, k = 0, t = 0;
    23     int ans = 0;
    24     while(1) {
    25         while(t <= p && sum < n) sum += pri[t++];
    26         if(pri[t-1] > n) break;
    27         if(sum == n) ans++;
    28         sum -= pri[k++];
    29     }
    30     printf("%d
    ", ans);
    31 }
    32 int main()
    33 {
    34 //freopen("in", "r", stdin);
    35     ready();
    36     while(~scanf("%d", &n)) {
    37         if(n == 0) break;
    38         solve();
    39     }
    40     return 0;
    41 }
    View Code
     
     
  • 相关阅读:
    idea vue 格式化 并保存文件 宏 快捷键 ctrl+s
    IIS web.config 跨域设置 不包含 options的设置 thinkphp tp3 跨域
    vue peek 解决了 vue-template 加载 相对目录 ./components 组件内容 vscode
    base-table 加入动态slot 流程 vue2
    原码、反码、补码知识详细讲解
    巴什博奕
    Integer.bitCount() 函数理解
    el-table中的el-image预览小记
    shell 从变量中切割字符串
    QGIS,使用polygon裁剪栅格出现问题
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4767128.html
Copyright © 2011-2022 走看看