#include <cstdio> #include <algorithm> #include <vector> using namespace std; typedef long long ll; const int N = 1e4 + 7; const ll M = 1e9 + 7; ll newton[N][N]; int n; struct Sphere { bool P[N]; int r; bool& operator[](int i) { return P[i]; } bool operator==(Sphere& s) { if (r != s.r) return false; for (int i = 0; i < n; ++i) if (s[i] != P[i]) return false; return true; } }; ll get(int k, int i, int j) { if (j < i) return 0; if (i == 0) return newton[k][j]; ll res = newton[k][j] - newton[k][i - 1]; if (res < 0) res += M; return res; } ll count1(Sphere& s) { return newton[n][s.r]; } ll count2(Sphere& s1, Sphere& s2) { int zg = 0; int nzg = 0; for (int i = 0; i < n; ++i) if (s1[i] == s2[i]) ++zg; else ++nzg; ll res = 0; for (int i = 0; i <= min(s1.r, zg); ++i) { ll mult = get(zg, i, i); int maxi = min(s1.r - i, nzg); int mini = max(0, nzg - (s2.r - i)); ll sum = get(nzg, mini, maxi); res = (res + sum * mult) % M; } return res; } ll count3(Sphere& s1, Sphere& s2, Sphere& s3) { int zg = 0; int nzg1 = 0, nzg2 = 0, nzg3 = 0; for (int i = 0; i < n; ++i) if (s1[i] == s2[i] && s2[i] == s3[i]) ++zg; else if (s1[i] == s2[i]) ++nzg1; else if (s1[i] == s3[i]) ++nzg2; else if (s2[i] == s3[i]) ++nzg3; ll res = 0; for (int i = 0; i <= min(zg, s1.r); ++i) { ll mult1 = get(zg, i, i); int mini1 = max(0, nzg1 - (s3.r - i)); int maxi1 = min(nzg1, s1.r - i); // int should_skip = 0; for (int j = mini1; j <= maxi1; ++j) { ll mult2 = get(nzg1, j, j); ll acc1 = (mult1 * mult2) % M; int mini2 = max(0, nzg2 - (s2.r - (i + j))); int maxi2 = min(nzg2, min(s1.r - (i + j), s3.r - (i + nzg1 - j))); // bool always_zero = true; for (int k = mini2; k <= maxi2; ++k) { ll mult3 = get(nzg2, k, k); ll acc2 = (acc1 * mult3) % M; int left2 = s2.r - (i + j + nzg2 - k); int left3 = s3.r - (i + nzg1 - j + k); int maxi = min(nzg3, min(left2, left3)); int left1 = s1.r - (i + j + k); int mini = max(0, nzg3 - left1); ll sum = get(nzg3, mini, maxi); res = (res + acc2 * sum) % M; if (acc2 == 0 || sum == 0) break; } // if (always_zero) // ++should_skip; } } return res; } int main() { scanf("%d", &n); vector<Sphere> spheres; for (int i = 0; i < 3; ++i) { Sphere s; scanf("%d", &s.r); for (int j = 0; j < n; ++j) { char c; scanf(" %c", &c); if (c == '1') s[j] = 1; else s[j] = 0; } bool bad = false; for (int j = 0; j < spheres.size(); ++j) if (s == spheres[j]) { bad = true; break; } if (!bad) spheres.push_back(s); } for (int i = 0; i <= n; ++i) { newton[i][0] = 1; for (int j = 1; j <= i; ++j) { newton[i][j] = newton[i - 1][j - 1] + newton[i - 1][j]; if (newton[i][j] >= M) newton[i][j] -= M; } if (i != 0) for (int j = 1; j <= i - 1; ++j) { newton[i - 1][j] = newton[i - 1][j] + newton[i - 1][j - 1]; if (newton[i - 1][j] >= M) newton[i - 1][j] -= M; } } for (int i = 1; i <= n; ++i) { newton[n][i] = newton[n][i] + newton[n][i - 1]; if (newton[n][i] >= M) newton[n][i] -= M; } sort(spheres.begin(), spheres.end(), [](Sphere& s1, Sphere& s2){ return s1.r < s2.r; }); ll res = 0; int cnt = spheres.size(); for (int i = 0; i < cnt; ++i) res = (res + count1(spheres[i])) % M; for (int i = 0; i < cnt; ++i) for (int j = i + 1; j < cnt; ++j) res = (res - count2(spheres[i], spheres[j])) % M; if (cnt > 2) res = (res + count3(spheres[0], spheres[1], spheres[2])) % M; printf("%lld", (res + M) % M); 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 204 205 | #include <cstdio> #include <algorithm> #include <vector> using namespace std; typedef long long ll; const int N = 1e4 + 7; const ll M = 1e9 + 7; ll newton[N][N]; int n; struct Sphere { bool P[N]; int r; bool& operator[](int i) { return P[i]; } bool operator==(Sphere& s) { if (r != s.r) return false; for (int i = 0; i < n; ++i) if (s[i] != P[i]) return false; return true; } }; ll get(int k, int i, int j) { if (j < i) return 0; if (i == 0) return newton[k][j]; ll res = newton[k][j] - newton[k][i - 1]; if (res < 0) res += M; return res; } ll count1(Sphere& s) { return newton[n][s.r]; } ll count2(Sphere& s1, Sphere& s2) { int zg = 0; int nzg = 0; for (int i = 0; i < n; ++i) if (s1[i] == s2[i]) ++zg; else ++nzg; ll res = 0; for (int i = 0; i <= min(s1.r, zg); ++i) { ll mult = get(zg, i, i); int maxi = min(s1.r - i, nzg); int mini = max(0, nzg - (s2.r - i)); ll sum = get(nzg, mini, maxi); res = (res + sum * mult) % M; } return res; } ll count3(Sphere& s1, Sphere& s2, Sphere& s3) { int zg = 0; int nzg1 = 0, nzg2 = 0, nzg3 = 0; for (int i = 0; i < n; ++i) if (s1[i] == s2[i] && s2[i] == s3[i]) ++zg; else if (s1[i] == s2[i]) ++nzg1; else if (s1[i] == s3[i]) ++nzg2; else if (s2[i] == s3[i]) ++nzg3; ll res = 0; for (int i = 0; i <= min(zg, s1.r); ++i) { ll mult1 = get(zg, i, i); int mini1 = max(0, nzg1 - (s3.r - i)); int maxi1 = min(nzg1, s1.r - i); // int should_skip = 0; for (int j = mini1; j <= maxi1; ++j) { ll mult2 = get(nzg1, j, j); ll acc1 = (mult1 * mult2) % M; int mini2 = max(0, nzg2 - (s2.r - (i + j))); int maxi2 = min(nzg2, min(s1.r - (i + j), s3.r - (i + nzg1 - j))); // bool always_zero = true; for (int k = mini2; k <= maxi2; ++k) { ll mult3 = get(nzg2, k, k); ll acc2 = (acc1 * mult3) % M; int left2 = s2.r - (i + j + nzg2 - k); int left3 = s3.r - (i + nzg1 - j + k); int maxi = min(nzg3, min(left2, left3)); int left1 = s1.r - (i + j + k); int mini = max(0, nzg3 - left1); ll sum = get(nzg3, mini, maxi); res = (res + acc2 * sum) % M; if (acc2 == 0 || sum == 0) break; } // if (always_zero) // ++should_skip; } } return res; } int main() { scanf("%d", &n); vector<Sphere> spheres; for (int i = 0; i < 3; ++i) { Sphere s; scanf("%d", &s.r); for (int j = 0; j < n; ++j) { char c; scanf(" %c", &c); if (c == '1') s[j] = 1; else s[j] = 0; } bool bad = false; for (int j = 0; j < spheres.size(); ++j) if (s == spheres[j]) { bad = true; break; } if (!bad) spheres.push_back(s); } for (int i = 0; i <= n; ++i) { newton[i][0] = 1; for (int j = 1; j <= i; ++j) { newton[i][j] = newton[i - 1][j - 1] + newton[i - 1][j]; if (newton[i][j] >= M) newton[i][j] -= M; } if (i != 0) for (int j = 1; j <= i - 1; ++j) { newton[i - 1][j] = newton[i - 1][j] + newton[i - 1][j - 1]; if (newton[i - 1][j] >= M) newton[i - 1][j] -= M; } } for (int i = 1; i <= n; ++i) { newton[n][i] = newton[n][i] + newton[n][i - 1]; if (newton[n][i] >= M) newton[n][i] -= M; } sort(spheres.begin(), spheres.end(), [](Sphere& s1, Sphere& s2){ return s1.r < s2.r; }); ll res = 0; int cnt = spheres.size(); for (int i = 0; i < cnt; ++i) res = (res + count1(spheres[i])) % M; for (int i = 0; i < cnt; ++i) for (int j = i + 1; j < cnt; ++j) res = (res - count2(spheres[i], spheres[j])) % M; if (cnt > 2) res = (res + count3(spheres[0], spheres[1], spheres[2])) % M; printf("%lld", (res + M) % M); return 0; } |