#include <bits/stdc++.h> using namespace std; const int N = 10010, M = 1e9 + 7; int newton[N][N]; int atMost[N][N]; char seq1[N], seq2[N], seq3[N]; // int sumProgMax[N], sumProgMin[N]; int sumProg[N / 3][N]; int n, pot[N], c0, c1, c2, c3, s0, s1, s2, s3; void add(int &w, int u) { w += u; if (w >= M) { w -= M; } } int mul(int w, int u) { return (long long)w * u % M; } // int Newton(int n, int k) { // if (k > n) return; // return // } int main() { scanf("%d", &n); pot[0] = 1; for (int i = 1; i <= n; i++) { pot[i] = mul(pot[i - 1], 2); } for (int i = 0; i <= n; i++) { newton[i][0] = newton[i][i] = 1; } atMost[0][0] = 1; for (int i = 1; i <= n; i++) { atMost[i][0] = newton[i][0]; for (int j = 1; j < i; j++) { add(newton[i][j], newton[i - 1][j]); add(newton[i][j], newton[i - 1][j - 1]); add(atMost[i][j], atMost[i][j - 1]); add(atMost[i][j], newton[i][j]); } add(atMost[i][i], atMost[i][i - 1]); add(atMost[i][i], newton[i][i]); } // for (int i = 0; i <= n; i++) { // for (int j = 0; j <= i; j++) { // cerr << i << " " << j << " " << newton[i][j] << " " << atMost[i][j] << endl; // } // } scanf("%d %s %d %s %d %s", &s0, &seq1[1], &s1, &seq2[1], &s2, &seq3[1]); s0++, s1++, s2++; for (int i = 1; i <= n; i++) { int b1 = seq1[i] - '0'; int b2 = seq2[i] - '0'; int b3 = seq3[i] - '0'; int sum = b1 + b2 + b3; if (sum == 0 || sum == 2) { b1 = 1 - b1; b2 = 1 - b2; b3 = 1 - b3; sum = 3 - sum; } if (sum == 3) { c3++; } else if (b1) { c0++; } else if (b2) { c1++; } else { c2++; } } // cerr << c0 << " " << c1 << " " << c2 << " " << c3 << endl; // cerr << s0 << " " << s1 << " " << s2 << endl; if (c0 < c2) { swap(c0, c2); swap(s0, s2); } if (c1 < c2) { swap(c1, c2); swap(s1, s2); } // cerr << c0 << " " << c1 << " " << c2 << " " << c3 << endl; // cerr << s0 << " " << s1 << " " << s2 << endl; int mx = max(max(s0, s1), s2); for (int prog = 0; prog <= c2; prog++) { for (int brak = prog; brak <= mx; brak++) { add(sumProg[prog][brak], mul(newton[c2][prog], atMost[c3][c3 - brak + prog])); if (prog > 0) { add(sumProg[prog][brak], sumProg[prog - 1][brak]); } } } // for (int progMin = 0; progMin <= c3; progMin++) { // for (int k = 0; k <= progMin; k++) { // if (c3 < progMin - k) continue; // add(sumProgMin[progMin], mul(newton[c2][k], atMost[c3][c3 - (progMin - k)])); // } // } // // for (int progMax = 0; progMax <= c3; progMax++) { // for (int k = progMax; k <= c2; k++) { // if (c3 < k - progMax) break; // add(sumProgMax[progMax], mul(newton[c2][k], atMost[c3][c3 - (k - progMax)])); // } // } int ans = 0; for (int i = 0; i <= c0; i++) { for (int j = 0; j <= c1; j++) { int m0 = i + (c1 - j); int m1 = j + (c0 - i); int m2 = c0 - i + c1 - j; // int mini = min(m0, m1); int low = 0; int high = c2; int res = -1; while (low <= high) { int mid = (low + high) / 2; int mm0 = m0 + c2 - mid; int mm1 = m1 + c2 - mid; int mm2 = m2 + mid; int maxiBrak = max(0, max(s0 - mm0, s1 - mm1)); int brak2 = max(0, s2 - mm2); if (brak2 == 0 || brak2 <= maxiBrak) { high = mid - 1; } else { res = mid; low = mid + 1; } } int miniProg = res; low = 0; high = c2; res = c2 + 1; while (low <= high) { int mid = (low + high) / 2; int mm0 = m0 + c2 - mid; int mm1 = m1 + c2 - mid; int mm2 = m2 + mid; int maxiBrak = max(0, max(s0 - mm0, s1 - mm1)); int brak2 = max(0, s2 - mm2); if (maxiBrak == 0 || brak2 > maxiBrak) { low = mid + 1; } else { res = mid; high = mid - 1; } } // int maxiProg = res; int toMul = 0; if (miniProg >= 0) { assert(s2 - m2 > miniProg); add(toMul, sumProg[miniProg][s2 - m2]/*sumProgMin[s2 - m2 - miniProg]*/); // cerr << "sumProgMini " << miniProg << " " << s2 - m2 << " " << sumProg[miniProg][s2 - m2] << endl; } if (maxiProg <= c2 && c2 - maxiProg <= max(s0 - m0, s1 - m1)) { assert(max(s0 - m0, s1 - m1) >= c2 - maxiProg); add(toMul, sumProg[c2 - maxiProg][max(s0 - m0, s1 - m1)]/*sumProgMax[max(s0 - m0, s1 - m1) + c2 - maxiProg]*/); // cerr << "sumProgMaxi " << c2 - maxiProg << " " << max(s0 - m0, s1 - m1) << " " << sumProg[c2 - maxiProg][max(s0 - m0, s1 - m1)] << endl; } if (miniProg <= maxiProg - 1) { int x = atMost[c2][maxiProg - 1] - atMost[c2][miniProg]; // cerr << atMost[c2][maxiProg - 1] << " " << atMost[c2][miniProg] << endl; if (x < 0) x += M; // cerr << pot[c3] << endl; // cerr << "x, pot[c3] " << x << " " << pot[c3] << endl; add(toMul, mul(x, pot[c3])); } add(ans, mul(newton[c0][i], mul(newton[c1][j], toMul))); // cerr << "ans " << ans << endl; } } // cerr << ans << endl; printf("%d\n", ((pot[n] - ans) + M) % M); }
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 | #include <bits/stdc++.h> using namespace std; const int N = 10010, M = 1e9 + 7; int newton[N][N]; int atMost[N][N]; char seq1[N], seq2[N], seq3[N]; // int sumProgMax[N], sumProgMin[N]; int sumProg[N / 3][N]; int n, pot[N], c0, c1, c2, c3, s0, s1, s2, s3; void add(int &w, int u) { w += u; if (w >= M) { w -= M; } } int mul(int w, int u) { return (long long)w * u % M; } // int Newton(int n, int k) { // if (k > n) return; // return // } int main() { scanf("%d", &n); pot[0] = 1; for (int i = 1; i <= n; i++) { pot[i] = mul(pot[i - 1], 2); } for (int i = 0; i <= n; i++) { newton[i][0] = newton[i][i] = 1; } atMost[0][0] = 1; for (int i = 1; i <= n; i++) { atMost[i][0] = newton[i][0]; for (int j = 1; j < i; j++) { add(newton[i][j], newton[i - 1][j]); add(newton[i][j], newton[i - 1][j - 1]); add(atMost[i][j], atMost[i][j - 1]); add(atMost[i][j], newton[i][j]); } add(atMost[i][i], atMost[i][i - 1]); add(atMost[i][i], newton[i][i]); } // for (int i = 0; i <= n; i++) { // for (int j = 0; j <= i; j++) { // cerr << i << " " << j << " " << newton[i][j] << " " << atMost[i][j] << endl; // } // } scanf("%d %s %d %s %d %s", &s0, &seq1[1], &s1, &seq2[1], &s2, &seq3[1]); s0++, s1++, s2++; for (int i = 1; i <= n; i++) { int b1 = seq1[i] - '0'; int b2 = seq2[i] - '0'; int b3 = seq3[i] - '0'; int sum = b1 + b2 + b3; if (sum == 0 || sum == 2) { b1 = 1 - b1; b2 = 1 - b2; b3 = 1 - b3; sum = 3 - sum; } if (sum == 3) { c3++; } else if (b1) { c0++; } else if (b2) { c1++; } else { c2++; } } // cerr << c0 << " " << c1 << " " << c2 << " " << c3 << endl; // cerr << s0 << " " << s1 << " " << s2 << endl; if (c0 < c2) { swap(c0, c2); swap(s0, s2); } if (c1 < c2) { swap(c1, c2); swap(s1, s2); } // cerr << c0 << " " << c1 << " " << c2 << " " << c3 << endl; // cerr << s0 << " " << s1 << " " << s2 << endl; int mx = max(max(s0, s1), s2); for (int prog = 0; prog <= c2; prog++) { for (int brak = prog; brak <= mx; brak++) { add(sumProg[prog][brak], mul(newton[c2][prog], atMost[c3][c3 - brak + prog])); if (prog > 0) { add(sumProg[prog][brak], sumProg[prog - 1][brak]); } } } // for (int progMin = 0; progMin <= c3; progMin++) { // for (int k = 0; k <= progMin; k++) { // if (c3 < progMin - k) continue; // add(sumProgMin[progMin], mul(newton[c2][k], atMost[c3][c3 - (progMin - k)])); // } // } // // for (int progMax = 0; progMax <= c3; progMax++) { // for (int k = progMax; k <= c2; k++) { // if (c3 < k - progMax) break; // add(sumProgMax[progMax], mul(newton[c2][k], atMost[c3][c3 - (k - progMax)])); // } // } int ans = 0; for (int i = 0; i <= c0; i++) { for (int j = 0; j <= c1; j++) { int m0 = i + (c1 - j); int m1 = j + (c0 - i); int m2 = c0 - i + c1 - j; // int mini = min(m0, m1); int low = 0; int high = c2; int res = -1; while (low <= high) { int mid = (low + high) / 2; int mm0 = m0 + c2 - mid; int mm1 = m1 + c2 - mid; int mm2 = m2 + mid; int maxiBrak = max(0, max(s0 - mm0, s1 - mm1)); int brak2 = max(0, s2 - mm2); if (brak2 == 0 || brak2 <= maxiBrak) { high = mid - 1; } else { res = mid; low = mid + 1; } } int miniProg = res; low = 0; high = c2; res = c2 + 1; while (low <= high) { int mid = (low + high) / 2; int mm0 = m0 + c2 - mid; int mm1 = m1 + c2 - mid; int mm2 = m2 + mid; int maxiBrak = max(0, max(s0 - mm0, s1 - mm1)); int brak2 = max(0, s2 - mm2); if (maxiBrak == 0 || brak2 > maxiBrak) { low = mid + 1; } else { res = mid; high = mid - 1; } } // int maxiProg = res; int toMul = 0; if (miniProg >= 0) { assert(s2 - m2 > miniProg); add(toMul, sumProg[miniProg][s2 - m2]/*sumProgMin[s2 - m2 - miniProg]*/); // cerr << "sumProgMini " << miniProg << " " << s2 - m2 << " " << sumProg[miniProg][s2 - m2] << endl; } if (maxiProg <= c2 && c2 - maxiProg <= max(s0 - m0, s1 - m1)) { assert(max(s0 - m0, s1 - m1) >= c2 - maxiProg); add(toMul, sumProg[c2 - maxiProg][max(s0 - m0, s1 - m1)]/*sumProgMax[max(s0 - m0, s1 - m1) + c2 - maxiProg]*/); // cerr << "sumProgMaxi " << c2 - maxiProg << " " << max(s0 - m0, s1 - m1) << " " << sumProg[c2 - maxiProg][max(s0 - m0, s1 - m1)] << endl; } if (miniProg <= maxiProg - 1) { int x = atMost[c2][maxiProg - 1] - atMost[c2][miniProg]; // cerr << atMost[c2][maxiProg - 1] << " " << atMost[c2][miniProg] << endl; if (x < 0) x += M; // cerr << pot[c3] << endl; // cerr << "x, pot[c3] " << x << " " << pot[c3] << endl; add(toMul, mul(x, pot[c3])); } add(ans, mul(newton[c0][i], mul(newton[c1][j], toMul))); // cerr << "ans " << ans << endl; } } // cerr << ans << endl; printf("%d\n", ((pot[n] - ans) + M) % M); } |