I have already tried the basic way to solve this series but it takes time for the larger values of n
& r
. Is there any way to reduce this expression in a single expression whose time complexity doesn't depend on the value of n
OR r
.Range r,n<=10^5
NOTE: here r < n
i.e. i have to find the sum of first r+1
terms of this series.
I have already read this question but it doesn't help me:
Algorithm to find Sum of the first r binomial coefficients for fixed n modulo m
AFAIK, there is no such expression to which it can be reduced. But it can be done in O(r) time complexity as follows.
Consider an array A, where A[i] stores nci. Then we can easily verify that A[i] = A[i-1].(n-i+1)/(i)
So
A[0] = 1;
for(int i=1;i<=r;i++){
A[i] = A[i-1].(n-i+1)/(i);
}
int ans = 0; //The required answer
for(int i=0;i<=r;i++){
ans = ans+A[i];
}