Number of combinations (N choose R) in C++

后端 未结 7 2436
日久生厌
日久生厌 2020-11-27 18:17

Here I try to write a program in C++ to find NCR. But I\'ve got a problem in the result. It is not correct. Can you help me find what the mistake is in the program?

7条回答
  •  盖世英雄少女心
    2020-11-27 18:37

    The definition of N choose R is to compute the two products and divide one with the other,

    (N * N-1 * N-2 * ... * N-R+1) / (1 * 2 * 3 * ... * R)

    However, the multiplications may become too large really quick and overflow existing data type. The implementation trick is to reorder the multiplication and divisions as,

    (N)/1 * (N-1)/2 * (N-2)/3 * ... * (N-R+1)/R

    It's guaranteed that at each step the results is divisible (for n continuous numbers, one of them must be divisible by n, so is the product of these numbers).

    For example, for N choose 3, at least one of the N, N-1, N-2 will be a multiple of 3, and for N choose 4, at least one of N, N-1, N-2, N-3 will be a multiple of 4.

    C++ code given below.

    int NCR(int n, int r)
    {
        if (r == 0) return 1;
    
        /*
         Extra computation saving for large R,
         using property:
         N choose R = N choose (N-R)
        */
        if (r > n / 2) return NCR(n, n - r); 
    
        long res = 1; 
    
        for (int k = 1; k <= r; ++k)
        {
            res *= n - k + 1;
            res /= k;
        }
    
        return res;
    }
    

提交回复
热议问题