POJ2739(尺取法)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23931 | Accepted: 13044 |
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
Output
Sample Input
2
3
17
41
20
666
12
53
0
Sample Output
1
1
2
3
0
0
1
2
思路:尺取法操作连续子序列。
import java.util.Arrays;
import java.util.Scanner; public class Main {
Scanner in = new Scanner(System.in);
final int MAXN = 10005;
int[] prime = new int[MAXN];
boolean[] isPrime = new boolean[MAXN];
int[] sum = new int[MAXN];
int total;
void table() {
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for(int i = 2; i < MAXN; i++) {
if(isPrime[i]) {
prime[total++] = i;
for(int j = i + i; j < MAXN; j += i) {
isPrime[j] = false;
}
}
}
sum[0] = 0;
for(int i = 1; i < total; i++) {
sum[i] = sum[i-1] + prime[i-1];
}
}
Main() {
int n;
table();
while((n = in.nextInt()) != 0) {
int res = 0, sum = 0;
int front = 0, rear = 0;
while(true) {
while(rear < total && prime[rear] <= n && sum < n) {
sum += prime[rear++];
}
if(sum == n) {
res++;
}
sum -= prime[front++];
if(front >= total || front > rear) {
break;
}
}
System.out.println(res);
}
}
public static void main(String[] args) { new Main();
}
}
POJ2739(尺取法)的更多相关文章
- poj2739尺取法+素数筛
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- poj2739(尺取法+质数筛)
题意:给你一个数,问这个数能否等于一系列连续的质数的和: 解题思路:质数筛打出质数表:然后就是尺取法解决: 代码: #include<iostream> #include<algor ...
- POJ 尺取法
poj3061 Subsequence 题目链接: http://poj.org/problem?id=3061 挑战P146.题意:给定长度为n的数列整数a0,a1,...,a(n-1)以及整数S, ...
- 5806 NanoApe Loves Sequence Ⅱ(尺取法)
传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K ...
- POJ3061 尺取法
题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- CF 701C They Are Everywhere(尺取法)
题目链接: 传送门 They Are Everywhere time limit per test:2 second memory limit per test:256 megabytes D ...
- nyoj133_子序列_离散化_尺取法
子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 给定一个序列,请你求出该序列的一个连续的子序列,使原串中出现的所有元素皆在该子序列中出现过至少1次. 如2 8 ...
随机推荐
- Prism 4 文档 ---第6章 高级MVVM场景
在上一章中描述了如何通过将UI,表现逻辑,业务逻辑分别放到三个单独的类中(View,View Model,Model),实现这些类之间的交互(通过数据绑定,命令以及数据验证接口)以及实现一个策 ...
- mysql服务1067错误:修改mysql可执行文件路径
今天遇到mysql服务1067错误的问题,设置使用系统账户也无法启动mysql,后面认证看了系统的配置信息,发现启动文件也就是mysql安装路径是之前的(也说明之前安装mysql,没去卸载直接安装新的 ...
- is null 和=null的区别
数据库中 null 表示 不可知,不确定 所以 判断都用 字段 is null的方式进行判断 而 = null .<> null 的判断结果,仍然是不可知,不确定,所以 不会返回任何结果. ...
- (转)Mac os x 下配置Intellij IDEA + Tomcat 出现权限问题的解决办法
出现的错误提示如下: 下午9:11:27 All files are up-to-date下午9:11:27 All files are up-to-date下午9:11:27 Error runni ...
- [AirFlow]AirFlow使用指南二 DAG定义文件
1. Example """ Code that goes along with the Airflow tutorial located at: https://git ...
- flowable IdmEngine和IdmEngineConfiguration
IdmEngineConfiguration 继承了 AbstractEngineConfiguration. 一.创建EngineConfiguration实例 IdmEngineConfigura ...
- 手游服务端框架之使用Guava构建缓存系统
缓存的作用与应用场景 缓存,在项目中的应用非常之广泛.诸如这样的场景,某些对象计算或者获取的代码比较昂贵,并且在程序里你不止一次要用到这些对象,那么,你就应该使用缓存. 缓存跟java的Coucurr ...
- 关于HslCommunication组件的双模式客户端的说明,此说明适用于所有的派生类客户端,包括三菱,西门子,欧姆龙,modbustcp,机器人,simplifyNet客户端等等
前言 本文主要是答疑文章,针对广大网友非常频繁的提问而总结的问题 nuget地址:https://www.nuget.org/packages/HslCommunication/ ...
- 用stack实现min stack
遇到个好玩的问题,就是用一个stack实现min stack,什么意思呢,就是我实现stack,但是能以O(1)的时间复杂度和空间复杂度去找到我stack里面的最小值. 常规的方法是:用一个变量存放当 ...
- Java并发--ConcurrentModificationException(并发修改异常)异常原因和解决方法
在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常.下面我们就来讨论 ...