#include <bits/stdc++.h> using namespace std; static int choice[10240][10240] = {0}; static int upto[10240][10240] = {0}; int const MOD = 1000000007; void init_choice(int n) { for(int i = 0; i <= n; i++) { choice[i][0] = 1; for(int j = 1; j <= i; j++) choice[i][j] = (choice[i-1][j-1] + choice[i-1][j]) % MOD; } for(int i = 0; i <= n; i++) { upto[i][0] = 1; for(int j = 1; j <= n; j++) upto[i][j] = (upto[i][j-1] + choice[i][j]) % MOD; } } int intersect2(int n, int c, int ra, int rb) { int ans = 0; int d = n - c; int cd_limit = c; cd_limit = min(cd_limit, ra); cd_limit = min(cd_limit, rb); for(int cd = 0; cd <= cd_limit; cd++) { for(int da = max(0, d - (rb - cd)); da <= ra - cd; da++) { ans = (ans + choice[c][cd] * int64_t(choice[d][da])) % MOD; } } return ans; } int intersect3(int n, int c, int *R, int *C) { int ans = 0; int famin = C[0] - R[0]; int fbmin = C[1] - R[1]; int fcmin = C[2] - R[2]; for(int fa = max(0, famin + fbmin + fcmin); fa <= C[0]; fa++) { int fb0 = max(0, fa + fbmin + fcmin); int fb1 = C[1]; fb1 = min(fb1, fa - famin - fcmin); if(fb1 < fb0) continue; for(int fb = fb0; fb <= fb1; fb++) { int fc0 = max(0, fa + fb + fcmin); int fc1 = C[2]; fc1 = min(fc1, fa - famin - fb); fc1 = min(fc1, fb - fbmin - fa); if(fc1 < fc0) continue; for(int fc = fc0; fc <= fc1; fc++) { int u = c; u = min(u, R[0] + fa - C[0] - fb - fc); u = min(u, R[1] + fb - C[1] - fa - fc); u = min(u, R[2] + fc - C[2] - fa - fb); assert(u >= 0); int t = upto[c][u]; t = (int64_t(t) * choice[C[0]][fa]) % MOD; t = (int64_t(t) * choice[C[1]][fb]) % MOD; t = (int64_t(t) * choice[C[2]][fc]) % MOD; ans = (ans + t) % MOD; } } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; init_choice(n); static char S[3][10240]; int R[3]; cin >> R[0] >> S[0]; cin >> R[1] >> S[1]; cin >> R[2] >> S[2]; int common = 0; int C[3] = {0}; for(int i = 0; i < n; i++) { if(S[0][i] == S[1][i] and S[1][i] == S[2][i]) { common++; continue; } else if(S[0][i] == S[1][i]) { C[2]++; } else if(S[1][i] == S[2][i]) { C[0]++; } else /* if(S[0][i] == S[2][i]) */ { C[1]++; } } int V0 = upto[n][R[0]]; int V1 = upto[n][R[1]]; int V2 = upto[n][R[2]]; int V01 = intersect2(n, common + C[2], R[0], R[1]); int V02 = intersect2(n, common + C[1], R[0], R[2]); int V12 = intersect2(n, common + C[0], R[1], R[2]); int V012 = intersect3(n, common, R, C); int ans = 0; ans = (ans + V0) % MOD; ans = (ans + V1) % MOD; ans = (ans + V2) % MOD; ans = (ans - V01 + MOD) % MOD; ans = (ans - V02 + MOD) % MOD; ans = (ans - V12 + MOD) % MOD; ans = (ans + V012) % MOD; 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 | #include <bits/stdc++.h> using namespace std; static int choice[10240][10240] = {0}; static int upto[10240][10240] = {0}; int const MOD = 1000000007; void init_choice(int n) { for(int i = 0; i <= n; i++) { choice[i][0] = 1; for(int j = 1; j <= i; j++) choice[i][j] = (choice[i-1][j-1] + choice[i-1][j]) % MOD; } for(int i = 0; i <= n; i++) { upto[i][0] = 1; for(int j = 1; j <= n; j++) upto[i][j] = (upto[i][j-1] + choice[i][j]) % MOD; } } int intersect2(int n, int c, int ra, int rb) { int ans = 0; int d = n - c; int cd_limit = c; cd_limit = min(cd_limit, ra); cd_limit = min(cd_limit, rb); for(int cd = 0; cd <= cd_limit; cd++) { for(int da = max(0, d - (rb - cd)); da <= ra - cd; da++) { ans = (ans + choice[c][cd] * int64_t(choice[d][da])) % MOD; } } return ans; } int intersect3(int n, int c, int *R, int *C) { int ans = 0; int famin = C[0] - R[0]; int fbmin = C[1] - R[1]; int fcmin = C[2] - R[2]; for(int fa = max(0, famin + fbmin + fcmin); fa <= C[0]; fa++) { int fb0 = max(0, fa + fbmin + fcmin); int fb1 = C[1]; fb1 = min(fb1, fa - famin - fcmin); if(fb1 < fb0) continue; for(int fb = fb0; fb <= fb1; fb++) { int fc0 = max(0, fa + fb + fcmin); int fc1 = C[2]; fc1 = min(fc1, fa - famin - fb); fc1 = min(fc1, fb - fbmin - fa); if(fc1 < fc0) continue; for(int fc = fc0; fc <= fc1; fc++) { int u = c; u = min(u, R[0] + fa - C[0] - fb - fc); u = min(u, R[1] + fb - C[1] - fa - fc); u = min(u, R[2] + fc - C[2] - fa - fb); assert(u >= 0); int t = upto[c][u]; t = (int64_t(t) * choice[C[0]][fa]) % MOD; t = (int64_t(t) * choice[C[1]][fb]) % MOD; t = (int64_t(t) * choice[C[2]][fc]) % MOD; ans = (ans + t) % MOD; } } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; init_choice(n); static char S[3][10240]; int R[3]; cin >> R[0] >> S[0]; cin >> R[1] >> S[1]; cin >> R[2] >> S[2]; int common = 0; int C[3] = {0}; for(int i = 0; i < n; i++) { if(S[0][i] == S[1][i] and S[1][i] == S[2][i]) { common++; continue; } else if(S[0][i] == S[1][i]) { C[2]++; } else if(S[1][i] == S[2][i]) { C[0]++; } else /* if(S[0][i] == S[2][i]) */ { C[1]++; } } int V0 = upto[n][R[0]]; int V1 = upto[n][R[1]]; int V2 = upto[n][R[2]]; int V01 = intersect2(n, common + C[2], R[0], R[1]); int V02 = intersect2(n, common + C[1], R[0], R[2]); int V12 = intersect2(n, common + C[0], R[1], R[2]); int V012 = intersect3(n, common, R, C); int ans = 0; ans = (ans + V0) % MOD; ans = (ans + V1) % MOD; ans = (ans + V2) % MOD; ans = (ans - V01 + MOD) % MOD; ans = (ans - V02 + MOD) % MOD; ans = (ans - V12 + MOD) % MOD; ans = (ans + V012) % MOD; cout << ans << '\n'; return 0; } |