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.
# 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)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Div and Span
You are viewing a single comment's thread. Return to all comments →
Python3 solution