I have to find the value nCr%m where 0 < r < 10^6 and 0 < n < 10^18 and m is a prime (for sake of convenience 10^9+7)
I have read various articles where we can find nCr%m in O(1) time if n<10^6 by pre-calculating the factorial and multiplicative inverse from 1 to n.
My question:-
Now is there any solution to find nCr%m in O(1) time or better than O(r) (if n<10^18) (above trick fails as we cannot store the factorial n is too large for array size).
My solution:-
I have found O(r) solution by finding
numerator-->n*(n-1)(n-2)...(n-r)
denominator-->1*2*3*4*5......r
ans=numerator*multiplicative_inverse(denominator)%mod