AhmedAshraff

Untitled

Jun 17th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5. #define sz(s) (int)(s).size()
  6. #define all(s) s.begin(),s.end()
  7.  
  8. void Speed() {
  9.     ios_base::sync_with_stdio(false);
  10.     cin.tie(NULL);
  11. }
  12. const int mod = 1e9 + 7;
  13. vector<pair<int,int>> arr(40);
  14. int n;
  15. const  int N = 30 + 1;
  16. ll dp[N][N][N][N * N];
  17. ll solve(int i , int op , int cl , int sum){
  18.     if(i == n){
  19.         return sum == 0;
  20.     }
  21.  
  22.     if(sum < 0)
  23.         return 0;
  24.     ll &ret = dp[i][op][cl][sum];
  25.     if(~ret)
  26.         return ret;
  27.     ll ans = 0;
  28.  
  29.     ans = (ans + solve(i + 1, arr[i + 1].first , arr[i + 1].second , sum)) % mod;
  30.     if(op > 0)
  31.         ans = (ans + op * solve(i , op - 1 , cl , sum + 1) % mod) % mod;
  32.     if(cl > 0)
  33.         ans = (ans + cl * solve(i , op , cl - 1, sum - 1) % mod) % mod;
  34.     return ret = ans;
  35. }
  36.  
  37. void solve() {
  38.     memset(dp,-1,sizeof dp);
  39.     cin >> n;
  40.     for(int i = 0; i < n; i++){
  41.         string s;
  42.         cin >> s;
  43.         int op = 0 , cl = 0;
  44.         for(auto x : s)
  45.             if(x == '(')
  46.                 op++;
  47.             else cl++;
  48.         arr[i] = {op,cl};
  49.     }
  50.  
  51.     cout << solve(0 , arr[0].first , arr[0].second , 0) << "\n";
  52. }
  53.  
  54. int main() {
  55.     freopen("count.in", "r", stdin);
  56.     Speed();
  57.     int tc = 1;
  58.     //cin >> tc;
  59.     while (tc--) {
  60.         solve();
  61.     }
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment