The war has been declared in the Clash of Clans and the clan's leader has designed a smart strategy to confirm his side's victory in this war.
His strategy is as follows -
An army will be represented as a string of Lava Hounds "L" and P.E.K.K.A. "P".
Example : If the army consists of N=5 troops, then there are 14 possible combinations for a balanced army ( condition number 2 ). The Lexicographical Order of this is - LLPLP, LLPPL, LPLLP, LPLPL, LPLPP, LPPLL, LPPLP, PLLPL, PLLPP, PLPLL, PLPLP, PLPPL, PPLLP and PPLPL.
All the possible balanced combinations of army of a certain length N can be ordered lexicographically with index starting from 1. Example for N=5, army at number 7 is LPPLP (see in the above example).
Since the leader is busy in donating the troops to his clan members therefore he needs your help in finding the number(index) assigned to an army for a given value of N(number of P.E.K.K.A. and Lava Hounds in army).
There can be large number of such possible combinations for a particular value of N therefore output the result modulo M.
Input 1: 5 4 LPPLPOutput 1: 3Input 2: 1 7 POutput 2: 2
Explanation
Example case 1.The actual position of LPPLP in the list of possible combinations is 7.
Therefore 7 modulo 4 is 3. (See above example for the combinations)
#include <cstdio> #include <algorithm> #include <iostream> using namespace std; char gar [1000001]; int pow2 [1000001]; int main (){ int n, m; scanf("%d%d", &n, &m); scanf("%s", gar); pow2[0] = 1 ; for ( int i = 1 ; i <= n; i++) pow2[i] = (2 * pow2[i - 1])% m; int ans = 0 , k = n; int nowdx = 0 , mindx = 0 , maxdx = 0 ; for ( int i = 0 ; gar[i]; i++) { k--; if (gar[i] == 'P' ) { int ndx = nowdx + 1 ; if (max(ndx, maxdx) - mindx == 1 ) ans = (ans + pow2[k / 2 ] + pow2[(k + 1 ) / 2 ] + m - 1 )% m; else if (max (ndx, maxdx) - mindx == 2 ) ans = (ans + pow2[(k + (mindx + 1 == ndx)) / 2 ])% m; nowdx--; mindx = min(mindx, nowdx); } else { nowdx ++; maxdx = max (maxdx, nowdx); } } printf("%d\n" , (ans + 1 )% m); return 0; }
#include <bits/stdc++.h> #include <ext/hash_map> #include <ext/hash_set> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/priority_queue.hpp> using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; #define XINF INT_MAX #define INF 0x3F3F3F3F #define MP(X,Y) make_pair(X,Y) #define PB(X) push_back(X) #define REP(X,N) for(int X=0;X<N;X++) #define REP2(X,L,R) for(int X=L;X<=R;X++) #define DEP(X,R,L) for(int X=R;X>=L;X--) #define CLR(A,X) memset(A,X,sizeof(A)) #define IT iterator #define RIT reverse_iterator typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> PII; typedef vector<PII> VII; typedef vector<int> VI; typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree_set; typedef tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree; #define PQ std::priority_queue #define HEAP __gnu_pbds::priority_queue #define X first #define Y second #define lson(X) ((X)<<1) #define rson(X) ((X)<<1|1) void MOD(int &a, int m) { if(a >= m) a-=m; } int pre[1000010][3][3]; int main() { ios::sync_with_stdio(false); int n,m; string s; cin>>n>>m>>s; int ans = 1; REP(a,3) { REP(b,3) { REP(i,n+1) REP(j,3) REP(k,3) pre[i][j][k] = 0; pre[0][a][b] = 1; REP(i,n) { REP(j,3) REP(k,3) { if(j<2) MOD(pre[i+1][j+1][max(k-1,0)] += pre[i][j][k], m); if(k<2) MOD(pre[i+1][max(j-1,0)][k+1] += pre[i][j][k], m); } } int mn=0, mx=0; REP(i,n) { if(s[i]=='L') { mn--; mx++; mn = max(mn, 0); }else{ int mn2 = max(mn-1, 0), mx2 = mx+1; if(mn2 == a && mx2 == b) { REP(j,3) REP(k,3) MOD(ans += pre[n-i-1][j][k], m); } mn++; mx--; mx = max(mx, 0); } } } } cout<<ans<<endl; return 0; }