#include <bits/stdc++.h> #define DEBUG if(0) #define COUT cout << "\e[36m" #define ENDL "\e[39m" << endl #define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] " typedef long long LL; using namespace std; const long long MOD = 1e9+7; int n; vector<long long> factorial; vector<vector<short>> coords; vector<int> rvec; long long fastpow(LL a, LL b) { //DEBUG COUT << VAR(a) << VAR(b) << ENDL; if(b == 0) return 1; if(b%2 == 1) { LL ret = (a*fastpow(a, b-1))%MOD; //DEBUG COUT <<VAR(ret) << ENDL; return ret; //return (a*fastpow(a, b-1))%MOD; } if(b%2 == 0) { LL root = fastpow(a, b/2); return (root*root)%MOD; } } void prefactor() { DEBUG COUT << VAR(n) << ENDL; factorial.resize(n+1); for (int i = 0; i <= n; ++i) { factorial[i] = ((i == 0)? 1 : (factorial[i-1]*i)%MOD); } DEBUG for(auto el : factorial) COUT << el << " "; DEBUG COUT << ENDL; } map<pair<LL, LL>, LL> newtonmap; long long newton (LL nel, LL k) { //return (((factorial[nel]*fastpow(factorial[k], p-2))%MOD)*fastpow(factorial[n-k], MOD-2)) %MOD; //DEBUG COUT << VAR(fastpow((factorial[k]*factorial[nel-k])%MOD, MOD-2)) << ENDL; DEBUG COUT << VAR(nel) << VAR(k) << VAR(factorial.size()) << ENDL; if(k > nel) return 0; if(newtonmap.count(make_pair(nel, k))) return newtonmap[make_pair(nel, k)]; LL ret = (factorial[nel] * fastpow((factorial[k]*factorial[nel-k])%MOD, MOD-2) ) %MOD; newtonmap[make_pair(nel, k)] = ret; return ret; } long long onesphere(int a) { LL ret = 0; for (int i = 0; i <= rvec[a]; ++i) { ret += newton(n, i); ret %= MOD; DEBUG COUT << VAR(i) << VAR(ret) << VAR(newton(n, i))<< ENDL; } DEBUG COUT << "ONE" << VAR(a) << VAR(ret) << ENDL<< ENDL; assert(ret >= 0); return ret; } LL twospheres(const vector<int>& ixs) { DEBUG for(auto el : coords[ixs[0]]) COUT << el; DEBUG COUT << ENDL; DEBUG for(auto el : coords[ixs[1]]) COUT << el; DEBUG COUT << ENDL; LL ret = 0; int eqbits = 0, diffbits = 0; for (int i = 0; i < n; ++i) { if(coords[ixs[0]][i] == coords[ixs[1]][i]) eqbits++; else diffbits++; } DEBUG COUT << VAR(eqbits) << VAR(diffbits) << ENDL; for (int i = 0; i <= rvec[ixs[0]]; ++i) { for (int j = 0; j <= i; ++j) { DEBUG COUT << VAR(i) << VAR(j) << ENDL; if(diffbits+j-(i-j) <= rvec[ixs[1]]) { ret = (ret +newton(eqbits, j)*newton(diffbits, i-j))%MOD; DEBUG COUT << VAR(ret) << ENDL; } else break; } } DEBUG COUT << "TWO " << ixs[0] << " " << ixs[1] << VAR(ret) << ENDL; assert(ret >= 0); return ret; } void subtasksolve() { prefactor(); LL ans = 0; for (int i = 0; i < 2; ++i) { ans = ans+onesphere(i); } ans -= twospheres({0, 1}); ans %= MOD; cout << ans << endl; } void brtsolve() { long long ans = 0; vector<short> tmpcoords(n, 0); for (int i = 0; i <= n; ++i) { do { DEBUG for(auto el : tmpcoords) COUT << el; DEBUG COUT << ENDL; for (int j = 0; j < 3; ++j) { int diff = 0; for (int k = 0; k < n; ++k) { if(tmpcoords[k] != coords[j][k]) diff++; } if(diff <= rvec[j]) { ans = (ans+1)%MOD; break; } DEBUG COUT << VAR(j) << VAR(diff) << ENDL; } } while(next_permutation(tmpcoords.begin(), tmpcoords.end())); //next_permutation(tmpcoords.begin(), tmpcoords.end()); /*DEBUG COUT << " " <<ENDL; DEBUG for(auto el : tmpcoords) COUT << el; DEBUG COUT << ENDL;*/ if(i <= n-1) tmpcoords[n-i-1] = 1; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; coords.resize(3, vector<short>(n)); rvec.resize(3); for (int i = 0; i < 3; ++i) { cin >> rvec[i]; for (int j = 0; j < n; ++j) { char ch; cin >> ch; coords[i][j] = ch-'0'; } } DEBUG for (int i = 0; i < 3; ++i) { COUT << i << ": " << rvec[i] << " "; for(auto el : coords[i]) cout << el; cout << ENDL; } if((rvec[0] == rvec[1] && coords[0] == coords[1]) || (rvec[1] == rvec[2] && coords[1] == coords[2]) || (rvec[0] == rvec[2] && coords[0] == coords[2])) { if((rvec[0] == rvec[1] && coords[0] == coords[1])) { swap(rvec[1], rvec[2]); swap(coords[1], coords[2]); } subtasksolve(); } else { brtsolve(); } 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | #include <bits/stdc++.h> #define DEBUG if(0) #define COUT cout << "\e[36m" #define ENDL "\e[39m" << endl #define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] " typedef long long LL; using namespace std; const long long MOD = 1e9+7; int n; vector<long long> factorial; vector<vector<short>> coords; vector<int> rvec; long long fastpow(LL a, LL b) { //DEBUG COUT << VAR(a) << VAR(b) << ENDL; if(b == 0) return 1; if(b%2 == 1) { LL ret = (a*fastpow(a, b-1))%MOD; //DEBUG COUT <<VAR(ret) << ENDL; return ret; //return (a*fastpow(a, b-1))%MOD; } if(b%2 == 0) { LL root = fastpow(a, b/2); return (root*root)%MOD; } } void prefactor() { DEBUG COUT << VAR(n) << ENDL; factorial.resize(n+1); for (int i = 0; i <= n; ++i) { factorial[i] = ((i == 0)? 1 : (factorial[i-1]*i)%MOD); } DEBUG for(auto el : factorial) COUT << el << " "; DEBUG COUT << ENDL; } map<pair<LL, LL>, LL> newtonmap; long long newton (LL nel, LL k) { //return (((factorial[nel]*fastpow(factorial[k], p-2))%MOD)*fastpow(factorial[n-k], MOD-2)) %MOD; //DEBUG COUT << VAR(fastpow((factorial[k]*factorial[nel-k])%MOD, MOD-2)) << ENDL; DEBUG COUT << VAR(nel) << VAR(k) << VAR(factorial.size()) << ENDL; if(k > nel) return 0; if(newtonmap.count(make_pair(nel, k))) return newtonmap[make_pair(nel, k)]; LL ret = (factorial[nel] * fastpow((factorial[k]*factorial[nel-k])%MOD, MOD-2) ) %MOD; newtonmap[make_pair(nel, k)] = ret; return ret; } long long onesphere(int a) { LL ret = 0; for (int i = 0; i <= rvec[a]; ++i) { ret += newton(n, i); ret %= MOD; DEBUG COUT << VAR(i) << VAR(ret) << VAR(newton(n, i))<< ENDL; } DEBUG COUT << "ONE" << VAR(a) << VAR(ret) << ENDL<< ENDL; assert(ret >= 0); return ret; } LL twospheres(const vector<int>& ixs) { DEBUG for(auto el : coords[ixs[0]]) COUT << el; DEBUG COUT << ENDL; DEBUG for(auto el : coords[ixs[1]]) COUT << el; DEBUG COUT << ENDL; LL ret = 0; int eqbits = 0, diffbits = 0; for (int i = 0; i < n; ++i) { if(coords[ixs[0]][i] == coords[ixs[1]][i]) eqbits++; else diffbits++; } DEBUG COUT << VAR(eqbits) << VAR(diffbits) << ENDL; for (int i = 0; i <= rvec[ixs[0]]; ++i) { for (int j = 0; j <= i; ++j) { DEBUG COUT << VAR(i) << VAR(j) << ENDL; if(diffbits+j-(i-j) <= rvec[ixs[1]]) { ret = (ret +newton(eqbits, j)*newton(diffbits, i-j))%MOD; DEBUG COUT << VAR(ret) << ENDL; } else break; } } DEBUG COUT << "TWO " << ixs[0] << " " << ixs[1] << VAR(ret) << ENDL; assert(ret >= 0); return ret; } void subtasksolve() { prefactor(); LL ans = 0; for (int i = 0; i < 2; ++i) { ans = ans+onesphere(i); } ans -= twospheres({0, 1}); ans %= MOD; cout << ans << endl; } void brtsolve() { long long ans = 0; vector<short> tmpcoords(n, 0); for (int i = 0; i <= n; ++i) { do { DEBUG for(auto el : tmpcoords) COUT << el; DEBUG COUT << ENDL; for (int j = 0; j < 3; ++j) { int diff = 0; for (int k = 0; k < n; ++k) { if(tmpcoords[k] != coords[j][k]) diff++; } if(diff <= rvec[j]) { ans = (ans+1)%MOD; break; } DEBUG COUT << VAR(j) << VAR(diff) << ENDL; } } while(next_permutation(tmpcoords.begin(), tmpcoords.end())); //next_permutation(tmpcoords.begin(), tmpcoords.end()); /*DEBUG COUT << " " <<ENDL; DEBUG for(auto el : tmpcoords) COUT << el; DEBUG COUT << ENDL;*/ if(i <= n-1) tmpcoords[n-i-1] = 1; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; coords.resize(3, vector<short>(n)); rvec.resize(3); for (int i = 0; i < 3; ++i) { cin >> rvec[i]; for (int j = 0; j < n; ++j) { char ch; cin >> ch; coords[i][j] = ch-'0'; } } DEBUG for (int i = 0; i < 3; ++i) { COUT << i << ": " << rvec[i] << " "; for(auto el : coords[i]) cout << el; cout << ENDL; } if((rvec[0] == rvec[1] && coords[0] == coords[1]) || (rvec[1] == rvec[2] && coords[1] == coords[2]) || (rvec[0] == rvec[2] && coords[0] == coords[2])) { if((rvec[0] == rvec[1] && coords[0] == coords[1])) { swap(rvec[1], rvec[2]); swap(coords[1], coords[2]); } subtasksolve(); } else { brtsolve(); } return 0; } |