尺取法 || POJ 2739 Sum of Consecutive Prime Numbers
#include <iostream>
#include <cstdio>
using namespace std;
#define SZ 11000
bool isprime[SZ];
int prime[SZ], num[SZ];
int cnt;
void prim(int n)
{
memset(isprime, , sizeof(isprime));//1->是素数,0->不是素数
memset(prime, , sizeof(prime));
isprime[] = ;
isprime[] = ;
cnt = ;
for(int i = ; i <= n; i++)
{
if(isprime[i]) prime[cnt++] = i;
for(int j = ; j <= cnt && i * prime[j] <= n; j++)
{
isprime[i * prime[j]] = ;
if(i % prime[j] == ) break;
}
}
return;
}
int main()
{
while()
{
int n;
scanf("%d", &n);
if(n == ) break;
prim(n);
num[] = ;
for(int i = ; i < cnt; i++)
num[i] = num[i - ] + prime[i];
int l = , r = , ans = ;
while(r < cnt)
{
if(num[r] - num[l - ] < n) r++;
else if(num[r] - num[l - ] > n) l++;
else if(num[r] - num[l - ] == n) ans++, l++, r++;
}
printf("%d\n", ans);
}
return ;
}
尺取法 || POJ 2739 Sum of Consecutive Prime Numbers的更多相关文章
- 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(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- 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 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- 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 尺取法
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
- poj 2739 Sum of Consecutive Prime Numbers 小结
Description Some positive integers can be represented by a sum of one or more consecutive prime num ...
随机推荐
- 容器vector 迭代器iterator 应用
#include <iostream> #include <vector> using namespace std; int main() { vector<int> ...
- Android Layout XML属性研究--android:layout_marginBottom (转载)
转自:http://blog.csdn.net/yanfangjin/article/details/7393023 在如下的xml配置文件中,起初对于android:layout_marginBot ...
- Cg(C for Graphic)语言语义词与语义绑定详述 (转)
摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 语义词( Semantic )与语义绑定 ...
- python 蓝牙模块pybluz安装
最近项目运用了蓝牙,所以来学一学蓝牙. 经过查阅,知道python的蓝牙模块是pybluz,然后老管理进行安装 出错,提示“Could not find the Windows Platform SD ...
- 跟我一起玩Win32开发(2):完整的开发流程
上一篇中我给各位说了一般人认为C++中较为难的东西——指针.其实对于C++,难点当然不局限在指针这玩意儿上,还有一些有趣的概念,如模板类.虚基类.纯虚函数等,这些都是概念性的东西,几乎每一本C++书上 ...
- Qt容器类之三:通用算法
在<QtAlgorithm>头文件中,Qt提供了一些全局的模板函数,这些函数是可以使用在容器上的十分常用的算法.我们可以在任何提供了STL风格迭代器的容器类上用这些算法,包括QList.Q ...
- HTML中div的悬浮标题
<div title="我是鼠标悬停文字">我是一个DIV</div> <div class="diggao" title=&qu ...
- h5-18-文件上传
参考博客地址:https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects 参考博客地址:http: ...
- JavaScript星级评分,仿百度,增强版
JavaScript星级评分,仿百度,增强版 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...
- Split方法,拆分字符串后,去除返回的空值
我们在使用Split('')方法的时候,根据指定的 '字符' 对字符串进行拆分,当'字符’为最后一个,将会拆分一个空值进行返回. 这个时候我们可以使用 string.Split(new ch ...