【问题描述】
A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integersn and k, you should count the number of ways to express n as a sum ofk 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. Forn = 24 and k = 2, the answer is three, because there are three sets {5, 19}, {7, 17} and {11, 13}. Forn = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. Forn = 1 and k = 1, the answer is zero. As 1 is not a prime, you shouldn’t count {1}. Forn = 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 givenn 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 integersn and k separated by a space. You may assume that n ≤ 1120 andk ≤ 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 forn 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
【解题思路】
特别提示:此题不能用递归做,用递归会超时。递归易理解但是耗时更长。
先离线计算出[2-1120]的素数,再穷举将结果存入以待分解数据和须分解的项数为下标的二维数组中,输入数据时查询对应值输出即可。
设su[]为[2...1200]的素数表;f[i][j]未拆分成i个素数和的方案数(1<=i<=14,su[i]<=j<=1199)。显然,边界值f[0][0]=1。
首先,采用筛选法计算素数表su[],表长为num。然后每输入一对n和k,使用动态规划方法计算k个不同素数和为n的方案总数:
枚举su[]表中的每个素数su[i](1<=i<=num);
按递减顺序枚举素数个数j(j=14..1);
按递减顺序枚举前j个素数的和p(p=1199...su[i]);
累计su[i]作为第j个素数的方案总数f[j][p]+=f[j-1][p-su[i]];
最后得出的f[k][n]即为问题解。
【具体实现】
错误:递归实现
#include<iostream>
/*保存输入数num的最大值*/
#define maxNum 1120+5
/*保存需要分解的项数times的最大值*/
#define maxK 14
using namespace std;
/*保存素数筛*/
int SIGN[maxNum];
/*保存结果*/
int N = 0;
/*离线求出素数筛*/
void PrimeSum(){
SIGN[0] = SIGN[1] = 1;
for (int i = 2; i < maxNum; ++i)
if (!SIGN[i])
for (int j = i * 2; j < maxNum; j += i)
SIGN[j] = 1;
}
/*递归调用求出可分解出的种数*/
void Fun(int num, int times, int up){
/*当times为1的时候,递归结束。分解种数+1*/
if (times == 1){
if (!SIGN[num] && num != up)
N++;
}
/*如果times不为1,则继续递归*/
else
for (int i = up + 1; i <= num / times; ++i)
if (!SIGN[i])
/*此处不能用--times,因为当times为1时,--times则为0,
递归结束回溯时,循环条件中的分母值为0,循环结束,异常*/
Fun(num - i, times - 1, i);
}
int main(){
PrimeSum();
int num, times;
while (cin >> num >> times && (num + times) && times <= maxK){
N = 0;
Fun(num, times, 1);
cout << N << endl;
}
return 0;
}
正确解法:
#include<iostream>
/*保存输入数num的最大值*/
#define maxNum 1120+5
using namespace std;
/*保存素数筛*/
int SIGN[maxNum];
/*保存素数集*/
int PRIM[maxNum];
/*保存待分解的数和分解的项数对应的结果*/
int DP[maxNum][50];
/*保存素数集的大小*/
int num = 0;
void PrimeSum(){
/*离线求出素数筛*/
SIGN[0] = SIGN[1] = 1;
for (int i = 2; i < maxNum; ++i)
if (!SIGN[i])
for (int j = i * 2; j < maxNum; j += i)
SIGN[j] = 1;
/*离线求出素数集*/
for (int k = 0;k<maxNum;++k)
if (!SIGN[k])
PRIM[num++] = k;
}
int main(){
/*输入前处理*/
PrimeSum();
/*将待处理数和需分解的项数对应值存入二维数组,
输入对应值时查询输出即可,可将数组写下来分析
即可明白原理*/
DP[0][0] = 1;
for (int i = 0; i<num; i++)
for (int j = maxNum; j >= PRIM[i]; j--)
for (int k = 15; k>0; k--)
DP[j][k] += DP[j - PRIM[i]][k - 1];
int sum, n;
/*输入数据,查询即可输出结果,保证较低的时间复杂度*/
while (cin>>sum>>n, sum + n)
cout << DP[sum][n] << endl;
return 0;
}
【额外补充】
逻辑上递归无次数限制,但语言执行器或者程序堆栈会限制递归的次数。