尺取法 || 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 ...
随机推荐
- 数据库几种Top子句的使用方法
转自:https://blog.csdn.net/melody_susan/article/details/47011331
- android调用第三方库——第一篇 (转载)
转自:http://blog.csdn.net/jiuyueguang/article/details/9447245 版权声明:本文为博主原创文章,未经博主允许不得转载. 0:前言: 这两天一直在研 ...
- adb logcat 查看日志 (转载)
转自:http://blog.csdn.net/xyz_lmn/article/details/7004710 使用 logcat 命令 查看和跟踪系统日志缓冲区的命令logcat的一般用法是: [a ...
- Codeforces703B Mishka and trip
题意: 就是有n个点,本来相邻点之间就有一条边,1和n之间也有一条,然后给你几个特殊点,说这些特殊点和其他所有点都连起来了,然后算一个所有边的权值和,每条边的权值等于两个点的c相乘. 思路: 水题啊- ...
- android 在一个应用中启动另一个应用
在程序开发过程当中,常遇到需要启动另一个应用程序的情况,比如在点击软件的一个按钮可以打开地图软件. 如果既有包名又有主类的名字,那就好 办了, 直接像下面就行: [html] Intent inte ...
- C#中,用HashTable,DataTable等复制和克隆浅谈
如有雷同,不胜荣欣,若转载,请注明 在C#中,用HashTable,DataTable等复制和克隆浅谈,下面直接看例子 HashTable ht = null; ht = new HashTable( ...
- AtCoder Grand Contest 001 D - Arrays and Palindrome
题目传送门:https://agc001.contest.atcoder.jp/tasks/agc001_d 题目大意: 现要求你构造两个序列\(a,b\),满足: \(a\)序列中数字总和为\(N\ ...
- URAL 1890 . Money out of Thin Air (dfs序hash + 线段树)
题目链接: URAL 1890 . Money out of Thin Air 题目描述: 给出一个公司里面上司和下级的附属关系,还有每一个人的工资,然后有两种询问: 1:employee x y z ...
- Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推
题目链接: Hdu 5451 Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩 ...
- 转 PHP scandir() 函数
实例 列出 images 目录中的文件和目录: <?php $dir = "/images/"; // 以升序排序 - 默认 $a = scandir($dir); // 以 ...