题目链接: 传送门

Sum of Consecutive Prime Numbers

Time Limit: 1000MS     Memory Limit: 65536K

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

解题思路

题目大意:问有几种方式,把一个数分成几个连续的素数的和。
跑一遍埃氏筛选法,筛选出素数,然后尺取法的思路不断推进。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 10005;
int prime[MAX];
bool is_prime[MAX];

int main()
{
    int p = 0;
    memset(is_prime,true,sizeof(is_prime));
    is_prime[0] = is_prime[1] = false;
    for (int i = 2;i <= MAX;i++)
    {
        if (is_prime[i])
        {
            prime[p++] = i;
            for (int j = 2*i;j <= MAX;j += i)
            {
                is_prime[j] = false;
            }
        }
    }
    int N;
    while (~scanf("%d",&N) && N)
    {
        bool flag = false;
        int cnt = 0,s = 0,t = 0,sum = 0;
        for (;;)
        {
            if (prime[t] >= N)  break;
            while (sum < N)
            {
                sum += prime[t++];
                if (sum == N)
                {
                    flag = true;
                }
            }
            if (flag)
            {
                cnt++;
                flag = false;
            }
            sum -= prime[s++];
            if (sum == N && prime[t] < N)
            {
                cnt++;
                sum -= prime[s++];
            }
        }
        if (is_prime[N])printf("%d\n",cnt+1);
        else if (cnt)printf("%d\n",cnt);
        else printf("0\n");
    }
    return 0;
}

POJ 2739 Sum of Consecutive Prime Numbers(尺取法)的更多相关文章

  1. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...

  2. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  3. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  6. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  8. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  9. poj 2739 Sum of Consecutive Prime Numbers 小结

     Description Some positive integers can be represented by a sum of one or more consecutive prime num ...

随机推荐

  1. 在线音乐网站【03】Part one 功能实现

    今天打算把网站功能的具体实现给总结一下,如果你想了解整个小项目,建议你先看看前面2篇博客. 1.在线音乐网站(1)需求和功能结构 2.在线音乐网站(2)数据库和开发环境 7.网站主要模块实现 a.在线 ...

  2. Java:泛型

    一.序言 变化一: 在引入范型之前,Java中的类型分为原始类型.复杂类型,其中复杂类型分为数组和类:引入范型后,一个复杂类型可以细分成更多的类型. 例如,原先的List类型,现在细分成List< ...

  3. 30行代码实现Javascript中的MVC

    从09年左右开始,MVC逐渐在前端领域大放异彩,并终于在刚刚过去的2015年随着React Native的推出而迎来大爆发:AngularJS.EmberJS.Backbone.ReactJS.Rio ...

  4. 深入理解OOP(三):多态和继承(动态绑定和运行时多态)

    在前面的文章中,我们介绍了编译期多态.params关键字.实例化.base关键字等.本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定. 深入理解OOP(一):多态和继承(初期绑定和编译时 ...

  5. SDRAM读写一字(上)

    SDRAM读写一字 系统设计 SDRAM指令 指令 常量名 CKE CSn RAS CASn WEn 备注 空操作 NOP 1 0 1 1 1   行激活 ACTIVE 1 0 0 1 1   读操作 ...

  6. Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.2.3:run (default-cli) on project Maven_WebTest: Execution default-cli of goal org.codehaus.cargo:cargo-maven2-plugin:1.2.3:run failed: C

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. window 安装redis服务、卸载redis服务和启动redis服务

    1.安装redis服务 redis-install.bat 1 echo install redis-server23 D:\redis\redis-server.exe --service-inst ...

  8. 第八章:Java集合

    1.Java集合 A:对象的容器. B:实现数据结构(栈.队列) 2.  Set:无序不重复 List: 有序可重复,长度可变. Map: 存放键值对. 3.  Iterator foreach

  9. canvas三角函数直线运动

    var canvas = document.getElementById("canvas"); var cxt = canvas.getContext("2d" ...

  10. mysql5.5的安装与配置(亲测版)

    mysql5.5.x的编译安装 说明:5.5和5.1差不多,只是在编译时增加了一个cmake,其他基本一样,mysql5.5默认编码是utf-8,在使用mysql5.5配置lnmp或者lamp后安装d ...