POJ3132 Sum of Different Primes

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3473   Accepted: 2154

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

Source

Japan 2006
 

用素数筛打一个素数表出来,然后在素数表上背包动规。

 1 /*by SilverN*/
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 using namespace std;
 8 int read(){
 9     int x=0,f=1;char ch=getchar();
10     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
11     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
12     return x*f;
13 }
14 const int mxn=5000;
15 int pri[mxn],cnt=0;
16 int vis[mxn];
17 int n,k;
18 int f[mxn][30];
19 void Pri(){
20     int i,j;
21     for(i=2;i<=n;i++){
22         if(!vis[i]){
23             pri[++cnt]=i;
24         }
25         for(j=1;j<=cnt && i*pri[j]<=n;j++){
26             vis[i*pri[j]]=1;
27             if(i%pri[j]==0)break;
28         }
29     }
30     return;
31 }
32 int main(){
33     n=1250;
34     Pri();
35     int i,j;
36     f[0][0]=1;
37     for(i=1;i<=cnt;i++){
38         for(j=n;j>=pri[i];j--){
39             for(int k=1;k<=15;k++)
40                 f[j][k]+=f[j-pri[i]][k-1];
41         }
42     }
43     while(scanf("%d%d",&n,&k) && n){
44         printf("%d\n",f[n][k]);
45     }
46     return 0;
47 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5892553.html

你可能感兴趣的:(POJ3132 Sum of Different Primes)