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. java 解决时间相减问题

    比如 Date currentTime ="2011-06-20 9:23:50"; Date endTime="2011-06-21 10:33:56"; 要 ...

  2. UICollection无法下拉刷新的问题

    当UICollectonView加载的内容不够多的时候会出现无法上下拉刷新的问题,折腾了半天,原来是有一个属性没有打开 设置 : self.collectionView.alwaysBounceVer ...

  3. 测试class

    各种断言方法: assertEqual(a,b) a == b assertNotEqual(a,b) a != b assertTrue(x) x == True assertFalse(x) x ...

  4. Html_Task4(知识点:水平居中+垂直居中/position/float/border-radius)

    任务四:定位和居中问题 任务目标 实践HTML/CSS布局方式 深入了解position等CSS属性 任务描述 实现如 示例图(点击打开) 的效果 灰色元素水平垂直居中,有两个四分之一圆位于其左上角和 ...

  5. js实现超出一定字数隐藏并用省略号"..."代替,点击后又可进行展开和收起,

    原理简单阐述:放两个一模一样的div,把你要展示的文字放进去.页面初始化的时候,第一个div展示,第二个 div隐藏,就是这么简单.(ps:可以直接复制代码到你自己项目中,查看效果) 样式部分(记得引 ...

  6. C#中的委托(一)

    一.委托 把方法作为参数传给其他方法: 二.声明委托 在C#中使用一个类时,分两个阶段.首先,需要去定义一个类,然后实例化类的一个对象(只需要静态方法除外). 使用委托也需要经过这2个步骤,首先必须定 ...

  7. sqlserver删除重复的数据

    分享链接: http://blog.csdn.net/s630730701/article/details/52033018 http://blog.csdn.net/anya/article/det ...

  8. APUE-文件和目录(六)函数ftw和nftw

    名字 ftw,nftw - 文件树遍历 概要 #include <ftw.h> int nftw(const char *dirpath, int (*fn) (const char *f ...

  9. winform中的 datagriview 字段自动填充长度

    在winfrom 的 datagridview 中 绑定字段 经常回在最后面空出一部分来,显得不美观, 现在教大家如何让它自适应宽度 public static void Autogrid(DataG ...

  10. Safari Private 模式下 localStorage 的问题

    现如今好多浏览器都有「隐身模式」,Safari 管这叫「Private Browing」,国内各种牌子的套壳浏览器叫「无痕浏览」.私以为从命名上来说,倒是国内更中文一些. 这种模式下浏览网页踏雪无痕, ...