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 ...
随机推荐
- js字符串转换成数字,数字转换成字符串
转自网络,忘记出处了. js字符串转换成数字 将字符串转换成数字,得用到parseInt函数. parseInt(string) : 函数从string的开始解析,返回一个整数. 举例:parseIn ...
- 在VM克隆CENTOS以后,网卡的处理过程
会发现克隆CENTOS以后,网卡eth0无法启动.处理步骤如下:1. vi /etc/sysconfig/network-scripts/ifcfg-eth0删除HWADDR地址那行删除UUID的那行 ...
- [UE4]有限状态机、动画状态机、纯函数
有限状态机 FSM:Finite State Machine,表示有限个状态以及在这些状态之间转移和动作的数学模型 纯函数: 纯函数: 先后调用顺序不重要,没有修改任何数值,只是获取数值或者临时计算一 ...
- [UE4]为UStaticMeshComponent添加点击事件
BlockMesh->OnClicked.AddDynamic(this, &APuzzleBlock::BlockClicked); //鼠标点击事件 BlockMesh->On ...
- 6.26-EL表达式,JSTL标签
一.EL表达式 功能: 替代jsp中数据访问时的复杂java代码 语法: ${表达式} ${(5+9)*2} 访问顺序: page--->request--->session---> ...
- 对象DIY
1.在java开发中,好代码都是组织的比较好,模拟现实很好,而不是步骤指令 2.对象组织+继承(归类
- CSS Web安全字体组合
常用的字体组合 font-family属性是多种字体的名称,作为一个"应变"制度,以确保浏览器/操作系统之间的最大兼容性.如果浏览器不支持的第一个字体,它尝试下一个的字体. 你想要 ...
- pycharm下getpass.getpass()卡住
pycharm下getpass.getpass()卡住不运行是什么问题 python pycharm 首先声明 下面这几行代码在命令行和eclipse下都能正常运行 import getpass pr ...
- Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本)
Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本) Hive 安装依赖 Hadoop 的集群,它是运行在 Hadoop 的基础上. 所以在安装 Hive 之 ...
- ACM MM | 中山大学等提出HSE:基于层次语义嵌入模型的精细化物体分类
细粒度识别一般需要模型识别非常精细的子类别,它基本上就是同时使用图像全局信息和局部信息的分类任务.在本论文中,研究者们提出了一种新型层次语义框架,其自顶向下地由全局图像关注局部特征或更具判别性的区域. ...