#include <cctype> #include <cstdint> #include <cstdio> #include <cstdlib> #include <algorithm> #include <limits> #include <vector> // #define SHOWDEBUG struct hypercube_t { unsigned int radius; std::vector<char> center; }; static const uint32_t MODULUS = 1000 * 1000 * 1000 + 7; static const size_t MAX_N = 10 * 1000; std::vector<uint32_t> pascals[10]; template <typename T> auto cont_size(const T& t) -> decltype(t.size()) { return t.size(); } template <typename T> auto cont_size(T& t) -> decltype(t.size()) { return t.size(); } template <typename T, size_t S> auto cont_size(const T (&t)[S]) -> size_t { return S; } template <typename T, size_t S> auto cont_size(T (&t)[S]) -> size_t { return S; } hypercube_t read_hypercube(unsigned int dimensions) { unsigned int radius; scanf("%u", &radius); // Skip whitespace int c; while (!isdigit(c = getc(stdin))) ; ungetc(c, stdin); std::vector<char> center; center.resize((size_t)dimensions); fread(center.data(), 1, dimensions, stdin); return hypercube_t{radius, std::move(center)}; } inline uint32_t mod_mul(uint32_t a, uint32_t b) { return (uint32_t)(((uint64_t)a * (uint64_t)b) % (uint64_t)MODULUS); } inline uint32_t mod_add(uint32_t a, uint32_t b) { return (a + b) % MODULUS; } inline uint32_t mod_neg(uint32_t x) { return (x == 0) ? 0 : (MODULUS - x); } template <typename T> void print_container(const T& cont, size_t end = std::numeric_limits<size_t>::max()) { printf("["); for (size_t i = 0; i < end && i < cont_size(cont); i++) { if (i > 0) { printf(", "); } printf("%d", (int)cont[i]); } printf("]\n"); } uint32_t point_distance(const std::vector<char>& a, const std::vector<char>& b) { uint32_t result = 0; for (unsigned int i = 0; i < a.size(); i++) { result += (uint32_t)((a[i] != b[i]) ? 1 : 0); } return result; } uint32_t volume_of_hypersphere(const std::vector<uint32_t>& tri, unsigned int radius) { uint32_t sum = 0; for (unsigned int i = 0; i <= radius; i++) { sum = mod_add(sum, tri[i]); } #ifdef SHOWDEBUG printf("Single volume: %u\n", sum); #endif return sum; } uint32_t volume_of_intersection_of_hypersphere(unsigned int base, int32_t g0, int32_t g1, unsigned int r1, unsigned int r2) { uint32_t sum = 0; for (int32_t i = 0; i <= g0; i++) { for (int32_t j = 0; j <= g1; j++) { const int32_t budget1 = (int32_t)r1 - i - j; const int32_t budget2 = (int32_t)r2 - i - (g1 - j); if (budget1 < 0 || budget2 < 0) { continue; } sum = mod_add(sum, mod_mul(pascals[base + 0][i], pascals[base + 1][j])); } } #ifdef SHOWDEBUG printf("Intersection of volumes: %u\n", sum); #endif return sum; } uint32_t volume_of_intersection_of_all(int32_t g0, int32_t g1, int32_t g2, int32_t g3, unsigned int r1, unsigned int r2, unsigned int r3) { uint32_t sum = 0; for (int32_t i = 0; i <= g0; i++) { for (int32_t j = 0; j <= g1; j++) { for (int32_t k = 0; k <= g2; k++) { for (int32_t l = 0; l <= g3; l++) { const int32_t budget1 = (int32_t)r1 - i - j - k - l; const int32_t budget2 = (int32_t)r2 - i - (g1 - j) - k - (g3 - l); const int32_t budget3 = (int32_t)r3 - i - j - (g2 - k) - (g3 - l); if (budget1 < 0 || budget2 < 0 || budget3 < 0) { continue; } sum = mod_add(sum, mod_mul(mod_mul(pascals[0][i], pascals[1][j]), mod_mul(pascals[2][k], pascals[3][l]))); } } } } #ifdef SHOWDEBUG printf("Intersection of all volumes: %u\n", (unsigned int)sum); #endif return sum; } int main() { unsigned int dims; scanf("%u", &dims); auto hc1 = read_hypercube(dims); auto hc2 = read_hypercube(dims); auto hc3 = read_hypercube(dims); // Flipping a digit in the same position in all hypercubes does not change the // volume. Rearranging digits with the same permutation does not change the // volume. This means we can categorize positions into 4 groups. int32_t groups[10] = {0, 0, 0, 0}; for (unsigned int i = 0; i < dims; i++) { const unsigned int d12 = (hc1.center[i] != hc2.center[i]) ? 1 : 0; const unsigned int d13 = (hc1.center[i] != hc3.center[i]) ? 1 : 0; groups[d12 + 2 * d13]++; groups[4 + ((hc1.center[i] != hc2.center[i]) ? 1 : 0)]++; groups[6 + ((hc1.center[i] != hc3.center[i]) ? 1 : 0)]++; groups[8 + ((hc2.center[i] != hc3.center[i]) ? 1 : 0)]++; } // Compute Pascal triangles std::pair<unsigned int, unsigned int> sorted_groups[10]; for (unsigned int i = 0; i < 10; i++) { sorted_groups[i] = std::make_pair(groups[i], i); } std::sort(sorted_groups, sorted_groups + 10); std::vector<uint32_t> tri; std::vector<uint32_t> tmp_tri; auto inc_tri = [&] { tmp_tri.resize(tri.size(), 1); for (unsigned int i = 1; i < tri.size(); i++) { tmp_tri[i] = mod_add(tri[i - 1], tri[i]); } tmp_tri.push_back(1); std::swap(tri, tmp_tri); }; for (auto p : sorted_groups) { while (tri.size() <= p.first) { inc_tri(); } pascals[p.second] = tri; } while (tri.size() <= dims) { inc_tri(); } tmp_tri.clear(); // Check for duplicates auto dedup = [&](hypercube_t& a, hypercube_t& b) { if (a.center.size() == 0 || b.center.size() == 0) { return; } const auto distance = point_distance(a.center, b.center); #ifdef SHOWDEBUG printf("%d %d %d\n", a.radius, distance, b.radius); #endif // Triangle inequality if (a.radius >= b.radius + distance) { // `b` is contained inside `a` b.center.clear(); } else if (b.radius >= a.radius + distance) { // `a` is contained inside `b` a.center.clear(); } }; dedup(hc2, hc3); dedup(hc1, hc3); dedup(hc1, hc2); uint32_t total = 0; // Add volume of individual spheres if (!hc1.center.empty()) { total = mod_add(total, volume_of_hypersphere(tri, hc1.radius)); } if (!hc2.center.empty()) { total = mod_add(total, volume_of_hypersphere(tri, hc2.radius)); } if (!hc3.center.empty()) { total = mod_add(total, volume_of_hypersphere(tri, hc3.radius)); } // Subtract intersections of pairs of spheres if (!hc1.center.empty() && !hc2.center.empty()) { total = mod_add(total, mod_neg(volume_of_intersection_of_hypersphere( 4, groups[4], groups[5], hc1.radius, hc2.radius))); } if (!hc1.center.empty() && !hc3.center.empty()) { total = mod_add(total, mod_neg(volume_of_intersection_of_hypersphere( 6, groups[6], groups[7], hc1.radius, hc3.radius))); } if (!hc2.center.empty() && !hc3.center.empty()) { total = mod_add(total, mod_neg(volume_of_intersection_of_hypersphere( 8, groups[8], groups[9], hc2.radius, hc3.radius))); } // Add intersection of all three if (!hc1.center.empty() && !hc2.center.empty() && !hc3.center.empty()) { total = mod_add(total, volume_of_intersection_of_all( groups[0], groups[1], groups[2], groups[3], hc1.radius, hc2.radius, hc3.radius)); } printf("%u\n", (unsigned int)total); 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | #include <cctype> #include <cstdint> #include <cstdio> #include <cstdlib> #include <algorithm> #include <limits> #include <vector> // #define SHOWDEBUG struct hypercube_t { unsigned int radius; std::vector<char> center; }; static const uint32_t MODULUS = 1000 * 1000 * 1000 + 7; static const size_t MAX_N = 10 * 1000; std::vector<uint32_t> pascals[10]; template <typename T> auto cont_size(const T& t) -> decltype(t.size()) { return t.size(); } template <typename T> auto cont_size(T& t) -> decltype(t.size()) { return t.size(); } template <typename T, size_t S> auto cont_size(const T (&t)[S]) -> size_t { return S; } template <typename T, size_t S> auto cont_size(T (&t)[S]) -> size_t { return S; } hypercube_t read_hypercube(unsigned int dimensions) { unsigned int radius; scanf("%u", &radius); // Skip whitespace int c; while (!isdigit(c = getc(stdin))) ; ungetc(c, stdin); std::vector<char> center; center.resize((size_t)dimensions); fread(center.data(), 1, dimensions, stdin); return hypercube_t{radius, std::move(center)}; } inline uint32_t mod_mul(uint32_t a, uint32_t b) { return (uint32_t)(((uint64_t)a * (uint64_t)b) % (uint64_t)MODULUS); } inline uint32_t mod_add(uint32_t a, uint32_t b) { return (a + b) % MODULUS; } inline uint32_t mod_neg(uint32_t x) { return (x == 0) ? 0 : (MODULUS - x); } template <typename T> void print_container(const T& cont, size_t end = std::numeric_limits<size_t>::max()) { printf("["); for (size_t i = 0; i < end && i < cont_size(cont); i++) { if (i > 0) { printf(", "); } printf("%d", (int)cont[i]); } printf("]\n"); } uint32_t point_distance(const std::vector<char>& a, const std::vector<char>& b) { uint32_t result = 0; for (unsigned int i = 0; i < a.size(); i++) { result += (uint32_t)((a[i] != b[i]) ? 1 : 0); } return result; } uint32_t volume_of_hypersphere(const std::vector<uint32_t>& tri, unsigned int radius) { uint32_t sum = 0; for (unsigned int i = 0; i <= radius; i++) { sum = mod_add(sum, tri[i]); } #ifdef SHOWDEBUG printf("Single volume: %u\n", sum); #endif return sum; } uint32_t volume_of_intersection_of_hypersphere(unsigned int base, int32_t g0, int32_t g1, unsigned int r1, unsigned int r2) { uint32_t sum = 0; for (int32_t i = 0; i <= g0; i++) { for (int32_t j = 0; j <= g1; j++) { const int32_t budget1 = (int32_t)r1 - i - j; const int32_t budget2 = (int32_t)r2 - i - (g1 - j); if (budget1 < 0 || budget2 < 0) { continue; } sum = mod_add(sum, mod_mul(pascals[base + 0][i], pascals[base + 1][j])); } } #ifdef SHOWDEBUG printf("Intersection of volumes: %u\n", sum); #endif return sum; } uint32_t volume_of_intersection_of_all(int32_t g0, int32_t g1, int32_t g2, int32_t g3, unsigned int r1, unsigned int r2, unsigned int r3) { uint32_t sum = 0; for (int32_t i = 0; i <= g0; i++) { for (int32_t j = 0; j <= g1; j++) { for (int32_t k = 0; k <= g2; k++) { for (int32_t l = 0; l <= g3; l++) { const int32_t budget1 = (int32_t)r1 - i - j - k - l; const int32_t budget2 = (int32_t)r2 - i - (g1 - j) - k - (g3 - l); const int32_t budget3 = (int32_t)r3 - i - j - (g2 - k) - (g3 - l); if (budget1 < 0 || budget2 < 0 || budget3 < 0) { continue; } sum = mod_add(sum, mod_mul(mod_mul(pascals[0][i], pascals[1][j]), mod_mul(pascals[2][k], pascals[3][l]))); } } } } #ifdef SHOWDEBUG printf("Intersection of all volumes: %u\n", (unsigned int)sum); #endif return sum; } int main() { unsigned int dims; scanf("%u", &dims); auto hc1 = read_hypercube(dims); auto hc2 = read_hypercube(dims); auto hc3 = read_hypercube(dims); // Flipping a digit in the same position in all hypercubes does not change the // volume. Rearranging digits with the same permutation does not change the // volume. This means we can categorize positions into 4 groups. int32_t groups[10] = {0, 0, 0, 0}; for (unsigned int i = 0; i < dims; i++) { const unsigned int d12 = (hc1.center[i] != hc2.center[i]) ? 1 : 0; const unsigned int d13 = (hc1.center[i] != hc3.center[i]) ? 1 : 0; groups[d12 + 2 * d13]++; groups[4 + ((hc1.center[i] != hc2.center[i]) ? 1 : 0)]++; groups[6 + ((hc1.center[i] != hc3.center[i]) ? 1 : 0)]++; groups[8 + ((hc2.center[i] != hc3.center[i]) ? 1 : 0)]++; } // Compute Pascal triangles std::pair<unsigned int, unsigned int> sorted_groups[10]; for (unsigned int i = 0; i < 10; i++) { sorted_groups[i] = std::make_pair(groups[i], i); } std::sort(sorted_groups, sorted_groups + 10); std::vector<uint32_t> tri; std::vector<uint32_t> tmp_tri; auto inc_tri = [&] { tmp_tri.resize(tri.size(), 1); for (unsigned int i = 1; i < tri.size(); i++) { tmp_tri[i] = mod_add(tri[i - 1], tri[i]); } tmp_tri.push_back(1); std::swap(tri, tmp_tri); }; for (auto p : sorted_groups) { while (tri.size() <= p.first) { inc_tri(); } pascals[p.second] = tri; } while (tri.size() <= dims) { inc_tri(); } tmp_tri.clear(); // Check for duplicates auto dedup = [&](hypercube_t& a, hypercube_t& b) { if (a.center.size() == 0 || b.center.size() == 0) { return; } const auto distance = point_distance(a.center, b.center); #ifdef SHOWDEBUG printf("%d %d %d\n", a.radius, distance, b.radius); #endif // Triangle inequality if (a.radius >= b.radius + distance) { // `b` is contained inside `a` b.center.clear(); } else if (b.radius >= a.radius + distance) { // `a` is contained inside `b` a.center.clear(); } }; dedup(hc2, hc3); dedup(hc1, hc3); dedup(hc1, hc2); uint32_t total = 0; // Add volume of individual spheres if (!hc1.center.empty()) { total = mod_add(total, volume_of_hypersphere(tri, hc1.radius)); } if (!hc2.center.empty()) { total = mod_add(total, volume_of_hypersphere(tri, hc2.radius)); } if (!hc3.center.empty()) { total = mod_add(total, volume_of_hypersphere(tri, hc3.radius)); } // Subtract intersections of pairs of spheres if (!hc1.center.empty() && !hc2.center.empty()) { total = mod_add(total, mod_neg(volume_of_intersection_of_hypersphere( 4, groups[4], groups[5], hc1.radius, hc2.radius))); } if (!hc1.center.empty() && !hc3.center.empty()) { total = mod_add(total, mod_neg(volume_of_intersection_of_hypersphere( 6, groups[6], groups[7], hc1.radius, hc3.radius))); } if (!hc2.center.empty() && !hc3.center.empty()) { total = mod_add(total, mod_neg(volume_of_intersection_of_hypersphere( 8, groups[8], groups[9], hc2.radius, hc3.radius))); } // Add intersection of all three if (!hc1.center.empty() && !hc2.center.empty() && !hc3.center.empty()) { total = mod_add(total, volume_of_intersection_of_all( groups[0], groups[1], groups[2], groups[3], hc1.radius, hc2.radius, hc3.radius)); } printf("%u\n", (unsigned int)total); return 0; } |