Sum of Consecutive Prime Numbers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22019 Accepted: 12051

Description

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime 
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

The 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

The 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

思路:先筛选0到10000的素数,尺取跑一遍。

 1 import java.util.*; 2 import java.lang.*; 3 import java.io.*; 4  5 public class Main { 6     public static void main(final String[] args) { 7         Scanner in = new Scanner(System.in); 8         int n, i, j, k, p, q; 9         int aa[] = new int[20000];10         int bb[] = new int[20000];11         Arrays.fill(aa, 0);12         aa[1] = 1;13         aa[0] = 1;14         int cnt = 0;15         for (i = 2; i <= 100; i++) {16             if (aa[i] == 0) {17                 for (j = i; i * j <= 10000; j++) {18                     aa[i * j] = 1;19                 }20             }21 22         }23         for (i = 0; i <= 10000; i++) {24             if (aa[i] == 0) {25                 bb[cnt++] = i;26             }27         }28         int ss = 1;29         while (ss == 1) {30             k = in.nextInt();31             int ans = 0;32             if (k == 0) {33                 break;34             } else {35                 int sum = 0;36                 int ll = 0;37                 int rr = 0;38                 while (ss == 1) {39                     if (rr > cnt) {40                         break;41                     }42                     if (bb[rr] > k) {43                         break;44                     }45                     if (ll > rr) {46                         break;47                     }48                     while (ss == 1) {49                         sum += bb[rr];50                         if (sum >= k) {51                             break;52                         } else {53                             rr++;54                         }55                         if(rr>cnt)56                         {break;}57                     }58                     if (sum == k) {59                         ans++;60                     }61                     sum -= bb[ll];62                     sum -= bb[rr];63                     ll++;64                 }65             }66             System.out.println(ans);67         }return ;68     }69 }

Sum of Consecutive Prime Numbers(poj2739)的更多相关文章

  1. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  2. POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25225 ...

  3. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  4. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  5. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers http://poj.org/problem?id=2739 Time Limit: 1000MS   Memory Limit: 6 ...

  6. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  7. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  8. poj 2379 Sum of Consecutive Prime Numbers

                                                                                                        ...

  9. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895 ...

随机推荐

  1. Linux内存管理和寻址详解

    1.概念 内存管理模式 段式:内存分为了多段,每段都是连续的内存,不同的段对应不用的用途.每个段的大小都不是统一的,会导致内存碎片和内存交换效率低的问题. 页式:内存划分为多个内存页进行管理,如在 L ...

  2. words in English that contradict themselves

    [S1E10, TBBT]Leonard: I don't get it. I already told her a lie. Why would I replace it with a differ ...

  3. Flink(五) 【消费kafka】

    目录 0.目的 1.本地测试 2.线上测试 提交作业 0.目的 测试flink消费kafka的几种消费策略 kafkaSource.setStartFromEarliest() //从起始位置 kaf ...

  4. Hadoop【Hadoop-HA搭建(HDFS、YARN)】

    目录 0.HDFS-HA的工作机制 1. HDFS-HA集群配置 1.1 环境准备 1.2 规划集群 1.3 配置Zookeeper集群 2. 配置HDFS-HA集群 3. 启动HDFS-HA集群 4 ...

  5. Linux学习 - 挂载命令

    一.mount 1 功能 将外设手工挂载到目标挂载点 2 语法 mount  [-t 文件系统]  [设备文件名]  [挂载点] 3 范例 mkdir  /mnt/cdrom 在/mnt下创建一个cd ...

  6. 【Linux】【Basis】文件

    refer to: https://en.wikipedia.org/wiki/POSIX refer to: https://en.wikipedia.org/wiki/Unix_file_type ...

  7. 会话-cookie

    package com.hopetesting.cookie;import javax.servlet.ServletException;import javax.servlet.annotation ...

  8. 面向切面编程(Spring AOP)

    一.什么是AOP AOP即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.主要体现在日志记录.性能统计.安全控制.事务处理和异常处理等. 1.相关概念 二.切面.切入点配 ...

  9. 【C/C++】例题5-4 反片语/算法竞赛入门经典/C++与STL入门/映射:map

    本题是映射:map的例题. map:键值对. [题目] 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词. 在判断是否满足条件时,字母不分大小写,但在输出 ...

  10. C#中继承和多态

    1.继承的概念 继承是使用已存在的类的定义作为基础建立新类的技术,新类的定义可以增加新的数据或新的功能,也可以用已存在的类的功能. 为了提高软件模块的可复用性和可扩充性,以便提高软件的开发效率,我们总 ...