이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <array>
#include <set>
#include <string>
using namespace std;
using ll = long long;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
ll m;
string s;
cin >> n >> m >> s;
ll res = 1;
array<ll, 1000001> powers;
powers[0] = 1;
for (ll i = 1; i <= 1e6; ++i) powers[i] = (powers[i - 1] * 2) % m;
int pref = 0, maxP = 0, minP = 0;
for (int i = 0; i < n; ++i)
{
if (s[i] == 'P')
{
--pref;
int nmax = max(maxP, pref), nmin = min(minP, pref);
if (nmax - nmin <= 2)
{
if (nmax - nmin == 2) res += (pref != nmax && pref != nmin ? powers[(n - i) / 2] : powers[(n - i - 1) / 2]);
else
{
res += powers[(n - i) / 2] + powers[(n - i - 1) / 2] - 1;
if (res >= m) res -= m;
}
if (res >= m) res -= m;
}
++pref;
}
pref += (s[i] == 'P' ? 1 : -1);
maxP = max(maxP, pref);
minP = max(minP, pref);
}
cout << res;
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... |