#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef const int cint; const int mod = 1000000007; const int N = 10005; int n, i; int V, res, out; int a, b, c, d; int A, B, C, D; int R1, R2, R3; int C1, C2, E1, E2; int fac [N]; int rev [N]; char t1 [N]; char t2 [N]; char t3 [N]; int L1 [N][N]; int L2 [N][N]; inline int pw (int a, int b) { int res = 1; while (b != 0) { if (b%2 == 1) res = (1ll * res * a)%mod; a = (1ll * a * a)%mod; b /= 2; } return res; } inline int newton (int n, int k) { return (1ll*((1ll*fac[n]*rev[k])%mod)*rev[n-k])%mod; } inline void add (int& a, cint& b) { a += b; if (a < 0) a += mod; if (a >= mod) a -= mod; } inline void update (int tab [N][N], cint& a, cint& b, cint& v) { if (a<0 || a>A) return; if (b<0 || b>B) return; add(tab[a][b], v); } int intersection (char* t1, char* t2, int r1, int r2) { res = 0; A = B = 0; for (i=1; i<=n; i++) (t1[i] == t2[i] ? B : A)++; for (a=0; a<=A; a++) { D = min(B, min(r1 - a, r2 - A + a)); for (b=0; b<=D; b++) add(res, (1ll * L2[A][a] * L2[B][b])%mod); } return res; } int main () { scanf ("%d%d%s%d%s%d%s", &n, &R1, t1+1, &R2, t2+1, &R3, t3+1); fac[0] = rev[0] = 1; for (i=1; i<=n; i++) { fac[i] = (1ll * fac[i-1] * i)%mod; rev[i] = pw(fac[i], mod-2); } for (i=1; i<=n; i++) { t1[i] -= '0'; t2[i] -= '0'; t3[i] -= '0'; t1[i] ^= t3[i]; t2[i] ^= t3[i]; t3[i] ^= t3[i]; if (t1[i]==0 && t2[i]==0) A++; if (t1[i]==1 && t2[i]==1) B++; if (t1[i]==0 && t2[i]==1) C++; if (t1[i]==1 && t2[i]==0) D++; } for (c=0; c<=C; c++) for (d=0; d<=D; d++) { C1 = A + B; C1 = min(C1, R3 - c - d); C2 = A; C2 = min(C2, R1 - B - D - c + d); C2 = min(C2, R2 - B - C + c - d); if (C1 < 0) continue; if (C2 < -B) continue; E1 = E2 = (C1 - C2)/2; if ((C1 - C2)%2 != 0) { if (C1 - C2 < 0) E1--; if (C1 - C2 > 0) E2++; } if (C1-E2 < 0) continue; V = (1ll * newton(C, c) * newton(D, d))%mod; if (E1 >= B) { update(L2, max(0, C2), max(0, -C2), V); continue; } if (E2 <= 0) { update(L1, max(0, C1 - B), min(B, C1), V); continue; } if (E1 == E2) E2++; update(L1, max(0, C1 - B), min(B, C1), V); update(L2, max(0, C2), max(0, -C2), V); update(L1, min(C1-E1, A), E1, -V); update(L2, min(C2+E2, A), E2, -V); } for (a=0; a<=A; a++) for (b=0; b<=B; b++) { update(L1, a+1, b-1, L1[a][b]); update(L2, a+1, b+1, L2[a][b]); } for (b=B; b>=0; b--) update(L1, A, b-1, L1[A][b]); for (b=0; b<=B; b++) update(L2, A, b+1, L2[A][b]); for (a=A; a>=0; a--) for (b=0; b<=B; b++) { update(L1, a-1, b, L1[a][b]); update(L2, a-1, b, L2[a][b]); } for (a=0; a<=A; a++) for (b=0; b<=B; b++) add(L1[a][b], L2[a][b]); for (a=0; a<=n; a++) for (b=0; b<=n; b++) { if (b>a) { L2[a][b] = 0; continue; } if (b==0 || b==a) { L2[a][b] = 1; continue; } L2[a][b] = L2[a-1][b-1] + L2[a-1][b]; if (L2[a][b] >= mod) L2[a][b] -= mod; } for (a=0; a<=A; a++) for (b=0; b<=B; b++) { res = (1ll * L2[A][a] * L2[B][b])%mod; res = (1ll * res * L1[a][b])%mod; add(out, res); } for (i=0; i<=R1; i++) add(out, L2[n][i]); for (i=0; i<=R2; i++) add(out, L2[n][i]); for (i=0; i<=R3; i++) add(out, L2[n][i]); add(out, -intersection(t1, t2, R1, R2)); add(out, -intersection(t1, t3, R1, R3)); add(out, -intersection(t2, t3, R2, R3)); printf ("%d\n", out); 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 199 200 201 202 203 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef const int cint; const int mod = 1000000007; const int N = 10005; int n, i; int V, res, out; int a, b, c, d; int A, B, C, D; int R1, R2, R3; int C1, C2, E1, E2; int fac [N]; int rev [N]; char t1 [N]; char t2 [N]; char t3 [N]; int L1 [N][N]; int L2 [N][N]; inline int pw (int a, int b) { int res = 1; while (b != 0) { if (b%2 == 1) res = (1ll * res * a)%mod; a = (1ll * a * a)%mod; b /= 2; } return res; } inline int newton (int n, int k) { return (1ll*((1ll*fac[n]*rev[k])%mod)*rev[n-k])%mod; } inline void add (int& a, cint& b) { a += b; if (a < 0) a += mod; if (a >= mod) a -= mod; } inline void update (int tab [N][N], cint& a, cint& b, cint& v) { if (a<0 || a>A) return; if (b<0 || b>B) return; add(tab[a][b], v); } int intersection (char* t1, char* t2, int r1, int r2) { res = 0; A = B = 0; for (i=1; i<=n; i++) (t1[i] == t2[i] ? B : A)++; for (a=0; a<=A; a++) { D = min(B, min(r1 - a, r2 - A + a)); for (b=0; b<=D; b++) add(res, (1ll * L2[A][a] * L2[B][b])%mod); } return res; } int main () { scanf ("%d%d%s%d%s%d%s", &n, &R1, t1+1, &R2, t2+1, &R3, t3+1); fac[0] = rev[0] = 1; for (i=1; i<=n; i++) { fac[i] = (1ll * fac[i-1] * i)%mod; rev[i] = pw(fac[i], mod-2); } for (i=1; i<=n; i++) { t1[i] -= '0'; t2[i] -= '0'; t3[i] -= '0'; t1[i] ^= t3[i]; t2[i] ^= t3[i]; t3[i] ^= t3[i]; if (t1[i]==0 && t2[i]==0) A++; if (t1[i]==1 && t2[i]==1) B++; if (t1[i]==0 && t2[i]==1) C++; if (t1[i]==1 && t2[i]==0) D++; } for (c=0; c<=C; c++) for (d=0; d<=D; d++) { C1 = A + B; C1 = min(C1, R3 - c - d); C2 = A; C2 = min(C2, R1 - B - D - c + d); C2 = min(C2, R2 - B - C + c - d); if (C1 < 0) continue; if (C2 < -B) continue; E1 = E2 = (C1 - C2)/2; if ((C1 - C2)%2 != 0) { if (C1 - C2 < 0) E1--; if (C1 - C2 > 0) E2++; } if (C1-E2 < 0) continue; V = (1ll * newton(C, c) * newton(D, d))%mod; if (E1 >= B) { update(L2, max(0, C2), max(0, -C2), V); continue; } if (E2 <= 0) { update(L1, max(0, C1 - B), min(B, C1), V); continue; } if (E1 == E2) E2++; update(L1, max(0, C1 - B), min(B, C1), V); update(L2, max(0, C2), max(0, -C2), V); update(L1, min(C1-E1, A), E1, -V); update(L2, min(C2+E2, A), E2, -V); } for (a=0; a<=A; a++) for (b=0; b<=B; b++) { update(L1, a+1, b-1, L1[a][b]); update(L2, a+1, b+1, L2[a][b]); } for (b=B; b>=0; b--) update(L1, A, b-1, L1[A][b]); for (b=0; b<=B; b++) update(L2, A, b+1, L2[A][b]); for (a=A; a>=0; a--) for (b=0; b<=B; b++) { update(L1, a-1, b, L1[a][b]); update(L2, a-1, b, L2[a][b]); } for (a=0; a<=A; a++) for (b=0; b<=B; b++) add(L1[a][b], L2[a][b]); for (a=0; a<=n; a++) for (b=0; b<=n; b++) { if (b>a) { L2[a][b] = 0; continue; } if (b==0 || b==a) { L2[a][b] = 1; continue; } L2[a][b] = L2[a-1][b-1] + L2[a-1][b]; if (L2[a][b] >= mod) L2[a][b] -= mod; } for (a=0; a<=A; a++) for (b=0; b<=B; b++) { res = (1ll * L2[A][a] * L2[B][b])%mod; res = (1ll * res * L1[a][b])%mod; add(out, res); } for (i=0; i<=R1; i++) add(out, L2[n][i]); for (i=0; i<=R2; i++) add(out, L2[n][i]); for (i=0; i<=R3; i++) add(out, L2[n][i]); add(out, -intersection(t1, t2, R1, R2)); add(out, -intersection(t1, t3, R1, R3)); add(out, -intersection(t2, t3, R2, R3)); printf ("%d\n", out); return 0; } |