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>
#define file(name) freopen(name".inp" , " r " , stdin);freopen(name".out" , " w " , stdout)
#define faster ios_base :: sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
#define dp(i , mi , ma) dp[i][mi + 2][ma]
using namespace std;
int n , m;
string s;
int dp[1000000][3][3];
void solve()
{
cin >> n >> m >> s;
for (int mi = -2; mi <= 0; ++mi)
for (int ma = 0; ma <= 2; ++ma)
dp(n + 1, mi, ma) = 1;
for (int i = n; i >= 1; --i) {
for (int mi = -2; mi <= 0; ++mi) {
for (int ma = 0; ma <= 2; ++ma) {
if (mi > -2) dp(i, mi, ma) += dp(i + 1, mi - 1, max(0, ma - 1));
if (ma < 2) dp(i, mi, ma) += dp(i + 1, min(0, mi + 1), ma + 1);
dp(i, mi, ma) %= m;
}
}
}
s = " " + s;
int ans = 1;
for (int i = 1 , mi = 0 , ma = 0 ; i <= n ; ++i)
if (s[i] == 'L')
{
mi--;
ma = max(0 , ma - 1);
}
else
{
if (mi > -2) ans = (ans + dp(i + 1 , mi - 1 , max(mi - 1 , 0))) % m;
ma++;
mi = min(0 , mi + 1);
}
cout << ans << endl;
}
int32_t main()
{
faster;
solve();
return 0;
}
# | 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... |