#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define dbl(k, x) fixed << setprecision(k) << (x) template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 1e4 + 7; const int M = 1e9 + 7; LL fast(LL a, LL n) { LL w = 1; while(n) { if(n & 1) w = w * a % M; a = a * a % M; n /= 2; } return w; } LL fac[N], rev[N], pre[N]; LL choose(int n, int k) { assert(0 <= k && k <= n); return fac[n] * rev[k] % M * rev[n - k] % M; } int n, r[3], x[3][N]; LL solve1(int t1) { LL ans = 0; for(int i = 0; i <= r[t1]; i++) ans = (ans + choose(n, i)) % M; return ans; } LL solve2(int t1, int t2) { //DBG(t1, t2); LL ans = 0; int l[2] = {0, 0}; for(int i = 0; i < n; i++) l[x[t1][i] != x[t2][i]]++; pre[0] = 0; for(int i = 1; i <= l[1] + 1; i++) pre[i] = (pre[i - 1] + choose(l[1], i - 1)) % M; for(int i = 0; i <= min({r[t1], r[t2], l[0]}); i++) { LL tmp = 0; // j + i <= r[t1] && l[1] - j + i <= r[t2] // j <= r[t1] - i && l[1] - r[t2] + i <= j int a = max(0, l[1] - r[t2] + i), b = min(r[t1] - i, l[1]); if(a <= b) tmp = (tmp + pre[b + 1] - pre[a] + M) % M; ans = (ans + tmp * choose(l[0], i)) % M; //DBG(tmp, choose(l[0],i), ans); } return ans; } int sum[N][N]; LL tmp0[N], tmp1[N], tmp2[N]; LL solve3() { LL ans = 0; int l[4] = {0, 0, 0, 0}; for(int i = 0; i < n; i++) { if(x[0][i] == x[1][i] && x[1][i] == x[2][i]) l[3]++; else if(x[1][i] == x[2][i]) l[0]++; else if(x[0][i] == x[2][i]) l[1]++; else l[2]++; } for(int i = 0; i <= l[0]; i++) tmp0[i] = choose(l[0], i); for(int i = 0; i <= l[1]; i++) tmp1[i] = choose(l[1], i); for(int i = 0; i <= l[2]; i++) tmp2[i] = choose(l[2], i); for(int i = 0; i <= l[1]; i++) { for(int j = 0; j <= l[2]; j++) { sum[i + j + 1][i - j + l[2] + 1] = tmp1[i] * tmp2[j] % M; } } for(int i = 1; i <= l[1] + l[2] + 1; i++) { for(int j = 1; j <= l[1] + l[2] + 1; j++) { LL y = (LL)sum[i][j] + sum[i - 1][j] + sum[i][j - 1] + M - sum[i - 1][j - 1]; sum[i][j] = y % M; } } //DBG(l[0], l[1], l[2], l[3]); for(int i = 0; i <= min({r[0], r[1], r[2], l[3]}); i++) { LL tmp = 0; int c0 = r[0] - l[0] - i; int c1 = r[1] - l[1] - i; int c2 = r[2] - l[2] - i; for(int x0 = 0; x0 <= l[0]; x0++) { int a = min(l[1] + l[2], c0 + x0); int b1 = max(0, x0 - c1 + l[2]), b2 = min(l[1] + l[2], c2 - x0 + l[2]); if(b1 <= b2 && a >= 0) tmp = (tmp + tmp0[x0] * (sum[a + 1][b2 + 1] - sum[a + 1][b1])) % M; //DBG(i, a, b1, b2, x0, tmp); } ans = (ans + tmp * choose(l[3], i)) % M; } return ans; } int main() { _upgrade cin >> n; for(int i = 0; i < 3; i++) { cin >> r[i]; string s; cin >> s; for(int j = 0; j < n; j++) x[i][j] = s[j] - '0'; } fac[0] = 1; for(int i = 1; i <= n; i++) fac[i] = (LL)i * fac[i - 1] % M; rev[n] = fast(fac[n], M - 2); for(int i = n; i; i--) rev[i - 1] = (LL)i * rev[i] % M; LL ans = solve1(0) + solve1(1) + solve1(2); //DBG(ans); ans -= solve2(0, 1) + solve2(0, 2) + solve2(1, 2); //DBG(ans); ans += solve3(); ans = (ans % M + M) % M; //DBG(ans); cout << ans << "\n"; 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define dbl(k, x) fixed << setprecision(k) << (x) template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 1e4 + 7; const int M = 1e9 + 7; LL fast(LL a, LL n) { LL w = 1; while(n) { if(n & 1) w = w * a % M; a = a * a % M; n /= 2; } return w; } LL fac[N], rev[N], pre[N]; LL choose(int n, int k) { assert(0 <= k && k <= n); return fac[n] * rev[k] % M * rev[n - k] % M; } int n, r[3], x[3][N]; LL solve1(int t1) { LL ans = 0; for(int i = 0; i <= r[t1]; i++) ans = (ans + choose(n, i)) % M; return ans; } LL solve2(int t1, int t2) { //DBG(t1, t2); LL ans = 0; int l[2] = {0, 0}; for(int i = 0; i < n; i++) l[x[t1][i] != x[t2][i]]++; pre[0] = 0; for(int i = 1; i <= l[1] + 1; i++) pre[i] = (pre[i - 1] + choose(l[1], i - 1)) % M; for(int i = 0; i <= min({r[t1], r[t2], l[0]}); i++) { LL tmp = 0; // j + i <= r[t1] && l[1] - j + i <= r[t2] // j <= r[t1] - i && l[1] - r[t2] + i <= j int a = max(0, l[1] - r[t2] + i), b = min(r[t1] - i, l[1]); if(a <= b) tmp = (tmp + pre[b + 1] - pre[a] + M) % M; ans = (ans + tmp * choose(l[0], i)) % M; //DBG(tmp, choose(l[0],i), ans); } return ans; } int sum[N][N]; LL tmp0[N], tmp1[N], tmp2[N]; LL solve3() { LL ans = 0; int l[4] = {0, 0, 0, 0}; for(int i = 0; i < n; i++) { if(x[0][i] == x[1][i] && x[1][i] == x[2][i]) l[3]++; else if(x[1][i] == x[2][i]) l[0]++; else if(x[0][i] == x[2][i]) l[1]++; else l[2]++; } for(int i = 0; i <= l[0]; i++) tmp0[i] = choose(l[0], i); for(int i = 0; i <= l[1]; i++) tmp1[i] = choose(l[1], i); for(int i = 0; i <= l[2]; i++) tmp2[i] = choose(l[2], i); for(int i = 0; i <= l[1]; i++) { for(int j = 0; j <= l[2]; j++) { sum[i + j + 1][i - j + l[2] + 1] = tmp1[i] * tmp2[j] % M; } } for(int i = 1; i <= l[1] + l[2] + 1; i++) { for(int j = 1; j <= l[1] + l[2] + 1; j++) { LL y = (LL)sum[i][j] + sum[i - 1][j] + sum[i][j - 1] + M - sum[i - 1][j - 1]; sum[i][j] = y % M; } } //DBG(l[0], l[1], l[2], l[3]); for(int i = 0; i <= min({r[0], r[1], r[2], l[3]}); i++) { LL tmp = 0; int c0 = r[0] - l[0] - i; int c1 = r[1] - l[1] - i; int c2 = r[2] - l[2] - i; for(int x0 = 0; x0 <= l[0]; x0++) { int a = min(l[1] + l[2], c0 + x0); int b1 = max(0, x0 - c1 + l[2]), b2 = min(l[1] + l[2], c2 - x0 + l[2]); if(b1 <= b2 && a >= 0) tmp = (tmp + tmp0[x0] * (sum[a + 1][b2 + 1] - sum[a + 1][b1])) % M; //DBG(i, a, b1, b2, x0, tmp); } ans = (ans + tmp * choose(l[3], i)) % M; } return ans; } int main() { _upgrade cin >> n; for(int i = 0; i < 3; i++) { cin >> r[i]; string s; cin >> s; for(int j = 0; j < n; j++) x[i][j] = s[j] - '0'; } fac[0] = 1; for(int i = 1; i <= n; i++) fac[i] = (LL)i * fac[i - 1] % M; rev[n] = fast(fac[n], M - 2); for(int i = n; i; i--) rev[i - 1] = (LL)i * rev[i] % M; LL ans = solve1(0) + solve1(1) + solve1(2); //DBG(ans); ans -= solve2(0, 1) + solve2(0, 2) + solve2(1, 2); //DBG(ans); ans += solve3(); ans = (ans % M + M) % M; //DBG(ans); cout << ans << "\n"; return 0; } |