Sum of Consecutive Prime Numbers

http://poj.org/problem?id=2739

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28929   Accepted: 15525

Description

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime 
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

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the 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的更多相关文章

  1. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  2. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  3. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  4. 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 ...

  5. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  6. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  7. poj 2379 Sum of Consecutive Prime Numbers

                                                                                                        ...

  8. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895 ...

  9. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

随机推荐

  1. MySQL工具 Navicat

    F6打开命令行窗口 ctrl+shift+r  运行选中语句   #不过不建议使用,因为ctrl+r是执行当前所有语句,如果语句当中有delete update 后果不堪设想

  2. CA证书认证单向和双向的区别

     我觉得最科学的应该是,单向的,每次客户端发两把锁住的东西给服务端,服务端解密两次,服务端用客户端发来的对称密钥加密数据,发送给客户端,客户端只需解密一次,然后客户端每次修改随机密码,传给服务端,服务 ...

  3. keras输出中间层结果,某一层的权重、偏置

    转载:https://blog.csdn.net/hahajinbu/article/details/77982721 from keras.models import Sequential,Mode ...

  4. golang web框架 beego 学习 (四) 连接mysql

    1 DB参数配置在app.conf appname = gowebProject httpport = runmode = dev [db] host= localhost port= databas ...

  5. linux进程与线程的区别【转】

    知乎上总结: "linux使用的1:1的线程模型,在内核中是不区分线程和进程的,都是可运行的任务而已.fork调用clone(最少的共享),pthread_create也是调用clone(最 ...

  6. 阳虚体质外感/胃脘痛/经期抽搐x案

    * 咽干咽痛   某女 42岁 在40岁产下一子,后体质明显不如以前,几年以来,易感冒,咳嗽 每次在社区医院输液,少则一个月,多则几个月方能愈,几天前外感微咳,咽痛声嘶 观其咽并不红,舌淡苔薄白 双手 ...

  7. javascript日期相减,求时间差

    //计算时间差 var from_date = new Date(from_time); var end_date = new Date(end_time); var time_different = ...

  8. Linux开机挂载windows共享文件夹

    https://blog.csdn.net/zhaogang1993/article/details/79573271  (可行) 命令: mount -t cifs -o username=&quo ...

  9. 网络性能测试工具iperf

    参考网站:https://www.cnblogs.com/yingsong/p/5682080.html https://docs.azure.cn/zh-cn/articles/azure-oper ...

  10. Genymotion——VirtualBox cannot start virtual device

    提示"VirtualBox cannot start virtual device" 打开VirtualBox,想要在里面直接启动Genymotion模拟器,又出现错误,提示“Un ...