题目链接:

POJ:

id=3132">http://poj.org/problem?id=3132

ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?

problemCode=2822

Description

A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integers n and k, you should count the number of ways to express n as a sum of kdifferent primes.
Here, two ways are considered to be the same if they sum up the same set of the primes. For example, 8 can be expressed as 3 + 5 and 5 + 3 but the are not distinguished.

When n and k are 24 and 3 respectively, the answer is two because there are two sets {2, 3, 19} and {2, 5, 17} whose sums are equal to 24. There are not other sets of three primes that sum up to 24. For n= 24 and k = 2,
the answer is three, because there are three sets {5, 19}, {7, 17} and {11, 13}. For n = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. For n = 1 and k = 1, the answer is zero. As 1 is
not a prime, you shouldn’t count {1}. For n = 4 and k = 2, the answer is zero, because there are no sets of two different primes whose sums are 4.

Your job is to write a program that reports the number of such ways for the given n and k.

Input

The input is a sequence of datasets followed by a line containing two zeros separated by a space. A dataset is a line containing two positive integers n and k separated by a space. You may assume that n ≤ 1120 and k ≤
14.

Output

The output should be composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of the ways for n and k specified in the corresponding dataset. You may assume
that it is less than 231.

Sample Input

24 3
24 2
2 1
1 1
4 2
18 3
17 1
17 3
17 4
100 5
1000 10
1120 14
0 0

Sample Output

2
3
1
0
0
2
1
0
1
55
200102899
2079324314

Source

题意:

给出n和k,求找出k个不同样的素数,他们的和为n。求这种组合有多少种。

PS:

刚看到这题没有什么思路! 暴力和搜索是肯定不行的。后来发现能够用类似背包的方法!

代码例如以下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 1700;
int dp[maxn][17];
int prime(int i)
{
int m = sqrt(i*1.0);
int j;
for(j = 2; j <= m; j++)
{
if(i%j == 0)
break;
}
if(j > m)
return 1;
return 0;
}
int p[maxn];
int l = 0;
void init()
{
for(int i = 2; i <= maxn; i++)
{
if(prime(i))
{
p[l++] = i;
}
}
}
int main()
{
int n, k;
init();
while(~scanf("%d%d",&n,&k))
{
if(n == 0 && k == 0)
break;
memset(dp,0,sizeof(dp));
dp[0][0] = 1;
for(int i = 0; i < l; i++)
{
for(int j = n; j >= p[i]; j--)
{
for(int x = k; x > 0; x--)
{
dp[j][x]+=dp[j-p[i]][x-1];
}
}
}
printf("%d\n",dp[n][k]);
}
return 0;
}

POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)的更多相关文章

  1. zoj 2822 Sum of Different Primes (01背包)

    ///给你n 求他能分解成多少个的不同的k个素数相加之和 ///01背包,素数打表 # include <stdio.h> # include <algorithm> # in ...

  2. UVa 1213 Sum of Different Primes (DP)

    题意:给定两个数 n 和 k,问你用 k 个不同的质数组成 n,有多少方法. 析:dp[i][j] 表示 n 由 j 个不同的质数组成,然后先打表素数,然后就easy了. 代码如下: #pragma ...

  3. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  4. poj 3132

    Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3360   Accepted ...

  5. POJ 3132 DP+素数筛

    Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3684   Accepted ...

  6. 【POJ 2750】 Potted Flower(线段树套dp)

    [POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   ...

  7. ZOJ 3802 Easy 2048 Again 状态DP

    zoj 上次的月赛题,相当牛的题目啊,根本想不到是状态压缩好吧 有个预先要知道的,即500个16相加那也是不会超过8192,即,合并最多合并到4096,只有2的12次方 所以用状态压缩表示前面有的序列 ...

  8. ZOJ 3723 (浙大月赛)状压DP

    A了一整天~~~终于搞掉了. 真是血都A出来了. 题目意思很清楚,肯定是状压DP. 我们可以联系一下POJ 1185  炮兵阵地,经典的状压DP. 两道题的区别就在于,这道题的攻击是可以被X挡住的,而 ...

  9. POJ 3017 Cut the Sequence (单调队列优化DP)

    题意: 给定含有n个元素的数列a,要求将其划分为若干个连续子序列,使得每个序列的元素之和小于等于m,问最小化所有序列中的最大元素之和为多少?(n<=105.例:n=8, m=17,8个数分别为2 ...

随机推荐

  1. [Redux-Observable && Unit Testing] Mocking an ajax request when testing epics

    Often in unit tests we are focussing on the logic involved in crafting a network request, & how ...

  2. Hibernate之关于多对多单向关联映射

    [Hibernate]之关于多对多单向关联映射 老师和学生,最典型的多对多关联, Teacher和Student.所谓单向意思就是说.老师知道自己的教的是哪些学生而学生不知道是哪些老师教. 也能够这么 ...

  3. vim-大小写装换

    以下内容参考自 http://blog.csdn.net/yangzhongxuan/article/details/8484167 自己验证了效果 命令一:单个字符转换(光标所在位置的字母进行转换) ...

  4. softInputMode- 软件盘监听事件

    软件盘的监听事件,如下 private final OnKeyListener mSubjectKeyListener = new OnKeyListener() { @Override public ...

  5. js插件---放大镜如何使用

    js插件---放大镜如何使用 一.总结 一句话总结:一张高清图片被用了两次,一次做缩略图,一次做放大后显示用的的图片(其实这个图片就是高清图片本身,而且是部分) 14 <figure class ...

  6. IAR for STM8介绍、下载、安装与注册--转

    Ⅰ.写在前面 本文讲述的内容是IAR for STM8的介绍.下载.安装与注册,其安装.注册过程和IAR for ARM类似,如果需要了解IAR for ARM相关的文章,可以到我博客,或微信公众号查 ...

  7. 2、TaskFactory类

    使用实例化的TaskFactory类,在其中把TaskMethod方法传递给StartNew()方法,就会立即启动任务. 1: TaskFactory tf = new TaskFactory(); ...

  8. Redis .Net客户端源码

    1.简单介绍 当前NoSql使用已经极为普遍,无论是Java生态圈,还是.net生态圈.大大小小的Web站点在追求高性能高可靠性方面,不由自主都选择了NoSQL技术作为优先考虑的方面.主流的技术有:H ...

  9. JQuery 当点击input后,单选多选的选中状态

    1.当点击input元素,此元素的选中checked的值 = 此元素此时表现的选中与否的状态. eg:input元素开始是未选中,点击后表现的是选中状态,此元素的checked为true(和此元素开始 ...

  10. css实现水波纹效果

    1. HTML 代码: <div class="example"> <div class="dot"></div> < ...