尺取法 || 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 ...
随机推荐
- 【旧文章搬运】炉子给的SYSTEM_HANDLE_TYPE有点错误
原文发表于百度空间,2008-12-03========================================================================== 今天写程序 ...
- centos7无故重启-内核升级
最近有一台物理服务器,centos7操作系统,无故重启,每天都会发生这种情况: 解决: 升级内核 CentOS 允许使用 ELRepo,这是一个第三方仓库,可以将内核升级到最新版本,使用ELRepo升 ...
- HDU 5901 Count primes (模板题)
题意:给求 1 - n 区间内的素数个数,n <= 1e11. 析:模板题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...
- Android应用开发基础篇(12)-----Socket通信(转载)
转自:http://www.devdiv.com/android_socket_-blog-258060-10594.html 一.概述 网络通信无论在手机还是其他设备上都应用得非常广泛,因此掌握网络 ...
- E20180406-hm
dressing n. 穿衣; 调味品; 肥料 dress n. 衣服; 礼服; 连衣裙; 装饰; vt. 打扮; 穿着; 给…穿衣; monde n. 时髦社交界,上流社会; ingredient ...
- rabbitMQ的使用
介绍 一款消息队列数据库,类似redis发布订阅,但是rq 做了功能完善和数据持久化.在项目中,将一些无需即时返回且耗时的操作提取出来,进行了异步处理,而这种异步处理的方式大大的节省了服务器的请求响应 ...
- python 中site-packages 和 dist-packages的区别
dist-packages is a Debian-specific convention that is also present in its derivatives, like Ubuntu. ...
- HTML5中div,article,section的区别
最近正在学习html5,刚接触html5,感觉有点不适应,因为有一些标签改变了,特别是div, section article这三个标签,查了一些资料,也试着用html5和css3布局网页,稍微有点头 ...
- PHPmail 亲测可用
2017年5月8日9:10:47 1.在模块的配置文件中加入下里面代码,账号最好用126邮箱'THINK_EMAIL' => array( 'SMTP_HOST' => 'smtp.163 ...
- ARC 100
链接 https://arc100.contest.atcoder.jp/ C Linear Approximation 题解 把ai减去i后排序, 我们要的b就是排完序后的中位数 Code #inc ...