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 ...
随机推荐
- vue基础指令
- bzoj2929
题解: 网络流裸题 代码: #include<cstdio> #include<cstring> #include<algorithm> #include<c ...
- 重温HTML
1 <h1> </h1>标题标签 <p> </p>段落标签 <img src=“ ”>图片标签 2. <em>和<stro ...
- Flask-WTF表单
Web表单 Web 表单是 Web 应用程序的基本功能. 它是HTML页面中负责数据采集的部件.表单有三个部分组成:表单标签.表单域.表单按钮.表单允许用户输入数据,负责HTML页面数据采集,通过表单 ...
- core文件介绍
原文链接:http://team.eyou.com/?p=27 如有侵犯您的版权,请联系windeal12@qq.com linux下,产生core文件,和不产生core文件的条件: 当我们的程序崩溃 ...
- 自己定义的Excetpion继承哪个异常有什么讲究?[待解答]
try catch的地方需要用到一个自定义的DBException,如下: 于是我就自定义了一个DBException,继承Excetpion,以父类Exception构造器创建构造器: DBExce ...
- linux下c语言源码编译
一.源码编译过程 源码 ---> 预处理 ---> 编译 ---> 汇编 ---> 链接 --->执行 我们可以把它分为三部分来完成: ./configure ...
- 数据库连接池----Druid配置详解
什么是连接池? 数据库连接池出现的原因在数据库连接资源的低效管理,使用数据库连接池是基于设计模式中的资源池的概念,从而解决资源频繁是分配.释放所造成的问题. 数据库连接池的基本思想就是为数据库连接建立 ...
- 一段处理json的C#代码
服务器端: using Newtonsoft.Json; using Newtonsoft.Json.Linq; public ActionResult GetGatherData() { IList ...
- Win10 64bit下安装GPU版Tensorflow+Keras
Tensorflow和Keras都是支持Python接口的,所以本文中说的都是搭建一个Python的深度学习环境. Keras是对Tensorflow或者Theano的再次封装,也就是以Tensorf ...