题面
来源:USACO 官网。模型收到的是同一份题面的文字。
It's the year $3000$, and Bessie became the first cow in space! During her journey between the stars, she found a number line with $N$ ($2 \leq N \leq 5 \cdot 10^5$) points, numbered from $1$ to $N$. All points are initially colored white. She can perform the following operation any number of times.
- Choose a position $i$ within the number line and a positive integer $x$. Then, color all the points in the interval $[i, i + x - 1]$ red and all points in $[i + x, i + 2x - 1]$ blue. All chosen intervals must be disjoint (i.e. no points in $[i, i + 2x - 1]$ can be already colored red or blue). The entire interval must also fall within the number line (i.e. $1 \leq i \leq i + 2x - 1 \leq N$).
Farmer John gives Bessie a string $s$ of length $N$ consisting of characters $R$, $B$, and $X$. The string represents Farmer John's color preferences for each point: $s_i=R$ means the $i$'th point must be colored red, $s_i = B$ means the $i$'th point must be colored blue, and $s_i = X$ means there is no constraint on the color for the $i$'th point.
Help Bessie count the number of distinct ways for the number line to be colored while satisfying Farmer John's preferences. Two colorings are different if there is at least one corresponding point with a different color. Because the answer may be large, output it modulo $10^9+7$.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains an integer $N$.The following line contains string $s$.
OUTPUT FORMAT (print output to the terminal / stdout):
Output the number of distinct ways for the number line to be colored while satisfying Farmer John's preferences modulo $10^9+7$.SAMPLE INPUT:
6 RXXXXB
SAMPLE OUTPUT:
5Bessie can choose $i=1,x=1$ (i.e. color point $1$ red and point $2$ blue) and $i=3,x=2$ (i.e. color points $3,4$ red and points $5,6$ blue) to produce the coloring $RBRRBB$.
The other colorings are $RRBBRB$, $RBWWRB$, $RRRBBB$, and $RBRBRB$.
SAMPLE INPUT:
6 XXRBXX
SAMPLE OUTPUT:
6The six colorings are $WWRBWW$, $WWRBRB$, $WRRBBW$, $RBRBWW$, $RBRBRB$, and $RRRBBB$.
SAMPLE INPUT:
12 XBXXXXRXRBXX
SAMPLE OUTPUT:
18
SCORING:
- Input 4: $N\le 500$
- Inputs 5-6: $N\le 10^4$
- Inputs 7-13: All but at most $100$ characters in $s$ are $X$.
- Inputs 14-23: No additional constraints
Problem credits: Chongtian Ma, Alex Liang
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 36.0k平均输出 token(含思考) ↓
- 23.8输出速度 tokens/s ↑
- 1591 秒延迟 ↓
- 0.94×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 44.7k平均输出 token(含思考) ↓
- 200.5输出速度 tokens/s ↑
- 226 秒延迟 ↓
- 1.31×CPU 时间 / 官方解 ↓
- 不确定 1、同阶 2时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 32.5k平均输出 token(含思考) ↓
- 142.8输出速度 tokens/s ↑
- 215 秒延迟 ↓
- 0.97×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string s; cin >> N >> s; vector<int> nextB(N + 1, N), nextR(N + 1, N), prevR(N, -1); for (int i = N - 1; i >= 0; --i) { nextB[i] = (s[i] == 'B' ? i : nextB[i + 1]); nextR[i] = (s[i] == 'R' ? i : nextR[i + 1]); } int last = -1; for (int i = 0; i < N; ++i) { if (s[i] == 'R') last = i; prevR[i] = last; } vector<int> dp(N + 1, 0); array<vector<int>, 2> diff; diff[0].assign(N + 3, 0); diff[1].assign(N + 3, 0); long long active[2] = {0, 0}; dp[0] = 1; for (int p = 0; p <= N; ++p) { int parity = p & 1; active[parity] += diff[parity][p]; if (active[parity] >= MOD) active[parity] -= MOD; int val = dp[p] + (int)active[parity]; if (val >= MOD) val -= MOD; dp[p] = val; if (p == N) break; if (val == 0) continue; if (s[p] == 'X') { dp[p + 1] += val; if (dp[p + 1] >= MOD) dp[p + 1] -= MOD; } int U = min((N - p) / 2, nextB[p] - p); if (U <= 0) continue; auto &D = diff[parity]; // Add val to dp[p+2*x] for every x in [a,b]. auto addInterval = [&](int a, int b) { if (a > b) return; int l = p + 2 * a; int r = p + 2 * b; int z = D[l] + val; if (z >= MOD) z -= MOD; D[l] = z; z = D[r + 2] - val; if (z < 0) z += MOD; D[r + 2] = z; }; /* * For an R at distance d from p, the operation is invalid exactly * for half-lengths x in [d/2+1, d]. Only d <= 2*U-1 matters. * Merge these intervals; their union has O(log N) components. */ int lastPos = p + 2 * U - 1; int rpos = nextR[p + 1]; int cursor = 1; while (rpos <= lastPos) { int d = rpos - p; int L = d / 2 + 1; addInterval(cursor, L - 1); int c = d; while (c < U) { int far = prevR[p + 2 * c + 1]; if (far > p + c) { c = far - p; } else { break; } } if (c >= U) { cursor = U + 1; break; } cursor = c + 1; int nextPos = p + 2 * c + 2; rpos = (nextPos > lastPos ? N : nextR[nextPos]); } addInterval(cursor, U); } cout << dp[N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static constexpr int MOD = 1000000007; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string s; cin >> N >> s; vector<int> nextR(N + 2, N + 1), nextB(N + 2, N + 1); for (int i = N; i >= 1; --i) { nextR[i] = (s[i - 1] == 'R') ? i : nextR[i + 1]; nextB[i] = (s[i - 1] == 'B') ? i : nextB[i + 1]; } vector<int> prevR(N + 1, 0); for (int i = 1; i <= N; ++i) { prevR[i] = (s[i - 1] == 'R') ? i : prevR[i - 1]; } vector<int> dp(N + 2, 0); vector<int> diff[2]; diff[0].assign(N + 3, 0); diff[1].assign(N + 3, 0); int running[2] = {0, 0}; dp[1] = 1; for (int i = 1; i <= N + 1; ++i) { int parity = i & 1; int k = i >> 1; running[parity] += diff[parity][k]; if (running[parity] >= MOD) running[parity] -= MOD; dp[i] += running[parity]; if (dp[i] >= MOD) dp[i] -= MOD; if (i == N + 1) break; int value = dp[i]; if (value == 0) continue; if (s[i - 1] == 'X') { dp[i + 1] += value; if (dp[i + 1] >= MOD) dp[i + 1] -= MOD; } int maxX = min((N - i + 1) / 2, nextB[i] - i); if (maxX <= 0) continue; auto addRange = [&](int l, int r) { if (l > r) return; int left = (i + 2 * l) >> 1; int right = (i + 2 * r) >> 1; int p = i & 1; diff[p][left] += value; if (diff[p][left] >= MOD) diff[p][left] -= MOD; diff[p][right + 1] -= value; if (diff[p][right + 1] < 0) diff[p][right + 1] += MOD; }; int r = nextR[i + 1]; if (r > N) { addRange(1, maxX); continue; } int d = r - i; int a = d / 2 + 1; if (a > maxX) { addRange(1, maxX); continue; } int current = 1; while (true) { addRange(current, a - 1); int end = d; while (end < maxX) { int limit = i + 2 * end + 1; if (limit > N) limit = N; int last = prevR[limit]; if (last <= r) break; r = last; end = r - i; } if (end >= maxX) break; current = end + 1; r = (r < N) ? nextR[r + 1] : N + 1; if (r > N) { addRange(current, maxX); break; } d = r - i; a = d / 2 + 1; if (a > maxX) { addRange(current, maxX); break; } } } cout << dp[N + 1] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static constexpr int MOD = 1000000007; struct Fenwick { int n = 0; vector<int> tree; void init(int size) { n = size; tree.assign(n + 1, 0); } // Adds val to source index pos (0-based). void add(int pos, int val) { for (int i = pos + 1; i <= n; i += i & -i) { tree[i] += val; if (tree[i] >= MOD) tree[i] -= MOD; } } // Sum of source indices 0 through cnt-1. int prefix(int cnt) const { long long result = 0; for (int i = cnt; i > 0; i -= i & -i) { result += tree[i]; } return static_cast<int>(result % MOD); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string s; cin >> N >> s; // nextB[i] = first forced B at position i or later. vector<int> nextB(N + 2, N + 1); int nxt = N + 1; for (int i = N; i >= 1; --i) { if (s[i - 1] == 'B') nxt = i; nextB[i] = nxt; } vector<int> dp(N + 1, 0); dp[0] = 1; Fenwick bit[2]; bit[0].init(N + 1); bit[1].init(N + 1); int activeSum[2] = {0, 0}; vector<vector<int>> removals(N + 2); int lastR = 0; for (int b = 1; b <= N; ++b) { for (int a : removals[b]) { int p = a & 1; bit[p].add(a, MOD - dp[a]); activeSum[p] -= dp[a]; if (activeSum[p] < 0) activeSum[p] += MOD; } // A source a becomes available for tokens ending at b = a + 2. if (b >= 2) { int a = b - 2; if (dp[a] != 0) { int t = nextB[a + 1]; long long expiration; if (t == N + 1) { expiration = N + 1LL; } else { expiration = 2LL * t - a - 1; } if (expiration >= b) { int p = a & 1; bit[p].add(a, dp[a]); activeSum[p] += dp[a]; if (activeSum[p] >= MOD) activeSum[p] -= MOD; if (expiration + 1 <= N) { removals[static_cast<int>(expiration + 1)].push_back(a); } } } } if (s[b - 1] == 'R') lastR = b; // Blue-half compatibility requires a >= 2*lastR-b. int lower = max(0, 2 * lastR - b); int p = b & 1; long long ways = activeSum[p] - bit[p].prefix(lower); if (ways < 0) ways += MOD; if (s[b - 1] == 'X') { ways += dp[b - 1]; if (ways >= MOD) ways -= MOD; } dp[b] = static_cast<int>(ways); } cout << dp[N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; struct Run { int a, b, len, val; }; int N, M; vector<Run> runs; vector<int> seg; vector<int> pref0, pref1; int curI, curL, curBlock; void buildSeg(int node, int l, int r) { if (l == r) { seg[node] = runs[l].val; return; } int mid = (l + r) >> 1; buildSeg(node << 1, l, mid); buildSeg(node << 1 | 1, mid + 1, r); seg[node] = max(seg[node << 1], seg[node << 1 | 1]); } void addRun(int idx) { const Run &r = runs[idx]; int lo = max(r.a, curI - curL); lo = max(lo, (curI + r.a) / 2); int hi = min(r.b, curI - 1); if (lo > hi) return; int pL = 2 * lo - curI; int pH = 2 * hi - curI; if (pH < 0) return; if (pL < 0) pL = 0; int total; if (curI & 1) { total = pref1[pH]; if (pL > 0) total -= pref1[pL - 1]; } else { total = pref0[pH]; if (pL > 0) total -= pref0[pL - 1]; } if (total < 0) total += MOD; curBlock += total; if (curBlock >= MOD) curBlock -= MOD; } void processRange(int node, int l, int r, int ql, int qr, int threshold) { if (r < ql || l > qr || seg[node] < threshold) return; if (l == r) { addRun(l); return; } int mid = (l + r) >> 1; processRange(node << 1, l, mid, ql, qr, threshold); processRange(node << 1 | 1, mid + 1, r, ql, qr, threshold); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; string s; cin >> s; vector<int> runId(N + 1, -1); vector<int> ends; for (int pos = 1; pos <= N;) { if (s[pos - 1] == 'B') { ++pos; continue; } int a = pos; while (pos <= N && s[pos - 1] != 'B') { ++pos; } int b = pos - 1; int len = b - a + 1; int idx = (int)runs.size(); runs.push_back({a, b, len, b + len}); ends.push_back(b); for (int p = a; p <= b; ++p) { runId[p] = idx; } } M = (int)runs.size(); if (M > 0) { seg.assign(4 * M + 5, 0); buildSeg(1, 0, M - 1); } vector<int> dp(N + 1, 0); pref0.assign(N + 1, 0); pref1.assign(N + 1, 0); dp[0] = 1; pref0[0] = 1; int Lprev = 0; int rightIdx = -1; for (int i = 1; i <= N; ++i) { int L; if (s[i - 1] != 'R') L = Lprev + 1; else L = 0; Lprev = L; while (M > 0 && rightIdx + 1 < M && runs[rightIdx + 1].b <= i - 1) { ++rightIdx; } int ways = 0; if (s[i - 1] == 'X') { ways = dp[i - 1]; } int block = 0; if (L > 0 && M > 0) { int lowerPos = i - L; int lidx = lower_bound(ends.begin(), ends.end(), lowerPos) - ends.begin(); int ridx = rightIdx; curI = i; curL = L; curBlock = 0; if (lidx <= ridx) { processRange(1, 0, M - 1, lidx, ridx, i); } if (i >= 2 && s[i - 2] != 'B') { int rid = runId[i - 1]; if (rid != -1 && runs[rid].b >= i) { addRun(rid); } } block = curBlock; } int val = ways + block; if (val >= MOD) val -= MOD; dp[i] = val; pref0[i] = pref0[i - 1]; pref1[i] = pref1[i - 1]; if (i & 1) { pref1[i] += dp[i]; if (pref1[i] >= MOD) pref1[i] -= MOD; } else { pref0[i] += dp[i]; if (pref0[i] >= MOD) pref0[i] -= MOD; } } cout << dp[N] % MOD << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; struct BIT { int n; vector<int> tree; BIT() {} BIT(int n) { init(n); } void init(int n_) { n = n_; tree.assign(n + 1, 0); } void add(int idx, int val) { if (val < 0) val += MOD; for (; idx <= n; idx += idx & -idx) { tree[idx] += val; if (tree[idx] >= MOD) tree[idx] -= MOD; } } int sum(int idx) const { int res = 0; for (; idx > 0; idx -= idx & -idx) { res += tree[idx]; if (res >= MOD) res -= MOD; } return res; } int rangeSum(int l, int r) const { if (l > r) return 0; int res = sum(r) - sum(l - 1); if (res < 0) res += MOD; return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; string s; cin >> s; vector<int> nextB(N + 2); int nb = N + 1; for (int i = N; i >= 1; --i) { if (s[i - 1] == 'B') nb = i; nextB[i] = nb; } vector<int> expire(N + 2); for (int L = 1; L <= N; ++L) { int p = nextB[L]; long long e = 2LL * p - L - 1; if (e > N + 1) e = N + 1; expire[L] = (int)e; } vector<int> dp(N + 1, 0); dp[0] = 1; BIT bit[2]; bit[0].init(N); bit[1].init(N); vector<vector<int>> removeAt(N + 2); int lastR = 0; for (int i = 1; i <= N; ++i) { for (int L : removeAt[i]) { int w = dp[L - 1]; bit[L & 1].add(L, -w); } int Lins = i - 1; if (Lins >= 1 && expire[Lins] >= i) { int w = dp[Lins - 1]; bit[Lins & 1].add(Lins, w); int remTime = expire[Lins] + 1; if (remTime <= N) { removeAt[remTime].push_back(Lins); } } if (s[i - 1] == 'R') lastR = i; int bound = 2 * lastR - i + 1; if (bound < 1) bound = 1; int tokenWays = 0; if (bound <= i - 1) { int parity = (i - 1) & 1; tokenWays = bit[parity].rangeSum(bound, i - 1); } long long ways = tokenWays; if (s[i - 1] == 'X') { ways += dp[i - 1]; } dp[i] = ways % MOD; } cout << dp[N] % MOD << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static const int MOD = 1000000007; struct Fenwick { int n; vector<int> bit; Fenwick() : n(0) {} Fenwick(int n) : n(n), bit(n + 1, 0) {} void add(int idx, int val) { for (int i = idx + 1; i <= n; i += i & -i) { int x = bit[i] + val; if (x >= MOD) x -= MOD; bit[i] = x; } } int sumPrefix(int idx) const { if (idx < 0) return 0; if (idx >= n) idx = n - 1; int res = 0; for (int i = idx + 1; i > 0; i -= i & -i) { res += bit[i]; if (res >= MOD) res -= MOD; } return res; } int rangeSum(int l, int r) const { if (l > r) return 0; int res = sumPrefix(r) - sumPrefix(l - 1); if (res < 0) res += MOD; return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string s; cin >> N; cin >> s; vector<int> nextB(N + 1); nextB[N] = N; // one past the end for (int i = N - 1; i >= 0; --i) { if (s[i] == 'B') nextB[i] = i; else nextB[i] = nextB[i + 1]; } vector<int> expire(N + 1); for (int a = 0; a <= N; ++a) { long long u = 2LL * nextB[a] - a; if (u > N) u = N; expire[a] = (int)u; } vector<int> dp(N + 1, 0); dp[0] = 1; Fenwick fen[2] = {Fenwick(N + 1), Fenwick(N + 1)}; vector<vector<int>> removals(N + 2); int lastR = -1; for (int i = 1; i <= N; ++i) { // Remove starts that are no longer usable for this end. for (int a : removals[i]) { int p = a & 1; int val = (MOD - dp[a]) % MOD; fen[p].add(a, val); } // Start a = i - 2 first becomes usable for a token of length 2. int aNew = i - 2; if (aNew >= 0 && expire[aNew] >= i) { int p = aNew & 1; fen[p].add(aNew, dp[aNew]); if (expire[aNew] + 1 <= N) { removals[expire[aNew] + 1].push_back(aNew); } } if (s[i - 1] == 'R') { lastR = i - 1; } // Blue half must contain no R. int lower = 2 * lastR - i + 1; if (lower < 0) lower = 0; int upper = i - 2; int tokenWays = 0; if (lower <= upper) { int p = i & 1; tokenWays = fen[p].rangeSum(lower, upper); } int ways = tokenWays; // Last atom is a single white point. if (s[i - 1] == 'X') { ways += dp[i - 1]; if (ways >= MOD) ways -= MOD; } dp[i] = ways; } cout << dp[N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; constexpr int MOD = 1'000'000'007; struct Fenwick { int n; int total = 0; vector<int> bit; explicit Fenwick(int size) : n(size), bit(size + 1, 0) {} void add(int index, int value) { if (value == 0) return; total += value; if (total >= MOD) total -= MOD; for (int i = index; i <= n; i += i & -i) { bit[i] += value; if (bit[i] >= MOD) bit[i] -= MOD; } } int prefixSum(int index) const { int result = 0; for (int i = index; i > 0; i -= i & -i) { result += bit[i]; if (result >= MOD) result -= MOD; } return result; } int suffixSum(int firstIndex) const { if (firstIndex > n) return 0; int result = total - prefixSum(firstIndex - 1); if (result < 0) result += MOD; return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string s; cin >> N >> s; s.insert(s.begin(), ' '); // 1-indexed /* For each prefix boundary j, let b_j be the first fixed B strictly after j. A block starting at j+1 can end at t only while t < 2*b_j - j. expiration[j] is therefore the first endpoint where j becomes invalid. Expirations are stored in linked-list buckets. */ vector<int> head(N + 2, -1); vector<int> nextInBucket(N + 1, -1); int nextB = N + 1; for (int j = N; j >= 0; --j) { // nextB is the first fixed B strictly after j. int expiration = 2 * nextB - j; if (expiration <= N) { nextInBucket[j] = head[expiration]; head[expiration] = j; } // Position j becomes available for boundaries strictly before j. if (j >= 1 && s[j] == 'B') nextB = j; } /* Separate trees for even and odd prefix boundaries. Boundary j is stored at coordinate j/2 + 1 in its parity's tree. */ Fenwick trees[2] = { Fenwick(N / 2 + 1), Fenwick((N + 1) / 2) }; vector<int> dp(N + 1, 0); dp[0] = 1; trees[0].add(1, dp[0]); int lastR = 0; for (int t = 1; t <= N; ++t) { // These boundaries have just become invalid. for (int j = head[t]; j != -1; j = nextInBucket[j]) { if (dp[j] != 0) { trees[j & 1].add(j / 2 + 1, MOD - dp[j]); } } if (s[t] == 'R') lastR = t; /* A block covering positions j+1 ... t is valid only if j >= 2*lastR - t. Also j and t must have the same parity. */ int lower = 2 * lastR - t; if (lower < 0) lower = 0; int parity = t & 1; if ((lower & 1) != parity) ++lower; int firstCoordinate = lower / 2 + 1; int ways = trees[parity].suffixSum(firstCoordinate); // Position t can instead remain white. if (s[t] == 'X') { ways += dp[t - 1]; if (ways >= MOD) ways -= MOD; } dp[t] = ways; // This boundary may be used by future blocks. trees[parity].add(t / 2 + 1, dp[t]); } cout << dp[N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; static constexpr int MOD = 1'000'000'007; struct Fenwick { int n; vector<int> bit; explicit Fenwick(int n) : n(n), bit(n + 1, 0) {} void add(int idx, int value) { if (value < 0) value += MOD; if (value >= MOD) value -= MOD; for (; idx <= n; idx += idx & -idx) { bit[idx] += value; if (bit[idx] >= MOD) bit[idx] -= MOD; } } int prefixSum(int idx) const { int result = 0; for (; idx > 0; idx -= idx & -idx) { result += bit[idx]; if (result >= MOD) result -= MOD; } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string s; cin >> N >> s; s = " " + s; // 1-indexed // nextB[k] is the first fixed B strictly after position k. vector<int> nextB(N + 1, N + 1); int nearest = N + 1; for (int k = N; k >= 0; --k) { nextB[k] = nearest; if (k >= 1 && s[k] == 'B') nearest = k; } /* A block beginning at k+1 and ending at i has midpoint (k+i)/2. If its first fixed B is b, it remains valid with respect to B's until i reaches expiry[k] = 2*b-k. */ vector<int> expiry(N + 1); vector<int> head(N + 1, -1), link(N + 1, -1); for (int k = 0; k <= N; ++k) { int b = nextB[k]; expiry[k] = 2 * b - k; if (k + 2 <= N && expiry[k] <= N) { link[k] = head[expiry[k]]; head[expiry[k]] = k; } } // Separate Fenwick trees for even and odd prefix lengths. int fenwickSize = N / 2 + 2; Fenwick fen[2] = {Fenwick(fenwickSize), Fenwick(fenwickSize)}; int total[2] = {0, 0}; vector<unsigned char> active(N + 1, false); vector<int> dp(N + 1, 0); dp[0] = 1; int lastR = 0; for (int i = 1; i <= N; ++i) { if (s[i] == 'R') lastR = i; // Prefixes whose final block has just become invalid due to a B. for (int k = head[i]; k != -1; k = link[k]) { if (!active[k]) continue; int parity = k & 1; fen[parity].add(k / 2 + 1, MOD - dp[k]); total[parity] -= dp[k]; if (total[parity] < 0) total[parity] += MOD; active[k] = false; } // Prefix k = i-2 can now be the beginning of a block ending at i. if (i >= 2) { int k = i - 2; if (expiry[k] > i) { int parity = k & 1; fen[parity].add(k / 2 + 1, dp[k]); total[parity] += dp[k]; if (total[parity] >= MOD) total[parity] -= MOD; active[k] = true; } } // Leave position i white. int answer = (s[i] == 'X' ? dp[i - 1] : 0); // End a red-blue block at position i. if (i >= 2) { int parity = i & 1; // The blue half contains no fixed R iff k >= 2*lastR-i. int lower = 2 * lastR - i; int minimumK; if (lower <= 0) { minimumK = parity; } else { minimumK = lower; if ((minimumK & 1) != parity) ++minimumK; } int idx = minimumK / 2 + 1; int contribution = total[parity] - fen[parity].prefixSum(idx - 1); if (contribution < 0) contribution += MOD; answer += contribution; if (answer >= MOD) answer -= MOD; } dp[i] = answer; } cout << dp[N] << '\n'; return 0; }
// YOUR CODE HERE #include <bits/stdc++.h> using namespace std; static constexpr int MOD = 1'000'000'007; class Fenwick { int n; vector<int> tree; public: explicit Fenwick(int n) : n(n), tree(n + 1, 0) {} void add(int index, int delta) { for (int p = index + 1; p <= n; p += p & -p) { tree[p] += delta; if (tree[p] >= MOD) tree[p] -= MOD; } } // Sum over indices [0, index]. int prefix(int index) const { if (index < 0) return 0; index = min(index, n - 1); int result = 0; for (int p = index + 1; p > 0; p -= p & -p) { result += tree[p]; if (result >= MOD) result -= MOD; } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; string s; cin >> N >> s; // nextB[j] = first position strictly after j that is forced to be B. vector<int> nextB(N + 1); int nearestB = N + 1; for (int j = N; j >= 0; --j) { nextB[j] = nearestB; if (j > 0 && s[j - 1] == 'B') { nearestB = j; } } // A block [j+1, i] is red-compatible exactly when i <= limit[j]. vector<int> limit(N + 1); vector<int> expireHead(N + 2, -1); vector<int> expireNext(N + 1, -1); for (int j = 0; j + 2 <= N; ++j) { limit[j] = 2 * nextB[j] - j - 2; // The source j becomes active at j+2 and expires after limit[j]. if (limit[j] >= j + 2 && limit[j] + 1 <= N) { int when = limit[j] + 1; expireNext[j] = expireHead[when]; expireHead[when] = j; } } // Separate Fenwick trees for even and odd source indices j. Fenwick bit[2] = {Fenwick(N + 1), Fenwick(N + 1)}; int total[2] = {0, 0}; vector<int> dp(N + 1, 0); dp[0] = 1; int lastR = 0; for (int i = 1; i <= N; ++i) { // Remove sources whose red halves have become incompatible. for (int j = expireHead[i]; j != -1; j = expireNext[j]) { if (dp[j] != 0) { int parity = j & 1; bit[parity].add(j, MOD - dp[j]); total[parity] -= dp[j]; if (total[parity] < 0) total[parity] += MOD; } } // The source j = i-2 can now form a block ending at i. int j = i - 2; if (j >= 0 && limit[j] >= i && dp[j] != 0) { int parity = j & 1; bit[parity].add(j, dp[j]); total[parity] += dp[j]; if (total[parity] >= MOD) total[parity] -= MOD; } if (s[i - 1] == 'R') { lastR = i; } // The blue half is compatible iff j >= 2*lastR - i. int lowerBound = 2 * lastR - i; int parity = i & 1; int blockWays = total[parity]; if (lowerBound > 0) { blockWays -= bit[parity].prefix(lowerBound - 1); if (blockWays < 0) blockWays += MOD; } dp[i] = blockWays; // Alternatively, leave position i white. if (s[i - 1] == 'X') { dp[i] += dp[i - 1]; if (dp[i] >= MOD) dp[i] -= MOD; } } cout << dp[N] << '\n'; return 0; }