#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; using LL = long long; struct Foo { vector<int> sil; vector<vector<int>> memo; vector<vector<int>> memo_comb; const int m = 10101; Foo() { memo.resize(m); memo_comb.resize(m); sil.resize(m + 1); sil[0] = 1; for (LL i = 1; i <= m; i++) { sil[i] = sil[i - 1] * i % mod; } } LL pot(LL b, int e) { LL r = 1; while (e) { if (e & 1) r = r * b % mod; b = b * b % mod; e >>= 1; } return r; } LL dziel(LL a, LL b) { return (a * pot(b, mod - 2)) % mod; } int inv(int a) { return dziel(1, a); } int comb_(int n, int k) { k = min(k, n - k); //cerr << "comb " << n << " " << k << endl; return sil[n] * LL(inv(sil[k])) % mod * LL(inv(sil[n - k])) % mod; } void prep_comb(int n) { if (memo_comb[n].empty()) { cerr << "prep_comb " << n << endl; memo_comb[n].resize(n / 2 + 1); for (size_t k = 0; k < memo_comb[n].size(); k++) { memo_comb[n][k] = comb_(n, k); } } } int comb(int n, int k) { prep_comb(n); k = min(k, n - k); return memo_comb[n][k]; } int cnt(int bits, int dist) { if (dist > bits) { cerr << "dupa3" << endl; dist = bits; //exit(0); } if (memo[bits].size() == 0) { memo[bits].resize(bits + 1); LL summ = 0LL; for (int i = 0; i <= bits; i++) { summ += comb(bits, i); summ %= mod; memo[bits][i] = summ; } } auto r = memo[bits][dist]; return r; } } ncalc; int calc2(int bits, int dist, int r1, int r2) { LL res = 0; int r1min = max(0, dist - r2); int r1max = min(r1, dist); for (int r1_ = r1min; r1_ <= r1max; r1_++) { int r2_ = dist - r1_; int left = min(r1 - r1_, r2 - r2_); left = min(left, bits - dist); res += ncalc.comb(dist, r1_) * LL(ncalc.cnt(bits - dist, left)); res %= mod; } return res; } int calc3(int t0, int t1, int t2, int t3, int r0, int r1, int r2) { LL res = 0; int rAmin = 0; rAmin = max(rAmin, t3 - min(r1, r2)); rAmin = max(rAmin, t3 - r1 + t2 - r2); rAmin = max(rAmin, -r1 + t1 + t3 - min(r2, t1)); rAmin = max(rAmin, -r2 + t2 + t3 - min(r1, t2)); int rAmax = min(r0, t3); rAmax = min(rAmax, r0 - t1 - t2 + min(r1, t2) + min(r2, t1)); //cerr << "A " << rAmin << " " << rAmax << endl; for (int rA = rAmin; rA <= rAmax; rA++) { LL cA = ncalc.comb(t3, rA) % mod; int rBmin = 0; rBmin = max(rBmin, -r2 + t2 + t3 - rA); rBmin = max(rBmin, -r0 + rA + t1 + t2 - r2); int rBmax = min(r1, t2); rBmax = min(rBmax, r1 - t1 - t3 + rA + min(r2, t1)); //cerr << "B " << rBmin << " " << (rBmax-rBmin) << endl; for (int rB = rBmin; rB <= rBmax; rB++) { LL cBA = cA * ncalc.comb(t2, rB) % mod; int rCmin = 0; rCmin = max(rCmin, -r1 + rB + t1 + t3 - rA); rCmin = max(rCmin, -r0 + rA + t1 + t2 - rB); int rCmax = min(r2, t1); rCmax = min(rCmax, r2 - t2 - t3 + rA + rB); //cerr << "C " << rB << " " << rCmin << " " << (rCmax-rCmin) << endl; if (rCmax - rCmin < -3) { rB += (rCmin - rCmax) / 2 - 1; } for (int rC = rCmin; rC <= rCmax; rC++) { LL cCBA = cBA * ncalc.comb(t1, rC) % mod; int rAf = r0 - rA - t1 - t2 + rB + rC; int rBf = r1 - rB - t1 - t3 + rA + rC; int rCf = r2 - rC - t2 - t3 + rA + rB; if (rAf < 0 || rBf < 0 || rCf < 0) { cerr << "dupa2" << endl; continue; } int left_ = min(min(rAf, rBf), rCf); left_ = min(left_, t0); LL tmp = cCBA * ncalc.cnt(t0, left_) % mod; res += tmp; res %= mod; } } } return res; } int calc2f(int t0, int t1, int r0, int r1) { int bits = t0 + t1; LL res = 0; res += ncalc.cnt(bits, r0); res += ncalc.cnt(bits, r1); res -= calc2(bits, t1, r0, r1); while (res < 0) res += mod; return res % mod; } int calc3f(int t0, int t1, int t2, int t3, int r0, int r1, int r2) { int bits = t0 + t1 + t2 + t3; LL res = 0; res += ncalc.cnt(bits, r0); cerr << res << endl; res += ncalc.cnt(bits, r1); cerr << res << endl; res += ncalc.cnt(bits, r2); cerr << res << endl; res -= calc2(bits, t2 + t3, r0, r1); cerr << res << endl; res -= calc2(bits, t1 + t3, r0, r2); cerr << res << endl; res -= calc2(bits, t1 + t2, r1, r2); cerr << res << endl; res += calc3(t0, t1, t2, t3, r0, r1, r2); while (res < 0) res += mod; return res % mod; } vector<int> group(string& v1, string& v2, string& v3) { // t[A=B=C, !C, !B, !A] vector<int> t(4); for (size_t i = 0; i < v1.size(); i++) { if (v1[i] == v2[i] && v2[i] == v3[i]) { t[0] += 1; } else if (v1[i] == v2[i]) { t[1] += 1; } else if (v1[i] == v3[i]) { t[2] += 1; } else { t[3] += 1; } } return t; } int main() { ios_base::sync_with_stdio(0); //vector<int> t = {46, 62, 37, 55}; //vector<int> r = {120, 49, 118}; vector<int> t = {2509, 2492, 2518, 2481}; vector<int> r = {2881, 7075, 3223}; if (true) { int n; cin >> n; //vector<int> r(3); vector<string> s(3); for (int i = 0; i < 3; i++) { cin >> r[i] >> s[i]; } t = group(s[0], s[1], s[2]); } int t0 = t[0], t1 = t[1], t2 = t[2], t3 = t[3]; int r0 = r[0], r1 = r[1], r2 = r[2]; cerr << t0 << " " << t1 << " " << t2 << " " << t3 << endl; int diffs = t1 + t2 + t3; if (t1 == diffs || t2 == diffs || t3 == diffs) { // repeated centers - this is now faster because of slow calc3 cerr << "repeated centers" << endl; if (t1 == diffs) { // A==B r0 = max(r[0], r[1]); r1 = r[2]; } else if (t2 == diffs) { // A==C r0 = max(r[0], r[2]); r1 = r[1]; } else { // B==C r0 = max(r[1], r[2]); r1 = r[0]; } int res = calc2f(t0, diffs, r0, r1); cout << res << endl; } else { int res = calc3f(t0, t1, t2, t3, r0, r1, r2); cout << res << 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 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 | #include <bits/stdc++.h> using namespace std; const int mod = 1000000007; using LL = long long; struct Foo { vector<int> sil; vector<vector<int>> memo; vector<vector<int>> memo_comb; const int m = 10101; Foo() { memo.resize(m); memo_comb.resize(m); sil.resize(m + 1); sil[0] = 1; for (LL i = 1; i <= m; i++) { sil[i] = sil[i - 1] * i % mod; } } LL pot(LL b, int e) { LL r = 1; while (e) { if (e & 1) r = r * b % mod; b = b * b % mod; e >>= 1; } return r; } LL dziel(LL a, LL b) { return (a * pot(b, mod - 2)) % mod; } int inv(int a) { return dziel(1, a); } int comb_(int n, int k) { k = min(k, n - k); //cerr << "comb " << n << " " << k << endl; return sil[n] * LL(inv(sil[k])) % mod * LL(inv(sil[n - k])) % mod; } void prep_comb(int n) { if (memo_comb[n].empty()) { cerr << "prep_comb " << n << endl; memo_comb[n].resize(n / 2 + 1); for (size_t k = 0; k < memo_comb[n].size(); k++) { memo_comb[n][k] = comb_(n, k); } } } int comb(int n, int k) { prep_comb(n); k = min(k, n - k); return memo_comb[n][k]; } int cnt(int bits, int dist) { if (dist > bits) { cerr << "dupa3" << endl; dist = bits; //exit(0); } if (memo[bits].size() == 0) { memo[bits].resize(bits + 1); LL summ = 0LL; for (int i = 0; i <= bits; i++) { summ += comb(bits, i); summ %= mod; memo[bits][i] = summ; } } auto r = memo[bits][dist]; return r; } } ncalc; int calc2(int bits, int dist, int r1, int r2) { LL res = 0; int r1min = max(0, dist - r2); int r1max = min(r1, dist); for (int r1_ = r1min; r1_ <= r1max; r1_++) { int r2_ = dist - r1_; int left = min(r1 - r1_, r2 - r2_); left = min(left, bits - dist); res += ncalc.comb(dist, r1_) * LL(ncalc.cnt(bits - dist, left)); res %= mod; } return res; } int calc3(int t0, int t1, int t2, int t3, int r0, int r1, int r2) { LL res = 0; int rAmin = 0; rAmin = max(rAmin, t3 - min(r1, r2)); rAmin = max(rAmin, t3 - r1 + t2 - r2); rAmin = max(rAmin, -r1 + t1 + t3 - min(r2, t1)); rAmin = max(rAmin, -r2 + t2 + t3 - min(r1, t2)); int rAmax = min(r0, t3); rAmax = min(rAmax, r0 - t1 - t2 + min(r1, t2) + min(r2, t1)); //cerr << "A " << rAmin << " " << rAmax << endl; for (int rA = rAmin; rA <= rAmax; rA++) { LL cA = ncalc.comb(t3, rA) % mod; int rBmin = 0; rBmin = max(rBmin, -r2 + t2 + t3 - rA); rBmin = max(rBmin, -r0 + rA + t1 + t2 - r2); int rBmax = min(r1, t2); rBmax = min(rBmax, r1 - t1 - t3 + rA + min(r2, t1)); //cerr << "B " << rBmin << " " << (rBmax-rBmin) << endl; for (int rB = rBmin; rB <= rBmax; rB++) { LL cBA = cA * ncalc.comb(t2, rB) % mod; int rCmin = 0; rCmin = max(rCmin, -r1 + rB + t1 + t3 - rA); rCmin = max(rCmin, -r0 + rA + t1 + t2 - rB); int rCmax = min(r2, t1); rCmax = min(rCmax, r2 - t2 - t3 + rA + rB); //cerr << "C " << rB << " " << rCmin << " " << (rCmax-rCmin) << endl; if (rCmax - rCmin < -3) { rB += (rCmin - rCmax) / 2 - 1; } for (int rC = rCmin; rC <= rCmax; rC++) { LL cCBA = cBA * ncalc.comb(t1, rC) % mod; int rAf = r0 - rA - t1 - t2 + rB + rC; int rBf = r1 - rB - t1 - t3 + rA + rC; int rCf = r2 - rC - t2 - t3 + rA + rB; if (rAf < 0 || rBf < 0 || rCf < 0) { cerr << "dupa2" << endl; continue; } int left_ = min(min(rAf, rBf), rCf); left_ = min(left_, t0); LL tmp = cCBA * ncalc.cnt(t0, left_) % mod; res += tmp; res %= mod; } } } return res; } int calc2f(int t0, int t1, int r0, int r1) { int bits = t0 + t1; LL res = 0; res += ncalc.cnt(bits, r0); res += ncalc.cnt(bits, r1); res -= calc2(bits, t1, r0, r1); while (res < 0) res += mod; return res % mod; } int calc3f(int t0, int t1, int t2, int t3, int r0, int r1, int r2) { int bits = t0 + t1 + t2 + t3; LL res = 0; res += ncalc.cnt(bits, r0); cerr << res << endl; res += ncalc.cnt(bits, r1); cerr << res << endl; res += ncalc.cnt(bits, r2); cerr << res << endl; res -= calc2(bits, t2 + t3, r0, r1); cerr << res << endl; res -= calc2(bits, t1 + t3, r0, r2); cerr << res << endl; res -= calc2(bits, t1 + t2, r1, r2); cerr << res << endl; res += calc3(t0, t1, t2, t3, r0, r1, r2); while (res < 0) res += mod; return res % mod; } vector<int> group(string& v1, string& v2, string& v3) { // t[A=B=C, !C, !B, !A] vector<int> t(4); for (size_t i = 0; i < v1.size(); i++) { if (v1[i] == v2[i] && v2[i] == v3[i]) { t[0] += 1; } else if (v1[i] == v2[i]) { t[1] += 1; } else if (v1[i] == v3[i]) { t[2] += 1; } else { t[3] += 1; } } return t; } int main() { ios_base::sync_with_stdio(0); //vector<int> t = {46, 62, 37, 55}; //vector<int> r = {120, 49, 118}; vector<int> t = {2509, 2492, 2518, 2481}; vector<int> r = {2881, 7075, 3223}; if (true) { int n; cin >> n; //vector<int> r(3); vector<string> s(3); for (int i = 0; i < 3; i++) { cin >> r[i] >> s[i]; } t = group(s[0], s[1], s[2]); } int t0 = t[0], t1 = t[1], t2 = t[2], t3 = t[3]; int r0 = r[0], r1 = r[1], r2 = r[2]; cerr << t0 << " " << t1 << " " << t2 << " " << t3 << endl; int diffs = t1 + t2 + t3; if (t1 == diffs || t2 == diffs || t3 == diffs) { // repeated centers - this is now faster because of slow calc3 cerr << "repeated centers" << endl; if (t1 == diffs) { // A==B r0 = max(r[0], r[1]); r1 = r[2]; } else if (t2 == diffs) { // A==C r0 = max(r[0], r[2]); r1 = r[1]; } else { // B==C r0 = max(r[1], r[2]); r1 = r[0]; } int res = calc2f(t0, diffs, r0, r1); cout << res << endl; } else { int res = calc3f(t0, t1, t2, t3, r0, r1, r2); cout << res << endl; } } |