#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long int lint; lint MagicPrime = 1000000007; struct MyInt { int val; // explicit MyInt(int _val) // : val(_val) // {} MyInt(lint _val) : val(static_cast<int>(_val % MagicPrime)) { if (val < 0) { throw "this should have never happened"; } } MyInt operator+(const MyInt& other) const { return MyInt(asLint() + other.asLint()); } MyInt operator-(const MyInt& other) const { lint diff = asLint() - other.asLint(); while (diff < 0) { diff += MagicPrime; } return MyInt(diff); } MyInt& operator+=(const MyInt& other) { val += other.val; val %= MagicPrime; return *this; } MyInt operator*(const MyInt& other) const { return MyInt(asLint() * other.asLint()); } lint asLint() const { return static_cast<lint>(val); } }; struct Newton { int N; int **t; ~Newton() { for (int i = 0; i <= N; ++i) { delete [] t[i]; } delete [] t; } void init(int N) { this->N = N; t = new int*[N+1]; for (int i = 0; i <= N; ++i) { t[i] = new int[i+1]; for (int j = 0; j <= i; ++j) { if (j == 0 || j == i) { t[i][j] = 1; } else { t[i][j] = (t[i-1][j-1] + t[i-1][j]) % MagicPrime; } } } } MyInt operator()(int n, int k) { if (n < 0 || n > N || k < 0 || k > n) { return MyInt(0); } else { return MyInt(t[n][k]); } } } newton; struct Kula { int r; string coord; int n; Kula(int _r, string _coord) : r(_r) , coord(_coord) , n(_coord.length()) {} int dist(const Kula& other) const { int n = coord.length(); int d = 0; for (int i = 0; i < n; ++i) { if (coord[i] != other.coord[i]) { d++; } } return d; } bool contains(const Kula& other) const { int d = dist(other); return r >= other.r + d; } MyInt vol() const { MyInt sum = 0; for (int i = 0; i <= r; ++i) { sum += newton(n, i); } return sum; } MyInt common(const Kula& A) const { int rb = this->r; int ra = A.r; int k = dist(A); MyInt sum = 0; int i_max = min(k, rb); for (int i = 0; i <= i_max; ++i) { int j_max = mymin(n - k, rb - i, ra + i - k); for (int j = 0; j <= j_max; ++j) { sum += newton(k, i) * newton(n - k, j); } } return sum; } MyInt common(const Kula& A, const Kula& B) const { int ra = A.r; int rb = B.r; int rc = this->r; const Kula& C = *this; int k_a = 0, k_b = 0, k_c = 0; for (int i = 0; i < n; ++i) { if (A.coord[i] == B.coord[i] && B.coord[i] != C.coord[i]) { k_c++; } else if (B.coord[i] == C.coord[i] && C.coord[i] != A.coord[i]) { k_a++; } else if (A.coord[i] == C.coord[i] && C.coord[i] != B.coord[i]) { k_b++; } } int k_bc = k_b + k_c; int k_ac = k_a + k_c; MyInt sum_ia = 0; int ia_max = min(k_a, rc); for (int ia = 0; ia <= ia_max; ++ia) { //cerr << "ia = " << ia << " / " << ia_max << endl; MyInt sum_ib = 0; int ib_max = min(k_b, rc - ia); for (int ib = 0; ib <= ib_max; ++ib) { MyInt sum_ic = 0; int ic_max = min(k_c, rc - ia - ib); for (int ic = 0; ic <= ic_max; ++ic) { MyInt sum_j = 0; int j_max = mymin(n - k_a - k_b - k_c, rc - ia - ib - ic, rb - ia - (k_bc - ib - ic), ra - ib - (k_ac - ia -ic) ); for (int j = 0; j <= j_max; ++j) { sum_j += newton(n - k_a - k_b - k_c, j); } sum_ic += newton(k_c, ic) * sum_j; } sum_ib += newton(k_b, ib) * sum_ic; } sum_ia += newton(k_a, ia) * sum_ib; } return sum_ia; } template <typename T> static T mymin(T v) { return v; } template <typename T, typename ...Args> static T mymin(T v, Args... vals) { return min(v, mymin(vals...)); } }; struct Solver { MyInt solve(vector<Kula>& balls) { sort(balls.begin(), balls.end(), [](const Kula& a, const Kula& b) { return a.r >= b.r; }); const Kula& A = balls[0]; const Kula& B = balls[1]; const Kula& C = balls[2]; //cout << "Halo " << A.r << " " << B.r << " " << C.r << endl; if (A.contains(C) || B.contains(C)) { if (A.contains(B)) { return _solve(A); } else { return _solve(A, B); } } else { if (A.contains(B)) { return _solve(A, C); } else { return _solve(A, B, C); } } } MyInt _solve(const Kula& A) { //cerr << "Solving for one: " << A.r << " " << A.coord << endl; return A.vol(); } MyInt _solve(const Kula& A, const Kula& B) { //cerr << "Solving for two: " << A.r << " " << A.coord << " & " << B.r << " " << B.coord << endl; return A.vol() + B.vol() - B.common(A); } MyInt _solve(const Kula& A, const Kula& B, const Kula& C) { //cerr << "Solving for three" << endl; // MyInt a = A.vol(), // b = B.vol(), // c = C.vol(), // ab = B.common(A), // bc = C.common(B), // ca = C.common(A), // abc = C.common(A, B); // cerr << "A = " << a.val << endl; // cerr << "B = " << b.val << endl; // cerr << "C = " << c.val << endl; // cerr << "AB = " << ab.val << endl; // cerr << "BC = " << bc.val << endl; // cerr << "CA = " << ca.val << endl; // cerr << "ABC = " << abc.val << endl; return A.vol() + B.vol() + C.vol() - B.common(A) - C.common(A) - C.common(B) + C.common(A, B); } }; int main() { int n; cin >> n; newton.init(n); // for (int i = 0; i <= n; ++i) { // for (int j = 0; j <= i; ++j) { // cout << newton(i, j).val << (i == j ? '\n' : ' '); // } // } vector<Kula> balls; for (int i = 0; i < 3; ++i) { int r; cin >> r; string coords; cin >> coords; balls.push_back(Kula(r, coords)); } Solver s; MyInt res = s.solve(balls); cout << res.val << 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 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long int lint; lint MagicPrime = 1000000007; struct MyInt { int val; // explicit MyInt(int _val) // : val(_val) // {} MyInt(lint _val) : val(static_cast<int>(_val % MagicPrime)) { if (val < 0) { throw "this should have never happened"; } } MyInt operator+(const MyInt& other) const { return MyInt(asLint() + other.asLint()); } MyInt operator-(const MyInt& other) const { lint diff = asLint() - other.asLint(); while (diff < 0) { diff += MagicPrime; } return MyInt(diff); } MyInt& operator+=(const MyInt& other) { val += other.val; val %= MagicPrime; return *this; } MyInt operator*(const MyInt& other) const { return MyInt(asLint() * other.asLint()); } lint asLint() const { return static_cast<lint>(val); } }; struct Newton { int N; int **t; ~Newton() { for (int i = 0; i <= N; ++i) { delete [] t[i]; } delete [] t; } void init(int N) { this->N = N; t = new int*[N+1]; for (int i = 0; i <= N; ++i) { t[i] = new int[i+1]; for (int j = 0; j <= i; ++j) { if (j == 0 || j == i) { t[i][j] = 1; } else { t[i][j] = (t[i-1][j-1] + t[i-1][j]) % MagicPrime; } } } } MyInt operator()(int n, int k) { if (n < 0 || n > N || k < 0 || k > n) { return MyInt(0); } else { return MyInt(t[n][k]); } } } newton; struct Kula { int r; string coord; int n; Kula(int _r, string _coord) : r(_r) , coord(_coord) , n(_coord.length()) {} int dist(const Kula& other) const { int n = coord.length(); int d = 0; for (int i = 0; i < n; ++i) { if (coord[i] != other.coord[i]) { d++; } } return d; } bool contains(const Kula& other) const { int d = dist(other); return r >= other.r + d; } MyInt vol() const { MyInt sum = 0; for (int i = 0; i <= r; ++i) { sum += newton(n, i); } return sum; } MyInt common(const Kula& A) const { int rb = this->r; int ra = A.r; int k = dist(A); MyInt sum = 0; int i_max = min(k, rb); for (int i = 0; i <= i_max; ++i) { int j_max = mymin(n - k, rb - i, ra + i - k); for (int j = 0; j <= j_max; ++j) { sum += newton(k, i) * newton(n - k, j); } } return sum; } MyInt common(const Kula& A, const Kula& B) const { int ra = A.r; int rb = B.r; int rc = this->r; const Kula& C = *this; int k_a = 0, k_b = 0, k_c = 0; for (int i = 0; i < n; ++i) { if (A.coord[i] == B.coord[i] && B.coord[i] != C.coord[i]) { k_c++; } else if (B.coord[i] == C.coord[i] && C.coord[i] != A.coord[i]) { k_a++; } else if (A.coord[i] == C.coord[i] && C.coord[i] != B.coord[i]) { k_b++; } } int k_bc = k_b + k_c; int k_ac = k_a + k_c; MyInt sum_ia = 0; int ia_max = min(k_a, rc); for (int ia = 0; ia <= ia_max; ++ia) { //cerr << "ia = " << ia << " / " << ia_max << endl; MyInt sum_ib = 0; int ib_max = min(k_b, rc - ia); for (int ib = 0; ib <= ib_max; ++ib) { MyInt sum_ic = 0; int ic_max = min(k_c, rc - ia - ib); for (int ic = 0; ic <= ic_max; ++ic) { MyInt sum_j = 0; int j_max = mymin(n - k_a - k_b - k_c, rc - ia - ib - ic, rb - ia - (k_bc - ib - ic), ra - ib - (k_ac - ia -ic) ); for (int j = 0; j <= j_max; ++j) { sum_j += newton(n - k_a - k_b - k_c, j); } sum_ic += newton(k_c, ic) * sum_j; } sum_ib += newton(k_b, ib) * sum_ic; } sum_ia += newton(k_a, ia) * sum_ib; } return sum_ia; } template <typename T> static T mymin(T v) { return v; } template <typename T, typename ...Args> static T mymin(T v, Args... vals) { return min(v, mymin(vals...)); } }; struct Solver { MyInt solve(vector<Kula>& balls) { sort(balls.begin(), balls.end(), [](const Kula& a, const Kula& b) { return a.r >= b.r; }); const Kula& A = balls[0]; const Kula& B = balls[1]; const Kula& C = balls[2]; //cout << "Halo " << A.r << " " << B.r << " " << C.r << endl; if (A.contains(C) || B.contains(C)) { if (A.contains(B)) { return _solve(A); } else { return _solve(A, B); } } else { if (A.contains(B)) { return _solve(A, C); } else { return _solve(A, B, C); } } } MyInt _solve(const Kula& A) { //cerr << "Solving for one: " << A.r << " " << A.coord << endl; return A.vol(); } MyInt _solve(const Kula& A, const Kula& B) { //cerr << "Solving for two: " << A.r << " " << A.coord << " & " << B.r << " " << B.coord << endl; return A.vol() + B.vol() - B.common(A); } MyInt _solve(const Kula& A, const Kula& B, const Kula& C) { //cerr << "Solving for three" << endl; // MyInt a = A.vol(), // b = B.vol(), // c = C.vol(), // ab = B.common(A), // bc = C.common(B), // ca = C.common(A), // abc = C.common(A, B); // cerr << "A = " << a.val << endl; // cerr << "B = " << b.val << endl; // cerr << "C = " << c.val << endl; // cerr << "AB = " << ab.val << endl; // cerr << "BC = " << bc.val << endl; // cerr << "CA = " << ca.val << endl; // cerr << "ABC = " << abc.val << endl; return A.vol() + B.vol() + C.vol() - B.common(A) - C.common(A) - C.common(B) + C.common(A, B); } }; int main() { int n; cin >> n; newton.init(n); // for (int i = 0; i <= n; ++i) { // for (int j = 0; j <= i; ++j) { // cout << newton(i, j).val << (i == j ? '\n' : ' '); // } // } vector<Kula> balls; for (int i = 0; i < 3; ++i) { int r; cin >> r; string coords; cin >> coords; balls.push_back(Kula(r, coords)); } Solver s; MyInt res = s.solve(balls); cout << res.val << endl; return 0; } |