Sort by

recency

|

59 Discussions

|

  • + 0 comments

    Interesting combinatorics problem since every valid path forms the same word, the task reduces to counting paths in an m × n grid moving only RIGHT or DOWN, which is a classic binomial coefficient calculation; platforms like LemTask often frame such challenges to test mathematical insight over brute force. The total number of tracings is C(m+n-2, m-1) modulo 10^9+7, ensuring efficiency even for very large constraints.

  • + 0 comments

    MOD = 10**9 + 7

    def precompute(max_n): fact = [1] * (max_n + 1) inv_fact = [1] * (max_n + 1) for i in range(2, max_n + 1): fact[i] = fact[i-1] * i % MOD inv_fact[max_n] = pow(fact[max_n], MOD-2, MOD) for i in range(max_n-1, -1, -1): inv_fact[i] = inv_fact[i+1] * (i+1) % MOD return fact, inv_fact

    def nCr(n, r, fact, inv_fact): if r < 0 or r > n: return 0 return fact[n] * inv_fact[r] % MOD * inv_fact[n-r] % MOD

    def solve(): t = int(input().strip()) cases = [] max_mn = 0 for _ in range(t): m, n = map(int, input().split()) cases.append((m, n)) max_mn = max(max_mn, m + n)

    fact, inv_fact = precompute(max_mn)
    
    for m, n in cases:
        total = nCr(m + n - 2, m - 1, fact, inv_fact)
        print(total)
    

    if name == "main": solve()

  • + 0 comments

    This works. Hint: 10^9 +7 is prime

    ```
    def solve(n, m):
            a=min(n-1,m-1)
            b=max(n-1,m-1)
            x=1
            for i in range(1,a+1):
                    x=(x%(10**9+7))*((b+i)%(10**9+7))*pow(i,-1,10**9+7)
            return x%(10**9+7)
    

    `

  • + 0 comments

    This is a combinatorial problem number of ways to move from top-left to bottom-right using only right and down moves is given by C(m+n−2, m−1). So the total tracings = (m+n−2)! / ((m−1)! * (n−1)!). For large values, take result mod (10^9+7). For example, m=2, n=3 → C(3,1)=3.

  • + 0 comments

    import sys

    MAX_R = 2000001 MOD = 10**9 + 7 FACT = [1] * MAX_R

    def power(a, b): res = 1 a %= MOD while b > 0: if b % 2 == 1: res = (res * a) % MOD a = (a * a) % MOD b //= 2 return res

    def modInverse(n): return power(n, MOD - 2)

    def precompute_factorials(): for i in range(2, MAX_R): FACT[i] = (FACT[i-1] * i) % MOD

    def combinations(n, k): if k < 0 or k > n: return 0 if k == 0 or k == n: return 1 if k > n // 2: k = n - k

    numerator = FACT[n]
    denominator = (FACT[k] * FACT[n - k]) % MOD
    
    return (numerator * modInverse(denominator)) % MOD
    

    def solve_matrix_tracing(M, N): R = M + N - 2 K = M - 1

    if R < 0:
        return 0
    
    return combinations(R, K)
    

    if name == 'main': precompute_factorials()

    try:
        t_line = sys.stdin.readline()
        if not t_line:
            sys.exit(0)
    
        t = int(t_line.strip())
    
        for _ in range(t):
            try:
                line = sys.stdin.readline().rstrip().split()
                if not line:
                    continue
    
                M = int(line[0])
                N = int(line[1])
    
                result = solve_matrix_tracing(M, N)
    
                print(result)
    
            except EOFError:
                break
            except Exception as e:
                continue
    
    except Exception as e:
        pass