#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 10100; const int mod = 1000000007; int n; int r[3]; char s[3][N]; int C[N][N]; int S[N]; int T1[N][6677], T2[N][6677]; int main() { scanf("%d", &n); FOR(i,n+1) C[i][0] = C[i][i] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j < i; j++) { C[i][j] = C[i-1][j-1] + C[i-1][j]; if (C[i][j] >= mod) C[i][j] -= mod; } FOR(i,3) scanf("%d %s", &r[i], s[i]); int res = 0; FOR(x,3) { FOR(i,r[x]+1) { res += C[n][i]; if (res >= mod) res -= mod; } } FOR(x,3) FOR(y,x) { int ns = 0, nd = 0; FOR(i,n) { if (s[x][i] == s[y][i]) ns++; else nd++; } FOR(i,ns+1) FOR(j,nd+1) { if (i+j <= r[x] && i+nd-j <= r[y]) { res = (mod - 1LL * C[ns][i] * C[nd][j] % mod + res) % mod; } } } int ns = 0, na = 0, nb = 0, nc = 0; FOR(i,n) { if (s[0][i] == s[1][i] && s[1][i] == s[2][i]) ns++; else if (s[0][i] == s[1][i]) nc++; else if (s[1][i] == s[2][i]) na++; else nb++; } if (nc > na) { swap(na, nc); swap(r[0], r[2]); } if (nc > nb) { swap(nb, nc); swap(r[1], r[2]); } S[0] = C[ns][0]; for (int i = 1; i <= ns; i++) { S[i] = S[i-1] + C[ns][i]; if (S[i] >= mod) S[i] -= mod; } for (int i = 0; i <= n; i++) { for (int j = 0; j <= nc; j++) { T1[i][j] = (1LL * C[ns][i] * C[nc][j] + (i>0 ? 1LL * C[ns][i-1] * C[nc][j] : 0LL) + (i>0&&j>0 ? T1[i-1][j-1] : 0) + (i>0&&j<nc ? T1[i-1][j+1] : 0) - (i>1&&j>0&&j<nc ? T1[i-2][j] : 0) + mod) % mod; T2[i][j] = (1LL * C[ns][i] * C[nc][j] + (j<nc? 1LL * C[ns][i] * C[nc][j+1] : 0LL) + (i>0&&j>0 ? T2[i-1][j-1] : 0) + (i>0&&j<nc ? T2[i-1][j+1] : 0) - (i>1&&j>0&&j<nc ? T2[i-2][j] : 0) + mod) % mod; if (j == 0 && i > 0) T2[i][j] = (1LL * C[ns][i-1] * C[nc][j] + T2[i][j]) % mod; } } if (nc == 0) { T1[0][0] = T2[0][0] = 1; for (int i = 1; i <= n; i++) { T2[i][0] = T1[i][0] = (C[ns][i] + T1[i-1][0]) % mod; } } //cerr << "C*C = \n"; //for (int i = n; i >= 0; i--) { // for (int j = 0; j <= nc; j++) cerr << 1LL * C[ns][i] * C[nc][j] % mod << "\t"; // cerr << "\n"; //} //cerr << "T1 = \n"; //for (int i = n; i >= 0; i--) { // for (int j = 0; j <= nc; j++) cerr << 1LL * T1[i][j] % mod << "\t"; // cerr << "\n"; //} //cerr << "T2 = \n"; //for (int i = n; i >= 0; i--) { // for (int j = 0; j <= nc; j++) cerr << 1LL * T2[i][j] % mod << "\t"; // cerr << "\n"; //} //cerr << "n = " << na << " " << nb << " " << nc << " " << ns << "\n"; //cerr << "sumab = \n"; FOR(i,na+1) FOR(j,nb+1) { int Q = i+j+nc-r[2], P = min(r[0]-na+i-j, r[1]-nb+j-i); //cerr << "need " << i << " " << j << " " << P << " " << Q << " " << sumab << "\n"; int sumcur = 0; if (P >= 0 && P >= Q && Q <= nc) { if (P+Q < 0) { sumcur = T1[P][0]; } else if (P+Q > 2*nc) { sumcur = T1[nc-Q][nc]; } else if ((P+Q)%2 == 0) { sumcur = T1[(P-Q)/2][(P+Q)/2]; } else { sumcur = T2[(P-Q)/2][(P+Q)/2]; } //cerr << "have " << sumcur << "\n"; } else { //cerr << "have 0\n"; } res = (1LL * C[na][i] * C[nb][j] % mod * sumcur + res) % mod; } printf("%d\n", res); 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 10100; const int mod = 1000000007; int n; int r[3]; char s[3][N]; int C[N][N]; int S[N]; int T1[N][6677], T2[N][6677]; int main() { scanf("%d", &n); FOR(i,n+1) C[i][0] = C[i][i] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j < i; j++) { C[i][j] = C[i-1][j-1] + C[i-1][j]; if (C[i][j] >= mod) C[i][j] -= mod; } FOR(i,3) scanf("%d %s", &r[i], s[i]); int res = 0; FOR(x,3) { FOR(i,r[x]+1) { res += C[n][i]; if (res >= mod) res -= mod; } } FOR(x,3) FOR(y,x) { int ns = 0, nd = 0; FOR(i,n) { if (s[x][i] == s[y][i]) ns++; else nd++; } FOR(i,ns+1) FOR(j,nd+1) { if (i+j <= r[x] && i+nd-j <= r[y]) { res = (mod - 1LL * C[ns][i] * C[nd][j] % mod + res) % mod; } } } int ns = 0, na = 0, nb = 0, nc = 0; FOR(i,n) { if (s[0][i] == s[1][i] && s[1][i] == s[2][i]) ns++; else if (s[0][i] == s[1][i]) nc++; else if (s[1][i] == s[2][i]) na++; else nb++; } if (nc > na) { swap(na, nc); swap(r[0], r[2]); } if (nc > nb) { swap(nb, nc); swap(r[1], r[2]); } S[0] = C[ns][0]; for (int i = 1; i <= ns; i++) { S[i] = S[i-1] + C[ns][i]; if (S[i] >= mod) S[i] -= mod; } for (int i = 0; i <= n; i++) { for (int j = 0; j <= nc; j++) { T1[i][j] = (1LL * C[ns][i] * C[nc][j] + (i>0 ? 1LL * C[ns][i-1] * C[nc][j] : 0LL) + (i>0&&j>0 ? T1[i-1][j-1] : 0) + (i>0&&j<nc ? T1[i-1][j+1] : 0) - (i>1&&j>0&&j<nc ? T1[i-2][j] : 0) + mod) % mod; T2[i][j] = (1LL * C[ns][i] * C[nc][j] + (j<nc? 1LL * C[ns][i] * C[nc][j+1] : 0LL) + (i>0&&j>0 ? T2[i-1][j-1] : 0) + (i>0&&j<nc ? T2[i-1][j+1] : 0) - (i>1&&j>0&&j<nc ? T2[i-2][j] : 0) + mod) % mod; if (j == 0 && i > 0) T2[i][j] = (1LL * C[ns][i-1] * C[nc][j] + T2[i][j]) % mod; } } if (nc == 0) { T1[0][0] = T2[0][0] = 1; for (int i = 1; i <= n; i++) { T2[i][0] = T1[i][0] = (C[ns][i] + T1[i-1][0]) % mod; } } //cerr << "C*C = \n"; //for (int i = n; i >= 0; i--) { // for (int j = 0; j <= nc; j++) cerr << 1LL * C[ns][i] * C[nc][j] % mod << "\t"; // cerr << "\n"; //} //cerr << "T1 = \n"; //for (int i = n; i >= 0; i--) { // for (int j = 0; j <= nc; j++) cerr << 1LL * T1[i][j] % mod << "\t"; // cerr << "\n"; //} //cerr << "T2 = \n"; //for (int i = n; i >= 0; i--) { // for (int j = 0; j <= nc; j++) cerr << 1LL * T2[i][j] % mod << "\t"; // cerr << "\n"; //} //cerr << "n = " << na << " " << nb << " " << nc << " " << ns << "\n"; //cerr << "sumab = \n"; FOR(i,na+1) FOR(j,nb+1) { int Q = i+j+nc-r[2], P = min(r[0]-na+i-j, r[1]-nb+j-i); //cerr << "need " << i << " " << j << " " << P << " " << Q << " " << sumab << "\n"; int sumcur = 0; if (P >= 0 && P >= Q && Q <= nc) { if (P+Q < 0) { sumcur = T1[P][0]; } else if (P+Q > 2*nc) { sumcur = T1[nc-Q][nc]; } else if ((P+Q)%2 == 0) { sumcur = T1[(P-Q)/2][(P+Q)/2]; } else { sumcur = T2[(P-Q)/2][(P+Q)/2]; } //cerr << "have " << sumcur << "\n"; } else { //cerr << "have 0\n"; } res = (1LL * C[na][i] * C[nb][j] % mod * sumcur + res) % mod; } printf("%d\n", res); return 0; } |