이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
#define pii pair<int, int>
#define F first
#define S second
#define PB push_back
#define ALL(x) x.begin(), x.end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define NYOOM ios::sync_with_stdio(0); cin.tie(0);
#define endl '\n'
const int INF = 1e9 + 7;
const ll LLINF = 1ll<<60;
const int maxn = 1e6 + 10;
int n, m, dp[3][3][maxn];
string s;
void tabu(){
FOR(mn, 3){
FOR(mx, 3){
dp[mn][mx][0] = 1;
}
}
for (int rem = 1; rem <= n; rem++){
for (int mn = -2; mn <= 0; mn++){
for (int mx = 0; mx <= 2; mx++){
int go_l = 0, go_p = 0;
if (mx + 1 < 3) go_l = dp[abs(min(mn + 1, 0))][mx + 1][rem - 1];
if (mn - 1 > -3) go_p = dp[abs(mn - 1)][max(0, mx - 1)][rem - 1];
dp[abs(mn)][mx][rem] = (go_l + go_p) % m;
}
}
}
}
int cnt(int mn, int mx, int rem){ // mn = min(l - p), mx = max(l - p)
if (mn <= -3 || mx >= 3) return 0;
if (rem <= 0) return 1;
if (dp[-mn][mx][rem] != -1) return dp[-mn][mx][rem];
int go_l = cnt(min(mn + 1, 0), mx + 1, rem - 1);
int go_p = cnt(mn - 1, max(0, mx - 1), rem - 1);
dp[-mn][mx][rem] = (go_l + go_p) % m;
return dp[-mn][mx][rem];
}
signed main(){
NYOOM;
fill(&dp[0][0][0], &dp[0][0][0] + 3 * 3 * maxn, -1);
cin >> n >> m;
cin >> s;
tabu();
int re = 1, mn = 0, mx = 0;
FOR(i, n){
if (s[i] == 'L'){
mn = min(0, mn + 1), mx = mx + 1;
continue;
}
// cur is p
int rem = n - i - 1;
// int go_l = cnt(min(mn + 1, 0), mx + 1, rem);
int go_l = 0;
if (mx + 1 < 3) go_l = dp[min(mn + 1, 0)][mx + 1][rem];
mn = mn - 1, mx = max(0, mx - 1);
re = (re + go_l) % m;
}
cout << re;
}
# | 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... |