If n can be as large as 1M and r something like 100, then what is the most efficient way to calculate nPr.
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- Generate a random, non-repeating subset of possibl
- How to get a fixed number of evenly spaced points
相关文章
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- How to measure complexity of a string?
- Select unique/deduplication in SSE/AVX
- How to smooth the blocks of a 3D voxel world?
We easily can remove (n-r)! from nominator and denominator. Now formula is
.
.
.
Old answer, not for this question, about combinations:
We easily can remove (n-k)! from nominator and denominator. Now formula is
If we first calculate full nominator, it will very big.
But we can alternate steps - take n, then divide by the first term of denominator (1), multiplication by (n-1) - division by the second denominator term (2) and so on.
Note that partial nominator product always i divisible by partial denominator product of the same length.
With this approach intermediate result is not very big, and calculations with big numbers (long arithmetics) will be faster.