【题目链接】 http://poj.org/problem?id=2739

【题目大意】

  求出一个数能被拆分为相邻素数相加的种类

【题解】

  将素数筛出到一个数组,题目转化为求区段和等于某数的次数,尺取法即可。

【代码】

#include <cstdio>
#include <algorithm>
using namespace std;
int p[10010],cnt,a[10010],x;
int main(){
for(int i=2;i<=10000;i++){
if(!a[i]){p[++cnt]=i;for(int j=i+i;j<=10000;j+=i)a[j]=1;}
}
while(scanf("%d",&x)&&x){
int l=1,r=0,ans=0,s=0;
for(;l<=cnt;s-=p[l],l++){
while(r<cnt&&s<x){r++;s+=p[r];}
if(s==x)ans++;
if(p[r]>x)break;
}printf("%d\n",ans);
}return 0;
}

  

POJ 2739:Sum of Consecutive Prime Numbers(Two pointers)的更多相关文章

  1. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  2. Sum of Consecutive Prime Numbers(素数打表+尺取)

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

  3. [UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  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(素数)

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

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

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

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

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

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

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

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

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

随机推荐

  1. Python利器一之requests

    Python利器一之requests 一.教程涉及开发语言.脚本.框架.数据库等内容 Python + requests 通过 pip 安装: pip install requests 通过 easy ...

  2. Restful API实战

    简介:随着移动互联网的发展,客户端层出不穷,app,web,微信端等等,而后端业务逻辑基于是一致的,如何做到业务逻辑“一次编写,随时接入”?答案是通过远程调用API,而目前比较火的方案是“Restfu ...

  3. 指定user镜像安装的磁盘

    ironic node-update <node uuid> add properties/root_device='{"name":"/dev/sdb&qu ...

  4. mongo数据库 启动报错

    报错信息如下: [root@166 bin]# mongoMongoDB shell version v3.4.6-22-ga109a23connecting to: mongodb://127.0. ...

  5. 系统编程--标准IO

    1.流和FILE对象 对于国际字符集,一个字符可以由一个以上的字节来表示.标准I/O文件流可以用来操作单字节和多字节(宽,wide)字符集.一个流的方向(orientation)决定了字符是以单字节还 ...

  6. JavaWeb笔记(十)非关系型数据库Redis

    Redis Redis是一款高性能的NOSQL系列的非关系型数据库 主流的NOSQL产品 键值(Key-Value)存储数据库 相关产品: Tokyo Cabinet/Tyrant.Redis.Vol ...

  7. 实用JS系列——面向对象中的类和继承

    背景: 在最开始学习JavaScript时,我们就知道,它是一种脚本语言,也有面向对象机制.但它的面向对象继承机制是基于原型的,即Prototype.今天,我们就来找一下JS中OO的影子. 创建类 1 ...

  8. redis各种数据结构使用场景

    一.redis 数据结构使用场景 原来看过 redisbook 这本书,对 redis 的基本功能都已经熟悉了,从上周开始看 redis 的源码.目前目标是吃透 redis 的数据结构.我们都知道,在 ...

  9. LeetCode -- Product of Array Except Self My Submissions Question

    Question: Given an array of n integers where n > 1, nums, return an array output such that output ...

  10. 添加对WCF的调用(内网状态下)。

    在能连接到内网的电脑上,运行SvcUtil.exe工具即可. 如打开文件后闪退,则可打开cmd后,将文件拖入到cmd中,然后再加上文件的地址.