为什么大量nCr给出错误答案

时间:2020-04-29 18:45:09

标签: algorithm combinations dynamic-programming

我正在尝试解决一个问题 https://practice.geeksforgeeks.org/problems/nth-catalan-number/0 通过使用其中k = n -r

的方法
for(int i=0; i<k; i++){
                        res = res * (n - i);
                        res = (res/(i+1))%1000000007;
                    }
for n and r
but it get failed in the input n=778  and r = 116
but using the dynamic programming (by using two dimensional array) method it give the right answer
what are the reason of failure of first approach and success of second (dynamic programming) approach

1 个答案:

答案 0 :(得分:0)

(a / b) mod p = ((a mod p) * (b^(-1) mod p)) mod p

您应该使用模块化除法。

看看这个问题Link

相关问题