fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long lld;
  4. const int MOD = 1e9 + 7;
  5. const int MN = 2005;
  6. int n, m;
  7. int D[MN][MN];
  8.  
  9. void add(int& a, int b)
  10. {
  11. a += b;
  12. if(a >= MOD) a -= MOD;
  13. }
  14.  
  15. int solve(int open, int len)
  16. {
  17. if(open > 2000) return 0;
  18. if(len == 0){
  19. if(open == 0){
  20. return 1;
  21. }else{
  22. return 0;
  23. }
  24. }
  25. if(open < 0) return 0;
  26. int& ret = D[open][len];
  27. if(ret != -1) return ret;
  28. ret = 0;
  29. add(ret, solve(open - 1, len - 1));
  30. add(ret, solve(open + 1, len - 1));
  31. return ret;
  32. }
  33.  
  34. int main()
  35. {
  36. ios_base::sync_with_stdio(false);
  37. cin.tie(0);
  38. cin >> n >> m;
  39. string s;
  40. cin >> s;
  41. if(n % 2){
  42. cout << 0 << '\n';
  43. return 0;
  44. }
  45. int r = 0;
  46. int cnt = 0;
  47. for(int i = 0; i < s.size(); ++i){
  48. if(s[i] == '(') ++cnt;
  49. else --cnt;
  50. r = max(r, -cnt);
  51. }
  52. memset(D, -1, sizeof(D));
  53. int ans = 0;
  54. for(int i = r; i <= n - m; ++i){
  55. for(int j = 0; j <= n - m; ++j){
  56. add(ans, (lld)solve(i, j) * solve(i + cnt, n - m - j) % MOD);
  57. }
  58. }
  59. cout << ans << '\n';
  60. }
  61.  
Success #stdin #stdout 0.01s 19160KB
stdin
4 1
(
stdout
4