#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define PB push_back #define SIZE(a) (int(a.size())) const int mod = 1e9+7; const int maxn = 10100; ll fact[maxn], fact_inv[maxn]; ll po(ll a, ll k) { ll res=1; while(k) { if(k%2) { res = (res*a)%mod; } a = (a*a)%mod; k/=2; } return res; } ll inv(ll a) { return po(a, mod-2); } vector<ll> gen_newton_row(int n) { vector<ll> r(n+1); for(int i=0; i <= n; i++) { r[i] = (fact[n]*fact_inv[i])%mod; r[i] = (r[i]*fact_inv[n-i])%mod; } return r; } ll ne(int n, int k) { if(k < 0 || k > n) { return 0; } ll res = (fact[n]*fact_inv[k])%mod; res = (res*fact_inv[n-k])%mod; return res; } ll single(int r, string s) { auto ne = gen_newton_row(SIZE(s)); ll res=0; for(int i=0; i <= r; i++) { res = (res+ne[i])%mod; } return res; } ll intersect2(int r0, string s0, int r1, string s1) { int d[] = {0, 0}; for(int i=0; i < SIZE(s0); i++) { if(s0[i] == s1[i]) { d[0]++; } else { d[1]++; } } ll b=0; int p = d[1]-r1, q = r0; for(int x1=d[1]-r1; x1 <= r0; x1++) { b = (b+ne(d[1], x1))%mod; } ll a=0; for(int x0=0; x0 <= d[0]; x0++) { a = (a+b*ne(d[0], x0))%mod; b = (b-ne(d[1], p)-ne(d[1], q))%mod; p++; q--; if(p > q) { break; } } return a; } ll get_diagonal(int a, int p, int q, vector<vector<ll>> &v) { if(p > q) { return 0; } int d = (q-p)/2; int c = 0; if(a < 0) { c-=a; d+=a; a=0; } if(d < 0 || c >= SIZE(v) || a >= SIZE(v[0])) { return 0; } // cerr << "d: " << d << endl; // cerr << c << " " << a << " " << d << " " << SIZE(v) << " " << SIZE(v[0]) << "\n"; return (v[c][a]-v[min(SIZE(v)-1, c+d+1)][min(SIZE(v[0])-1, a+d+1)]); } ll intersect3(int r0, string s0, int r1, string s1, int r2, string s2) { int d[] = {0, 0, 0, 0}; for(int i=0; i < SIZE(s0); i++) { int c=0; if(s0[i] == '1') { c+=1; } if(s1[i] == '1') { c+=2; } if(s2[i] == '1') { c^=3; } d[c]++; } vector<vector<ll>> v(d[0]+2); v[d[0]+1] = vector<ll> (d[3]+2); for(int i=0; i <= d[0]; i++) { v[i] = vector<ll> (d[3]+2); ll n = ne(d[0], i); for(int j=0; j <= d[3]; j++) { v[i][j] = (n*ne(d[3], j))%mod; } } for(int i=d[0]; i >= 0; i--) { for(int j=d[3]; j >= 0; j--) { v[i][j] = (v[i][j]+v[i+1][j+1])%mod; } } r1-=d[2]+d[3]; r0-=d[1]+d[3]; ll a=0; for(int i=0; i <= d[1]; i++) { ll b=0, c=0; //gen c int p=max(i-r1, -i-r0), q=r2-i; for(int j=p; j <= q; j++) { // cerr << "j:" << j << "\n"; c = (c+get_diagonal(j, j, q, v))%mod; } for(int j=0; j <= d[2]; j++) { // cerr << c << " " << p << " " << q << "\n"; b=(b+ne(d[2], j)*c)%mod; //update c int np=max(i-j-1-r1, -i+j+1-r0), nq=r2-i-j-1; if(p < np) { c = (c-get_diagonal(p, p, q, v))%mod; } else if(p > np) { c = (c+get_diagonal(np, np, q, v))%mod; } c = (c-get_diagonal(d[3]-q, np, q, v))%mod; p = np, q = nq; } a=(a+ne(d[1], i)*b)%mod; } return a; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; fact[0] = 1; fact_inv[0] = 1; for(ll i=1; i <= n; i++) { fact[i] = (i*fact[i-1])%mod; fact_inv[i] = inv(fact[i]); } int r0, r1, r2; string s0, s1, s2; cin >> r0 >> s0; cin >> r1 >> s1; cin >> r2 >> s2; // cerr << single(r0, s0) << "\n"; // cerr << single(r1, s1) << "\n"; // cerr << single(r2, s2) << "\n"; // cerr << intersect2(r0, s0, r1, s1) << "\n"; // cerr << intersect2(r0, s0, r2, s2) << "\n"; // cerr << intersect2(r1, s1, r2, s2) << "\n"; // cerr << intersect3(r0, s0, r1, s1, r2, s2) << "\n"; ll res = single(r0, s0)+single(r1, s1)+single(r2, s2)-intersect2(r0, s0, r1, s1)-intersect2(r0, s0, r2, s2)-intersect2(r1, s1, r2, s2)+intersect3(r0, s0, r1, s1, r2, s2); res = (res%mod+mod)%mod; cout << res << "\n"; }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define PB push_back #define SIZE(a) (int(a.size())) const int mod = 1e9+7; const int maxn = 10100; ll fact[maxn], fact_inv[maxn]; ll po(ll a, ll k) { ll res=1; while(k) { if(k%2) { res = (res*a)%mod; } a = (a*a)%mod; k/=2; } return res; } ll inv(ll a) { return po(a, mod-2); } vector<ll> gen_newton_row(int n) { vector<ll> r(n+1); for(int i=0; i <= n; i++) { r[i] = (fact[n]*fact_inv[i])%mod; r[i] = (r[i]*fact_inv[n-i])%mod; } return r; } ll ne(int n, int k) { if(k < 0 || k > n) { return 0; } ll res = (fact[n]*fact_inv[k])%mod; res = (res*fact_inv[n-k])%mod; return res; } ll single(int r, string s) { auto ne = gen_newton_row(SIZE(s)); ll res=0; for(int i=0; i <= r; i++) { res = (res+ne[i])%mod; } return res; } ll intersect2(int r0, string s0, int r1, string s1) { int d[] = {0, 0}; for(int i=0; i < SIZE(s0); i++) { if(s0[i] == s1[i]) { d[0]++; } else { d[1]++; } } ll b=0; int p = d[1]-r1, q = r0; for(int x1=d[1]-r1; x1 <= r0; x1++) { b = (b+ne(d[1], x1))%mod; } ll a=0; for(int x0=0; x0 <= d[0]; x0++) { a = (a+b*ne(d[0], x0))%mod; b = (b-ne(d[1], p)-ne(d[1], q))%mod; p++; q--; if(p > q) { break; } } return a; } ll get_diagonal(int a, int p, int q, vector<vector<ll>> &v) { if(p > q) { return 0; } int d = (q-p)/2; int c = 0; if(a < 0) { c-=a; d+=a; a=0; } if(d < 0 || c >= SIZE(v) || a >= SIZE(v[0])) { return 0; } // cerr << "d: " << d << endl; // cerr << c << " " << a << " " << d << " " << SIZE(v) << " " << SIZE(v[0]) << "\n"; return (v[c][a]-v[min(SIZE(v)-1, c+d+1)][min(SIZE(v[0])-1, a+d+1)]); } ll intersect3(int r0, string s0, int r1, string s1, int r2, string s2) { int d[] = {0, 0, 0, 0}; for(int i=0; i < SIZE(s0); i++) { int c=0; if(s0[i] == '1') { c+=1; } if(s1[i] == '1') { c+=2; } if(s2[i] == '1') { c^=3; } d[c]++; } vector<vector<ll>> v(d[0]+2); v[d[0]+1] = vector<ll> (d[3]+2); for(int i=0; i <= d[0]; i++) { v[i] = vector<ll> (d[3]+2); ll n = ne(d[0], i); for(int j=0; j <= d[3]; j++) { v[i][j] = (n*ne(d[3], j))%mod; } } for(int i=d[0]; i >= 0; i--) { for(int j=d[3]; j >= 0; j--) { v[i][j] = (v[i][j]+v[i+1][j+1])%mod; } } r1-=d[2]+d[3]; r0-=d[1]+d[3]; ll a=0; for(int i=0; i <= d[1]; i++) { ll b=0, c=0; //gen c int p=max(i-r1, -i-r0), q=r2-i; for(int j=p; j <= q; j++) { // cerr << "j:" << j << "\n"; c = (c+get_diagonal(j, j, q, v))%mod; } for(int j=0; j <= d[2]; j++) { // cerr << c << " " << p << " " << q << "\n"; b=(b+ne(d[2], j)*c)%mod; //update c int np=max(i-j-1-r1, -i+j+1-r0), nq=r2-i-j-1; if(p < np) { c = (c-get_diagonal(p, p, q, v))%mod; } else if(p > np) { c = (c+get_diagonal(np, np, q, v))%mod; } c = (c-get_diagonal(d[3]-q, np, q, v))%mod; p = np, q = nq; } a=(a+ne(d[1], i)*b)%mod; } return a; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; fact[0] = 1; fact_inv[0] = 1; for(ll i=1; i <= n; i++) { fact[i] = (i*fact[i-1])%mod; fact_inv[i] = inv(fact[i]); } int r0, r1, r2; string s0, s1, s2; cin >> r0 >> s0; cin >> r1 >> s1; cin >> r2 >> s2; // cerr << single(r0, s0) << "\n"; // cerr << single(r1, s1) << "\n"; // cerr << single(r2, s2) << "\n"; // cerr << intersect2(r0, s0, r1, s1) << "\n"; // cerr << intersect2(r0, s0, r2, s2) << "\n"; // cerr << intersect2(r1, s1, r2, s2) << "\n"; // cerr << intersect3(r0, s0, r1, s1, r2, s2) << "\n"; ll res = single(r0, s0)+single(r1, s1)+single(r2, s2)-intersect2(r0, s0, r1, s1)-intersect2(r0, s0, r2, s2)-intersect2(r1, s1, r2, s2)+intersect3(r0, s0, r1, s1, r2, s2); res = (res%mod+mod)%mod; cout << res << "\n"; } |