Submission #7115801
Source Code Expand
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int MOD = (int)1e9 + 7;
//---------------------------------------------------------------
template<int MOD> struct ModInt { int x;
explicit operator bool() const { return !!x; }
ModInt(int v = 0) : x(v % MOD) { if (x < 0) x += MOD; }
ModInt(long long v) : x(v % MOD) { if (x < 0) x += MOD; }
ModInt &operator+=(const ModInt &r) { if ((x += r.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(const ModInt &r) { if ((x += MOD - r.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(const ModInt &r) { x = 1LL * x * r.x % MOD; return *this; }
ModInt &operator/=(const ModInt &r) { return *this *= r.inv(); }
ModInt operator-() const { return x ? ModInt(MOD - x) : ModInt(x); }
ModInt operator+(const ModInt &r) const { return ModInt(*this) += r; }
ModInt operator-(const ModInt &r) const { return ModInt(*this) -= r; }
ModInt operator*(const ModInt &r) const { return ModInt(*this) *= r; }
ModInt operator/(const ModInt &r) const { return ModInt(*this) /= r; }
ModInt inv() const { long long a = x, b = MOD, u = 1, v = 0;
while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); }
return ModInt(u); } // (*this).pow(MOD - 2);
ModInt pow(long long k) const { ModInt r(1), a(x);
while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
bool operator==(const ModInt r) const { return x == r.x; }
bool operator!=(const ModInt r) const { return x != r.x; }
bool operator< (const ModInt r) const { return x < r.x; }
friend ostream& operator<<(ostream &os, const ModInt<MOD>& a) { return os << a.x; }
friend istream& operator>>(istream &is, ModInt<MOD>& a) { return is >> a.x; }
};
template<typename T, int SZ> struct Comb { vector<T> _fac, _ifac, _inv;
Comb() : _fac(SZ + 1), _ifac(SZ + 1), _inv(SZ + 1) {
_fac[0] = _ifac[SZ] = _inv[0] = 1;
for (int i = 1; i <= SZ; i++) _fac[i] = _fac[i - 1] * i;
_ifac[SZ] /= _fac[SZ];
for (int i = SZ - 1; i >= 0; i--) _ifac[i] = _ifac[i + 1] * (i + 1);
for (int i = 1; i <= SZ; i++) _inv[i] = _ifac[i] * _fac[i - 1]; }
T inv(int n) { return n < 0 ? T(0) : _inv[n]; }
T fac(int n) { return n < 0 ? T(0) : _fac[n]; }
T ifac(int n) { return n < 0 ? T(0) : _ifac[n]; }
T P(int a, int b) { return (b < 0 || a < b) ? T(0) : _fac[a] * _ifac[a - b]; }
T C(int a, int b) { return b < 0 ? T(0) : P(a, b) * _ifac[b]; }
T H(int n, int k) { if (n < 0 || k < 0) return T(0);
return k == 0 ? T(1) : C(n + k - 1, k); }
T S(int n, int k) { T r = 0;
for (int i = 0; i <= k; i++) {
T t = C(k, i) * T(i).pow(n); r += ((k - i) & 1 ? -t : t);
}
return r * _ifac[k];
}
T B(int n, int k) {
if (n == 0) return T(1);
T r = 0; k = min(k, n);
vector<T> dp(k + 1); dp[0] = T(1);
for (int i = 1; i <= k; i++) dp[i] = dp[i - 1] + (i & 1 ? -_ifac[i] : _ifac[i]);
for (int i = 1; i <= k; i++) r += T(i).pow(n) * _ifac[i] * dp[k - i];
return r;
}
};
typedef ModInt<MOD> mint;
//---------------------------------------------------------------
template<class T> ostream& operator<<(ostream& os, const vector<T>& vec) { for (auto &vi: vec) os << vi << " "; return os; }
int main() {
Comb<mint, 200000> com;
int n; cin >> n;
string s; cin >> s;
map<pair<int, int>, mint> dp;
dp[{0, 0}] = 1;
for (int i = 0; i < n * 2; i++) {
map<pair<int, int>, mint> dp2;
for (auto &p: dp) {
auto &st = p.first;
int op = st.first, cl = st.second;
mint cur = p.second;
if (s[i] == 'W') {
if (op % 2 == 0) { // close
if (cl + 1 <= n && op) dp2[{op - 1, cl + 1}] += cur * op;
} else { // open
if (op + 1 <= n) dp2[{op + 1, cl}] += cur;
}
}
if (s[i] == 'B') {
if (op % 2 == 1) { // close
if (cl + 1 <= n && op) dp2[{op - 1, cl + 1}] += cur * op;
} else { // open
if (op + 1 <= n) dp2[{op + 1, cl}] += cur;
}
}
}
swap(dp, dp2);
}
cout << dp[{0, n}] * com.fac(n) << endl;
return 0;
}
Submission Info
| Submission Time |
|
| Task |
C - Cell Inversion |
| User |
kyuna |
| Language |
C++14 (GCC 5.4.1) |
| Score |
500 |
| Code Size |
4521 Byte |
| Status |
AC |
| Exec Time |
25 ms |
| Memory |
2948 KiB |
Judge Result
| Set Name |
All |
Sample |
| Score / Max Score |
500 / 500 |
0 / 0 |
| Status |
|
|
| Set Name |
Test Cases |
| All |
sample_01, sample_02, sample_03, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, testcase_14, testcase_15, testcase_16, testcase_17, testcase_18, testcase_19, testcase_20, testcase_21, testcase_22, testcase_23, testcase_24, testcase_25, testcase_26, testcase_27, testcase_28, testcase_29, testcase_30, testcase_31, testcase_32, testcase_33 |
| Sample |
sample_01, sample_02, sample_03 |
| Case Name |
Status |
Exec Time |
Memory |
| sample_01 |
AC |
6 ms |
2560 KiB |
| sample_02 |
AC |
6 ms |
2560 KiB |
| sample_03 |
AC |
6 ms |
2560 KiB |
| testcase_01 |
AC |
10 ms |
2816 KiB |
| testcase_02 |
AC |
10 ms |
2816 KiB |
| testcase_03 |
AC |
13 ms |
2948 KiB |
| testcase_04 |
AC |
10 ms |
2688 KiB |
| testcase_05 |
AC |
15 ms |
2816 KiB |
| testcase_06 |
AC |
13 ms |
2816 KiB |
| testcase_07 |
AC |
19 ms |
2944 KiB |
| testcase_08 |
AC |
25 ms |
2948 KiB |
| testcase_09 |
AC |
25 ms |
2948 KiB |
| testcase_10 |
AC |
6 ms |
2560 KiB |
| testcase_11 |
AC |
8 ms |
2816 KiB |
| testcase_12 |
AC |
13 ms |
2948 KiB |
| testcase_13 |
AC |
25 ms |
2948 KiB |
| testcase_14 |
AC |
25 ms |
2944 KiB |
| testcase_15 |
AC |
19 ms |
2948 KiB |
| testcase_16 |
AC |
15 ms |
2816 KiB |
| testcase_17 |
AC |
21 ms |
2948 KiB |
| testcase_18 |
AC |
20 ms |
2944 KiB |
| testcase_19 |
AC |
21 ms |
2944 KiB |
| testcase_20 |
AC |
13 ms |
2816 KiB |
| testcase_21 |
AC |
25 ms |
2948 KiB |
| testcase_22 |
AC |
24 ms |
2948 KiB |
| testcase_23 |
AC |
16 ms |
2816 KiB |
| testcase_24 |
AC |
24 ms |
2948 KiB |
| testcase_25 |
AC |
24 ms |
2948 KiB |
| testcase_26 |
AC |
6 ms |
2560 KiB |
| testcase_27 |
AC |
6 ms |
2560 KiB |
| testcase_28 |
AC |
24 ms |
2948 KiB |
| testcase_29 |
AC |
13 ms |
2948 KiB |
| testcase_30 |
AC |
24 ms |
2948 KiB |
| testcase_31 |
AC |
13 ms |
2948 KiB |
| testcase_32 |
AC |
19 ms |
2948 KiB |
| testcase_33 |
AC |
13 ms |
2948 KiB |