This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*#pragma GCC optimize("O3")*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#define ordered_set tree<int, null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update>
#define eps 1e-9
#define MOD1 998244353
#define MOD2 1000000007
#define INV_10 299473306
#define INF 1000000000
#define PI 3.14159265358979323846
using namespace std;
int dp[2][9][2];
bitset<1000001>a;
int n, m, pp, ll;
int solve()
{
dp[0][0][0]=1;
for(int idx = 0; idx < n; idx++)
{
for(int d = 0; d < 9; d++)
{
for(int b = 0; b < 2; b++)
{
if(a.test(idx))
{
if(d/3 < 2)
{
pp=d/3;
ll=d%3;
pp++;
ll--;
ll=max(ll, 0);
dp[(idx+1)%2][pp*3+ll][b]+=dp[idx%2][d][b];
dp[(idx+1)%2][pp*3+ll][b]%=m;
}
if(d%3 < 2)
{
pp=d/3;
ll=d%3;
pp++;
ll--;
ll=max(ll, 0);
dp[(idx+1)%2][pp*3+ll][1]+=dp[idx%2][d][b];
dp[(idx+1)%2][pp*3+ll][1]%=m;
}
}
else
{
if(d%3 < 2)
{
pp=d/3;
ll=d%3;
pp++;
ll--;
ll=max(ll, 0);
dp[(idx+1)%2][pp*3+ll][b]+=dp[idx%2][d][b];
dp[(idx+1)%2][pp*3+ll][b]%=m;
}
if(d/3 < 2 && b)
{
pp=d/3;
ll=d%3;
pp++;
ll--;
ll=max(ll, 0);
dp[(idx+1)%2][pp*3+ll][b]+=dp[idx%2][d][b];
dp[(idx+1)%2][pp*3+ll][b]%=m;
}
}
}
}
}
int ans=0;
for(int i = 0; i < 9; i++)
for(int j = 0; j < 2; j++)
ans+=dp[n%2][i][j];
return ans;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
char c;
for(int i = 0; i < n; i++)
{
cin >> c;
if(c=='P')
a[i]=1;
}
for(int i = 0; i <= n; i++)
for(int k = 0; k < 9; k++)
for(int l = 0; l < 2; l++)
dp[i][k][l]=-1;
cout << solve() << '\n';
}
//size
# | 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... |