【問題描述】ios
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.express
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.less
Inputide
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.ui
Outputspa
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.code
Sample Inputorm
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; }
【額外補充】
邏輯上遞歸無次數限制,但語言執行器或者程序堆棧會限制遞歸的次數。