#define make_pair mp #define emplace_back pb #include <bits/stdc++.h> using namespace std; const int N = 1e4 + 9; const long long MOD = 1e9 + 7; int n, d1, d2, d3, z0, z1, z2, z3; char s1[N], s2[N], s3[N]; long long sc[N], A[N], dwumz1[N], dwumz2[N], dwumz3[N], dwumz0[N]; unsigned int dwum[2*N][N]; long long prefsc(int x, int y) { y = min(y, z3); if(x > y) return 0; if(y < 0) return 0; if(x <= 0) return sc[y]; return sc[y] - sc[x - 1]; } int mod2(int x) { if(x > 0) return x % 2; return -(x % 2); } int maka(int b) { if(mod2(d2) == mod2(d3)) return min(b + (d2 - d3) / 2 - 1, z1); return min(b + (d2 - d3 - 1) / 2, z1); } int makb(int a) { if(mod2(d3) == mod2(d2)) return min(a + (d3 - d2) / 2, z2); return min(a + (d3 - d2 - 1) / 2, z2); } long long all() { d1 -= z3; d2 -= z2; d3 -= z1; //cout<<d1<<" "<<d2<<" "<<d3<<endl; //cout<<z0<<" "<<z1<<" "<<z2<<" "<<z3<<endl; sc[0] = 1; for(int i=1;i<=z3;i++) sc[i] = (long long)(sc[i-1] + (long long)dwum[z3][i]) % MOD; for(int i=0;i<=n;i++) dwumz0[i] = dwum[z0][i]; for(int i=0;i<=n;i++) dwumz1[i] = dwum[z1][i]; for(int i=0;i<=n;i++) dwumz2[i] = dwum[z2][i]; for(int i=0;i<=n;i++) dwumz3[i] = dwum[z3][i]; for(int k=-n;k<=z3;k++) { if(k >= 0) dwum[k+n+1][0] = dwumz3[k]; else dwum[k+n+1][0] = 0; for(int b=1;b<=z2;b++) { if(b + k >= 0) dwum[k+n+1][b] = (long long)((long long)dwum[k+n+1][b-1] + dwumz2[b] * dwumz3[b+k]) % MOD; else dwum[k+n+1][b] = 0; } } for(int a=0;a<=z1;a++) { int maksb = makb(a); for(int b=0;b<=maksb;b++) A[a] = (A[a] + dwumz2[b] * prefsc(a + b - d1, d2 + b - a)) % MOD; //cout<<a<<" "<<maksb<<" "<<A[a]<<" "<<a+maksb-d1<<" "<<d2+maksb-a<<endl; } long long ans = 0; for(int i=0;i<=z0;i++) { if(d1 + d2 - 2 * i < 0) break; int zakra = min((d1 + d2 - 2 * i) / 2, z1); if(i != 0) { for(int a=0;a<=zakra;a++) { int maksb = makb(a); if(maksb < 0) continue; int k1 = a - d1 + i - 1; int k2 = d2 - i - a + 1; if(k1 <= z3 && k1 + n + 1 >= 0) A[a] = A[a] - (long long)dwum[k1+n+1][maksb]; if(k2 <= z3 && k2 + n + 1 >= 0) A[a] = A[a] - (long long)dwum[k2+n+1][maksb]; A[a] %= MOD; //cout<<A[a]<<endl; } } long long tmp = 0; for(int a=0;a<=zakra;a++) tmp = (tmp + dwumz1[a] * A[a]) % MOD; tmp = tmp * dwumz0[i] % MOD; ans = (ans + tmp) % MOD; } for(int k=-n;k<=z3;k++) { if(k >= 0) dwum[k+n+1][0] = dwumz3[k]; else dwum[k+n+1][0] = 0; for(int a=1;a<=z1;a++) if(a + k >= 0) dwum[k+n+1][a] = (long long)((long long)dwum[k+n+1][a-1] + dwumz1[a] * dwumz3[a+k]) % MOD; else dwum[k+n+1][a] = 0; } for(int b=0;b<=z2;b++) { int maksa = maka(b); A[b] = 0; for(int a=0;a<=maksa;a++) A[b] = (A[b] + dwumz1[a] * prefsc(a + b - d1, d3 + a - b)) % MOD; } for(int i=0;i<=z0;i++) { if(d1 + d3 - 2 * i < 0) break; int zakrb = min((d1 + d3 - 2 * i) / 2, z2); if(i != 0) { for(int b=0;b<=zakrb;b++) { int maksa = maka(b); if(maksa < 0) continue; int k1 = b - d1 + i - 1; int k2 = d3 - i - b + 1; if(k1 <= z3 && k1 + n + 1 >= 0) A[b] = A[b] - (long long)dwum[k1+n+1][maksa]; if(k2 <= z3 && k2 + n + 1 >= 0) A[b] = A[b] - (long long)dwum[k2+n+1][maksa]; A[b] %= MOD; } } long long tmp = 0; for(int b=0;b<=zakrb;b++) tmp = (tmp + dwumz2[b] * A[b]) % MOD; tmp = tmp * dwumz0[i] % MOD; ans = (ans + tmp) % MOD; } if(ans < 0) ans += MOD; return ans; } long long one(int d) { long long ans = 0; for(int i=0;i<=d;i++) ans = (ans + (long long)dwum[n][i]) % MOD; return ans; } long long two(int z, int d1, int d2) { long long ans = 0; for(int i=0;i<=z;i++) { long long tmp = 0; for(int j=max(n-z+i-d2,0);j<=d1-i;j++) tmp = (tmp + (long long)dwum[n-z][j]) % MOD; tmp = tmp * (long long)dwum[z][i] % MOD; ans = (ans + tmp) % MOD; } return ans; } int main() { scanf("%d", &n); scanf("%d%s%d%s%d%s", &d1, s1, &d2, s2, &d3, s3); for(int i=0;i<n;i++) { if(s1[i] == s2[i] && s2[i] == s3[i]) z0++; else if(s1[i] == s2[i]) z1++; else if(s1[i] == s3[i]) z2++; else z3++; } dwum[0][0] = 1; dwum[1][0] = 1; dwum[1][1] = 1; for(int i=2;i<=n;i++) { dwum[i][0] = 1; for(int j=1;j<=i;j++) dwum[i][j] = (long long)((long long)dwum[i-1][j] + (long long)dwum[i-1][j-1]) % MOD; } long long ans = 0; ans = one(d1) + one(d2) + one(d3) - two(z1 + z0, d1, d2) - two(z2 + z0, d1, d3) - two(z3 + z0, d2, d3); ans += all(); ans %= MOD; if(ans < 0) ans += MOD; printf("%lld", ans); 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 | #define make_pair mp #define emplace_back pb #include <bits/stdc++.h> using namespace std; const int N = 1e4 + 9; const long long MOD = 1e9 + 7; int n, d1, d2, d3, z0, z1, z2, z3; char s1[N], s2[N], s3[N]; long long sc[N], A[N], dwumz1[N], dwumz2[N], dwumz3[N], dwumz0[N]; unsigned int dwum[2*N][N]; long long prefsc(int x, int y) { y = min(y, z3); if(x > y) return 0; if(y < 0) return 0; if(x <= 0) return sc[y]; return sc[y] - sc[x - 1]; } int mod2(int x) { if(x > 0) return x % 2; return -(x % 2); } int maka(int b) { if(mod2(d2) == mod2(d3)) return min(b + (d2 - d3) / 2 - 1, z1); return min(b + (d2 - d3 - 1) / 2, z1); } int makb(int a) { if(mod2(d3) == mod2(d2)) return min(a + (d3 - d2) / 2, z2); return min(a + (d3 - d2 - 1) / 2, z2); } long long all() { d1 -= z3; d2 -= z2; d3 -= z1; //cout<<d1<<" "<<d2<<" "<<d3<<endl; //cout<<z0<<" "<<z1<<" "<<z2<<" "<<z3<<endl; sc[0] = 1; for(int i=1;i<=z3;i++) sc[i] = (long long)(sc[i-1] + (long long)dwum[z3][i]) % MOD; for(int i=0;i<=n;i++) dwumz0[i] = dwum[z0][i]; for(int i=0;i<=n;i++) dwumz1[i] = dwum[z1][i]; for(int i=0;i<=n;i++) dwumz2[i] = dwum[z2][i]; for(int i=0;i<=n;i++) dwumz3[i] = dwum[z3][i]; for(int k=-n;k<=z3;k++) { if(k >= 0) dwum[k+n+1][0] = dwumz3[k]; else dwum[k+n+1][0] = 0; for(int b=1;b<=z2;b++) { if(b + k >= 0) dwum[k+n+1][b] = (long long)((long long)dwum[k+n+1][b-1] + dwumz2[b] * dwumz3[b+k]) % MOD; else dwum[k+n+1][b] = 0; } } for(int a=0;a<=z1;a++) { int maksb = makb(a); for(int b=0;b<=maksb;b++) A[a] = (A[a] + dwumz2[b] * prefsc(a + b - d1, d2 + b - a)) % MOD; //cout<<a<<" "<<maksb<<" "<<A[a]<<" "<<a+maksb-d1<<" "<<d2+maksb-a<<endl; } long long ans = 0; for(int i=0;i<=z0;i++) { if(d1 + d2 - 2 * i < 0) break; int zakra = min((d1 + d2 - 2 * i) / 2, z1); if(i != 0) { for(int a=0;a<=zakra;a++) { int maksb = makb(a); if(maksb < 0) continue; int k1 = a - d1 + i - 1; int k2 = d2 - i - a + 1; if(k1 <= z3 && k1 + n + 1 >= 0) A[a] = A[a] - (long long)dwum[k1+n+1][maksb]; if(k2 <= z3 && k2 + n + 1 >= 0) A[a] = A[a] - (long long)dwum[k2+n+1][maksb]; A[a] %= MOD; //cout<<A[a]<<endl; } } long long tmp = 0; for(int a=0;a<=zakra;a++) tmp = (tmp + dwumz1[a] * A[a]) % MOD; tmp = tmp * dwumz0[i] % MOD; ans = (ans + tmp) % MOD; } for(int k=-n;k<=z3;k++) { if(k >= 0) dwum[k+n+1][0] = dwumz3[k]; else dwum[k+n+1][0] = 0; for(int a=1;a<=z1;a++) if(a + k >= 0) dwum[k+n+1][a] = (long long)((long long)dwum[k+n+1][a-1] + dwumz1[a] * dwumz3[a+k]) % MOD; else dwum[k+n+1][a] = 0; } for(int b=0;b<=z2;b++) { int maksa = maka(b); A[b] = 0; for(int a=0;a<=maksa;a++) A[b] = (A[b] + dwumz1[a] * prefsc(a + b - d1, d3 + a - b)) % MOD; } for(int i=0;i<=z0;i++) { if(d1 + d3 - 2 * i < 0) break; int zakrb = min((d1 + d3 - 2 * i) / 2, z2); if(i != 0) { for(int b=0;b<=zakrb;b++) { int maksa = maka(b); if(maksa < 0) continue; int k1 = b - d1 + i - 1; int k2 = d3 - i - b + 1; if(k1 <= z3 && k1 + n + 1 >= 0) A[b] = A[b] - (long long)dwum[k1+n+1][maksa]; if(k2 <= z3 && k2 + n + 1 >= 0) A[b] = A[b] - (long long)dwum[k2+n+1][maksa]; A[b] %= MOD; } } long long tmp = 0; for(int b=0;b<=zakrb;b++) tmp = (tmp + dwumz2[b] * A[b]) % MOD; tmp = tmp * dwumz0[i] % MOD; ans = (ans + tmp) % MOD; } if(ans < 0) ans += MOD; return ans; } long long one(int d) { long long ans = 0; for(int i=0;i<=d;i++) ans = (ans + (long long)dwum[n][i]) % MOD; return ans; } long long two(int z, int d1, int d2) { long long ans = 0; for(int i=0;i<=z;i++) { long long tmp = 0; for(int j=max(n-z+i-d2,0);j<=d1-i;j++) tmp = (tmp + (long long)dwum[n-z][j]) % MOD; tmp = tmp * (long long)dwum[z][i] % MOD; ans = (ans + tmp) % MOD; } return ans; } int main() { scanf("%d", &n); scanf("%d%s%d%s%d%s", &d1, s1, &d2, s2, &d3, s3); for(int i=0;i<n;i++) { if(s1[i] == s2[i] && s2[i] == s3[i]) z0++; else if(s1[i] == s2[i]) z1++; else if(s1[i] == s3[i]) z2++; else z3++; } dwum[0][0] = 1; dwum[1][0] = 1; dwum[1][1] = 1; for(int i=2;i<=n;i++) { dwum[i][0] = 1; for(int j=1;j<=i;j++) dwum[i][j] = (long long)((long long)dwum[i-1][j] + (long long)dwum[i-1][j-1]) % MOD; } long long ans = 0; ans = one(d1) + one(d2) + one(d3) - two(z1 + z0, d1, d2) - two(z2 + z0, d1, d3) - two(z3 + z0, d2, d3); ans += all(); ans %= MOD; if(ans < 0) ans += MOD; printf("%lld", ans); return 0; } |