이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int N = 1e6 + 3e2;
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Vector dimension must be greater than zero!");
template<typename... Args>
Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
}
};
template<typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {
}
};
void solve(){
int n, MOD; string s;
cin >> n >> MOD >> s;
Vec<4, int> dp(2, 3, 5, 5);
auto ndp = dp;
function<int(int, int, int, int)> calc = [&](int small, int mx, int mn, int bal){
mx = max(mx, bal);
mn = min(mn, bal);
if (mx > 2 || bal < -2 || bal > 2 || mn < -2) return 0;
return dp[small][mx][2 + mn][2 + bal];
};
for (int i = n; i >= 1; i--){
for (int small = 0; small <= 1; small++){
for (int mx = 0; mx <= 2; mx++){
for (int mn = -2; mn <= 0; mn++){
for (int bal = mn; bal <= mx; bal++){
int &res = ndp[small][mx][2 + mn][2 + bal];
res = 0;
if (mx - mn > 2) continue;
if (i == n){
res = 1;
continue;
}
if (small){
res += calc(1, mx, mn, bal - 1);
res += calc(1, mx, mn, bal + 1);
} else {
if (s[i] == 'L'){
res += calc(0, mx, mn, bal - 1);
} else {
res += calc(1, mx, mn, bal - 1);
res += calc(0, mx, mn, bal + 1);
}
}
res %= MOD;
}
}
}
}
swap(dp, ndp);
}
int res = 0;
if (s[0] == 'L'){
res += calc(0, 0, -1, -1);
} else {
res += calc(1, 0, -1, -1);
res += calc(0, 1, 0, 1);
}
res %= MOD;
cout << res << endl;
}
int main(){
clock_t startTime = clock();
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int test = 1; test <= test_cases; test++){
// cout << (solve() ? "YES" : "NO") << endl;
solve();
}
#ifdef LOCAL
cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
linear_garden.cpp: In function 'int main()':
linear_garden.cpp:98:13: warning: unused variable 'startTime' [-Wunused-variable]
98 | clock_t startTime = clock();
| ^~~~~~~~~
# | 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... |