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.

大意是,一个数可能可以拆成几个连续质数之和。有多组数据(以0结束),求 该数有几种拆法。
#include<stdio.h>
#include<cmath>
using namespace std;
long i,j,a[1230],n;
bool p(long k)
{
  for (long i=2;i<=trunc(sqrt(k));i++)
    if (k%i==0) return false;
  return true;
}
int main()
{
  for (i=1;i<=1229;i++) a[i]=0;
  long cnt=1;a[cnt]=2;
  for (i=3;i<=10000;i++)
    if (p(i)){cnt++;a[cnt]=a[cnt-1]+i;}
  scanf("%ld",&n);
  while (n!=0)
  {
    long ans=0;
    for (i=1;i<=cnt;i++)
      for (j=i;j<=cnt;j++)
        {
          if (a[j]-a[i-1]==n) ans++;
          else if (a[j]-a[i-1]>n) break;
        }
    printf("%ld\n",ans);
    scanf("%ld",&n);
  }
  return 0;
} 

poj 2739 Sum of Consecutive Prime Numbers 小结的更多相关文章

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

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

  2. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  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. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

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

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

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

  7. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  8. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...

  9. poj 2739 Sum of Consecutive Prime Numbers 解题报告

    题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...

随机推荐

  1. OpenStack命令 创建网络和路由管理

    1.登陆用户 :tdy(前提条件创建了tdy用户) 编写登陆用户tdy用户 脚本文件  user-operc.sh user-operc.sh : 登陆用户tdy: $ source user-ope ...

  2. kali虚拟机安装提示安装系统步骤失败

    首先虚拟机不论是VM还是VirtualBox都可以直接安装kali镜像文件的,不过如果你采用虚拟机默认硬盘8G设置的话,到的系统安装步骤会出错无法继续,具体原因不明. 解决办法却很简单,将虚拟机的硬盘 ...

  3. npm 一条命令更换淘宝源

    一条命令更换淘宝源 npm config set registry https://registry.npm.taobao.org

  4. 互联网“剁手”新方向,VR全景购物忙——全景智慧城市常诚

    随着VR和AR技术的兴起,各行各业都在寻求VR+的对接方式,除了游戏和社交平台,另一大对VR有着浓厚兴趣的就是电商平台了,阿里.京东等电商巨头纷纷成立VR事业部,如何让亿万用户在VR中愉快的买买买,已 ...

  5. java中File类应用:遍历文件夹下所有文件

    练习: 要求指定文件夹下的所有文件,包括子文件夹下的文件 代码: package 遍历文件夹所有文件; import java.io.File; public class Test { public ...

  6. MySql俩种分页区别(注意)

    注意俩个分页的区别哦~ SELECT * FROM city LIMIT 2 OFFSET 1; 从第二条记录开始 取二条记录 如下: SELECT * FROM city LIMIT 3,2; 从第 ...

  7. 【原创】Ajax的用法总结

    一.什么是Ajax Ajax英文全称为“ Asynchr JavsScript and XML”(异步的JavaScript和XML),是一种创建 交互式网页的开发技术. 二.Ajax技术的核心 Aj ...

  8. 编写一个简单的Web Server

    编写一个简单的Web Server其实是轻而易举的.如果我们只是想托管一些HTML页面,我们可以这么实现: 在VS2013中创建一个C# 控制台程序 编写一个字符串扩展方法类,主要用于在URL中截取文 ...

  9. 懵懂oracle之存储过程2

    上篇<懵懂oracle之存储过程>已经给大家介绍了很多关于开发存储过程相关的基础知识,笔者尽最大的努力总结了所有接触到的关于存储过程的知识,分享给大家和大家一起学习进步.本篇文章既是完成上 ...

  10. 摘记:Web应用系统测试内容

    表示层: 内容测试,包括整体审美.字体.色彩.拼写.内容准确性和默认值 Web站点结构,包括无效的链接或图形 用户环境,包括Web浏览器版本和操作系统配置(每一个浏览器都有不同的脚本引擎或虚拟机在客户 ...