#include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " // ****************************************************************************** int mod = 1e9+7; LL mod2 = (LL)mod * mod; const int MXN = 1e4+5; int n; int r[3]; int in[3][MXN]; int t[2][2]; int dwumian[MXN][MXN]; int dp2[MXN]; int dp3[MXN][MXN]; inline LL npok(int a, int b) { return dwumian[a][b]; } void solve_pre(int jed_cel, int jed_beg, int zer_beg, int dp[MXN]) { FOR(jed_na_jed, max(0, jed_cel-zer_beg), min(jed_cel, jed_beg)) { int zer_na_jed = jed_cel - jed_na_jed; int jed_na_zera = jed_beg - jed_na_jed; int r = zer_na_jed + jed_na_zera; dp[r] = npok(jed_beg, jed_na_jed) * npok(zer_beg, zer_na_jed) % mod; } } void solve2_1_pre(int i) { solve_pre(i, t[1][1], t[1][0], dp2); } inline void add(int &a, int b) { a += b; if(a >= mod)a -= mod; } void solve2_2_pre() { FOR(i, 0, n) { solve_pre(i, t[0][0], t[0][1], dp3[i]); FOR(j, 1, n)add(dp3[i][j], dp3[i][j-1]); if(i)FOR(j, 0, n)add(dp3[i][j], dp3[i-1][j]); } } int solve4(int i, int j) { memset(dp2, 0, sizeof(dp2[0]) * (r[2]+1)); solve2_1_pre(i); LL res = 0; FOR(dist, 0, r[2]) { res += (LL)dp2[r[2]-dist] * dp3[j][dist]; if(res >= mod2)res -= mod2; } return res % mod; } int main() { scanf("%d", &n); dwumian[0][0] = 1; FOR(i, 1, n) { dwumian[i][0] = 1; FOR(j, 1, i) { dwumian[i][j] = (dwumian[i-1][j-1] + dwumian[i-1][j]); if(dwumian[i][j] >= mod)dwumian[i][j] -= mod; } } REP(i, 3) { scanf("%d", &r[i]); FOR(j, 1, n) { char c; scanf(" %c", &c); in[i][j] = c - '0'; } } FOR(i, 1, n) t[in[1][i] ^ in[0][i]][in[2][i] ^ in[0][i]]++; solve2_2_pre(); LL res = 0; FOR(i, 0, n) // ile jedynek w wynikowym ciągu { if(i <= r[0]){res += npok(n, i); continue;} // te robione przez 1 int poc = max(0, i - t[0][0] - t[0][1]); int kon = min(i, t[1][0] + t[1][1]); FOR(jed_na_jed, poc, kon) { int zer_na_jed = i-jed_na_jed; // ile zer ciągu drugiego zmieni się na 1 int jed_na_zera = t[1][0] + t[1][1] - jed_na_jed; if(zer_na_jed + jed_na_zera <= r[1]) { res += npok(t[1][0] + t[1][1], jed_na_jed) * npok(t[0][0] + t[0][1], zer_na_jed); // te robione przez 2 i nie 1 if(res >= mod2)res -= mod2; } } } res %= mod; FOR(i, 0, t[1][0] + t[1][1]) { int max_j = min(t[0][0] + t[0][1] - r[0] + i - 1, n - r[1] - i - 1); if(max_j >= 0)res += solve4(i, max_j); // te robione przez 3 i nie 1 ani 2 res %= mod; } cout << res << endl; }
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 | #include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " // ****************************************************************************** int mod = 1e9+7; LL mod2 = (LL)mod * mod; const int MXN = 1e4+5; int n; int r[3]; int in[3][MXN]; int t[2][2]; int dwumian[MXN][MXN]; int dp2[MXN]; int dp3[MXN][MXN]; inline LL npok(int a, int b) { return dwumian[a][b]; } void solve_pre(int jed_cel, int jed_beg, int zer_beg, int dp[MXN]) { FOR(jed_na_jed, max(0, jed_cel-zer_beg), min(jed_cel, jed_beg)) { int zer_na_jed = jed_cel - jed_na_jed; int jed_na_zera = jed_beg - jed_na_jed; int r = zer_na_jed + jed_na_zera; dp[r] = npok(jed_beg, jed_na_jed) * npok(zer_beg, zer_na_jed) % mod; } } void solve2_1_pre(int i) { solve_pre(i, t[1][1], t[1][0], dp2); } inline void add(int &a, int b) { a += b; if(a >= mod)a -= mod; } void solve2_2_pre() { FOR(i, 0, n) { solve_pre(i, t[0][0], t[0][1], dp3[i]); FOR(j, 1, n)add(dp3[i][j], dp3[i][j-1]); if(i)FOR(j, 0, n)add(dp3[i][j], dp3[i-1][j]); } } int solve4(int i, int j) { memset(dp2, 0, sizeof(dp2[0]) * (r[2]+1)); solve2_1_pre(i); LL res = 0; FOR(dist, 0, r[2]) { res += (LL)dp2[r[2]-dist] * dp3[j][dist]; if(res >= mod2)res -= mod2; } return res % mod; } int main() { scanf("%d", &n); dwumian[0][0] = 1; FOR(i, 1, n) { dwumian[i][0] = 1; FOR(j, 1, i) { dwumian[i][j] = (dwumian[i-1][j-1] + dwumian[i-1][j]); if(dwumian[i][j] >= mod)dwumian[i][j] -= mod; } } REP(i, 3) { scanf("%d", &r[i]); FOR(j, 1, n) { char c; scanf(" %c", &c); in[i][j] = c - '0'; } } FOR(i, 1, n) t[in[1][i] ^ in[0][i]][in[2][i] ^ in[0][i]]++; solve2_2_pre(); LL res = 0; FOR(i, 0, n) // ile jedynek w wynikowym ciągu { if(i <= r[0]){res += npok(n, i); continue;} // te robione przez 1 int poc = max(0, i - t[0][0] - t[0][1]); int kon = min(i, t[1][0] + t[1][1]); FOR(jed_na_jed, poc, kon) { int zer_na_jed = i-jed_na_jed; // ile zer ciągu drugiego zmieni się na 1 int jed_na_zera = t[1][0] + t[1][1] - jed_na_jed; if(zer_na_jed + jed_na_zera <= r[1]) { res += npok(t[1][0] + t[1][1], jed_na_jed) * npok(t[0][0] + t[0][1], zer_na_jed); // te robione przez 2 i nie 1 if(res >= mod2)res -= mod2; } } } res %= mod; FOR(i, 0, t[1][0] + t[1][1]) { int max_j = min(t[0][0] + t[0][1] - r[0] + i - 1, n - r[1] - i - 1); if(max_j >= 0)res += solve4(i, max_j); // te robione przez 3 i nie 1 ani 2 res %= mod; } cout << res << endl; } |