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>
using namespace std;
const int N = 1000010;
const int INF = 1e9;
int n, m;
char str[N];
int dp[N][14];
bool vis[N][14];
inline int add(int a, int b) { return (a+b)%m; }
int pos(int mn, int mx, int bal)
{
if (mn == 0) {
if (mx == 0) return 0;
if (mx == 1) return 1+bal;
if (mx == 2) return 3+bal;
} else if (mn == -1) {
if (mx == 0) return 7+bal;
if (mx == 1) return 9+bal;
} else {
return 13+bal;
}
}
int solve1(int i, int mn, int mx, int bal)
{
if (mx-mn > 2) return 0;
if (i == n) return 1;
int j = pos(mn, mx, bal);
if (vis[i][j]) return dp[i][j];
return dp[i][j] = add(solve1(i+1, min(mn, bal-1), mx, bal-1), solve1(i+1, mn, max(mx, bal+1), bal+1));
}
int solve2(int i, int mn, int mx, int bal)
{
if (i == n) return 1;
if (str[i] == 'L')
return solve2(i+1, min(mn, bal-1), mx, bal-1);
else
return add(solve1(i+1, min(mn, bal-1), mx, bal-1), solve2(i+1, mn, max(mx, bal+1), bal+1));
}
int main()
{
scanf("%d%d %s", &n, &m, str);
printf("%d\n", solve2(0, 0, 0, 0));
return 0;
}
Compilation message (stderr)
linear_garden.cpp: In function 'int pos(int, int, int)':
linear_garden.cpp:25:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
linear_garden.cpp: In function 'int main()':
linear_garden.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d %s", &n, &m, str);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |