题目:n种大小不同的数字 ai,每种各mi个,判断是否可以从这些数字之中选出若干个使他们的和恰好为K。
动态规划
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
const double PI = acos(-1.0);
///
const int MAX_N = 104;
const int MAX_K = 100009;
int n;
int k;
int a[MAX_N];
int m[MAX_N];
int dp[MAX_N];
void solve() {
memset(dp, -1, sizeof(dp));
dp[0] = 0;
for (int i = 0; i<n; i++) {
for (int j = 0; j <= k; j++) {
if (dp[j] >= 0) {
dp[j] = m[i];
}
else if (j<a[i] || dp[j - a[i]] <= 0) {
dp[j] = -1;
}
else {
dp[j] = dp[j - a[i]] - 1;
}
}
}
if (dp[k] >= 0) cout << "Yes\n";
else cout << "No\n";
}
///
int main(int argc, char**argv) {
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("input.txt", "r", stdin);、
//freopen("output.txt", "w", stdout);
cin >> n;
cin >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> m[i];
}
solve();
//system("pause");
return 0;
}
//END