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 <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... |