Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers
http://poj.org/problem?id=2739
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 28929 | Accepted: 15525 |
Description
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20.
Your mission is to write a program that reports the number of representations for the given positive integer.
Input
Output
Sample Input
2
3
17
41
20
666
12
53
0
Sample Output
1
1
2
3
0
0
1
2
Source
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<queue>
#include<stack>
#define PI acos(-1.0)
#define eps 1e-9
using namespace std;
int prime[];//存素数
bool vis[];//保证不做素数的倍数
void dabiao(int n){
int cnt = ;
memset(vis, false, sizeof(vis));//初始化
memset(prime, , sizeof(prime));
for(int i = ; i <= n; i++)
{
if(!vis[i])//不是目前找到的素数的倍数
prime[cnt++] = i;//找到素数~
for(int j = ; j<cnt && i*prime[j]<=n; j++)
{
vis[i*prime[j]] = true;//找到的素数的倍数不访问
if(i % prime[j] == ) break;//关键!!!!
}
}
} int main(){ dabiao();
int n;
while(~scanf("%d",&n)){
if(!n) break;
int L=,R=;
int ans=;
int sum=;
int pos=upper_bound(prime,prime+,n)-prime;
while(L<=R){ if(ans<=n&&R<pos){
ans+=prime[R++];
}
else if(ans>n||R==pos){
ans-=prime[L++];
}
if(ans==n){
sum++;
}
}
printf("%d\n",sum);
} }
Sum of Consecutive Prime Numbers的更多相关文章
- 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(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25225 ...
- 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(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- poj 2379 Sum of Consecutive Prime Numbers
...
- 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 ...
随机推荐
- html如何设置打印样式?
转自网络,忘记出处了. html/jsp/网页/打印相关/打印预览/js设置页眉页脚 <html> <head> <title>打印相关</title& ...
- Phoenix 安装完的几个简单使用
Phoenix 安装完之后的一些简单使用: 本屌丝的运行环境是3节点集群,先启动hadoop,每个节点分别启动zookeeper,启动hbase(具体怎么启动这里就不多说了) 进入 phoenix的b ...
- 并发工具类(五) Phaser类
前言 JDK中为了处理线程之间的同步问题,除了提供锁机制之外,还提供了几个非常有用的并发工具类:CountDownLatch.CyclicBarrier.Semphore.Exchanger.Ph ...
- openStack 租户控制台修改虚拟机账户密码
- cloud-init方式 该种方式需要虚拟机镜像安装cloud-init,将重置密码脚本注入到虚拟机中.nova boot –image=image-id –nic net-id=net-id – ...
- CUDA C Programming Guide 在线教程学习笔记 Part 3
▶ 表面内存使用 ● 创建 cuda 数组时使用标志 cudaArraySurfaceLoadStore 来创建表面内存,可以用表面对象(surface object)或表面引用(surface re ...
- 微擎框架小程序 uitl
获取用户信息 util.getUserInfo(callback) 获取成功后会将用户信息写入到缓存中,如果指定了回调函数,则会调用回调函数 callback 获取成功后的回调函数 示例 var ap ...
- Windows系统日常运维
WINDOWS系统日常运维 http://www.docin.com/p-677263438.html
- tab form
procedure TForm2.ToolButton1Click(Sender: TObject); var frm: TForm; begin frm := TForm.Create(Applic ...
- Virus
c:\windows\syswow64\rundll32.exe C:\Users\Administrator.mt1-PC\AppData\Roaming\FishSearch
- Simple2D-23(重构)反走样几何图形
以前 Simple2D 使用 Canvas2D 对象来绘制几何图形,而且渲染出来的几何图形存在明显的锯齿.如果想要抗锯齿的几何图形,则需要开启 OpenGL 的 MSAA,这需要很大的开销. 如果不使 ...