#include<bits/stdc++.h> using namespace std; #define ll long long const ll m = 1000000007; ll facs[10009]; int n; ll facmod(ll n) { if(n == 0) return 1; for(ll i = n - 1; i >= 1; i--) { n = (n*i)%m; } return n; } ll modinv(ll a) { ll res = 1; ll exp = m - 2; while(exp > 0) { if(exp % 2 == 1) { res = (res*a)%m; } exp >>= 1; a = (a*a)%m; } return res; } ll choose(ll n, ll k) { ll a = facs[n]; ll b = (facs[k] * facs[n-k]) % m; return (a * modinv(b))%m; } int dist(string a, string b) { int res = 0; for(int i = 0; i < a.length(); i++) if(a[i] != b[i]) res++; return res; } ll vol(int r) { ll w = 0; for(int i = 0; i <= r; i++) w = (w + choose(n, i)) % m; return w; } ll inter2(int r1, int r2, int d) { int ones = d; int zeroes = n - d; ll w = vol(r2); ll res = 0; for(int i = r1 + 1; i <= min(r2 + ones, n); i++) { int tmp = 0; while(i - ones + 2*tmp <= r2 and i - ones + 2*tmp >= 0) { res = (res + (choose(zeroes, i - ones + tmp) * choose(ones, tmp)) % m) % m; tmp++; } } return (w + m - res) % m; } ll inter3(int r1, int r2, int r3, string x1, string x2, string x3) { ll c1 = 0, c2 = 0, c3 = 0, c4 = 0; for(int i = 0; i < n; i++) { if(x1[i] != x2[i] and x2[i] == x3[i]) c1++; else if(x2[i] != x3[i] and x3[i] == x1[i]) c2++; else if(x3[i] != x1[i] and x1[i] == x2[i]) c3++; else if(x1[i] == x2[i] == x3[i]) c4++; } ll res = 0; ll p1, p2, p3; ll tmp; for(int x = 0; x <= c1; x++) { for(int y = 0; y <= c2; y++) { for(int z = 0; z <= c3; z++) { p1 = c1 - x + y + z; p2 = c2 - y + x + z; p3 = c3 - z + y + x; if(p1 > r1 or p2 > r2 or p3 > r3) continue; tmp = min(r1 - p1, min(r2 - p2, r3 - p3)); res += choose(c4, min(c4,tmp)); res %= m; } } } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for(int i = 0; i < 10006; i++) { facs[i] = facmod(i); } cin >> n; int r1, r2, r3; string x1, x2, x3; cin >> r1 >> x1; cin >> r2 >> x2; cin >> r3 >> x3; ll res = 3*m; if((r1 == r2) and (x1 == x2)) { res += vol(r3) + vol(r1); res -= inter2(r1, r3, dist(x1, x3)); } else if((r1 == r3) and (x1 == x3)) { res += vol(r2) + vol(r3); res -= inter2(r1, r2, dist(x1, x2)); } else if((r2 == r3) and (x2 == x3)) { res += vol(r2) + vol(r1); res -= inter2(r2, r3, dist(x2, x3)); } else { res += (vol(r1) + vol(r2) + vol(r3)); res -= (inter2(r1, r2, dist(x1, x2)) + inter2(r1, r3, dist(x1, x3)) + inter2(r2, r3, dist(x2, x3))); res += inter3(r1, r2, r3, x1, x2, x3); } cout << res % m << endl; }
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 | #include<bits/stdc++.h> using namespace std; #define ll long long const ll m = 1000000007; ll facs[10009]; int n; ll facmod(ll n) { if(n == 0) return 1; for(ll i = n - 1; i >= 1; i--) { n = (n*i)%m; } return n; } ll modinv(ll a) { ll res = 1; ll exp = m - 2; while(exp > 0) { if(exp % 2 == 1) { res = (res*a)%m; } exp >>= 1; a = (a*a)%m; } return res; } ll choose(ll n, ll k) { ll a = facs[n]; ll b = (facs[k] * facs[n-k]) % m; return (a * modinv(b))%m; } int dist(string a, string b) { int res = 0; for(int i = 0; i < a.length(); i++) if(a[i] != b[i]) res++; return res; } ll vol(int r) { ll w = 0; for(int i = 0; i <= r; i++) w = (w + choose(n, i)) % m; return w; } ll inter2(int r1, int r2, int d) { int ones = d; int zeroes = n - d; ll w = vol(r2); ll res = 0; for(int i = r1 + 1; i <= min(r2 + ones, n); i++) { int tmp = 0; while(i - ones + 2*tmp <= r2 and i - ones + 2*tmp >= 0) { res = (res + (choose(zeroes, i - ones + tmp) * choose(ones, tmp)) % m) % m; tmp++; } } return (w + m - res) % m; } ll inter3(int r1, int r2, int r3, string x1, string x2, string x3) { ll c1 = 0, c2 = 0, c3 = 0, c4 = 0; for(int i = 0; i < n; i++) { if(x1[i] != x2[i] and x2[i] == x3[i]) c1++; else if(x2[i] != x3[i] and x3[i] == x1[i]) c2++; else if(x3[i] != x1[i] and x1[i] == x2[i]) c3++; else if(x1[i] == x2[i] == x3[i]) c4++; } ll res = 0; ll p1, p2, p3; ll tmp; for(int x = 0; x <= c1; x++) { for(int y = 0; y <= c2; y++) { for(int z = 0; z <= c3; z++) { p1 = c1 - x + y + z; p2 = c2 - y + x + z; p3 = c3 - z + y + x; if(p1 > r1 or p2 > r2 or p3 > r3) continue; tmp = min(r1 - p1, min(r2 - p2, r3 - p3)); res += choose(c4, min(c4,tmp)); res %= m; } } } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for(int i = 0; i < 10006; i++) { facs[i] = facmod(i); } cin >> n; int r1, r2, r3; string x1, x2, x3; cin >> r1 >> x1; cin >> r2 >> x2; cin >> r3 >> x3; ll res = 3*m; if((r1 == r2) and (x1 == x2)) { res += vol(r3) + vol(r1); res -= inter2(r1, r3, dist(x1, x3)); } else if((r1 == r3) and (x1 == x3)) { res += vol(r2) + vol(r3); res -= inter2(r1, r2, dist(x1, x2)); } else if((r2 == r3) and (x2 == x3)) { res += vol(r2) + vol(r1); res -= inter2(r2, r3, dist(x2, x3)); } else { res += (vol(r1) + vol(r2) + vol(r3)); res -= (inter2(r1, r2, dist(x1, x2)) + inter2(r1, r3, dist(x1, x3)) + inter2(r2, r3, dist(x2, x3))); res += inter3(r1, r2, r3, x1, x2, x3); } cout << res % m << endl; } |