POJ 2739:Sum of Consecutive Prime Numbers(Two pointers)
【题目链接】 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)的更多相关文章
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- Sum of Consecutive Prime Numbers(素数打表+尺取)
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
- [UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- POJ.2739 Sum of Consecutive Prime Numbers(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
随机推荐
- 使用python 3导入MySQLdb 报No module named 'MySQLdb'异常错误
MySQLdb只支持Python2.*,还不支持3.* 可以用PyMySQL代替安装PyMySQL后,在使用模块时使用import pymysql as MySQLdb 后续使用方式与MySQLdb ...
- 数据结构与算法之顺序表C语言实现
顺序表等相关概念请自行查阅资料,这里主要是实现. 注: 1.顺序表C语言实现: 2.按较简单的方式实现,主要帮助理解,可在此基础上修改,更加完善: 3.提供几个简单函数,可自行添加功能: 4.可用C+ ...
- android 在自定义的listview(有刷新加载项)列表中,数据过少时不能铺满整个屏幕时,header和footer同时显示问题
android 在自定义的listview(有刷新加载项)列表中,数据过少时,当刷新时,加载项也会显示,这是很头疼的一个问题,查阅了一些资料,总结了一个比较不错的方法: 原来代码: @Overrid ...
- 多线程 线程池 ExecutorService
package org.zln.thread; import java.util.Date; import java.util.concurrent.ExecutorService; import j ...
- 实时流处理Storm、Spark Streaming、Samza、Flink孰优孰劣
对于一个成熟的消息中间件而言,消息格式不仅关系到功能维度的扩展,还牵涉到性能维度的优化.随着Kafka的迅猛发展,其消息格式也在不断的升级改进,从0.8.x版本开始到现在的1.1.x版本,Kafka的 ...
- 7月14号day6总结
今天学习过程和总结 IOC和DIO IOC相当于一个容器,在容器中加注解.接口存在意义依赖注入.4个注解都行,依赖注入只能发生在IOC容器里, pring IOC 容器可以管理Bean 的生命周期,S ...
- 3.Docker与LXC、虚拟化技术的区别——虚拟化技术本质上是在模拟硬件,Docker底层是LXC,本质都是cgroups是在直接操作硬件
先说和虚拟化技术的区别 难道虚拟技术就做不到吗? 不不不,虚拟技术也可以做到,但是会有一定程度的性能损失,灵活度也会下降.容器技术不是模仿硬件层次,而是 在Linux内核里使用cgroup和names ...
- tomcat + apache 动静分离
原文地址:http://blog.csdn.net/gengv/article/details/5739438 从网上查了不少资料,想了解一下如何整合Apache和Tomcat,以便让Apache的h ...
- OpenGL入门学习(一)(转)--环境搭建
http://blog.chinaunix.net/uid-20622737-id-1912797.html 说起编程作图,大概还有很多人想起TC的#include <graphics.h> ...
- Android NDK使用
1. 介绍 这里主要想记录一下Android NDK开发C程序的使用方法 2. ndk下载 到google官网或者国内镜像网站下载android-ndk形如: android-ndk-r<ve ...