POJ 3132 Sum of Different Primes DP背包

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

题意:

给定n和k,问用恰好k个不同的质数来表示n的方案数。

分析:

n和k都很小。反正就是个背包,选k个物品恰好填满n即可。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4
 5 bool dp[1200][15];
 6 int ct[1200][15];
 7 int p[1200];
 8 bool a[1200];
 9 int n, k, size;
10 void create_prime()
11 {
12     memset(a, true, sizeof(a));
13     size = 0;
14     for (int i = 2; i <= 1120; i++){
15         if (a[i]) p[size++] = i;
16         for (int j = 2*i; j <= 1120; j += i)
17             a[j] = false;
18     }
19 }
20 int main()
21 {
22     create_prime();
23     while(scanf("%d%d", &n, &k) && (n + k))
24     {
25         memset(dp, 0, sizeof(dp));
26         dp[0][0] = true;
27         ct[0][0] = 1;
28         for (int i = 0; i < size; i++){
29             int tmp = p[i];
30             if (tmp > n) break;
31             for (int j = n; j >= tmp; j--)
32                 for (int kk = 1; kk <= k; kk++) if (dp[j-tmp][kk-1]){
33                     if (!dp[j][kk]){
34                         dp[j][kk] = true;
35                         ct[j][kk] = ct[j-tmp][kk-1];
36                     }
37                     else
38                         ct[j][kk] += ct[j-tmp][kk-1];
39             }
40         }
41         if (!dp[n][k]) ct[n][k] = 0;
42         printf("%d\n", ct[n][k]);
43     }
44     return 0;
45 }

POJ 3132 Sum of Different Primes DP背包,布布扣,bubuko.com

时间: 2024-12-26 10:36:52

POJ 3132 Sum of Different Primes DP背包的相关文章

POJ 3132 Sum of Different Primes ( 满背包问题)

Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3280   Accepted: 2040 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integer

5 POJ 3132 Sum of Different Primes

dp[j][k]表示用j个质数表示k这个值得方法数. 一个质数只能用一次,类似01背包. #include<cstdio> #include<cstring> const int maxn=1130; int pri[maxn+10],dp[20][maxn+10]; void init() { int i,j; for(i=0;i<=maxn;i++) pri[i]=1; pri[0]=pri[1]=0; for(i=2;i<=maxn;i++) { if(!pri[

Poj 1112 Rebuilding Roads(树形DP+背包)

题意:给你由N个点构成一颗树,问要孤立出一个有P个节点的子树最少需要删除多少条边.N的范围最大为150 N的范围不大,很容易想到在树上面做背包.把每个节点都看成一个背包,然后把每个儿子节点都看成是一组物品.为什么是一组呢,那是因为假设以儿子为根的节点的子树有S个节点,那么就有S+1种情况,要么将这整棵子树舍弃,要么从这个子树中取1-S个节点. 设f[i][j]为以i为根节点的子树,孤立出以i为根节点,一共含有j个节点的子树最少需要删除的边数(不包括删除i和他父亲的连接的那条边(假设i不是根节点)

POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)

题目链接: POJ: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 int

zoj 2822 Sum of Different Primes (01背包)

///给你n 求他能分解成多少个的不同的k个素数相加之和 ///01背包,素数打表 # include <stdio.h> # include <algorithm> # include <string.h> # include <math.h> # include <iostream> using namespace std; int cot; int used[1500]; int prime[1500]; void sushu()///素数

UVA1213 Sum of Different Primes(素数打表+dp)

UVA - 1213 Sum of Different Primes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two

HDU 1011 Starship Troopers(树形dp+背包)

Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13109    Accepted Submission(s): 3562 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of

poj 3132

Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3360   Accepted: 2092 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integer

hdu1011 树形dp背包

http://acm.hdu.edu.cn/showproblem.php?pid=1011 Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with