#include <bits/stdc++.h> using namespace std; using vi=vector<int>; using ll=long long; #define pb push_back #define fst first #define snd second using vll=vector<ll>; using Pii=pair<int,int>; using Pll=pair<ll,ll>; using vpii=vector<Pii>; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define sim template <class c #define mor > muu & operator<<( #define ris return *this #define R22(r) sim> typename enable_if<1 r sizeof(dud<c>(0)),muu&>::type operator<<(c g) { sim> struct rge {c b, e;}; sim> rge<c> range(c i, c j) {return rge<c>{i, j};} sim> auto dud(c*r)->decltype(cerr << *r); sim> char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() {cerr << a.str() << endl;} R22(<) a << boolalpha << g; ris;} R22(==) ris << range(begin(g), end(g));} sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } sim, class m mor pair<m,c> r) {ris << "(" << r.first << ", " << r.second << ")";} #else sim mor const c &){ris;} #endif muu & operator()(){ris;} }; #define imie(r) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": " ) typedef long long ll; typedef pair<ll, ll> Pll; int n; const int maxn = 1.1e4; const ll mod = 1e9 + 7; ll rev(ll x) { ll e = mod - 2; ll res = 1; while(e) { if(e & 1) res = res * x % mod; x = x * x % mod; e /= 2; } return res; } int to_mask(vi a) { int res = 0; for(int v : a) res = 2 * res + v; return res; } ll fac[maxn]; ll fac_rev[maxn]; void init() { fac[0] = 1; for(int i = 1; i <= n; ++i) fac[i] = i * fac[i-1] % mod; for(int i = 0; i <= n; ++i) fac_rev[i] = rev(fac[i]); } ll binom(ll n1, ll k1) { return fac[n1] * fac_rev[k1] % mod * fac_rev[n1 - k1] % mod; } ll przetnij_kule(vi a, int r1, vi b, int r2) { for(int i = 0; i < n; ++i) b[i] ^= a[i]; int zera, jedynki = 0; for(int i = 0; i < n; ++i) if(b[i] == 1) ++jedynki; zera = n - jedynki; ll res = 0; vector<ll> bin_zer(zera + 1); vector<ll> bin_jed(jedynki + 1); for(int i = 0; i <= zera; ++i) bin_zer[i] = binom(zera, i); for(int j = 0; j <= jedynki; ++j) bin_jed[j] = binom(jedynki, j); for(int i = 0; i <= zera; ++i) for(int j = 0; j <= jedynki; ++j) if(i + jedynki - j <= r1 && i + j <= r2) res += bin_zer[i] * bin_jed[j] % mod; return res % mod; } int tab[maxn][maxn]; vector<ll> gen_binomy(int gora) { vector<ll> res(gora + 1); for(int i = 0; i <= gora; ++i) res[i] = binom(gora, i); return res; } ll przetnij_kule_3(vi a, int ra, vi b, int rb, vi c, int rc) { for(int i = 0; i < n; ++i) { b[i] ^= a[i]; c[i] ^= a[i]; } int zz = 0, zj = 0, jz = 0, jj = 0; for(int i = 0; i < n; ++i) { if(b[i] == 0 && c[i] == 0) ++zz; if(b[i] == 1 && c[i] == 0) ++jz; if(b[i] == 0 && c[i] == 1) ++zj; if(b[i] == 1 && c[i] == 1) ++jj; } vector<ll> bzz = gen_binomy(zz); vector<ll> bjj = gen_binomy(jj); vector<ll> bzj = gen_binomy(zj); vector<ll> bjz = gen_binomy(jz); for(int i = 0; i <= zz; ++i) for(int j = 0; j <= jj; ++j) tab[i + j][i + (jj - j)] = bzz[i] * bjj[j] % mod; for(int j = 1; j <= n; ++j) tab[0][j] = (tab[0][j] + tab[0][j-1]) % mod; for(int i = 1; i <= n; ++i) { tab[i][0] = (tab[i][0] + tab[i-1][0]) % mod; for(int j = 1; j <= n; ++j) { ll val = mod + tab[i][j-1] + tab[i-1][j] - tab[i-1][j-1]; val %= mod; tab[i][j] = (tab[i][j] + val) % mod; } } ll res = 0; for(int jzn = 0; jzn <= jz; ++jzn) for(int zjn = 0; zjn <= zj; ++zjn) { int slack_a = ra - jzn - zjn; int slack_b = rb - (jz - jzn) - zjn; int slack_c = rc - (zj - zjn) - jzn; int slack_d = min(slack_b, slack_c); if(min(slack_a, slack_d) < 0) continue; ll subres = tab[slack_a][slack_d]; res += subres * bjz[jzn] % mod * bzj[zjn] % mod; } return res % mod; } ll w_kuli(vi a, int r1) { ll res = 0; for(int i = 0; i <= r1; ++i) res += binom(n, i); return res % mod; } typedef pair<vi, int> kula; char buff[maxn]; kula wczytaj() { int r; scanf("%d", &r); scanf("%s", buff); vi res; for(int i = 0; i < n; ++i) res.push_back(buff[i] - '0'); debug() << res; return {res, r}; } int main() { scanf("%d", &n); init(); kula A = wczytaj(); kula B = wczytaj(); kula C = wczytaj(); #define kula(X) w_kuli(X.first, X.second) #define kula2(X, Y) przetnij_kule(X.first, X.second, Y.first, Y.second) #define kula3(X, Y, Z) przetnij_kule_3(X.first, X.second, Y.first, Y.second, Z.first, Z.second) ll res = kula(A) + kula(B) + kula(C) - kula2(A,B) - kula2(B,C) - kula2(A, C) + kula3(A, B, C); res = res % mod; if(res < 0) res += mod; printf("%lld\n", res); }
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 | #include <bits/stdc++.h> using namespace std; using vi=vector<int>; using ll=long long; #define pb push_back #define fst first #define snd second using vll=vector<ll>; using Pii=pair<int,int>; using Pll=pair<ll,ll>; using vpii=vector<Pii>; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define sim template <class c #define mor > muu & operator<<( #define ris return *this #define R22(r) sim> typename enable_if<1 r sizeof(dud<c>(0)),muu&>::type operator<<(c g) { sim> struct rge {c b, e;}; sim> rge<c> range(c i, c j) {return rge<c>{i, j};} sim> auto dud(c*r)->decltype(cerr << *r); sim> char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() {cerr << a.str() << endl;} R22(<) a << boolalpha << g; ris;} R22(==) ris << range(begin(g), end(g));} sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } sim, class m mor pair<m,c> r) {ris << "(" << r.first << ", " << r.second << ")";} #else sim mor const c &){ris;} #endif muu & operator()(){ris;} }; #define imie(r) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": " ) typedef long long ll; typedef pair<ll, ll> Pll; int n; const int maxn = 1.1e4; const ll mod = 1e9 + 7; ll rev(ll x) { ll e = mod - 2; ll res = 1; while(e) { if(e & 1) res = res * x % mod; x = x * x % mod; e /= 2; } return res; } int to_mask(vi a) { int res = 0; for(int v : a) res = 2 * res + v; return res; } ll fac[maxn]; ll fac_rev[maxn]; void init() { fac[0] = 1; for(int i = 1; i <= n; ++i) fac[i] = i * fac[i-1] % mod; for(int i = 0; i <= n; ++i) fac_rev[i] = rev(fac[i]); } ll binom(ll n1, ll k1) { return fac[n1] * fac_rev[k1] % mod * fac_rev[n1 - k1] % mod; } ll przetnij_kule(vi a, int r1, vi b, int r2) { for(int i = 0; i < n; ++i) b[i] ^= a[i]; int zera, jedynki = 0; for(int i = 0; i < n; ++i) if(b[i] == 1) ++jedynki; zera = n - jedynki; ll res = 0; vector<ll> bin_zer(zera + 1); vector<ll> bin_jed(jedynki + 1); for(int i = 0; i <= zera; ++i) bin_zer[i] = binom(zera, i); for(int j = 0; j <= jedynki; ++j) bin_jed[j] = binom(jedynki, j); for(int i = 0; i <= zera; ++i) for(int j = 0; j <= jedynki; ++j) if(i + jedynki - j <= r1 && i + j <= r2) res += bin_zer[i] * bin_jed[j] % mod; return res % mod; } int tab[maxn][maxn]; vector<ll> gen_binomy(int gora) { vector<ll> res(gora + 1); for(int i = 0; i <= gora; ++i) res[i] = binom(gora, i); return res; } ll przetnij_kule_3(vi a, int ra, vi b, int rb, vi c, int rc) { for(int i = 0; i < n; ++i) { b[i] ^= a[i]; c[i] ^= a[i]; } int zz = 0, zj = 0, jz = 0, jj = 0; for(int i = 0; i < n; ++i) { if(b[i] == 0 && c[i] == 0) ++zz; if(b[i] == 1 && c[i] == 0) ++jz; if(b[i] == 0 && c[i] == 1) ++zj; if(b[i] == 1 && c[i] == 1) ++jj; } vector<ll> bzz = gen_binomy(zz); vector<ll> bjj = gen_binomy(jj); vector<ll> bzj = gen_binomy(zj); vector<ll> bjz = gen_binomy(jz); for(int i = 0; i <= zz; ++i) for(int j = 0; j <= jj; ++j) tab[i + j][i + (jj - j)] = bzz[i] * bjj[j] % mod; for(int j = 1; j <= n; ++j) tab[0][j] = (tab[0][j] + tab[0][j-1]) % mod; for(int i = 1; i <= n; ++i) { tab[i][0] = (tab[i][0] + tab[i-1][0]) % mod; for(int j = 1; j <= n; ++j) { ll val = mod + tab[i][j-1] + tab[i-1][j] - tab[i-1][j-1]; val %= mod; tab[i][j] = (tab[i][j] + val) % mod; } } ll res = 0; for(int jzn = 0; jzn <= jz; ++jzn) for(int zjn = 0; zjn <= zj; ++zjn) { int slack_a = ra - jzn - zjn; int slack_b = rb - (jz - jzn) - zjn; int slack_c = rc - (zj - zjn) - jzn; int slack_d = min(slack_b, slack_c); if(min(slack_a, slack_d) < 0) continue; ll subres = tab[slack_a][slack_d]; res += subres * bjz[jzn] % mod * bzj[zjn] % mod; } return res % mod; } ll w_kuli(vi a, int r1) { ll res = 0; for(int i = 0; i <= r1; ++i) res += binom(n, i); return res % mod; } typedef pair<vi, int> kula; char buff[maxn]; kula wczytaj() { int r; scanf("%d", &r); scanf("%s", buff); vi res; for(int i = 0; i < n; ++i) res.push_back(buff[i] - '0'); debug() << res; return {res, r}; } int main() { scanf("%d", &n); init(); kula A = wczytaj(); kula B = wczytaj(); kula C = wczytaj(); #define kula(X) w_kuli(X.first, X.second) #define kula2(X, Y) przetnij_kule(X.first, X.second, Y.first, Y.second) #define kula3(X, Y, Z) przetnij_kule_3(X.first, X.second, Y.first, Y.second, Z.first, Z.second) ll res = kula(A) + kula(B) + kula(C) - kula2(A,B) - kula2(B,C) - kula2(A, C) + kula3(A, B, C); res = res % mod; if(res < 0) res += mod; printf("%lld\n", res); } |