题目链接:

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. windows安装memcached

    http://www.cnblogs.com/wujuntian/p/4791220.html

  2. logout命令用于退出当前登录的Shell

    logout命令用于退出当前登录的Shell

  3. Android中级第十讲--相机录像和查看系统相册图片

    博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 录像比较简单,开始录制: myCamera.unlock(); ...

  4. 注意string的insert函数的几种形式

    string (1) string& insert (size_t pos, const string& str); substring (2) string& insert ...

  5. UVa 11094 - Continents

    题目:有一些岛屿在湖中.地图用两种字符表示.当前处在位置是一个岛屿.求除了当前岛屿外的最大岛屿. 分析:图论,floodfill.直接利用dfs求联通部分的面积就可以,然后取出最大. 说明:横线没有边 ...

  6. shape- 设置虚线

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  7. JS 原型模式创建对象

    例子: class Test { constructor(val) { this.val = val } walk() { console.log(this) console.log('walk') ...

  8. 理解宏的使用 extern

    如何定义一个全局变量在一个文件中,然后在其它文件中调用就行,而不需要多次extern外部声明. 由于之前的公司的程序中全局的变量使用得很多,在多个.C文件中会调用,不这样处理做的话就会多处进行exte ...

  9. 在C#中实现Word页眉页脚的全部功能

    页眉页脚经常使用于文章排版,在Word工具栏里.我们能够加入页眉,页脚,页码,日期和时间.图片等信息和内容.页眉/页脚有两个额外选项:首页不同,奇偶页不同.有时在不同的节(section)里插入不同的 ...

  10. Android中使用HttpClient实现HTTP通信效果

    HTTP通信,这一案例在操作的时候遇到N多种种问题,是前面看过几个实例里面最麻烦的一个.由于没有系统的接触过JAVA,所以出了非常多错误,也无从下手解决,这里经过对错误的检索实现了HTTP通信,以做记 ...