#include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } constexpr int balls = 3; int n; constexpr int mod = int(1e9) + 7; int add(int a, int b) { a += b; if(a >= mod) a -= mod; return a; } int add_to(int &a, int b) { a += b; if(a >= mod) a -= mod; return a; } int mul(int a, int b) { return int(a * LL(b) % mod); } vector<vector<int>> newton; int solve(array<vector<int>, balls> srodek, array<int, balls> r) { array<array<int, 2>, 2> cnt; REP(a, 2) REP(b, 2) cnt[a][b] = 0; REP(i, n) { for(int b = 1; b < balls; ++b) srodek[b][i] ^= srodek[0][i]; srodek[0][i] = 0; ++cnt[srodek[1][i]][srodek[2][i]]; } const int up = cnt[0][0], right = cnt[0][1]; vector<int> left_triangle(up + right + 1); vector<vector<int>> single_triangle(right + 1, vector<int>(up + 1)), double_triangle = single_triangle; auto point = [&](int x, int y) -> int { return mul(newton[right][x], newton[up][y]); }; FOR(x, 0, right) FOR(y, 0, up) { int p = point(x, y); add_to(left_triangle[up - y + x], p); add_to(single_triangle[x][y], p); add_to(double_triangle[x][y], p); if(x != 0) add_to(double_triangle[x - 1][y], p); } for(int i = up + right - 1; i >= 0; --i) add_to(left_triangle[i], left_triangle[i + 1]); FOR(y, 1, up) FOR(x, 0, right) { if(right == 0) { add_to(single_triangle[x][y], single_triangle[x][y - 1]); continue; } if(x != 0) add_to(single_triangle[x][y], single_triangle[x - 1][y - 1]); if(x != right) add_to(single_triangle[x][y], single_triangle[x + 1][y - 1]); if(x != 0 and x != right and y >= 2) add_to(single_triangle[x][y], mod - single_triangle[x][y - 2]); add_to(single_triangle[x][y], point(x, y - 1)); } FOR(y, 1, up) FOR(x, 0, right) { if(x == right) { double_triangle[x][y] = single_triangle[x][y]; continue; } if(x == 0) { add_to(double_triangle[x][y], double_triangle[x + 1][y - 1]); add_to(double_triangle[x][y], point(x, y - 1)); continue; } if(x == right - 1) { add_to(double_triangle[x][y], double_triangle[x - 1][y - 1]); add_to(double_triangle[x][y], point(x + 1, y - 1)); continue; } add_to(double_triangle[x][y], double_triangle[x - 1][y - 1]); add_to(double_triangle[x][y], double_triangle[x + 1][y - 1]); if(x != 0 and x != right and y >= 2) add_to(double_triangle[x][y], mod - double_triangle[x][y - 2]); } auto solve = [&](int sum, int diff) { int dist_left = up - diff, dist_right = up + right - sum; if(dist_left < 0 and dist_right < 0) return left_triangle.front(); if(dist_left >= size(left_triangle) or dist_right >= size(left_triangle)) return 0; if(dist_left < 0) return left_triangle[dist_right]; if(dist_right < 0) return left_triangle[dist_left]; int intersecty2 = sum + diff; if(intersecty2 > 2 * up) return add( add(left_triangle[dist_left], left_triangle[dist_right]), mod - left_triangle.front() ); if(intersecty2 < 0) return 0; int intersectx2 = sum - diff; if(intersectx2 < 0) return left_triangle[dist_right]; if(intersectx2 > 2 * right) return left_triangle[dist_left]; if(intersecty2 % 2 == 0) return single_triangle[intersectx2 / 2][intersecty2 / 2]; else return double_triangle[intersectx2 / 2][intersecty2 / 2]; }; int ans = 0; FOR(c10, 0, cnt[1][0]) FOR(c11, 0, cnt[1][1]) { ans = add(ans, mul(mul(newton[cnt[1][0]][c10], newton[cnt[1][1]][c11]), solve( min(r[0] - c10 - c11, r[1] - cnt[1][0] - cnt[1][1] + c10 + c11), r[2] - cnt[0][1] - cnt[1][1] - c10 + c11 ))); } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; array<vector<int>, balls> srodek; array<int, balls> r; REP(b, balls) { string s; cin >> r[b]; srodek[b].resize(n); for(int &x : srodek[b]) { char c; cin >> c; x = int(c - '0'); } } newton.resize(n + 1, vector<int>(n + 1)); FOR(i, 0, n) newton[i][0] = 1; FOR(i, 1, n) FOR(j, 1, n) newton[i][j] = add(newton[i - 1][j], newton[i - 1][j - 1]); vector<int> sum_newton(n + 1); FOR(i, 0, n) { sum_newton[i] = newton[n][i]; if(i > 0) sum_newton[i] = add(sum_newton[i], sum_newton[i - 1]); } int ans = 0; REP(b, balls) ans = add(ans, sum_newton[r[b]]); REP(b1, balls) REP(b2, b1) ans = add(ans, mod - solve({srodek[b1], srodek[b2], srodek[b2]}, {r[b1], r[b2], r[b2]})); ans = add(ans, solve(srodek, r)); cout << ans << '\n'; }
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 | #include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } constexpr int balls = 3; int n; constexpr int mod = int(1e9) + 7; int add(int a, int b) { a += b; if(a >= mod) a -= mod; return a; } int add_to(int &a, int b) { a += b; if(a >= mod) a -= mod; return a; } int mul(int a, int b) { return int(a * LL(b) % mod); } vector<vector<int>> newton; int solve(array<vector<int>, balls> srodek, array<int, balls> r) { array<array<int, 2>, 2> cnt; REP(a, 2) REP(b, 2) cnt[a][b] = 0; REP(i, n) { for(int b = 1; b < balls; ++b) srodek[b][i] ^= srodek[0][i]; srodek[0][i] = 0; ++cnt[srodek[1][i]][srodek[2][i]]; } const int up = cnt[0][0], right = cnt[0][1]; vector<int> left_triangle(up + right + 1); vector<vector<int>> single_triangle(right + 1, vector<int>(up + 1)), double_triangle = single_triangle; auto point = [&](int x, int y) -> int { return mul(newton[right][x], newton[up][y]); }; FOR(x, 0, right) FOR(y, 0, up) { int p = point(x, y); add_to(left_triangle[up - y + x], p); add_to(single_triangle[x][y], p); add_to(double_triangle[x][y], p); if(x != 0) add_to(double_triangle[x - 1][y], p); } for(int i = up + right - 1; i >= 0; --i) add_to(left_triangle[i], left_triangle[i + 1]); FOR(y, 1, up) FOR(x, 0, right) { if(right == 0) { add_to(single_triangle[x][y], single_triangle[x][y - 1]); continue; } if(x != 0) add_to(single_triangle[x][y], single_triangle[x - 1][y - 1]); if(x != right) add_to(single_triangle[x][y], single_triangle[x + 1][y - 1]); if(x != 0 and x != right and y >= 2) add_to(single_triangle[x][y], mod - single_triangle[x][y - 2]); add_to(single_triangle[x][y], point(x, y - 1)); } FOR(y, 1, up) FOR(x, 0, right) { if(x == right) { double_triangle[x][y] = single_triangle[x][y]; continue; } if(x == 0) { add_to(double_triangle[x][y], double_triangle[x + 1][y - 1]); add_to(double_triangle[x][y], point(x, y - 1)); continue; } if(x == right - 1) { add_to(double_triangle[x][y], double_triangle[x - 1][y - 1]); add_to(double_triangle[x][y], point(x + 1, y - 1)); continue; } add_to(double_triangle[x][y], double_triangle[x - 1][y - 1]); add_to(double_triangle[x][y], double_triangle[x + 1][y - 1]); if(x != 0 and x != right and y >= 2) add_to(double_triangle[x][y], mod - double_triangle[x][y - 2]); } auto solve = [&](int sum, int diff) { int dist_left = up - diff, dist_right = up + right - sum; if(dist_left < 0 and dist_right < 0) return left_triangle.front(); if(dist_left >= size(left_triangle) or dist_right >= size(left_triangle)) return 0; if(dist_left < 0) return left_triangle[dist_right]; if(dist_right < 0) return left_triangle[dist_left]; int intersecty2 = sum + diff; if(intersecty2 > 2 * up) return add( add(left_triangle[dist_left], left_triangle[dist_right]), mod - left_triangle.front() ); if(intersecty2 < 0) return 0; int intersectx2 = sum - diff; if(intersectx2 < 0) return left_triangle[dist_right]; if(intersectx2 > 2 * right) return left_triangle[dist_left]; if(intersecty2 % 2 == 0) return single_triangle[intersectx2 / 2][intersecty2 / 2]; else return double_triangle[intersectx2 / 2][intersecty2 / 2]; }; int ans = 0; FOR(c10, 0, cnt[1][0]) FOR(c11, 0, cnt[1][1]) { ans = add(ans, mul(mul(newton[cnt[1][0]][c10], newton[cnt[1][1]][c11]), solve( min(r[0] - c10 - c11, r[1] - cnt[1][0] - cnt[1][1] + c10 + c11), r[2] - cnt[0][1] - cnt[1][1] - c10 + c11 ))); } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; array<vector<int>, balls> srodek; array<int, balls> r; REP(b, balls) { string s; cin >> r[b]; srodek[b].resize(n); for(int &x : srodek[b]) { char c; cin >> c; x = int(c - '0'); } } newton.resize(n + 1, vector<int>(n + 1)); FOR(i, 0, n) newton[i][0] = 1; FOR(i, 1, n) FOR(j, 1, n) newton[i][j] = add(newton[i - 1][j], newton[i - 1][j - 1]); vector<int> sum_newton(n + 1); FOR(i, 0, n) { sum_newton[i] = newton[n][i]; if(i > 0) sum_newton[i] = add(sum_newton[i], sum_newton[i - 1]); } int ans = 0; REP(b, balls) ans = add(ans, sum_newton[r[b]]); REP(b1, balls) REP(b2, b1) ans = add(ans, mod - solve({srodek[b1], srodek[b2], srodek[b2]}, {r[b1], r[b2], r[b2]})); ans = add(ans, solve(srodek, r)); cout << ans << '\n'; } |