Http://poj.org/problem? Id = 3132
Question:
Given N and K, the number of N schemes is represented by exactly k different prime numbers.
Analysis:
N and K are both small. It's a backpack. You just need to select K items to fill up 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 else38 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 }