#include <bits/stdc++.h> using namespace std; typedef long long int LL; const int N = 10007; const int MX = 1e9 + 7; int n; int r[3]; vector <int> in[3]; int pref[N]; int sum[N][N]; int newton[N][N]; void add(int &a, int b){ a += b; if(a >= MX) a -= MX; } void sub(int &a, int b){ a -= b; if(a < 0) a += MX; } void read(int id){ char tmp[N]; scanf("%d %s", &r[id], tmp); in[id].resize(n); for(int i = 0; i < n; ++i) in[id][i] = tmp[i] - '0'; } int calc1(int fa){ int ans = 0; for(int i = 0; i <= r[fa]; ++i) add(ans, newton[n][i]); return ans; } int calc2(int fa, int fb){ int eq = 0, df = 0; for(int i = 0; i < n; ++i) if(in[fa][i] != in[fb][i]) ++df; else ++eq; pref[0] = newton[eq][0]; for(int i = 1; i <= eq; ++i) pref[i] = (newton[eq][i] + pref[i - 1]) % MX; int ans = 0; for(int i = 0; i <= df; ++i){ if(i > r[fa]) break; if(df - i > r[fb]) continue; int Left = min(eq, min(r[fa] - i, r[fb] - df + i)); ans = (ans + 1LL * newton[df][i] * pref[Left]) % MX; } return ans; } int calc3(){ int ans = 0; int allEq = 0; vector <int> cnt(3, 0); for(int i = 0; i < n; ++i) if(in[0][i] == in[1][i] && in[1][i] == in[2][i]) ++allEq; else if(in[0][i] == in[1][i]) ++cnt[2]; else if(in[1][i] == in[2][i]) ++cnt[0]; else ++cnt[1]; for(int fa = 0; fa <= cnt[0]; ++fa) for(int fb = 0; fb <= cnt[1]; ++fb){ int downLimit = max(fa + fb + cnt[2] - r[2], 0); int upLimit = min(min(r[0] + fa - cnt[0] - fb, r[1] + fb - cnt[1] - fa), cnt[2]); if(downLimit > upLimit) continue; int LeftX = r[2] - fa - fb - cnt[2] + downLimit; int RightX = min(r[0] + fa - cnt[0] - fb - upLimit, r[1] + fb - cnt[1] - fa - upLimit); // printf("%d %d\n", LeftX, RightX); int tmpSum = RightX - LeftX + upLimit - downLimit; int midOption = tmpSum / 2; if(tmpSum < 0) midOption = -1; midOption += downLimit; int newtVal = (1LL * newton[cnt[0]][fa] * newton[cnt[1]][fb]) % MX; midOption = min(midOption, upLimit); if(downLimit <= midOption){ // printf("%d -> %d dodaje %d -> %d o wart %d\n", downLimit, midOption, LeftX, LeftX + midOption - downLimit, newtVal); add(sum[downLimit][LeftX], newtVal); add(sum[midOption + 1][LeftX + midOption - downLimit + 1], MX - newtVal); } } pref[0] = newton[allEq][0]; for(int i = 1; i <= n; ++i) pref[i] = (pref[i - 1] + newton[allEq][i]) % MX; for(int i = 0; i <= cnt[2]; ++i) for(int j = 0; j <= n; ++j){ if(i && j) add(sum[i][j], sum[i - 1][j - 1]); ans = (ans + ((1LL * newton[cnt[2]][i] * sum[i][j]) % MX) * pref[j]) % MX; } for(int i = 0; i <= cnt[2]; ++i) for(int j = 0; j <= n; ++j) sum[i][j] = 0; for(int fa = 0; fa <= cnt[0]; ++fa) for(int fb = 0; fb <= cnt[1]; ++fb){ int downLimit = max(fa + fb + cnt[2] - r[2], 0); int upLimit = min(min(r[0] + fa - cnt[0] - fb, r[1] + fb - cnt[1] - fa), cnt[2]); if(downLimit > upLimit) continue; int LeftX = r[2] - fa - fb - cnt[2] + downLimit; int RightX = min(r[0] + fa - cnt[0] - fb - upLimit, r[1] + fb - cnt[1] - fa - upLimit); int tmpSum = RightX - LeftX + upLimit - downLimit; int midOption = tmpSum / 2; if(tmpSum < 0) midOption = -1; midOption += downLimit; int newtVal = (1LL * newton[cnt[0]][fa] * newton[cnt[1]][fb]) % MX; midOption = max(midOption, downLimit - 1); if(midOption < upLimit){ // printf("%d -> %d dodaje %d -> %d o wart %d\n", upLimit, midOption + 1, RightX, RightX + upLimit - midOption - 1, newtVal); add(sum[upLimit][RightX], newtVal); if(midOption >= 0) add(sum[midOption][RightX + upLimit - midOption], MX - newtVal); } } for(int i = cnt[2]; i >= 0; --i) for(int j = 0; j <= n; ++j){ if(i < cnt[2] && j) add(sum[i][j], sum[i + 1][j - 1]); // if(sum[i][j] != 0) // printf("%d %d %d\n", i, j, sum[i][j]); ans = (ans + ((1LL * newton[cnt[2]][i] * sum[i][j]) % MX) * pref[j]) % MX; } for(int i = 0; i <= cnt[2]; ++i) for(int j = 0; j <= n; ++j) sum[i][j] = 0; return ans; } int main(){ scanf("%d", &n); for(int i = 0; i < 3; ++i) read(i); for(int i = 0; i <= n; ++i){ newton[i][0] = newton[i][i] = 1; for(int j = 1; j < i; ++j){ add(newton[i][j], newton[i - 1][j - 1]); add(newton[i][j], newton[i - 1][j]); } } LL ans = 0; for(int i = 0; i < 3; ++i) ans += calc1(i); ans -= calc2(0, 1); ans -= calc2(0, 2); ans -= calc2(1, 2); ans += calc3(); ans %= MX; ans += MX; ans %= MX; // printf("%d %d %d %d %d %d %d\n", calc1(0), calc1(1), calc1(2), calc2(0, 1), calc2(0, 2), calc2(1, 2), calc3()); printf("%lld\n", 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 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 int LL; const int N = 10007; const int MX = 1e9 + 7; int n; int r[3]; vector <int> in[3]; int pref[N]; int sum[N][N]; int newton[N][N]; void add(int &a, int b){ a += b; if(a >= MX) a -= MX; } void sub(int &a, int b){ a -= b; if(a < 0) a += MX; } void read(int id){ char tmp[N]; scanf("%d %s", &r[id], tmp); in[id].resize(n); for(int i = 0; i < n; ++i) in[id][i] = tmp[i] - '0'; } int calc1(int fa){ int ans = 0; for(int i = 0; i <= r[fa]; ++i) add(ans, newton[n][i]); return ans; } int calc2(int fa, int fb){ int eq = 0, df = 0; for(int i = 0; i < n; ++i) if(in[fa][i] != in[fb][i]) ++df; else ++eq; pref[0] = newton[eq][0]; for(int i = 1; i <= eq; ++i) pref[i] = (newton[eq][i] + pref[i - 1]) % MX; int ans = 0; for(int i = 0; i <= df; ++i){ if(i > r[fa]) break; if(df - i > r[fb]) continue; int Left = min(eq, min(r[fa] - i, r[fb] - df + i)); ans = (ans + 1LL * newton[df][i] * pref[Left]) % MX; } return ans; } int calc3(){ int ans = 0; int allEq = 0; vector <int> cnt(3, 0); for(int i = 0; i < n; ++i) if(in[0][i] == in[1][i] && in[1][i] == in[2][i]) ++allEq; else if(in[0][i] == in[1][i]) ++cnt[2]; else if(in[1][i] == in[2][i]) ++cnt[0]; else ++cnt[1]; for(int fa = 0; fa <= cnt[0]; ++fa) for(int fb = 0; fb <= cnt[1]; ++fb){ int downLimit = max(fa + fb + cnt[2] - r[2], 0); int upLimit = min(min(r[0] + fa - cnt[0] - fb, r[1] + fb - cnt[1] - fa), cnt[2]); if(downLimit > upLimit) continue; int LeftX = r[2] - fa - fb - cnt[2] + downLimit; int RightX = min(r[0] + fa - cnt[0] - fb - upLimit, r[1] + fb - cnt[1] - fa - upLimit); // printf("%d %d\n", LeftX, RightX); int tmpSum = RightX - LeftX + upLimit - downLimit; int midOption = tmpSum / 2; if(tmpSum < 0) midOption = -1; midOption += downLimit; int newtVal = (1LL * newton[cnt[0]][fa] * newton[cnt[1]][fb]) % MX; midOption = min(midOption, upLimit); if(downLimit <= midOption){ // printf("%d -> %d dodaje %d -> %d o wart %d\n", downLimit, midOption, LeftX, LeftX + midOption - downLimit, newtVal); add(sum[downLimit][LeftX], newtVal); add(sum[midOption + 1][LeftX + midOption - downLimit + 1], MX - newtVal); } } pref[0] = newton[allEq][0]; for(int i = 1; i <= n; ++i) pref[i] = (pref[i - 1] + newton[allEq][i]) % MX; for(int i = 0; i <= cnt[2]; ++i) for(int j = 0; j <= n; ++j){ if(i && j) add(sum[i][j], sum[i - 1][j - 1]); ans = (ans + ((1LL * newton[cnt[2]][i] * sum[i][j]) % MX) * pref[j]) % MX; } for(int i = 0; i <= cnt[2]; ++i) for(int j = 0; j <= n; ++j) sum[i][j] = 0; for(int fa = 0; fa <= cnt[0]; ++fa) for(int fb = 0; fb <= cnt[1]; ++fb){ int downLimit = max(fa + fb + cnt[2] - r[2], 0); int upLimit = min(min(r[0] + fa - cnt[0] - fb, r[1] + fb - cnt[1] - fa), cnt[2]); if(downLimit > upLimit) continue; int LeftX = r[2] - fa - fb - cnt[2] + downLimit; int RightX = min(r[0] + fa - cnt[0] - fb - upLimit, r[1] + fb - cnt[1] - fa - upLimit); int tmpSum = RightX - LeftX + upLimit - downLimit; int midOption = tmpSum / 2; if(tmpSum < 0) midOption = -1; midOption += downLimit; int newtVal = (1LL * newton[cnt[0]][fa] * newton[cnt[1]][fb]) % MX; midOption = max(midOption, downLimit - 1); if(midOption < upLimit){ // printf("%d -> %d dodaje %d -> %d o wart %d\n", upLimit, midOption + 1, RightX, RightX + upLimit - midOption - 1, newtVal); add(sum[upLimit][RightX], newtVal); if(midOption >= 0) add(sum[midOption][RightX + upLimit - midOption], MX - newtVal); } } for(int i = cnt[2]; i >= 0; --i) for(int j = 0; j <= n; ++j){ if(i < cnt[2] && j) add(sum[i][j], sum[i + 1][j - 1]); // if(sum[i][j] != 0) // printf("%d %d %d\n", i, j, sum[i][j]); ans = (ans + ((1LL * newton[cnt[2]][i] * sum[i][j]) % MX) * pref[j]) % MX; } for(int i = 0; i <= cnt[2]; ++i) for(int j = 0; j <= n; ++j) sum[i][j] = 0; return ans; } int main(){ scanf("%d", &n); for(int i = 0; i < 3; ++i) read(i); for(int i = 0; i <= n; ++i){ newton[i][0] = newton[i][i] = 1; for(int j = 1; j < i; ++j){ add(newton[i][j], newton[i - 1][j - 1]); add(newton[i][j], newton[i - 1][j]); } } LL ans = 0; for(int i = 0; i < 3; ++i) ans += calc1(i); ans -= calc2(0, 1); ans -= calc2(0, 2); ans -= calc2(1, 2); ans += calc3(); ans %= MX; ans += MX; ans %= MX; // printf("%d %d %d %d %d %d %d\n", calc1(0), calc1(1), calc1(2), calc2(0, 1), calc2(0, 2), calc2(1, 2), calc3()); printf("%lld\n", ans); return 0; } |