fork download
  1. // template : rjohari23
  2.  
  3. #include<iostream>
  4. #include<cstdio>
  5. #include<cmath>
  6. #include<cstring>
  7. #include<stdlib.h>
  8. #include<algorithm>
  9. #include<vector>
  10. #define getcx getchar_unlocked
  11. #define pc(x) putchar_unlocked(x);
  12. #ifndef ONLINE_JUDGE
  13. #define getcx getchar
  14. #endif
  15. using namespace std;
  16. #define clear(a) memset((a),0,sizeof(a))
  17. #define pb push_back
  18. #define SIZE(v) v.size()
  19. #define ull unsigned long long int
  20. #define lli long long int
  21. #define li long int
  22. #define ii int
  23. //#define mod 1000000007
  24.  
  25. inline int inp()
  26. {
  27. int n=0;
  28. int ch=getcx();int sign=1;
  29. while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getcx();}
  30.  
  31. while( ch >= '0' && ch <= '9' )
  32. n = (n<<3)+(n<<1) + ch-'0', ch=getcx();
  33. return n*sign;
  34. }
  35.  
  36. inline long long in()
  37. {
  38. long long n=0;
  39. long long ch=getcx();long long sign=1;
  40. while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getcx();}
  41.  
  42. while( ch >= '0' && ch <= '9' )
  43. n = (n<<3)+(n<<1) + ch-'0', ch=getcx();
  44. return n*sign;
  45. }
  46.  
  47. inline void write (lli n)
  48. {
  49. lli N = n, rev, count = 0;
  50. rev = N;
  51. if (N == 0) { pc('0');return ;}
  52. while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s
  53. rev = 0;
  54. while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;} //store reverse of N in rev
  55. while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;}
  56. while (count--) pc('0');
  57. }
  58.  
  59. /* http://d...content-available-to-author-only...f.com/questions/20106/fast-string-input */
  60. /* fast string input using getchar_unlocked */
  61. inline string ip()
  62. {
  63. string s = "";
  64. int temp=getchar_unlocked();
  65. while ( temp<'A'||temp>'Z' )
  66. temp = getchar_unlocked();
  67. while ( temp>='A'&&temp<='Z' )
  68. {
  69. s += (char)temp;
  70. temp = getchar_unlocked();
  71. }
  72. return s;
  73. }
  74.  
  75. #define lim 1000001
  76.  
  77. using namespace std;
  78. ii N,mod;
  79. int A[lim][4][4];
  80.  
  81. void solve()
  82. {
  83. int i,mx,mn;
  84.  
  85. for ( i=N;i>=0;--i )
  86. {
  87. for( mx=0;mx<=2;++mx )
  88. {
  89. for ( mn=0;mn<=2;++mn )
  90. {
  91. if ( i==N )
  92. {
  93. A[i][mx][mn] = 1;
  94. continue;
  95. }
  96. A[i][mx][mn] = A[i+1][mx+1][max(0,mn-1)]+A[i+1][max(0,mx-1)][mn+1];
  97. A[i][mx][mn] -= A[i][mx][mn] >= mod ? mod : 0;
  98. }
  99. }
  100. }
  101. }
  102.  
  103. string s;
  104.  
  105. inline ii query()
  106. {
  107. int idx,i,mx,mn;
  108.  
  109. mx = 0, mn = 0;
  110. idx = 0;
  111. for ( i=0;i<N;++i )
  112. {
  113. if ( s[i]=='L' )
  114. {
  115. mx = max(0,mx-1);
  116. mn++;
  117. continue;
  118. }
  119. idx += A[i+1][max(0,mx-1)][mn+1];
  120. idx -= idx >= mod ? mod : 0;
  121.  
  122. mx += 1;
  123. mn = max(0,mn-1);
  124. }
  125.  
  126. idx += 1;
  127. idx -= idx >= mod ? mod : 0;
  128.  
  129. return idx;
  130. }
  131.  
  132. int main()
  133. {
  134.  
  135. N = inp(); mod = inp();
  136.  
  137. s = ip();
  138.  
  139. solve();
  140. write(query());
  141.  
  142. return 0;
  143. }
Success #stdin #stdout 0s 65328KB
stdin
5
4
LPPLP
stdout
3