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.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'gridWalking' function below.## The function is expected to return an INTEGER.# The function accepts following parameters:# 1. INTEGER m# 2. INTEGER_ARRAY x# 3. INTEGER_ARRAY D#M=10**9+7_dp_d=dict()_dp_result=dict()_dp_binomial_mod_M=[]def_hash(m,x,d):returnstr(m)+" "+str(x)+" "+str(d)def_dp_dimension(m,x,d):global_dp_dh=_hash(m,x,d)ifhin_dp_d:return_dp_d[h]ifm==1:_dp_d[h]=2ifx<dandx>1else1ifx<dorx>1else0return_dp_d[h]_dp_d[h]=0ifx>1:_dp_d[h]+=_dp_dimension(m-1,x-1,d)%Mifx<d:_dp_d[h]+=_dp_dimension(m-1,x+1,d)%Mreturn_dp_d[h]%Mdef_build_binomial_mod_M():global_dp_binomial_mod_M_dp_binomial_mod_M=[[1for_inrange(102)]for_inrange(102)]_dp_binomial_mod_M[100][99]=100# TBD build binomial mod M only if bnomial mod is neededforiinrange(101):_dp_binomial_mod_M[i][1]=iforiinrange(2,101):forjinrange(2,i):_dp_binomial_mod_M[i][j]*=_dp_binomial_mod_M[i-1][j]%M+_dp_binomial_mod_M[i-1][j-1]%M%Mdef_fact(n):global_dp_fact_mod_Mreturn_dp_fact_mod_M[n]def_binomial(x,k):global_dp_binomial_mod_Mreturn_dp_binomial_mod_M[x][k]def_next_walk(m,x,D):_count=0global_dp_dh=_hash(m,x,D)_build_binomial_mod_M()ifhin_dp_d:return_dp_d[h]_last=0print(20*"*",m,x,D)fori,dinenumerate(D):print(10*"*",d,10*"*")_count=0ifi==0:_last+=_dp_dimension(m,x[i],d)print(_last)continueforsinrange(m):_count+=_last*_binomial(m,s)*_dp_dimension(m-s,x[i],d)%M%Mprint(f"binomial {_binomial(m, s)} _count {_count} m-s {m-s} dimension {_dp_dimension(m-s, x[i], d)}")_last+=_countprint(f"m {m}, x[i], {x[i]}, d, {d},_dp_dimension {_dp_dimension(m, x[i], d)}")return_last%MdefgridWalking(m,x,D):return_next_walk(m,x,D)# Write your code hereif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')t=int(input().strip())_build_binomial_mod_M()fort_itrinrange(t):first_multiple_input=input().rstrip().split()n=int(first_multiple_input[0])m=int(first_multiple_input[1])x=list(map(int,input().rstrip().split()))D=list(map(int,input().rstrip().split()))result=gridWalking(m,x,D)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Grid Walking
You are viewing a single comment's thread. Return to all comments →
What is wrong with the solution?