#include <iostream> #include <vector> #include <set> #include <map> #include <numeric> #include <algorithm> //#include "brut.cpp" using namespace std; constexpr int MOD = 1000000007; int solution(std::vector<int> input) { int subsets_ok = 0; map<int, int> possible_sets_for_sum; possible_sets_for_sum[0] = 1; for(auto x: input) { vector<pair<int, int>> to_modify; auto it = possible_sets_for_sum.lower_bound(x-1); while (it != possible_sets_for_sum.end()) { // printf("(%d + %d) += %d\n", it->first, x, possible_sets_for_sum[it->first]); to_modify.emplace_back(make_pair(it->first + x, it->first)); it++; } reverse(to_modify.begin(), to_modify.end()); // printf("\n"); for (auto x: to_modify) { possible_sets_for_sum[x.first] += possible_sets_for_sum[x.second]; possible_sets_for_sum[x.first] %= MOD; } } int subsets = 0; for(auto y: possible_sets_for_sum) { subsets += y.second; // cout << "(" << y.first << ", " << y.second << ")" << endl; } // cout << subsets-1 << endl; return subsets-1; } using ull = unsigned long long; int possible_sets_for_sum[5000*5000+100]; int solution2(std::vector<int> input) { int subsets_ok = 0; std::set<int> keys; possible_sets_for_sum[0] = 1; keys.insert(0); auto keys_it = keys.begin(); for(auto x: input) { while (*keys_it < x-1) { keys_it++; if (keys_it == keys.end()) { break; } } if (keys_it == keys.end()) { break; } auto it = keys.end(); auto last = keys_it; while (it != last) { it--; // printf("[%d + %d] += [%d]\n", *it, x, *it); if (possible_sets_for_sum[*it + x] == 0) { keys.insert(*it+x); } possible_sets_for_sum[*it + x] += possible_sets_for_sum[*it]; possible_sets_for_sum[*it + x] %= MOD; } // printf("\n"); } int subsets = 0; for(auto y: keys) { subsets += possible_sets_for_sum[y]; subsets %= MOD; // cout << "(" << y.first << ", " << y.second << ")" << endl; } // cout << subsets-1 << endl; return subsets-1; } //int main() { // srand(time(0)); // // while(true) { // int length = 2 + (rand() % 10); // std::vector<int> task; // // task.push_back(1); // for (int i = 1; i < length; ++i) { // task.push_back(1 + rand() % 5); // } // // std::sort(task.begin(), task.end()); // // for (auto x: task) { // printf("%d, ", x); // } // printf("\n"); // // int a = solution(task); // int b = brut(task); // // cout << a << endl << endl; // if (a != b) { // exit(1); // } // } //} int main() { int n; cin >> n; std::vector<int> task; for(int i = 0; i < n; ++i) { int a; cin >> a; task.push_back(a); } std::sort(task.begin(), task.end()); cout << solution2(task) << endl; }
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include <iostream> #include <vector> #include <set> #include <map> #include <numeric> #include <algorithm> //#include "brut.cpp" using namespace std; constexpr int MOD = 1000000007; int solution(std::vector<int> input) { int subsets_ok = 0; map<int, int> possible_sets_for_sum; possible_sets_for_sum[0] = 1; for(auto x: input) { vector<pair<int, int>> to_modify; auto it = possible_sets_for_sum.lower_bound(x-1); while (it != possible_sets_for_sum.end()) { // printf("(%d + %d) += %d\n", it->first, x, possible_sets_for_sum[it->first]); to_modify.emplace_back(make_pair(it->first + x, it->first)); it++; } reverse(to_modify.begin(), to_modify.end()); // printf("\n"); for (auto x: to_modify) { possible_sets_for_sum[x.first] += possible_sets_for_sum[x.second]; possible_sets_for_sum[x.first] %= MOD; } } int subsets = 0; for(auto y: possible_sets_for_sum) { subsets += y.second; // cout << "(" << y.first << ", " << y.second << ")" << endl; } // cout << subsets-1 << endl; return subsets-1; } using ull = unsigned long long; int possible_sets_for_sum[5000*5000+100]; int solution2(std::vector<int> input) { int subsets_ok = 0; std::set<int> keys; possible_sets_for_sum[0] = 1; keys.insert(0); auto keys_it = keys.begin(); for(auto x: input) { while (*keys_it < x-1) { keys_it++; if (keys_it == keys.end()) { break; } } if (keys_it == keys.end()) { break; } auto it = keys.end(); auto last = keys_it; while (it != last) { it--; // printf("[%d + %d] += [%d]\n", *it, x, *it); if (possible_sets_for_sum[*it + x] == 0) { keys.insert(*it+x); } possible_sets_for_sum[*it + x] += possible_sets_for_sum[*it]; possible_sets_for_sum[*it + x] %= MOD; } // printf("\n"); } int subsets = 0; for(auto y: keys) { subsets += possible_sets_for_sum[y]; subsets %= MOD; // cout << "(" << y.first << ", " << y.second << ")" << endl; } // cout << subsets-1 << endl; return subsets-1; } //int main() { // srand(time(0)); // // while(true) { // int length = 2 + (rand() % 10); // std::vector<int> task; // // task.push_back(1); // for (int i = 1; i < length; ++i) { // task.push_back(1 + rand() % 5); // } // // std::sort(task.begin(), task.end()); // // for (auto x: task) { // printf("%d, ", x); // } // printf("\n"); // // int a = solution(task); // int b = brut(task); // // cout << a << endl << endl; // if (a != b) { // exit(1); // } // } //} int main() { int n; cin >> n; std::vector<int> task; for(int i = 0; i < n; ++i) { int a; cin >> a; task.push_back(a); } std::sort(task.begin(), task.end()); cout << solution2(task) << endl; } |