【题目链接】 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. GCC 7.3 for Windows

    GCC 6.1x Compilers 下载地址1: Mingw gcc 6.30下载 这个是某微软员工编译的版本 MinGW is a port of GCC to Windows. It is fr ...

  2. 转:sift算法详解

    转自:http://blog.csdn.net/pi9nc/article/details/23302075 对于初学者,从David G.Lowe的论文到实现,有许多鸿沟,本文帮你跨越. 1.SIF ...

  3. Android记事本06

    昨天: activity的生命周期 今天: activity横竖屏切换的生命周期 遇到的问题: 为了匹配ADK,下载了更旧的版本SDK,布局文件仍然无法显示.

  4. 【bzoj2969】矩形粉刷 期望

    题目描述 为了庆祝新的一年到来,小M决定要粉刷一个大木板.大木板实际上是一个W*H的方阵.小M得到了一个神奇的工具,这个工具只需要指定方阵中两个格子,就可以把这两格子为对角的,平行于木板边界的一个子矩 ...

  5. BZOJ3143 [Hnoi2013]游走 【高斯消元】

    题目 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这条边的编 ...

  6. BZOJ1823 [JSOI2010]满汉全席 【2-sat】

    题目 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而能够烹饪出经过专家 ...

  7. position:absolute和float隐式改变display为inline-block

    不论之前是什么类型的元素(display:none除外), 只要设置了position:absolute或float, 都会让元素以display:inline-block的方式显示, 可以设置长宽, ...

  8. oracle 查看表空间的脚本

    SELECT upper(f.tablespace_name) "表空间名", d.Tot_grootte_Mb "表空间大小(M)", d.Tot_groot ...

  9. 《c程序设计语言》读书笔记-删除字符串中匹配的字符

    #include <stdio.h> #include <string.h> #define Num 10 int main() { int c,i,j = 0,m,n = 0 ...

  10. 【转】beyond compare 启动提示“应用程序发生错误”

    [转]beyond compare 启动提示“应用程序发生错误”   今天到公司BCompare不能打开,重新安装也不能打开.最后处理下,就解决了.方法是把C:\Documents and Setti ...