#include <algorithm> #include <iostream> #include <vector> using namespace std; #define M 1000000007 #define A 5000 int n; vector<int> nums; int C[A][A]; int P[A + 1]; int foo(int idx, int cumsum) { int out; if (idx == n || nums[idx] > cumsum + 1) { return 0; } if (cumsum >= A - 1) { // every forward subset is ok so return 2^(n - idx) return (P[n - idx] - 1) % M; } if (C[idx][cumsum] != -1) { return C[idx][cumsum]; } out = 1; out += foo(idx + 1, cumsum); out += foo(idx + 1, cumsum + nums[idx]); out %= M; C[idx][cumsum] = out; return out; } int main() { int i, j; for (i = 0; i < A; ++i) { for (j = 0; j < A; ++j) { C[i][j] = -1; } } P[0] = 1; for (i = 1; i <= A; ++i) { P[i] = (P[i - 1] * 2) % M; } ios_base::sync_with_stdio(0); cin >> n; nums.reserve(n); for (i = 0; i < n; ++i) { int a; cin >> a; nums.push_back(a); } sort(nums.begin(), nums.end()); cout << foo(0, 0) << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <algorithm> #include <iostream> #include <vector> using namespace std; #define M 1000000007 #define A 5000 int n; vector<int> nums; int C[A][A]; int P[A + 1]; int foo(int idx, int cumsum) { int out; if (idx == n || nums[idx] > cumsum + 1) { return 0; } if (cumsum >= A - 1) { // every forward subset is ok so return 2^(n - idx) return (P[n - idx] - 1) % M; } if (C[idx][cumsum] != -1) { return C[idx][cumsum]; } out = 1; out += foo(idx + 1, cumsum); out += foo(idx + 1, cumsum + nums[idx]); out %= M; C[idx][cumsum] = out; return out; } int main() { int i, j; for (i = 0; i < A; ++i) { for (j = 0; j < A; ++j) { C[i][j] = -1; } } P[0] = 1; for (i = 1; i <= A; ++i) { P[i] = (P[i - 1] * 2) % M; } ios_base::sync_with_stdio(0); cin >> n; nums.reserve(n); for (i = 0; i < n; ++i) { int a; cin >> a; nums.push_back(a); } sort(nums.begin(), nums.end()); cout << foo(0, 0) << endl; return 0; } |