POJ2739-Sum of Consecutive Prime Numbers
来源:互联网 发布:新浪通达信炒股软件 编辑:程序博客网 时间:2024/05/18 09:50
Sum of Consecutive Prime Numbers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 25209 Accepted: 13748
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.
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
2317412066612530
Sample Output
11230012
Source
Japan 2005
题意:输入一个数字,求该数可由几种在素数表中连续的素数之和组成
解题思路:可以先找出所有的素数,然后用尺取法来解决
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <cmath>#include <map>#include <bitset>#include <set>#include <vector>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int visit[20005],a[20005],cnt;void init(){ for(int i=2;i<=20000;i++) visit[i]=1; for(int i=2;i<=20000;i++) { if(visit[i]) { for(int j=i*2;j<=20000;j=i+j) visit[j]=0; } } cnt=1; for(int i=2;i<=20000;i++) if(visit[i]) a[cnt++]=i;}int main(){ init(); int n; while(~scanf("%d",&n)&&n) { int sum=0,head=0,rear=0,ans=0; while(rear<cnt-1) { rear++; sum+=a[rear]; while(sum>n) sum-=a[++head]; if(n==sum) ans++; } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- POJ2739 Sum of Consecutive Prime Numbers
- POJ2739 Sum of Consecutive Prime Numbers
- POJ2739--Sum of Consecutive Prime Numbers
- POJ2739-Sum of Consecutive Prime Numbers
- poj2739 Sum of Consecutive Prime Numbers 杂题
- [POJ2739]Sum of Consecutive Prime Numbers
- POJ2739-Sum of Consecutive Prime Numbers
- poj2739 Sum of Consecutive Prime Numbers
- POJ2739 Sum of Consecutive Prime Numbers
- POJ2739-Sum of Consecutive Prime Numbers
- POJ2739 Sum of Consecutive Prime Numbers
- poj2739 - Sum of Consecutive Prime Numbers(求素数)
- POJ2739 Sum of Consecutive Prime Numbers素数打表
- POJ2739---Sum of Consecutive Prime Numbers(尺取法)
- POJ2739--Sum of Consecutive Prime Numbers(尺取法)
- poj2739 Sum of Consecutive Prime Numbers (素数打表)
- POJ2739-Sum of Consecutive Prime Numbers【尺取法】
- POJ2739:Sum of Consecutive Prime Numbers(简单数论)
- spring四种依赖注入方式
- windows程序设计——图形基础
- Oracle存在修改,不存在插入记录
- Base64/32/16编码
- Java与C++编译过程比较
- POJ2739-Sum of Consecutive Prime Numbers
- 续流的理解
- BZOJ 3158: 千钧一发 最小割
- 文章标题 我的coding之路开始啦
- springmvc实现异步上传文件
- maven下载jar非常慢的解决方法
- 【个人笔记重点,不作为参考】主题:webstorm激活破解
- log 输出日志
- android sudio 如何获取sha1与md5值