题意:选择k个质数,使它们的和等于n,问有多少种方案。
分析:dp[i][j],选择j个质数,使它们的和等于i的方法数。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b) { if(fabs(a - b) < eps) return 0; return a < b ? -1 : 1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1120 + 10; const int MAXT = 10000 + 10; using namespace std; int dp[MAXN][20]; int vis[MAXN]; void init(){ vis[0] = vis[1] = 1; for(int i = 2; i <= sqrt(MAXN + 0.5); ++i){ if(!vis[i]){ for(int j = i * i; j < MAXN; j += i){ vis[j] = 1; } } } dp[0][0] = 1; for(int i = 0; i < MAXN; ++i){ if(vis[i]) continue; for(int j = 14; j >= 1; --j){ for(int k = MAXN - 1; k >= i; --k){ dp[k][j] += dp[k - i][j - 1]; } } } } int main(){ init(); int n, k; while(scanf("%d%d", &n, &k) == 2){ if(!n && !k) return 0; printf("%d\n", dp[n][k]); } return 0; }
UVA - 1213 Sum of Different Primes (不同素数之和)(dp)
原文:http://www.cnblogs.com/tyty-Somnuspoppy/p/6390773.html