This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
ll pow(ll x, ll p, ll m) {
p %= m;
ll r = 1;
while (p) {
if (p & 1LL) r = (r * x) % m;
x = (x * x) % m;
p >>= 1LL;
}
return r;
}
ll rem(ll& x, ll m) {
x--;
if (x == -1LL) x += m;
return x;
}
int main() {
std::ios::sync_with_stdio(0); std::cin.tie(0);
ll n, m; std::cin >> n >> m;
std::string s; std::cin >> s;
ll l = 0, r = 0, now = 0, ans = 0;
for (ll i = 0; i < n; i++) {
if (s[i] == 'L') now++, r = std::max(r, now);
else {
ll rr = std::max(r, now + 1LL);
ll rn = n - i - 1LL;
if (!(rr - l - 1LL)) ans = (ans + pow(2LL, rn >> 1LL, m) + pow(2LL, (rn + 1LL) >> 1LL, m)) % m, rem(ans, m);
else if (!(rr - l - 2)) {
if (now + 1LL == rr - 1LL) ans = (ans + pow(2LL, (rn + 1LL) >> 1LL, m)) % m;
else ans = (ans + pow(2LL, rn >> 1LL, m)) % m;
}
now--, l = std::min(l, now);
}
}
std::cout << (ans + 1LL) % m << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |