#include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; const int N = 10001; int binom[N][N], vals[N][N]; int vals2[N]; void add(int &x, int val) { x += val; if (x >= MOD) { x -= MOD; } } void sub(int &x, int val) { x -= val; if (x < 0) { x += MOD; } } void pre(int n) { for (int i = 0; i <= n; i++) { binom[i][0] = 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) { binom[i][j] = binom[i-1][j] + binom[i-1][j-1]; if (binom[i][j] >= MOD) { binom[i][j] -= MOD; } } } int solveFinal3(int r1, int r2, int r3, int y1, int y2, int y3, int y4) { int m = y3 + y4; for (int diff = 0; diff <= m; diff++) for (int sum = 0; sum <= m; sum++) { vals[diff][sum] = 0; } for (int x3 = 0; x3 <= y3; x3++) { for (int x4 = 0; x4 <= y4; x4++) { int diff = x4 - x3 + y3; int sum = x4 + x3; vals[diff][sum] = (vals[diff][sum] + (long long) binom[y3][x3] * binom[y4][x4]) % MOD; } } for (int diff = 0; diff <= m; diff++) for (int sum = 0; sum <= m; sum++) { if (diff > 0) { add(vals[diff][sum], vals[diff - 1][sum]); } if (sum > 0) { add(vals[diff][sum], vals[diff][sum - 1]); } if (diff > 0 && sum > 0) { sub(vals[diff][sum], vals[diff - 1][sum - 1]); } } int ans = 0; for (int x1 = 0; x1 <= y1; x1++) { for (int x2 = 0; x2 <= y2; x2++) { int maxDiff = min(r1 - x1 - x2, m); int maxSum = min({r2 - y2 - x1 + x2, r3 - y1 + x1 - x2, m}); if (maxDiff < 0 || maxSum < 0) { continue; } int mul = ((long long) binom[y1][x1] * binom[y2][x2]) % MOD; ans = (ans + (long long) mul * vals[maxDiff][maxSum]) % MOD; } } return ans; } int solveFinal2(int r1, int r2, int y1, int y2) { int ans = 0; for (int mx = 0; mx <= y2; mx++) { vals2[mx] = 0; } for (int x2 = 0; x2 <= y2; x2++) { vals2[x2] = binom[y2][x2]; if (x2 > 0) { vals2[x2] = (vals2[x2] + vals2[x2 - 1]) % MOD; } } for (int x1 = 0; x1 <= y1; x1++) { int mx = min({r1 - x1, r2 - y1 + x1, y2}); if (mx < 0) { continue; } ans = (ans + (long long) binom[y1][x1] * vals2[mx]) % MOD; } return ans; } int solve3(int n, const vector <int> &r, const vector <vector<int>> &seq) { vector <int> y(4, 0); for (int i = 0; i < n; i++) { int cnt = 0; for (int t = 0; t < 3; t++) { cnt += seq[t][i]; } if (cnt == 0 || cnt == 3) { y[3]++; } else { int outlier = cnt == 1; for (int t = 0; t < 3; t++) { if (seq[t][i] == outlier) { y[2 - t]++; break; } } } } return solveFinal3(r[0], r[1], r[2], y[0], y[1], y[2], y[3]); } int solve2(int n, const vector <int> &r, const vector <vector<int>> &seq) { int cnt[2] = {0, 0}; for (int i = 0; i < n; i++) { cnt[seq[0][i] == seq[1][i]]++; } return solveFinal2(r[0], r[1], cnt[0], cnt[1]); } int solveUnion(int n, const vector <int> &r, const vector <vector<int>> &seq) { int ans = 0; for (int t = 0; t < 3; t++) { for (int chg = 0; chg <= r[t]; chg++) { add(ans, binom[n][chg]); } } sub(ans, solve2(n, {r[0], r[1]}, {seq[0], seq[1]})); sub(ans, solve2(n, {r[0], r[2]}, {seq[0], seq[2]})); sub(ans, solve2(n, {r[1], r[2]}, {seq[1], seq[2]})); add(ans, solve3(n, r, seq)); return ans; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; pre(n); vector <int> r(3); vector <vector<int>> seq(3, vector <int> (n)); for (int t = 0; t < 3; t++) { cin >> r[t]; string s; cin >> s; for (int i = 0; i < n; i++) { seq[t][i] = s[i] - '0'; } } cout << solveUnion(n, r, seq); 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 | #include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; const int N = 10001; int binom[N][N], vals[N][N]; int vals2[N]; void add(int &x, int val) { x += val; if (x >= MOD) { x -= MOD; } } void sub(int &x, int val) { x -= val; if (x < 0) { x += MOD; } } void pre(int n) { for (int i = 0; i <= n; i++) { binom[i][0] = 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) { binom[i][j] = binom[i-1][j] + binom[i-1][j-1]; if (binom[i][j] >= MOD) { binom[i][j] -= MOD; } } } int solveFinal3(int r1, int r2, int r3, int y1, int y2, int y3, int y4) { int m = y3 + y4; for (int diff = 0; diff <= m; diff++) for (int sum = 0; sum <= m; sum++) { vals[diff][sum] = 0; } for (int x3 = 0; x3 <= y3; x3++) { for (int x4 = 0; x4 <= y4; x4++) { int diff = x4 - x3 + y3; int sum = x4 + x3; vals[diff][sum] = (vals[diff][sum] + (long long) binom[y3][x3] * binom[y4][x4]) % MOD; } } for (int diff = 0; diff <= m; diff++) for (int sum = 0; sum <= m; sum++) { if (diff > 0) { add(vals[diff][sum], vals[diff - 1][sum]); } if (sum > 0) { add(vals[diff][sum], vals[diff][sum - 1]); } if (diff > 0 && sum > 0) { sub(vals[diff][sum], vals[diff - 1][sum - 1]); } } int ans = 0; for (int x1 = 0; x1 <= y1; x1++) { for (int x2 = 0; x2 <= y2; x2++) { int maxDiff = min(r1 - x1 - x2, m); int maxSum = min({r2 - y2 - x1 + x2, r3 - y1 + x1 - x2, m}); if (maxDiff < 0 || maxSum < 0) { continue; } int mul = ((long long) binom[y1][x1] * binom[y2][x2]) % MOD; ans = (ans + (long long) mul * vals[maxDiff][maxSum]) % MOD; } } return ans; } int solveFinal2(int r1, int r2, int y1, int y2) { int ans = 0; for (int mx = 0; mx <= y2; mx++) { vals2[mx] = 0; } for (int x2 = 0; x2 <= y2; x2++) { vals2[x2] = binom[y2][x2]; if (x2 > 0) { vals2[x2] = (vals2[x2] + vals2[x2 - 1]) % MOD; } } for (int x1 = 0; x1 <= y1; x1++) { int mx = min({r1 - x1, r2 - y1 + x1, y2}); if (mx < 0) { continue; } ans = (ans + (long long) binom[y1][x1] * vals2[mx]) % MOD; } return ans; } int solve3(int n, const vector <int> &r, const vector <vector<int>> &seq) { vector <int> y(4, 0); for (int i = 0; i < n; i++) { int cnt = 0; for (int t = 0; t < 3; t++) { cnt += seq[t][i]; } if (cnt == 0 || cnt == 3) { y[3]++; } else { int outlier = cnt == 1; for (int t = 0; t < 3; t++) { if (seq[t][i] == outlier) { y[2 - t]++; break; } } } } return solveFinal3(r[0], r[1], r[2], y[0], y[1], y[2], y[3]); } int solve2(int n, const vector <int> &r, const vector <vector<int>> &seq) { int cnt[2] = {0, 0}; for (int i = 0; i < n; i++) { cnt[seq[0][i] == seq[1][i]]++; } return solveFinal2(r[0], r[1], cnt[0], cnt[1]); } int solveUnion(int n, const vector <int> &r, const vector <vector<int>> &seq) { int ans = 0; for (int t = 0; t < 3; t++) { for (int chg = 0; chg <= r[t]; chg++) { add(ans, binom[n][chg]); } } sub(ans, solve2(n, {r[0], r[1]}, {seq[0], seq[1]})); sub(ans, solve2(n, {r[0], r[2]}, {seq[0], seq[2]})); sub(ans, solve2(n, {r[1], r[2]}, {seq[1], seq[2]})); add(ans, solve3(n, r, seq)); return ans; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; pre(n); vector <int> r(3); vector <vector<int>> seq(3, vector <int> (n)); for (int t = 0; t < 3; t++) { cin >> r[t]; string s; cin >> s; for (int i = 0; i < n; i++) { seq[t][i] = s[i] - '0'; } } cout << solveUnion(n, r, seq); return 0; } |