#ifndef LOCAL #pragma GCC optimize ("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename A, typename B> using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>; using namespace __gnu_pbds; template <typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // ordered_set<int> s; s.insert(1); s.insert(2); // s.order_of_key(1); // Out: 0. // *s.find_by_order(1); // Out: 2. using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } template <typename Arg> Arg Min(Arg a) { return a; } template <typename Arg1, typename Arg2, typename ...Args> auto Min(Arg1 a1, Arg2 a2, Args ...args) { return min(a1, Min(a2, args...)); } template <typename Arg> Arg Max(Arg a) { return a; } template <typename Arg1, typename Arg2, typename ...Args> auto Max(Arg1 a1, Arg2 a2, Args ...args) { return max(a1, Max(a2, args...)); } constexpr int nax = 10 * 1000 + 105; int sil[nax]; int odwsil[nax]; int sumakab[nax][nax]; int sumakcd[nax][nax]; int CC(int n, int k) { if (0 <= k and k <= n) { return Mnoz(sil[n], Mnoz(odwsil[k], odwsil[n - k])); } return 0; } int DoTrzech(int A, int B, int C, int D, int alpha, int beta, int gamma) { debug() << "DoTrzech(" imie(A) imie(B) imie(C) imie(D) imie(alpha) imie(beta) imie(gamma) ")"; int wynik = 0; // for (int a = 0; a <= A; a++) { // for (int b = 0; b <= B; b++) { // for (int c = 0; c <= C; c++) { // for (int d = 0; d <= D; d++) { // if (A - a + c + b + d <= alpha) { // if (B - b + a + c + d <= beta) { // if (C - c + a + b + d <= gamma) { // const int count = Mnoz(Mnoz(CC(A, a), CC(B, b)), // Mnoz(CC(C, c), CC(D, d))); // debug() << imie(a) imie(b) imie(c) imie(d) imie(count); // } // } // } // } // } // } // } for (int ab = 0; ab <= A + B; ab++) { for (int a = 0; a <= ab; a++) { const int b = ab - a; sumakab[ab][a] = Mnoz(CC(A, a), CC(B, b)); if (a > 0) { DodajDo(sumakab[ab][a], sumakab[ab][a - 1]); } } } for (int cd = 0; cd <= C + D; cd++) { for (int c = 0; c <= cd; c++) { const int d = cd - c; sumakcd[cd][c] = Mnoz(CC(C, c), CC(D, d)); if (c > 0) { DodajDo(sumakcd[cd][c], sumakcd[cd][c - 1]); } } } for (int X = 0; X <= A + B; X++) { for (int Y = 0; Y <= C + D; Y++) { const int min_2a = Max(0, A + X + Y - alpha, 2 * (X - B)); const int max_2a = Min(2 * A, beta + X - B - Y, 2 * X); const int min_2c = Max(0, C + X + Y - gamma, 2 * (Y - D)); const int max_2c = Min(2 * C, 2 * Y); if (min_2a <= max_2a and min_2c <= max_2c) { const int min_a = (min_2a + 1) / 2; const int max_a = max_2a / 2; const int min_c = (min_2c + 1) / 2; const int max_c = max_2c / 2; // int sposoby_ab = 0; // for (int a = min_a; a <= max_a; a++) { // const int b = X - a; // DodajDo(sposoby_ab, Mnoz(CC(A, a), CC(B, b))); // } int sposoby_ab = sumakab[X][max_a]; if (min_a > 0) OdejmijOd(sposoby_ab, sumakab[X][min_a - 1]); // int sposoby_cd = 0; // for (int c = min_c; c <= max_c; c++) { // const int d = Y - c; // DodajDo(sposoby_cd, Mnoz(CC(C, c), CC(D, d))); // } int sposoby_cd = sumakcd[Y][max_c]; if (min_c > 0) OdejmijOd(sposoby_cd, sumakcd[Y][min_c - 1]); DodajDo(wynik, Mnoz(sposoby_ab, sposoby_cd)); } } } debug() << imie(wynik); return wynik; } int DoDwoch(int n, int r0, const string& s0, int r1, const string& s1) { int ksor[2] = {0, 0}; for (int i = 0; i < n; i++) { ksor[s0[i] == s1[i]]++; } const int A = ksor[0]; const int D = ksor[1]; const int X = r0; const int Y = r1; // debug() << "DoDwoch(" imie(A) imie(D) imie(X) imie(Y) ")"; int wynik = 0; vector<int> prepro(D + 1); for (int d = 0; d <= D; d++) { prepro[d] = CC(D, d); if (d > 0) { DodajDo(prepro[d], prepro[d - 1]); } } for (int a = 0; a <= A; a++) { const int d = min(D, min(Y - a, X - A + a)); if (0 <= d and d <= D) { DodajDo(wynik, Mnoz(CC(A, a), prepro[d])); } } // debug() << imie(wynik); return wynik; } int DoJednego(int n, int r) { // debug() << "DoJednego(" imie(n) imie(r) ")"; int wynik = 0; for (int i = 0; i <= r; i++) { DodajDo(wynik, CC(n, i)); } // debug() << imie(wynik); return wynik; } int Algos() { int n; cin >> n; int r[3]; string in[3]; for (int i = 0; i < 3; i++) { cin >> r[i] >> in[i]; assert((int) in[i].size() == n); } int wynik = 0; DodajDo(wynik, DoJednego(n, r[0])); DodajDo(wynik, DoJednego(n, r[1])); DodajDo(wynik, DoJednego(n, r[2])); auto DoDwochSmall = [&](int a, int b) -> int { return DoDwoch(n, r[a], in[a], r[b], in[b]); }; OdejmijOd(wynik, DoDwochSmall(0, 1)); OdejmijOd(wynik, DoDwochSmall(0, 2)); OdejmijOd(wynik, DoDwochSmall(1, 2)); int tab[4] = {0, 0, 0, 0}; for (int i = 0; i < n; i++) { const int a = (in[0][i] == '1'); const int b = (in[1][i] == '1'); const int c = (in[2][i] == '1'); tab[(a ^ b) + (a ^ c) * 2]++; } DodajDo(wynik, DoTrzech(tab[3], tab[1], tab[2], tab[0], r[0], r[1], r[2])); return wynik; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); sil[0] = 1; for (int i = 1; i < nax; i++) { sil[i] = Mnoz(sil[i - 1], i); } odwsil[nax - 1] = Odw(sil[nax - 1]); for (int i = nax - 2; i >= 0; i--) { odwsil[i] = Mnoz(odwsil[i + 1], i + 1); } cout << Algos() << 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 | #ifndef LOCAL #pragma GCC optimize ("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename A, typename B> using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>; using namespace __gnu_pbds; template <typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // ordered_set<int> s; s.insert(1); s.insert(2); // s.order_of_key(1); // Out: 0. // *s.find_by_order(1); // Out: 2. using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } template <typename Arg> Arg Min(Arg a) { return a; } template <typename Arg1, typename Arg2, typename ...Args> auto Min(Arg1 a1, Arg2 a2, Args ...args) { return min(a1, Min(a2, args...)); } template <typename Arg> Arg Max(Arg a) { return a; } template <typename Arg1, typename Arg2, typename ...Args> auto Max(Arg1 a1, Arg2 a2, Args ...args) { return max(a1, Max(a2, args...)); } constexpr int nax = 10 * 1000 + 105; int sil[nax]; int odwsil[nax]; int sumakab[nax][nax]; int sumakcd[nax][nax]; int CC(int n, int k) { if (0 <= k and k <= n) { return Mnoz(sil[n], Mnoz(odwsil[k], odwsil[n - k])); } return 0; } int DoTrzech(int A, int B, int C, int D, int alpha, int beta, int gamma) { debug() << "DoTrzech(" imie(A) imie(B) imie(C) imie(D) imie(alpha) imie(beta) imie(gamma) ")"; int wynik = 0; // for (int a = 0; a <= A; a++) { // for (int b = 0; b <= B; b++) { // for (int c = 0; c <= C; c++) { // for (int d = 0; d <= D; d++) { // if (A - a + c + b + d <= alpha) { // if (B - b + a + c + d <= beta) { // if (C - c + a + b + d <= gamma) { // const int count = Mnoz(Mnoz(CC(A, a), CC(B, b)), // Mnoz(CC(C, c), CC(D, d))); // debug() << imie(a) imie(b) imie(c) imie(d) imie(count); // } // } // } // } // } // } // } for (int ab = 0; ab <= A + B; ab++) { for (int a = 0; a <= ab; a++) { const int b = ab - a; sumakab[ab][a] = Mnoz(CC(A, a), CC(B, b)); if (a > 0) { DodajDo(sumakab[ab][a], sumakab[ab][a - 1]); } } } for (int cd = 0; cd <= C + D; cd++) { for (int c = 0; c <= cd; c++) { const int d = cd - c; sumakcd[cd][c] = Mnoz(CC(C, c), CC(D, d)); if (c > 0) { DodajDo(sumakcd[cd][c], sumakcd[cd][c - 1]); } } } for (int X = 0; X <= A + B; X++) { for (int Y = 0; Y <= C + D; Y++) { const int min_2a = Max(0, A + X + Y - alpha, 2 * (X - B)); const int max_2a = Min(2 * A, beta + X - B - Y, 2 * X); const int min_2c = Max(0, C + X + Y - gamma, 2 * (Y - D)); const int max_2c = Min(2 * C, 2 * Y); if (min_2a <= max_2a and min_2c <= max_2c) { const int min_a = (min_2a + 1) / 2; const int max_a = max_2a / 2; const int min_c = (min_2c + 1) / 2; const int max_c = max_2c / 2; // int sposoby_ab = 0; // for (int a = min_a; a <= max_a; a++) { // const int b = X - a; // DodajDo(sposoby_ab, Mnoz(CC(A, a), CC(B, b))); // } int sposoby_ab = sumakab[X][max_a]; if (min_a > 0) OdejmijOd(sposoby_ab, sumakab[X][min_a - 1]); // int sposoby_cd = 0; // for (int c = min_c; c <= max_c; c++) { // const int d = Y - c; // DodajDo(sposoby_cd, Mnoz(CC(C, c), CC(D, d))); // } int sposoby_cd = sumakcd[Y][max_c]; if (min_c > 0) OdejmijOd(sposoby_cd, sumakcd[Y][min_c - 1]); DodajDo(wynik, Mnoz(sposoby_ab, sposoby_cd)); } } } debug() << imie(wynik); return wynik; } int DoDwoch(int n, int r0, const string& s0, int r1, const string& s1) { int ksor[2] = {0, 0}; for (int i = 0; i < n; i++) { ksor[s0[i] == s1[i]]++; } const int A = ksor[0]; const int D = ksor[1]; const int X = r0; const int Y = r1; // debug() << "DoDwoch(" imie(A) imie(D) imie(X) imie(Y) ")"; int wynik = 0; vector<int> prepro(D + 1); for (int d = 0; d <= D; d++) { prepro[d] = CC(D, d); if (d > 0) { DodajDo(prepro[d], prepro[d - 1]); } } for (int a = 0; a <= A; a++) { const int d = min(D, min(Y - a, X - A + a)); if (0 <= d and d <= D) { DodajDo(wynik, Mnoz(CC(A, a), prepro[d])); } } // debug() << imie(wynik); return wynik; } int DoJednego(int n, int r) { // debug() << "DoJednego(" imie(n) imie(r) ")"; int wynik = 0; for (int i = 0; i <= r; i++) { DodajDo(wynik, CC(n, i)); } // debug() << imie(wynik); return wynik; } int Algos() { int n; cin >> n; int r[3]; string in[3]; for (int i = 0; i < 3; i++) { cin >> r[i] >> in[i]; assert((int) in[i].size() == n); } int wynik = 0; DodajDo(wynik, DoJednego(n, r[0])); DodajDo(wynik, DoJednego(n, r[1])); DodajDo(wynik, DoJednego(n, r[2])); auto DoDwochSmall = [&](int a, int b) -> int { return DoDwoch(n, r[a], in[a], r[b], in[b]); }; OdejmijOd(wynik, DoDwochSmall(0, 1)); OdejmijOd(wynik, DoDwochSmall(0, 2)); OdejmijOd(wynik, DoDwochSmall(1, 2)); int tab[4] = {0, 0, 0, 0}; for (int i = 0; i < n; i++) { const int a = (in[0][i] == '1'); const int b = (in[1][i] == '1'); const int c = (in[2][i] == '1'); tab[(a ^ b) + (a ^ c) * 2]++; } DodajDo(wynik, DoTrzech(tab[3], tab[1], tab[2], tab[0], r[0], r[1], r[2])); return wynik; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); sil[0] = 1; for (int i = 1; i < nax; i++) { sil[i] = Mnoz(sil[i - 1], i); } odwsil[nax - 1] = Odw(sil[nax - 1]); for (int i = nax - 2; i >= 0; i--) { odwsil[i] = Mnoz(odwsil[i + 1], i + 1); } cout << Algos() << endl; return 0; } |