#include <bits/stdc++.h> #define ll long long const ll MOD = 1e9+7; const int MAX_N = 5e3; const int MAX_S = MAX_N+3; ll dp[MAX_S+3]; std::vector<int> v; int n; ll pot[MAX_N+3]; int main() { //std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin >> n; for (int i = 1; i <= n; i++) { int x; std::cin >> x; v.push_back(x); } std::sort(v.begin(), v.end()); pot[0] = 1; for (int i = 1; i <= n; i++) pot[i] = (pot[i-1] + pot[i-1]) % MOD; int max = v.back(); dp[0] = 1; ll res = 0; int left = n; ll good = 0; for (auto x: v) { // nowe beda na przedziale [x-1, max] good = 0; for (int j = max+1; j >= x-1; j--) { if (j + x >= max+1) good = (good + dp[j]) % MOD; else dp[j+x] = (dp[j+x] + dp[j]) % MOD; } left--; ll tmp = (good * pot[left])% MOD; res = (res + tmp) % MOD; //res = (res + ((good * pot[left])%MOD)) % MOD; } for (int i = 1; i <= MAX_S; i++) res = (res + dp[i]) % MOD; std::cout << res << "\n"; }
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 | #include <bits/stdc++.h> #define ll long long const ll MOD = 1e9+7; const int MAX_N = 5e3; const int MAX_S = MAX_N+3; ll dp[MAX_S+3]; std::vector<int> v; int n; ll pot[MAX_N+3]; int main() { //std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cin >> n; for (int i = 1; i <= n; i++) { int x; std::cin >> x; v.push_back(x); } std::sort(v.begin(), v.end()); pot[0] = 1; for (int i = 1; i <= n; i++) pot[i] = (pot[i-1] + pot[i-1]) % MOD; int max = v.back(); dp[0] = 1; ll res = 0; int left = n; ll good = 0; for (auto x: v) { // nowe beda na przedziale [x-1, max] good = 0; for (int j = max+1; j >= x-1; j--) { if (j + x >= max+1) good = (good + dp[j]) % MOD; else dp[j+x] = (dp[j+x] + dp[j]) % MOD; } left--; ll tmp = (good * pot[left])% MOD; res = (res + tmp) % MOD; //res = (res + ((good * pot[left])%MOD)) % MOD; } for (int i = 1; i <= MAX_S; i++) res = (res + dp[i]) % MOD; std::cout << res << "\n"; } |