题目链接:

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. echo---打印变量或输出字符串

    cho命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下e ...

  2. Duboo入门示例(Idea开发环境)

    在学习Dubbo分布式框架时的官方入门例子,很有代表性.简单清晰. 有关Dubbo的概念.概述和简单的配置文件,可以看官方文档的简述 会很快对Duboo有个整体的概念. 准备工作: 下载示例,点击这里 ...

  3. Xcode 6 打包ipa文件

    随着Xcode6.1的普遍应用.随之而来的非常多问题就会出现.这里来说一下怎样在Xcode6.1上生成Ad-hoc ipa.首先是要在你的开发人员账号上生成一个.ipa的主要应用就是在你公布到AppS ...

  4. FragMent-通过Arguments方法 跟activity通信

    今天主要学习下通过Arguments,实现activity 给fragment传递数据.这个方法也是通过参数bundle来进行数据传输的 直接看如下代码 一,定义一个fragment,在oncreat ...

  5. MFC中对话框的各种消息触发时间

    小结:WM_CREATE是所有窗口都能响应的消息,表明本窗口已经创建完毕.可以安全的使用这个窗口了,例如在它上面画控件等等.这个状态肯定是在调用ShowWindows()显示窗口之前.WM_WM_IN ...

  6. CISP/CISA 每日一题 20

    CISSP 每日一题(答) What methods can be used to protectmobile devices such as a smartphone? Encryption,GPS ...

  7. JavaScript学习总结(5)——Javascript面向(基于)对象编程

    一.澄清概念 1.JS中"基于对象=面向对象" 2.JS中没有类(Class),但是它取了一个新的名字叫"原型对象",因此"类=原型对象" ...

  8. 16、cgminer学习之:pthread_mutex_init和pthread_cond_init

    1.原理 假设有两个线程同时访问一个全局变量 n,这个全局变量的初始值等于0. Int  n = 0 ; 消费者线程 A 进入临界区,访问 n,A 必须等到 n 大于 0 才能接着往下执行,如果 n= ...

  9. 将Maven的Java Project转换或修改为Web Project

    将Maven的Java  Project转换为Web Project关键是需要了解Eclipse和MyEclipse的工程中如下文件.classpath..project. .mymetadata和s ...

  10. nginx+tomcat 架构 HttpServletRequest.getScheme()获取正确的协议

    http://blog.csdn.net/ofofw/article/details/46791447