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.
- Prepare
- Mathematics
- Fundamentals
- Matrix Tracing
- Discussions
Matrix Tracing
Matrix Tracing
Sort by
recency
|
52 Discussions
|
Please Login in order to post a comment
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.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)My python3 solution but time limit execution
As there involves many large numbers in each test case, so applying the formula C ( m+n-2, m-1) or C (m+n-2, n-1) directly will not work.
We have to use a bit of number theory here.
To find the factorial of a number, n!, instead from 1 to n, we also have to mod put the value at each step of multiplication so that the multiplication becomes fast.
According to fermat's little theorem, a ** ( p - 1 ) == 1 mod( p )
==> ( a ** ( p - 1 ) ) * ( a ** ( -1 )) == a ** ( -1 ) mod( p )
==> a ** ( p - 1 - 1 ) == a ** ( -1 ) mod( p )
==> a ** ( p - 2 ) == a ** ( -1 ) mod( p )
==> a ** ( -1 ) == a ** ( p - 2 ) mod( p )