hdu 2739(尺取法)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22876 | Accepted: 12509 |
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 题意:给你一个数,询问有多少个连续质数序列和等于该数例如53=5 + 7 + 11 + 13 + 17
题解:筛选出所有质数,然后利用尺取法在O(n)的时间复杂度里面找到所有满足条件的子序列。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <map>
using namespace std;
typedef long long LL;
const int N = ;
bool p[N];
int a[];
void init(){
int id = ;
for(int i=;i<N;i++){
if(!p[i]){
a[id++] = i;
for(int j=i*i;j<N;j+=i){
p[j] = true;
}
}
}
}
int bin(int x){
int l = ,r=;
while(l<=r){
int mid = (l+r)>>;
if(a[mid]==x) return mid;
else if(a[mid]>x) r = mid-;
else l = mid+;
}
return l;
}
int main()
{
init();
int n;
while(scanf("%d",&n),n){
int l=,r=,sum=,cnt=;
int len = bin(n);
while(l<=len){
while(r<=len&&sum<n){
sum+=a[r++];
}
if(sum<n) break;
if(sum==n) cnt++;
sum-=a[l];
l++;
}
printf("%d\n",cnt);
}
return ;
}
hdu 2739(尺取法)的更多相关文章
- HDU 5358 尺取法+枚举
题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...
- HDU 6205(尺取法)2017 ACM/ICPC Asia Regional Shenyang Online
题目链接 emmmm...思路是群里群巨聊天讲这题是用尺取法.....emmm然后就没难度了,不过时间上3000多,有点.....盗了个低配本的读入挂发现就降到2800左右, 翻了下,发现神犇Clar ...
- hdu 5056(尺取法思路题)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 5510 Bazinga KMP+尺取法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:至多50组数据,每组数据至多500个字符串,每个字符串的长度最长为2000.问最大的下标( ...
- HDU 6103 Kirinriki(尺取法)
http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...
- HDU 6119 小小粉丝度度熊 【预处理+尺取法】(2017"百度之星"程序设计大赛 - 初赛(B))
小小粉丝度度熊 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 5672 String【尺取法】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5672 题意: 有一个10≤长度≤1,000,000的字符串,仅由小写字母构成.求有多少个子串,包含有 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- HDU 4123 (2011 Asia FZU contest)(树形DP + 维护最长子序列)(bfs + 尺取法)
题意:告诉一张带权图,不存在环,存下每个点能够到的最大的距离,就是一个长度为n的序列,然后求出最大值-最小值不大于Q的最长子序列的长度. 做法1:两步,第一步是根据图计算出这个序列,大姐头用了树形DP ...
随机推荐
- Diycode开源项目 ImageActivity分析
1.首先看一下效果 1.1做成了一个GIF 1.2.我用格式工厂有点问题,大小无法调到手机这样的大小,目前还没有解决方案. 1.3.网上有免费的MP4->GIF,参考一下这个网站吧. 1.4.讲 ...
- 1250 Fibonacci数列(矩阵乘法快速幂)
1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 定义:f0=f1=1, f ...
- Django基于Pycharm开发之三[LANGUAGE_CODE与TIME_ZONE]
在django/conf/global_settings.py 中,我们可以找到关于language和timezone的通用配置信息,源码如下: # Local time zone for this ...
- windows下虚拟环境中配置MySQL-python错误问题
下载mysql 下载mysql-python 这两步基本没有问题怪就怪的 MySQL-python-1.2.3.win-amd64-py2.7 文件只能安装到python27 路径下 然后在虚拟环境 ...
- loj2065 「SDOI2016」模式字符串
ref不是太懂 #include <iostream> #include <cstring> #include <cstdio> using namespace s ...
- loj2308 「APIO2017」商旅
ref #include <iostream> #include <cstring> #include <cstdio> #include <queue> ...
- 微信公众开发api接口
简介 微信公众平台消息接口为开发者提供了一种新的消息处理方式.微信公众平台消息接口为开发者提供与用户进行消息交互的能力.对于成功接入消息接口的微信公众账号,当用户发消息给公众号,微信公众平台服务器 ...
- Monkey log分析说明
运行命令: adb shell monkey -p com.crazyhornets.MyHokageAndroidZSY -v -v -v 20 -- throttle 1000 Log: :Mon ...
- Summary—【base】(JavaScript)
1.认识Js 1.1 Js就是一门运行在客户端浏览器的脚本编程语言 1.2 组成 ECMAScript:Js的语法标准 DOM:JS操作网页 ...
- C#,一种简单的方式实现滚动鼠标缩放图片,平移
1.缩放 private void ImageShow_Load(object sender, EventArgs e) { pictureBox1.Load(@"E:\SQ1.jpg&qu ...