Sort by

recency

|

3 Discussions

|

  • + 1 comment

    Solution in Python 3

    import sys
    
    P = 10**9 + 7
    
    fact = [1]
    for i in range(1, 200001):
    	fact.append(fact[-1] * i % P)
    
    inv = [0, 1]
    for i in range(2, 200001):
    	inv.append(P - P // i * inv[P % i] % P)
    inv_fact = [1]
    for i in range(1, 100001):
    	inv_fact.append(inv_fact[-1] * inv[i] % P)
    
    def ceil_div(a, b):
    	return -(-a // b)
    
    def part(n, k):
    	# print(n, k)
    	if k == 0:
    		return 1 if n == 0 else 0
    	return C(n + k - 1, n)
    
    def C(n, k):
    	return fact[n] * inv_fact[k] % P * inv_fact[n - k] % P if (n >= 0) and (k >= 0) and (k <= n) else 0
    
    def g(sum, n, b):
    	# print(sum, n, b)
    	ret = 0
    	for i in range(sum // b + 1):
    		sign = 1 if i & 1 == 0 else -1
    		ret += sign * part(sum - i * b, n) * C(n, i) % P
    		ret %= P
    	# print (ret)
    	return ret
    
    def f(n, k):
    	ret = 0
    	for max in range(ceil_div(n + k - 1, k), n + 1):
    		ret += g(n - max, k - 1, max)
    		ret %= P
    	return ret * k % P
    
    # print(g(6, 2, 4))
    
    t = int(input())
    
    for _ in range(t):
    	n, low, high = map(int, input().split())
    	print(f(n, high - low + 1))
    
  • + 0 comments

    Important: The following prcedure gives correct result for the testcase 0, testcase 1, and testcase 9.

    Procedure: Let m=hi-lo+1.

    My procedure is as follows:

    First Step: Calculate the number of possible solutions of the problem

    x1+x2+...+xk=n such that 1<= x1,x2,...,xk <= t

    where t varies from ceil(n/k) to n-k+1.

    Second Step: Multiply this number with C(n,k).

    Third step:

    varies k from (1, min(n-1,m))

    The code

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import math
    
    modnu=10**9+7
    def nchoosek(n,k):
        # calculating n choose k
        global modnu
        if n < k:
            return 0
        elif k==0:
            return 1
        else:
            t=1
            u=max(k,n-k)
            for i in range(1,min(k,n-k)+1):
                t=(t*(u+i)/i)%modnu
        return t
    
    
    
    def foxsequence(n,m):   
        global modnu
        fox_se=m
        for k in range(2,min(n-1,m)+1):
            t=0
            for l in range(int(math.ceil(n/float(k))),n-k+2):            
                for r in range(k):
                    if n-l*(r+1)+r-1>= k-2:                    
                        t=t+((-1)**(r%2))*nchoosek(k-1,r)*nchoosek(n-l*(r+1)+r-1,k-2)
                    else:
                        break 
    
            fox_se=(fox_se+(t*k*nchoosek(m,k)))%modnu
    
        return fox_se
    
    n=input()
    
    
    for i in range(n):
    
        t=map(int, raw_input().split())    
        print foxsequence(t[0],t[2]-t[1]+1)
    
  • + 1 comment

    The answer it seems should be there are 5 possible fox sequences not 4. What am I missing here? 4 4 4 4 *4 4 4 5* 4 4 5 5 *4 5 5 5* 5 5 5 5

No more comments