// Author: Kamil Nizinski // NOLINT(legal/copyright) #ifdef LOCAL #ifdef COLOR #include "bits/cp_local_color.h" #else #include "bits/cp_local.h" #endif #else #include "bits/stdc++.h" #define debug_arr(arr_name, first, last) #define debug(...) #define cerr if (false) cerr #define speed_of_cin_and_cout ios_base::sync_with_stdio(0), cin.tie(0) #define local if (false) #endif #define ft first #define sd second #define emb emplace_back #define sz(a) (static_cast<int>((a).size())) using namespace std; // NOLINT(build/namespaces) typedef int64_t LL; typedef uint64_t LLU; typedef long double LD; typedef pair<int, int> PII; const int kMaxN = 10007, kMod = 1000000007; int factorials[kMaxN], inverses_of_factorial_cache[kMaxN]; int binoms_cache[kMaxN][kMaxN]; int dp[kMaxN][kMaxN]; int pow_mod(int a, int x) { int result = 1; while (x > 0) { if (x & 1) { result = LL{1} * result * a % kMod; x--; } else { a = LL{1} * a * a % kMod; x >>= 1; } } return result; } int inverse_of_factorial(int k) { if (inverses_of_factorial_cache[k] != 0) return inverses_of_factorial_cache[k]; inverses_of_factorial_cache[k] = pow_mod(factorials[k], kMod - 2); return inverses_of_factorial_cache[k]; } int binom(int n, int k) { if ((k < 0) || (k > n)) return 0; if (binoms_cache[n][k] != 0) return binoms_cache[n][k]; binoms_cache[n][k] = LL{1} * factorials[n] * inverse_of_factorial(k) % kMod * inverse_of_factorial(n - k) % kMod; return binoms_cache[n][k]; // Maybe cache it? Done } void solve() { int n; cin >> n; // cout << "n read\n"; // scanf("%d", &n); factorials[0] = 1; for (int i = 0; i < n; i++) factorials[i + 1] = LL{1} * factorials[i] * (i + 1) % kMod; // array<int, 3> radia; static int radia[3]; // array<string, 3> centers; static string centers[3]; // static char centers[3][kMaxN]; for (int i = 0; i < 3; i++) { cin >> radia[i]; // scanf("%d", &radia[i]); cin >> centers[i]; // scanf("%s", centers[i]); debug(radia[i], centers[i]); } int result = 0; for (int i = 0; i < 3; i++) // Add the volume of this sphere. for (int dist = 0; dist <= radia[i]; dist++) result = (result + binom(n, dist)) % kMod; debug(result); for (int i = 0; i < 3; i++) { // Subtract the intersection of the other two spheres. int s = 0; int a = (i + 1) % 3, b = (i + 2) % 3; for (int j = 0; j < n; j++) if (centers[a][j] == centers[b][j]) s++; int d = n - s; for (int x = max(d - radia[b], 0); x <= min(radia[a], d); x++) { for (int y = 0; y <= min(radia[a] - x, radia[b] - (d - x)); y++) { result -= LL{1} * binom(d, x) * binom(s, y) % kMod; if (result < 0) result += kMod; debug(result); } } } debug(result); vector<PII> nums_unique_for_radius(3); int same_num = n; for (int i = 0; i < 3; i++) { nums_unique_for_radius[i] = {0, radia[i]}; for (int j = 0; j < n; j++) if (centers[i][j] != centers[(i + 1) % 3][j] && centers[i][j] != centers[(i + 2) % 3][j]) nums_unique_for_radius[i].ft++; same_num -= nums_unique_for_radius[i].ft; } nums_unique_for_radius.emb(same_num, -1); sort(nums_unique_for_radius.begin(), nums_unique_for_radius.end()); debug(same_num, nums_unique_for_radius); int with_same_idx; for (int i = 0; i < 4; i++) { if (nums_unique_for_radius[i].sd == -1) { with_same_idx = 3 - i; break; } } PII with_same = nums_unique_for_radius[with_same_idx]; nums_unique_for_radius.erase(nums_unique_for_radius.begin() + with_same_idx); for (int i = 0; i < 4; i++) { if (nums_unique_for_radius[i].sd == -1) { nums_unique_for_radius.erase(nums_unique_for_radius.begin() + i); break; } } debug(same_num, with_same); debug(nums_unique_for_radius); int max_dist_dp = same_num + with_same.ft; for (int i = 0; i <= max_dist_dp; i++) { for (int j = 0; j <= max_dist_dp; j++) { dp[i][j] = 0; } } for (int x = 0; x <= same_num; x++) { for (int y = 0; y <= with_same.ft; y++) { // y is how much I add on only the unique sphere // The first coordinate is the total distance so far on the unique sphere // The second coordinate--on the other two (it's the same for both of them). dp[x + y][x + with_same.ft - y] += LL{1} * binom(same_num, x) * binom(with_same.ft, y) % kMod; if (dp[x + y][x + with_same.ft - y] >= kMod) dp[x + y][x + with_same.ft - y] -= kMod; } } // debug(max_dist_dp); for (int i = 0; i <= max_dist_dp; i++) { for (int j = 0; j < max_dist_dp; j++) { dp[i][j + 1] += dp[i][j]; if (dp[i][j + 1] >= kMod) dp[i][j + 1] -= kMod; } } for (int i = 0; i < max_dist_dp; i++) { for (int j = 0; j <= max_dist_dp; j++) { dp[i + 1][j] += dp[i][j]; if (dp[i + 1][j] >= kMod) dp[i + 1][j] -= kMod; } } for (int i = 0; i <= max_dist_dp; i++) { debug(i); debug_arr(dp[i], 0, max_dist_dp + 1); } int intersection_size = 0; for (int x = 0; x <= nums_unique_for_radius[0].ft; x++) { for (int y = 0; y <= nums_unique_for_radius[1].ft; y++) { debug(x, y); int bound_on_the_third_one = with_same.sd - (nums_unique_for_radius[0].ft - x) - (nums_unique_for_radius[1].ft - y); int bound_on_these_two = min(nums_unique_for_radius[0].sd - x - (nums_unique_for_radius[1].ft - y), nums_unique_for_radius[1].sd - y - (nums_unique_for_radius[0].ft - x)); debug(bound_on_the_third_one, bound_on_these_two); debug(min(max_dist_dp, bound_on_the_third_one), min(max_dist_dp, bound_on_these_two)); if (bound_on_the_third_one >= 0 && bound_on_these_two >= 0) { debug(LL{1} * binom(nums_unique_for_radius[0].ft, x) * binom(nums_unique_for_radius[1].ft, y) % kMod * dp[min(max_dist_dp, bound_on_the_third_one)][min(max_dist_dp, bound_on_these_two)] % kMod); intersection_size += LL{1} * binom(nums_unique_for_radius[0].ft, x) * binom(nums_unique_for_radius[1].ft, y) % kMod * dp[min(max_dist_dp, bound_on_the_third_one)][min(max_dist_dp, bound_on_these_two)] % kMod; if (intersection_size >= kMod) intersection_size -= kMod; debug(intersection_size); } } } debug(intersection_size); result = (result + intersection_size) % kMod; cout << result << "\n"; // printf("%d\n", result); } int main() { speed_of_cin_and_cout; int test_cases_num = 1; // cin >> test_cases_num; for (int i = 1; i <= test_cases_num; i++) { local if (test_cases_num > 1) cerr << "Test #" << i << ":\n"; solve(); local if (test_cases_num > 1) cerr << "End of test #" << i << ".\n"; } 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 | // Author: Kamil Nizinski // NOLINT(legal/copyright) #ifdef LOCAL #ifdef COLOR #include "bits/cp_local_color.h" #else #include "bits/cp_local.h" #endif #else #include "bits/stdc++.h" #define debug_arr(arr_name, first, last) #define debug(...) #define cerr if (false) cerr #define speed_of_cin_and_cout ios_base::sync_with_stdio(0), cin.tie(0) #define local if (false) #endif #define ft first #define sd second #define emb emplace_back #define sz(a) (static_cast<int>((a).size())) using namespace std; // NOLINT(build/namespaces) typedef int64_t LL; typedef uint64_t LLU; typedef long double LD; typedef pair<int, int> PII; const int kMaxN = 10007, kMod = 1000000007; int factorials[kMaxN], inverses_of_factorial_cache[kMaxN]; int binoms_cache[kMaxN][kMaxN]; int dp[kMaxN][kMaxN]; int pow_mod(int a, int x) { int result = 1; while (x > 0) { if (x & 1) { result = LL{1} * result * a % kMod; x--; } else { a = LL{1} * a * a % kMod; x >>= 1; } } return result; } int inverse_of_factorial(int k) { if (inverses_of_factorial_cache[k] != 0) return inverses_of_factorial_cache[k]; inverses_of_factorial_cache[k] = pow_mod(factorials[k], kMod - 2); return inverses_of_factorial_cache[k]; } int binom(int n, int k) { if ((k < 0) || (k > n)) return 0; if (binoms_cache[n][k] != 0) return binoms_cache[n][k]; binoms_cache[n][k] = LL{1} * factorials[n] * inverse_of_factorial(k) % kMod * inverse_of_factorial(n - k) % kMod; return binoms_cache[n][k]; // Maybe cache it? Done } void solve() { int n; cin >> n; // cout << "n read\n"; // scanf("%d", &n); factorials[0] = 1; for (int i = 0; i < n; i++) factorials[i + 1] = LL{1} * factorials[i] * (i + 1) % kMod; // array<int, 3> radia; static int radia[3]; // array<string, 3> centers; static string centers[3]; // static char centers[3][kMaxN]; for (int i = 0; i < 3; i++) { cin >> radia[i]; // scanf("%d", &radia[i]); cin >> centers[i]; // scanf("%s", centers[i]); debug(radia[i], centers[i]); } int result = 0; for (int i = 0; i < 3; i++) // Add the volume of this sphere. for (int dist = 0; dist <= radia[i]; dist++) result = (result + binom(n, dist)) % kMod; debug(result); for (int i = 0; i < 3; i++) { // Subtract the intersection of the other two spheres. int s = 0; int a = (i + 1) % 3, b = (i + 2) % 3; for (int j = 0; j < n; j++) if (centers[a][j] == centers[b][j]) s++; int d = n - s; for (int x = max(d - radia[b], 0); x <= min(radia[a], d); x++) { for (int y = 0; y <= min(radia[a] - x, radia[b] - (d - x)); y++) { result -= LL{1} * binom(d, x) * binom(s, y) % kMod; if (result < 0) result += kMod; debug(result); } } } debug(result); vector<PII> nums_unique_for_radius(3); int same_num = n; for (int i = 0; i < 3; i++) { nums_unique_for_radius[i] = {0, radia[i]}; for (int j = 0; j < n; j++) if (centers[i][j] != centers[(i + 1) % 3][j] && centers[i][j] != centers[(i + 2) % 3][j]) nums_unique_for_radius[i].ft++; same_num -= nums_unique_for_radius[i].ft; } nums_unique_for_radius.emb(same_num, -1); sort(nums_unique_for_radius.begin(), nums_unique_for_radius.end()); debug(same_num, nums_unique_for_radius); int with_same_idx; for (int i = 0; i < 4; i++) { if (nums_unique_for_radius[i].sd == -1) { with_same_idx = 3 - i; break; } } PII with_same = nums_unique_for_radius[with_same_idx]; nums_unique_for_radius.erase(nums_unique_for_radius.begin() + with_same_idx); for (int i = 0; i < 4; i++) { if (nums_unique_for_radius[i].sd == -1) { nums_unique_for_radius.erase(nums_unique_for_radius.begin() + i); break; } } debug(same_num, with_same); debug(nums_unique_for_radius); int max_dist_dp = same_num + with_same.ft; for (int i = 0; i <= max_dist_dp; i++) { for (int j = 0; j <= max_dist_dp; j++) { dp[i][j] = 0; } } for (int x = 0; x <= same_num; x++) { for (int y = 0; y <= with_same.ft; y++) { // y is how much I add on only the unique sphere // The first coordinate is the total distance so far on the unique sphere // The second coordinate--on the other two (it's the same for both of them). dp[x + y][x + with_same.ft - y] += LL{1} * binom(same_num, x) * binom(with_same.ft, y) % kMod; if (dp[x + y][x + with_same.ft - y] >= kMod) dp[x + y][x + with_same.ft - y] -= kMod; } } // debug(max_dist_dp); for (int i = 0; i <= max_dist_dp; i++) { for (int j = 0; j < max_dist_dp; j++) { dp[i][j + 1] += dp[i][j]; if (dp[i][j + 1] >= kMod) dp[i][j + 1] -= kMod; } } for (int i = 0; i < max_dist_dp; i++) { for (int j = 0; j <= max_dist_dp; j++) { dp[i + 1][j] += dp[i][j]; if (dp[i + 1][j] >= kMod) dp[i + 1][j] -= kMod; } } for (int i = 0; i <= max_dist_dp; i++) { debug(i); debug_arr(dp[i], 0, max_dist_dp + 1); } int intersection_size = 0; for (int x = 0; x <= nums_unique_for_radius[0].ft; x++) { for (int y = 0; y <= nums_unique_for_radius[1].ft; y++) { debug(x, y); int bound_on_the_third_one = with_same.sd - (nums_unique_for_radius[0].ft - x) - (nums_unique_for_radius[1].ft - y); int bound_on_these_two = min(nums_unique_for_radius[0].sd - x - (nums_unique_for_radius[1].ft - y), nums_unique_for_radius[1].sd - y - (nums_unique_for_radius[0].ft - x)); debug(bound_on_the_third_one, bound_on_these_two); debug(min(max_dist_dp, bound_on_the_third_one), min(max_dist_dp, bound_on_these_two)); if (bound_on_the_third_one >= 0 && bound_on_these_two >= 0) { debug(LL{1} * binom(nums_unique_for_radius[0].ft, x) * binom(nums_unique_for_radius[1].ft, y) % kMod * dp[min(max_dist_dp, bound_on_the_third_one)][min(max_dist_dp, bound_on_these_two)] % kMod); intersection_size += LL{1} * binom(nums_unique_for_radius[0].ft, x) * binom(nums_unique_for_radius[1].ft, y) % kMod * dp[min(max_dist_dp, bound_on_the_third_one)][min(max_dist_dp, bound_on_these_two)] % kMod; if (intersection_size >= kMod) intersection_size -= kMod; debug(intersection_size); } } } debug(intersection_size); result = (result + intersection_size) % kMod; cout << result << "\n"; // printf("%d\n", result); } int main() { speed_of_cin_and_cout; int test_cases_num = 1; // cin >> test_cases_num; for (int i = 1; i <= test_cases_num; i++) { local if (test_cases_num > 1) cerr << "Test #" << i << ":\n"; solve(); local if (test_cases_num > 1) cerr << "End of test #" << i << ".\n"; } return 0; } |