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. printf 的 转义词 -转

    \n    换行 \r    回车键 \b   退后一格 \f    换页 \t    水平制表符 \v   垂直制表符 \a   发出鸣响 \? 插入问号 \"    插入双引号 \'   ...

  2. Prometheus_exporter安装与使用

    Promethues概述:可以看一下更详细的介绍,以下为转载的博客,原文链接,支持原创,请多多支持!!:闫世成的博客园 Prometheus-node-exporter 1.简介: 内核公开的硬件和操 ...

  3. C/C++ Qt StandardItemModel 数据模型应用

    QStandardItemModel 是标准的以项数据为单位的基于M/V模型的一种标准数据管理方式,Model/View 是Qt中的一种数据编排结构,其中Model代表模型,View代表视图,视图是显 ...

  4. 【leetcode】1217. Minimum Cost to Move Chips to The Same Position

    We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to ...

  5. OC中的结构体

    一.结构体 结构体只能在定义的时候进行初始化 给结构体属性赋值    + 强制转换: 系统并不清楚是数组还是结构体,需要在值前面加上(结构体名称)    +定义一个新的结构体,进行直接赋值    + ...

  6. 软件测试人员必备的linux命令

    1 目录与文件操作1.1 ls(初级)使用权限:所有人功能 : 显示指定工作目录下之内容(列出目前工作目录所含之档案及子目录). 参数 : -a 显示所有档案及目录 (ls内定将档案名或目录名称开头为 ...

  7. vue中vuex的五个属性和基本用法

    VueX 是一个专门为 Vue.js 应用设计的状态管理构架,统一管理和维护各个vue组件的可变化状态(你可以理解成 vue 组件里的某些 data ). Vuex有五个核心概念: state, ge ...

  8. Initialization of data members

    In C++, class variables are initialized in the same order as they appear in the class declaration. C ...

  9. java输入/输出流的基本知识

    通过流可以读写文件,流是一组有序列的数据序列,以先进先出方式发送信息的通道. 输入/输出流抽象类有两种:InputStream/OutputStream字节输入流和Reader/Writer字符输入流 ...

  10. 粒子群算法-PSO

    粒子群优化算法 1. 背景知识 1995年美国社会心理学家Kennedy和电气工程师Eberhart共同提出粒子群优化算法(Particle Swarm Optimization, PSO).PSO算 ...