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 ...
随机推荐
- 数据结构-队列(Queue)
#include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 1 ...
- Find a path HDU - 5492 (dp)
Find a path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 水题:HDU1303-Doubles
Doubles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 面试常问 Java基础 冒泡排序
冒泡排序就是对一个数组进行排序. 用双层for循环就可以解决. 第一层,决定排序的次数. n个数,进行n-1次排序就行了. 第二层,把相邻的两个数比较,谁小,放到后面和下一个数字比较,谁小谁放到后面, ...
- laravel5.2总结--响应
1 基本响应 1.1 返回一个字符串,指定的字符串会被框架自动转换成 HTTP 响应. Route::get('/', function () { return 'Hello World'; }) ...
- Python框架之Django的相册组件
Python框架之Django的相册组件 恩,没错,又是Django,虽然学习笔记已经结贴,但是学习笔记里都是基础的,Django的东西不管怎么说还是很多的,要学习的东西自然不会仅仅用十几篇博文就能学 ...
- Python Unicode与中文处理
转自:http://blog.csdn.net/dao123mao/article/details/5396497 python中的unicode是让人很困惑.比较难以理解的问题,本文力求彻底解决这些 ...
- CSU-1982 小M的移动硬盘
CSU-1982 小M的移动硬盘 Description 最近小M买了一个移动硬盘来储存自己电脑里不常用的文件.但是他把这些文件一股脑丢进移动硬盘后,觉得这些文件似乎没有被很好地归类,这样以后找起来岂 ...
- Normal synchronous FIFO mode 和 Show-ahead synchronous FIFO mode
FIFO是先进先出,可以用fifo来处理跨时钟域的数据传输问题,用到的地方特别多,一定要搞会. 在学习调用fifo的IP核中发现有normal synchronous FIFO mode 和 Show ...
- c#中dynamic ExpandoObject的用法
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...