POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)
描述
http://poj.org/problem?id=2739
多次询问,对于一个给定的n,求有多少组连续的素数,满足连续素数之和为n.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22737 | Accepted: 12432 |
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
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
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
分析
尺取法.
打个素数表,对于每一个n,尺取之.
换了种尺取法的写法= =.
#include<cstdio> const int maxn=;
int p,n;
int prime[maxn];
bool is_prime[maxn]; void get_prime()
{
for(int i=;i<maxn;i++) is_prime[i]=true;
is_prime[]=is_prime[]=false;
p=;
for(int i=;i<maxn;i++)
{
if(is_prime[i])
{
prime[++p]=i;
for(int j=*i;j<maxn;j+=i) is_prime[j]=false;
}
}
} void solve()
{
int l=,r=,sum=,ans=;
while(l<=p&&r<=p&&prime[l]<=n&&prime[r]<=n)
{
if(sum==n) { ans++; sum-=prime[l++]; }
else if(sum>n) sum-=prime[l++];
else sum+=prime[++r];
}
printf("%d\n",ans);
} void init()
{
get_prime();
while(scanf("%d",&n)==&&n!=) solve();
} int main()
{
freopen("sum.in","r",stdin);
freopen("sum.out","w",stdout);
init();
fclose(stdin);
fclose(stdout);
return ;
}
POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)的更多相关文章
- poj2739尺取法+素数筛
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...
- ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法
POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS Memory Limit:65536KB 64bit IO Fo ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- poj 2739 Sum of Consecutive Prime Numbers 尺取法
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
- 尺取法 poj 2566
尺取法:顾名思义就是像尺子一样一段一段去取,保存每次的选取区间的左右端点.然后一直推进 解决问题的思路: 先移动右端点 ,右端点推进的时候一般是加 然后推进左端点,左端点一般是减 poj 2566 题 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- poj_2739 尺取法
题目大意 给定一个数字N,N可能由1个或多个连续的素数求和得到,比如41 = 2+3+5+7+11+13, 41 = 11+13+17, 41 = 41.求出对于N,所有可能的组合形式. 题目分析 先 ...
- codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...
- POJ:3061-Subsequence(尺取法模板详解)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18795 Accepted: 8043 Descript ...
随机推荐
- SQL SERVER语句汇总
1.查询数据库中所有用户表名:用户表总数. select name from dbo.sysobjects where OBJECTPROPERTY(id,N'IsUserTable')=1 sele ...
- android中相关的图形类
Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一种存储对象比较好 ...
- std::map的操作:插入、修改、删除和遍历
using namespace std; std::map<int,int> m_map; 1.添加 for(int i=0;i<10;i++) { m_map.insert(mak ...
- c++ primer复习(三)
1 istream.ostream类型,cin.cout.cerr是istream或ostream类型的具体的对象,<<和>>是操纵符 getline函数的参数是istream ...
- 服务器卡死,重启报错: INFO: task blocked for more than 120 seconds
问题:服务器负载很高,但是CPU利用率不高.服务器经常夯住,网站打不开,SSH连接非常不稳定,输入命令夯住. 重启服务器报错: INFO: task blocked for more than 120 ...
- getDrawingRect,getHitRect,getLocalVisibleRect,getGlobalVisibleRect
本文主要大体讲下getHitRect().getDrawingRect().getLocalVisibleRect().getGlobalVisibleRect. getLocationOnScree ...
- top工具
top 显示进程所占系统资源 能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top命令打印出了很多信息,包括系统负载(loadaverage).进程数(Tasks).c ...
- Linux网络
netstat 查看网络状态 netstat: -r :显示路由表 -n: 以数字方式显示 -u: 显示UDP连接 -t :显示TCP连接 -l:显示监听状态的连接 -p: 显示监听指定套接字的进程的 ...
- jquery tab mouseover 特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SVG
目前SVG在国内的使用并不常见,并且关于svg的相关js库也不多,这里指出两款svg的库Snap.svg和svg.js,Snap.svg张鑫旭的博客上有关于他的使用APi http://www.zha ...