# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
109452 | MetB | Linear Garden (IOI08_linear_garden) | C++14 | 0 ms | 0 KiB |
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 <cstdlib>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <queue>
#include <math.h>
#include <stack>
#include <vector>
#include <string.h>
#include <random>
typedef long long ll;
const ll MOD = 1e9 + 7, INF = 1e18;
using namespace std;
string s;
ll n, d[1000000][5][5][5], m;
void add_self (int& a, ll b)
{
a += b;
if (a >= m) a -= m;
}
int main ()
{
cin >> n >> m;
d[0][2][3][3] = 1;
for (ll i = 0; i < n; i++)
for (ll j = 0; j < 5; j++)
for (ll k = j; k < 5; k++)
if (k - j <= 2)
{
for (ll f = j; f <= k; f++)
{
if (max (k, f + 1) - j <= 2) add_self (d[i + 1][j][max (k, f + 1)][f + 1], d[i][j][k][f]);
if (k - min (j, f - 1) <= 2) add_self (d[i + 1][min (j, f - 1)][k][f - 1], d[i][j][k][f]);
}
}
cin >> s;
ll max_balance = 2, min_balance = 2, balance = 2, ans = 0;
for (ll i = 0; i < n; i++)
{
if (s[i] == 'P')
{
for (ll j = 0; j < 5; j++)
for (ll k = j; k < 5; k++)
if (max (max_balance, k + balance - 2) - min (min_balance, j + balance - 2) <= 2)
{
for (ll f = j; f <= k; f++)
add_self (ans, d[n - i - 1][j][k][f]);
}
balance--;
}
else balance++;
max_balance = max (max_balance, balance);
min_balance = min (min_balance, balance);
}
cout << (ans + 1) % m;
}