Sort by

recency

|

4 Discussions

|

  • + 0 comments

    The solution can be found much more quickly than the editorial says due to combinatorial simplification; there is a two-line solution (that is, two lines in the query loop, one of them being the input line) that runs in O(X + Y + T) time. No Y^3 needed. A hint: https://oeis.org/A033184 I think the problem setter didn't check for an accidental identity.

  • + 0 comments

    Python3 solution

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    m = 10 ** 9 + 7
    # parantheses
    p_lim = 201
    # dpp[i][j] is # of paranth. configurations of length i, consisting of j elements
    dpp = [[0 for _ in range(p_lim)] for _ in range(p_lim)]
    dpp[0][0] = 1
    cumsum = [0 for _ in range(p_lim)]
    cumsum[0] = 1
    fact = [1]
    for i in range(1, 250000):
        fact.append((fact[-1] * i) % m)
    
    def egcd(a, b):
        '''
        Extended Euclidian algorithm
        '''
        if a == 0:
            return (b, 0, 1)
        else:
            g, y, x = egcd(b % a, a)
            return (g, x - (b // a) * y, y)
    
    def modinv(a, m):
        '''
        Finds modular inverse modulo m
        '''  
        while a < 0:
            a += m
        g, x, y = egcd(a, m)
        if g != 1:
            raise Exception('Modular inverse does not exist')
        else:
            return x % m
    
    def choose(n, k):
        '''
        Computes binomial efficiently
        '''
        r = (fact[n] * modinv(fact[k], m)) % m
        return (r * modinv(fact[n - k], m)) % m
        
    def C(n): # catalan number
        return (fact[2 * n] * modinv(fact[n + 1], m)) % m
    
    for i in range(1, p_lim):
        dpp[i][1] = cumsum[i - 1] % m
        cumsum[i] = (cumsum[i] + dpp[i][1]) % m
        for j in range(2, p_lim):
            for k in range(1, i - j + 2):
                dpp[i][j] = (dpp[i][j] + dpp[k][1] * dpp[i - k][j - 1]) % m   
            cumsum[i] = (cumsum[i] + dpp[i][j]) % m
    for _ in range(int(input())):
        x, y = map(int,input().split())
        r = 0
        for i in range(y + 1):
            r += dpp[y][i] * choose(2 * x + i, i)
            r %= m
        r = (r * fact[y]) % m
        r = (r * C(x)) % m
        print(r)
    
  • + 0 comments

    if there was no restriction on the arrangement of brackets what would be the ans??? can anyone help

  • + 0 comments

    Hint: there's a simple recursion relation to determine the number of ways of arranging Y pairs of non-distinguishable curly parentheses, with the number of square parentheses X=0. Let's define "blocks" as top-level parantheses and their contents, for example, for the configuration (()) () (), there are three blocks, the first being (()), and two more ().

    Let C(Y) be the number of arrangements of Y pairs of curly parentheses. Clearly, we have C(0)=1, e.g. just the empty arrangement, and C(1)=1, e.g. (). Next, we have C(2)=2, e.g. (()) and () ().

    Now let's consider arbitrary Y. The first block can contain Z pairs inside (not counting the top-level pair of the block itself), where Z can be 0,1,2...Y-1. Those Z can have C(Z) arrangements. Once we place Z pairs inside the first block, we will have Y-Z-1 pairs remaining, and those can have C(Y-Z-1) arrangements. Hence, we have

    C(Y) = Prod C(Z) C(Y-Z-1);  Z=0,1,2...Y-1
    

    This is exactly the recursion relation for the well-known Catalan numbers.

    Also, note that we need the Catalan number for X also, since the square brackets also must be arranged. Since X goes up to 10^5, the recursion above is not fast enough to compute. There is another form for Catalan number involving factorial that can be computed in O(X) time instead of O(X^2).

    Some more hints: we also need to compute D[i][j], the number of i-pair parentheses arrangements that consist of exactly j blocks. The recursion relation for this is slightly tricky to derive, but we need only fill out a matrix with O(Y^2) elements.

    Finally, note that the number of cases can go up to T~1000, but the computations above need only be done once, and once you have the Catalan numbers, factorials, and matrix elements of D computed, some further simple lookups is as that's needed to get all the answers.

No more comments