#include <iostream> #include <cmath> #include <vector> #include <map> #include <unordered_map> #include <algorithm> #include <cstdint> #include <cstdio> #include <cinttypes> using namespace std; map<vector<int>, bool> subsets; map<int, int> occur; vector<int> fact; int modulo = pow(10, 9) + 7; int sum(vector<int>& numbers) { int tot = 0; for (int num : numbers) { tot += num; } return tot; } void printVector(vector<int>& nums) { for (int num: nums) { cout << num << " "; } cout << endl; } bool calcSums(vector<int>& numbers, int add) { // cout << "[i] in calcSums "; // printVector(numbers); if (subsets.find(numbers) != subsets.end() && subsets[numbers]) { if (add <= sum(numbers) + 1) { // cout << "good vector: "; // printVector(numbers); return true; } else { // cout << "bad vector: "; // printVector(numbers); return false; } } subsets[numbers] = false; for (int i = 0; i < numbers.size(); ++i) { vector<int> newNumbers = numbers; newNumbers.erase(newNumbers.begin() + i); if (calcSums(newNumbers, numbers[i])) { subsets[numbers] = true; // cout << "it was good!!" << endl; } } return subsets[numbers]; } bool isSubsetValid(vector<int>& numbers) { //check if not already calculated if (subsets.find(numbers) != subsets.end()) { return subsets[numbers]; } return subsets[numbers]; } void calcSubsets(vector<int>& numbers) { int count = 0; uint64_t total; if (numbers.size() == 1) { if (numbers[0] == 1) { subsets[vector<int>{1}] = true; } return; } if (isSubsetValid(numbers)) { // cout << "already calculated "; // printVector(numbers); return; } for (int i = 0; i < numbers.size(); ++i) { vector<int> newNumbers = numbers; newNumbers.erase(newNumbers.begin() + i); calcSubsets(newNumbers); } //calculate if (calcSums(numbers, 0)) { subsets[numbers] = true; } else { subsets[numbers] = false; } return; } int calcBinomial(int n, int k) { // cout << n << " po " << k << endl; // cout << n << "! = " << fact[n] << endl; // cout << k << "! = " << fact[k] << endl; uint64_t bin; bin = (uint64_t) fact[n]; bin /= fact[k]; bin /= fact[n-k]; bin %= modulo; int res = bin; // cout << " = " << res << endl; return res; } int calcCombinations(map<int, int> localOccur) { uint64_t count = 1; for (auto it = localOccur.begin(); it != localOccur.end(); ++it) { count *= calcBinomial(occur[it->first], it->second); count %= modulo; } return count; } int findCombinations() { uint64_t total = 0; for (auto it = subsets.begin(); it != subsets.end(); ++it) { uint64_t cur = 0; if (it->second) { cur = 1; // cout << "good vector is "; map<int, int> localOccur; for (int num: it->first) { // cout << num << " "; if (localOccur.find(num) == localOccur.end()) { localOccur[num] = 0; } localOccur[num]++; } // cout << endl; cur = calcCombinations(localOccur); // cout << "in " << cur << "versions" << endl; total += cur; total %= modulo; } } return total; } void calcFactorials(int n) { fact.push_back(1); int i = 1; while (i <= n) { fact.push_back(fact[i-1]*i % modulo); // cout << i << "! = " << fact[i] << endl; i++; } } int main() { int n; cin >> n; vector<int> numbers; int maxOccur = 0; for (int i = 0; i < n; ++i) { int num; cin >> num; if (num == 1) { subsets[vector<int>{1}] = true; } numbers.push_back(num); if (occur.find(num) == occur.end()) { occur[num] = 0; } occur[num]++; if (occur[num] > maxOccur) { maxOccur = occur[num]; } } // cout << "max occur is " << maxOccur << endl; calcFactorials(maxOccur); //this will allow for easy access from the map, //as all subsets will be non-decreasing sort(numbers.begin(), numbers.end()); calcSubsets(numbers); cout << findCombinations() << 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 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | #include <iostream> #include <cmath> #include <vector> #include <map> #include <unordered_map> #include <algorithm> #include <cstdint> #include <cstdio> #include <cinttypes> using namespace std; map<vector<int>, bool> subsets; map<int, int> occur; vector<int> fact; int modulo = pow(10, 9) + 7; int sum(vector<int>& numbers) { int tot = 0; for (int num : numbers) { tot += num; } return tot; } void printVector(vector<int>& nums) { for (int num: nums) { cout << num << " "; } cout << endl; } bool calcSums(vector<int>& numbers, int add) { // cout << "[i] in calcSums "; // printVector(numbers); if (subsets.find(numbers) != subsets.end() && subsets[numbers]) { if (add <= sum(numbers) + 1) { // cout << "good vector: "; // printVector(numbers); return true; } else { // cout << "bad vector: "; // printVector(numbers); return false; } } subsets[numbers] = false; for (int i = 0; i < numbers.size(); ++i) { vector<int> newNumbers = numbers; newNumbers.erase(newNumbers.begin() + i); if (calcSums(newNumbers, numbers[i])) { subsets[numbers] = true; // cout << "it was good!!" << endl; } } return subsets[numbers]; } bool isSubsetValid(vector<int>& numbers) { //check if not already calculated if (subsets.find(numbers) != subsets.end()) { return subsets[numbers]; } return subsets[numbers]; } void calcSubsets(vector<int>& numbers) { int count = 0; uint64_t total; if (numbers.size() == 1) { if (numbers[0] == 1) { subsets[vector<int>{1}] = true; } return; } if (isSubsetValid(numbers)) { // cout << "already calculated "; // printVector(numbers); return; } for (int i = 0; i < numbers.size(); ++i) { vector<int> newNumbers = numbers; newNumbers.erase(newNumbers.begin() + i); calcSubsets(newNumbers); } //calculate if (calcSums(numbers, 0)) { subsets[numbers] = true; } else { subsets[numbers] = false; } return; } int calcBinomial(int n, int k) { // cout << n << " po " << k << endl; // cout << n << "! = " << fact[n] << endl; // cout << k << "! = " << fact[k] << endl; uint64_t bin; bin = (uint64_t) fact[n]; bin /= fact[k]; bin /= fact[n-k]; bin %= modulo; int res = bin; // cout << " = " << res << endl; return res; } int calcCombinations(map<int, int> localOccur) { uint64_t count = 1; for (auto it = localOccur.begin(); it != localOccur.end(); ++it) { count *= calcBinomial(occur[it->first], it->second); count %= modulo; } return count; } int findCombinations() { uint64_t total = 0; for (auto it = subsets.begin(); it != subsets.end(); ++it) { uint64_t cur = 0; if (it->second) { cur = 1; // cout << "good vector is "; map<int, int> localOccur; for (int num: it->first) { // cout << num << " "; if (localOccur.find(num) == localOccur.end()) { localOccur[num] = 0; } localOccur[num]++; } // cout << endl; cur = calcCombinations(localOccur); // cout << "in " << cur << "versions" << endl; total += cur; total %= modulo; } } return total; } void calcFactorials(int n) { fact.push_back(1); int i = 1; while (i <= n) { fact.push_back(fact[i-1]*i % modulo); // cout << i << "! = " << fact[i] << endl; i++; } } int main() { int n; cin >> n; vector<int> numbers; int maxOccur = 0; for (int i = 0; i < n; ++i) { int num; cin >> num; if (num == 1) { subsets[vector<int>{1}] = true; } numbers.push_back(num); if (occur.find(num) == occur.end()) { occur[num] = 0; } occur[num]++; if (occur[num] > maxOccur) { maxOccur = occur[num]; } } // cout << "max occur is " << maxOccur << endl; calcFactorials(maxOccur); //this will allow for easy access from the map, //as all subsets will be non-decreasing sort(numbers.begin(), numbers.end()); calcSubsets(numbers); cout << findCombinations() << endl; return 0; } |