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 integers n and k, you should count the number of ways to express n as a sum of k different 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

思路:prim[]为素数表;   f[i][j]为j拆分成i个素数和的方案数(1<=i&&i<=14,prim[i]<=j&&j<=1199) 边界f[0][0]=1;   int num 为prim[]的表长;   使用DP计算k个不同素数的和为n的方案总数:      枚举prim[]中的prim[i](0<=i&&i<=num);        按递减顺序枚举素数的个数j(14>=j&&j>=1);           递减枚举前j个素数的和p(1199>=p&&p>=prim[i]);              累计prim[i]作为第j个素数的方案总数f[j][p]+=f[j-1][p-prim[i]];         f[k][n]即为解!!!!!!
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<iomanip>
 7 #include<cmath>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 using namespace std;
12 #define PI 3.141592653589792128462643383279502
13 #define N 1200
14 int prim[N]={2,3},f[15][N],t;
15 int prime(){
16     int t=2,i,j,flag;
17     for(i=5;i<N;i+=2){
18         for(j=0,flag=1;prim[j]*prim[j]<=i;j++)
19             if(i%prim[j]==0) flag=0;
20         if(flag){
21             prim[t++]=i;
22         }
23     }
24     return t-1;
25 }
26 void s(){
27      for(int i=0;i<=t;i++){
28                 for(int j=14;j>=1;j--){
29                     for(int p=1199;p>=prim[i];p--)
30                         f[j][p]+=f[j-1][p-prim[i]];
31                 }
32             }
33 }
34 int main(){
35     //#ifdef CDZSC_June
36     //freopen("in.txt","r",stdin);
37     //#endif
38     //std::ios::sync_with_stdio(false);
39     t=prime();
40     int k,n;
41     while(scanf("%d%d",&n,&k)){
42         memset(f,0,sizeof(f));
43         f[0][0]=1;
44         if(k==0&&n==0) break;
45         s();
46          cout<<f[k][n]<<endl;
47     }
48     return 0;
49 }
时间: 2024-10-05 22:47:52

poj 3132的相关文章

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,

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

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

{POJ}{动态规划}

动态规划与贪心相关: {POJ}{2479}{Maximum Sum} (DP) 摘要: 题意:给定n个数,求两段连续子列的最大和.思路:先从左向右dp,求出一段连续子列的最大和,再从右向左dp,求出两段连续子列的最大和,方法还是挺经典的. {POJ}{1036}{Gansters} (DP) 摘要: 题意:有个伸缩门,门的宽度0~K,每个时间可以伸长或缩短1个单位,有N个流氓,他们在T时刻到达,如果这时门的宽度正好与他们的stoutness相等时,便可获得一定的收入,问最大的收入是多少. 思路

优质题表(机密版)

转载请注明出处:http://www.cnblogs.com/dashuzhilin/p/4556803.html 思维题: poj 1528 poj 1597 poj 2538 poj 2608 poj 2612 poj 2361 poj 2339 poj 2664 uva 10894 uva 10921   uva 10922   uva 10929 uva 10931   uva 10800   uva 10878 uva 10976   uva 10323   uva 201 poj 2

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is