test instructions is probably: give n coins, face value of a_i, asked to make up X yuan which coins are indispensable. 1≤n≤200, 1≤x≤10^4
Direct enumeration, and then the 01 backpack. To keep the complexity from multiplying by N, we'll go from left to right and from right to left. This determines that a coin is O (X). Total time complexity O (NX)
-----------------------------------------------------------------------------
#include <bits/stdc++.h>using namespace std;const int MAXN = 209;const int MAXV = 10009; bool L[MAXN][MAXV], R[MAXN][MAXV];int V[MAXN], N, V;vector<int> ans;int main () {memset (L, false, sizeof L);memset (R, False, sizeof R);cin >> N >> V;for (int i = 1; I <= N; i++)scanf ("%d", V + i);L[0][0] = r[n + 1][0] = true;for (int i = 1; I <= N; i++) {For (int j = 0; J <= V; j + +)L[i][j] = l[i-1][j];For (int j = V; J >= V[i]; j--)L[i][j] |= l[i-1][j-v[i];}for (int i = N; i; i--) {For (int j = 0; J <= V; j + +)R[i][j] = r[i + 1][j];For (int j = V; J >= V[i]; j--)R[i][j] |= r[i + 1][j-v[i];}for (int i = 1; I <= N; i++) {bool F = false;For (int j = 0; J <= V; j + +)if (L[i-1][j] && r[i + 1][v-j]) {F = true;Break ;}if (! F) Ans.push_back (V[i]);}printf ("%d\n", Ans.size ());for (int i = 0; i < ans.size (); i++)printf ("%d", ans[i]);return 0;}
-----------------------------------------------------------------------------
415. Necessary Coinstime limit per test:1.25 second (s)
Memory limit:262144 Kilobytesinput:standard
Output:standard
Vasya have been on vacation on Mars. He's a big fan of foreign coins, and thus has collected exactly one Martian coin of all denomination, for a total of
NCoins:
a
1Martian dollars,a2Martian dollars, etc,aNMartian dollars. Unfortunately, he couldn ' t stand ordering the Pan Galactic Gargle Blaster at the Starport, and have to pay for it-it cost SxMartian dollars. Vasya is wondering which of his coins be absolutely necessary to doing so (i.e., he's forced to abandon them). They don ' t offer change at the Starport Mars.
InputThe input file contains the integer numbersN andx(1≤N≤ 200, 1≤x≤104), followed byNdistinct integer numbersaI(1≤aI≤x).
OutputOn the first line of output, print the amount of denominations of coins. appear in any subset that sums toxMartian dollars. On the second line of output, print the denominations themselves, in any order, separated with single spaces. It is guaranteed this there exists at least one-to-payxMartian dollars with the given coins.
Example (s)
Sample input |
Sample output |
|
|
SGU 415. Necessary Coins (backpack DP)