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 ...
随机推荐
- java5初学
1.Eclipse快捷键 alt + / 代码提示,例如自动创建main方法ctrl + d 删除当前行ctrl + alt + up/down 复制当前行alt + up/down 交换行ctrl ...
- Metadata 的概念
https://www.ibm.com/developerworks/cn/cloud/library/1509_liukg_openstackmeta/ http://mathslinux.org/ ...
- [python] 网络数据采集 操作清单 BeautifulSoup、Selenium、Tesseract、CSV等
Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesseract.CSV等 Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesse ...
- [NC189A]数字权重
题目大意:有一个$n$位的数,设第$i$位为$a_i$(最高位为$a_1$).问满足$(\sum\limits_{i=2}^n(a_i-a_{i-1}))==k$的数的个数(不含前导零) 题解:发现$ ...
- BZOJ5301 [Cqoi2018]异或序列 【莫队】
题目链接 BZOJ5301 题解 莫队水题 BZOJ400AC纪念 #include<algorithm> #include<iostream> #include<cst ...
- 0-1背包问题(经典)HDU2602 Bone Collector
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 遍历Collection集合中的6种方法:
下面的代码演示了遍历Collection集合的6种方法,注意Collection集合的遍历远不止于增强for循环,和迭代器两种. 代码如下: package com.qls.traverse; imp ...
- Build RPM package from source code
# yum install rpm-build# wget http://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz# vim rsync.specNam ...
- HDU2824 The Euler function
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Description The Eule ...
- 各版本Sql Server下载地址全
SQL Server 2014简体中文企业版 文件名:cn_sql_server_2014_enterprise_edition 32位下载地址:ed2k://|file|cn_sql_server_ ...