#include <bits/stdc++.h> using namespace std; #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for(int i=0,_=(n);i<_;++i) #define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int MOD = 1e9 + 7; const int K = 3; template<typename T> ostream &operator<<(ostream &os, const vector <T> &v) { os << "vector["; for (const auto &e: v) { os << e << ", "; } os << "]"; return os; } vector <vi> binom; void populate_binom(int n) { binom.resize(n + 1); REP(i, n + 1) { binom[i].resize(i + 1); binom[i][0] = 1; FOR(j, 1, i - 1) { binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % MOD; } binom[i][i] = 1; } } int n; int r[K]; vector<bool> bits[K]; int stage1() { int result = 0; REP(k, K) { int part = 0; REP(j, r[k] + 1) { part += binom[n][j]; part %= MOD; } DEB("stage1 :: k[" << k << "]=" << part << "\n"); result += part; result %= MOD; } DEB("stage1=" << result << "\n"); return result; } int compute_diff(int k1, int k2) { int result = 0; REP(i, n) { if (bits[k1][i] != bits[k2][i]) { ++result; } } return result; } int stage2() { int result = 0; REP(k1, K) { int r1 = r[k1]; FOR(k2, k1 + 1, K - 1) { int part = 0; int r2 = r[k2]; int diff = compute_diff(k1, k2); int ok = n - diff; if (diff <= r1 + r2) { FOR (i, max(0, diff - r2), min(r1, diff)) { int j = diff - i; DEB("stage2 :: k[" << k1 << ", " << k2 << "]" << " diff=" << diff << " i=" << i << " j=" << j << "\n"); int b = binom[diff][i]; part += b; part %= MOD; int extra = min(ok, min(r1 - i, r2 - j)); int extra_parts = 0; FOR(e, 1, extra) { int extra_part = (static_cast<ll>(b) * static_cast<ll>(binom[ok][e])) % MOD; extra_parts += extra_part; extra_parts %= MOD; } DEB("b=" << b << " extra=" << extra << " extra_parts=" << extra_parts << "\n"); part += extra_parts; part %= MOD; } } DEB("stage2 :: k[" << k1 << ", " << k2 << "]=" << part << "\n"); result += part; result %= MOD; } } DEB("stage2=" << result << "\n"); return result; } int stage3() { int result = 0; // TODO vector<bool> guess(n); REP (j, n) { int cnt = 0; REP (i, K) { cnt += int(bits[i][j]); } guess[j] = (cnt > 1); } int guess_ranges[K]; int min_guess_range = n + 1; REP(i, K) { guess_ranges[i] = r[i]; REP(j, n) { if (guess[j] != bits[i][j]) { --guess_ranges[i]; } } min_guess_range = min(min_guess_range, guess_ranges[i]); DEB("guess_ranges[" << i << "]=" << guess_ranges[i] << "\n"); } if (min_guess_range >= 0) { result += binom[n][min(min_guess_range, n)]; result %= MOD; } DEB("stage3=" << result << "\n"); return result; } void inline one() { cin >> n; populate_binom(n); REP (i, K) { string s; cin >> r[i] >> s; bits[i].resize(n); REP(j, n) { bits[i][j] = s[j] == '1'; } DEB("bits: " << bits[i] << "\n"); } int result = 0; result += stage1(); result %= MOD; result -= stage2(); result %= MOD; result += stage3(); result %= MOD; cout << result << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); //int z; cin >> z; while(z--) one(); }
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> using namespace std; #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for(int i=0,_=(n);i<_;++i) #define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int MOD = 1e9 + 7; const int K = 3; template<typename T> ostream &operator<<(ostream &os, const vector <T> &v) { os << "vector["; for (const auto &e: v) { os << e << ", "; } os << "]"; return os; } vector <vi> binom; void populate_binom(int n) { binom.resize(n + 1); REP(i, n + 1) { binom[i].resize(i + 1); binom[i][0] = 1; FOR(j, 1, i - 1) { binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % MOD; } binom[i][i] = 1; } } int n; int r[K]; vector<bool> bits[K]; int stage1() { int result = 0; REP(k, K) { int part = 0; REP(j, r[k] + 1) { part += binom[n][j]; part %= MOD; } DEB("stage1 :: k[" << k << "]=" << part << "\n"); result += part; result %= MOD; } DEB("stage1=" << result << "\n"); return result; } int compute_diff(int k1, int k2) { int result = 0; REP(i, n) { if (bits[k1][i] != bits[k2][i]) { ++result; } } return result; } int stage2() { int result = 0; REP(k1, K) { int r1 = r[k1]; FOR(k2, k1 + 1, K - 1) { int part = 0; int r2 = r[k2]; int diff = compute_diff(k1, k2); int ok = n - diff; if (diff <= r1 + r2) { FOR (i, max(0, diff - r2), min(r1, diff)) { int j = diff - i; DEB("stage2 :: k[" << k1 << ", " << k2 << "]" << " diff=" << diff << " i=" << i << " j=" << j << "\n"); int b = binom[diff][i]; part += b; part %= MOD; int extra = min(ok, min(r1 - i, r2 - j)); int extra_parts = 0; FOR(e, 1, extra) { int extra_part = (static_cast<ll>(b) * static_cast<ll>(binom[ok][e])) % MOD; extra_parts += extra_part; extra_parts %= MOD; } DEB("b=" << b << " extra=" << extra << " extra_parts=" << extra_parts << "\n"); part += extra_parts; part %= MOD; } } DEB("stage2 :: k[" << k1 << ", " << k2 << "]=" << part << "\n"); result += part; result %= MOD; } } DEB("stage2=" << result << "\n"); return result; } int stage3() { int result = 0; // TODO vector<bool> guess(n); REP (j, n) { int cnt = 0; REP (i, K) { cnt += int(bits[i][j]); } guess[j] = (cnt > 1); } int guess_ranges[K]; int min_guess_range = n + 1; REP(i, K) { guess_ranges[i] = r[i]; REP(j, n) { if (guess[j] != bits[i][j]) { --guess_ranges[i]; } } min_guess_range = min(min_guess_range, guess_ranges[i]); DEB("guess_ranges[" << i << "]=" << guess_ranges[i] << "\n"); } if (min_guess_range >= 0) { result += binom[n][min(min_guess_range, n)]; result %= MOD; } DEB("stage3=" << result << "\n"); return result; } void inline one() { cin >> n; populate_binom(n); REP (i, K) { string s; cin >> r[i] >> s; bits[i].resize(n); REP(j, n) { bits[i][j] = s[j] == '1'; } DEB("bits: " << bits[i] << "\n"); } int result = 0; result += stage1(); result %= MOD; result -= stage2(); result %= MOD; result += stage3(); result %= MOD; cout << result << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); //int z; cin >> z; while(z--) one(); } |