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
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAARABIDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAMFBwT/xAAlEAACAQQCAQMFAAAAAAAAAAABAgMABAURBiESIjFBMjZxdbP/xAAYAQACAwAAAAAAAAAAAAAAAAAAAwEEBf/EABsRAQEAAgMBAAAAAAAAAAAAAAEAAgMEEyFh/9oADAMBAAIRAxEAPwDQeRW+SyVnctBIkiiScOk87qm0ciP0aZWA8dkEDZA2fcGPCWPI+PXkUt3GIcQjkyQxTGdtMrAhUVQO5CraVd/UB1pa7cnHmbaW5hjxEktoZJJGulnjChWYsT4lvLoHvr3B1vommvuQYaSe/jGSxrW9yXEiCWIiTe9eWohvs/LH8n5ocDh9jlnsER+zt+9wDE9G0uKWO4hSaGRJIpFDI6MCrKewQR7ilVfFPs7B/r4P5rStB8ZJW9KUqIlKUoi//9k=" alt="" /> Copy sample input to clipboard 
2
3
17
41
20
666
12
53
0
Sample Output
1
1
2
3
0
0
1
2
分析:首先根据题目要求是给定一个数,找到能够满足如下条件的序列个数:
序列所有元素是连续的素数
序列元素之和为给定数
首先我们已知数值范围是 ~,那么先找到 ~10000里面的所有素数,而不应该在每输入一个数都重新找一次。
还有一个是素数是要连续的,这样在寻找的时候就减少了很多搜索。对于每一个数都只需要判断它的下一个。 算法思想如下:
从小于给定数的最大素数出发,利用深搜,剪枝依据是每个元素的子节点只能是其素数列表的前一个元素,比如给定 ,那么第一个素数为7
>
==
终止
终止
在深搜的时候,判断当前素数之和是不是小于给定数,如果不是,就不需要继续考虑其子节点了。
#include <iostream>
#include <stack>
#include <cstring>
#include <map> using namespace std; void getPrimerList(map<int, int> &primerList, int num) { // 找到给定范围的所有素数
int *array = new int[num];
memset(array, , sizeof(array) * num);
int k = ;
for (int i = ; i < num; ++i) {
if (array[i] == ) {
primerList[k++] = i;
for (int j = * i; j < num; j += i) {
array[j] = ;
}
}
}
} struct Node {
Node(int v = , int s = ): value(v), sum(s) { }
int value;
int sum;
}; int main(int argc, char const *argv[])
{
map<int, int> primerList;
getPrimerList(primerList, );
int number;
while (cin >> number && number != ) {
int factorListNum = ;
if (number >= ) {
int numT;
map<int, int>::iterator iter = primerList.end();
--iter;
for (; iter != primerList.begin(); --iter) {
if (iter->second <= number) {
numT = iter->second;
break;
}
}
if (iter == primerList.begin())
numT = iter->second;
stack<Node> s;
// 从小于等于给定数的第一个素数开始,以每个素数为起点向前查找
// 也就是说,这里相当于深搜多棵树
for (; iter != primerList.begin(); --iter) {
s.push(Node(iter->first, iter->second));
}
s.push(Node(iter->first, iter->second));
while (!s.empty()) {
Node curent = s.top();
s.pop();
if (curent.sum == number) {
factorListNum++;
} else if (curent.sum < number){
if ((curent.value - ) >= ) {
s.push(Node(curent.value - , primerList[curent.value - ] + curent.sum));
}
} // else 其他情况下就不再需要继续往下子节点遍历
}
}
cout << factorListNum << endl;
}
return ;
}

sicily 1259. Sum of Consecutive Primes的更多相关文章

  1. POJ 2739. Sum of Consecutive Prime Numbers

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

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

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

  3. ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法

    POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Fo ...

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

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

  5. 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 ...

  6. Sum of Consecutive Prime Numbers

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

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

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

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

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

  9. poj 2379 Sum of Consecutive Prime Numbers

                                                                                                        ...

随机推荐

  1. 树状数组模板(pascal) 洛谷P3374 【模板】树状数组1

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  2. [您有新的未分配科技点]数位dp:从懵X到板子(例题:HDU2089 不要62)

    数位dp主要用来处理一系列需要数数的问题,一般套路为“求[l,r]区间内满足要求的数/数位的个数” 要求五花八门……比如“不出现某个数字序列”,“某种数的出现次数”等等…… 面对这种数数题,暴力的想法 ...

  3. Qt Creator中的3D绘图及动画教程(参照NeHe)

    Qt Creator中的3D绘图及动画教程(参照NeHe) http://blog.csdn.net/cly116/article/details/47184729 刚刚学习了Qt Creator,发 ...

  4. [COCI2011-2012#5] POPLOCAVANJE 后缀自动机

    题面:洛谷 题解: 其实还可以用AC自动机做,但是没调出来,,,不知道发生了什么... AC自动机做法如下: 观察到如果我们对给定的每个串建AC自动机,那么直接拿大串在上面匹配,如果遇到了一个单词的终 ...

  5. 【BZOJ4596】黑暗前的幻想乡(矩阵树定理,容斥)

    [BZOJ4596]黑暗前的幻想乡(矩阵树定理,容斥) 题面 BZOJ 有\(n\)个点,要求连出一棵生成树, 指定了一些边可以染成某种颜色,一共\(n-1\)种颜色, 求所有颜色都出现过的生成树方案 ...

  6. 【BZOJ 1129】[POI2008]Per 二叉堆

    这个东西读完题之后,就能知道我们要逐位计算贡献.推一下式子,会发现,这一位的贡献,是当前剩余的数字形成的序列的总数,乘上所剩数字中小于s上这一位的数的个数与所剩数字的总数的比.所以我们维护“当前剩余的 ...

  7. Hdu5693 D Game

    D Game Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  8. 洛谷P1195 口袋的天空

    口袋的天空 327通过 749提交 题目提供者该用户不存在 标签云端 难度普及+/提高 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 暂时没有讨论 题目背景 小杉坐在教室里,透 ...

  9. hihoCoder #1582 : Territorial Dispute 凸包

    #1582 : Territorial Dispute 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In 2333, the C++ Empire and the Ja ...

  10. apk签名验证机制

    声明: 1.本帖转载自:http://riusksk.blogbus.com/logs/272154406.html,仅供自用,勿喷 2.欢迎交流学习 签名后的APK,在/META-INF目录下会生成 ...