Sort by

recency

|

55 Discussions

|

  • + 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
    
  • + 1 comment

    As others have been saying, the answer numerically should be (m+n-2) choose (m-1) (mod 10^9 + 7). Of course, the problem is the time limit, which you will see if you try this method. Here are the two insights that I needed to solve this:

    1. since the answer is mod p, make your own factorial function that takes mod p for every successive multiplication
    2. Use FLT and fast exponentiation mod p to divide by (m-1)! and (n-1)! instead of calculating them and then dividing
  • + 0 comments

    Dive into the neon grid, dodging digital projectiles like a Moto X3M rider evading obstacles. Matrix Tracing: a mind-bending game of reflexes and precision. Each pulse of light tests your skill, demanding split-second decisions.

  • + 0 comments

    For those interested, the technique needed to solve this exercises is close to the implementation of math.comb in the Python standard library (albeit using modulo 2⁶⁴ which comes for free if using unsigned integers). You can watch Raymond Hettinger's keynote "Numerical Marvels Inside Python" for more explanations.

  • + 0 comments

    TLE'd on this but i was thinking somewhere along the lines of the followng: nc = int(math.factorial(m)//(math.factorial(r)*math.factorial(m-r))) where r in range(n)