We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
# Enter your code here. Read input from STDIN. Print output to STDOUTm=10**9+7# paranthesesp_lim=201# dpp[i][j] is # of paranth. configurations of length i, consisting of j elementsdpp=[[0for_inrange(p_lim)]for_inrange(p_lim)]dpp[0][0]=1cumsum=[0for_inrange(p_lim)]cumsum[0]=1fact=[1]foriinrange(1,250000):fact.append((fact[-1]*i)%m)defegcd(a,b):'''ExtendedEuclidianalgorithm'''ifa==0:return(b,0,1)else:g,y,x=egcd(b%a,a)return(g,x-(b// a) * y, y)defmodinv(a,m):'''Findsmodularinversemodulom'''whilea<0:a+=mg,x,y=egcd(a,m)ifg!=1:raiseException('Modularinversedoesnotexist')else:returnx%mdefchoose(n,k):'''Computesbinomialefficiently'''r=(fact[n]*modinv(fact[k],m))%mreturn(r*modinv(fact[n-k],m))%mdefC(n):#catalannumberreturn(fact[2*n]*modinv(fact[n+1],m))%mforiinrange(1,p_lim):dpp[i][1]=cumsum[i-1]%mcumsum[i]=(cumsum[i]+dpp[i][1])%mforjinrange(2,p_lim):forkinrange(1,i-j+2):dpp[i][j]=(dpp[i][j]+dpp[k][1]*dpp[i-k][j-1])%mcumsum[i]=(cumsum[i]+dpp[i][j])%mfor_inrange(int(input())):x,y=map(int,input().split())r=0foriinrange(y+1):r+=dpp[y][i]*choose(2*x+i,i)r%=mr=(r*fact[y])%mr=(r*C(x))%mprint(r)
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)=ProdC(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
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
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.
Python3 solution
if there was no restriction on the arrangement of brackets what would be the ans??? can anyone help
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
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.