3

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

Community
  • 1
  • 1
Mayur Kharche
  • 717
  • 1
  • 8
  • 27
  • 1
    What do you mean by 'larger values of n & r'. In which range do you want to select n and r? – MrSmith42 Jun 08 '16 at 09:56
  • How do you calculate the values of nCr at the moment? – MrSmith42 Jun 08 '16 at 09:57
  • What are the limits on r and n? Can we assume that r is relatively small? – Ivaylo Strandjev Jun 08 '16 at 10:02
  • Is this modulus anything? Otherwise, for "larger values of `n` and `r`" you will overflow the integers. – Kittsil Jun 08 '16 at 10:11
  • there is no good formula for this series, but it can be easily computed modulus some prime value `M > N` in `O(r)` time. – Yerken Jun 08 '16 at 10:42
  • If the time complexity is not allowed to depend on n or r, then it must be a constant, implying that you could write out at most some fixed number of digits as the answer. But obviously the number of digits of (n choose r) grows without bound as n and r grow, so this is a contradiction: no O(1) algorithm is possible. – j_random_hacker Jun 08 '16 at 12:09
  • Am I missing anything or is it just `n * (C0 + C1 + ... + Cr)`? In this case, you can try to find solutions for simple array sum. There is no math formula to simplify it, I'm afrais, but you could try parallel solutions. – FreeNickname Jun 08 '16 at 12:31
  • 1
    Regarding "already tried the basic way ...": you should post the important parts of your code, and mention how much time it takes. This is in order to let people know what this "basic way" is, so they won't post answers suggesting it. Also good to know how much time for you is too much. – anatolyg Jun 08 '16 at 12:34
  • range of r,n <= 10^5 – Mayur Kharche Jun 08 '16 at 13:38

2 Answers2

1

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];
}
2rd_7
  • 462
  • 1
  • 3
  • 15
  • 1
    I'd just like to add that if `n/2 << r < n` then you can save some computational time by subtracting the sum from `r to n` and subtracting that from `2^n`, which is the sum of the _entire_ row –  Jun 08 '16 at 12:37
  • I have to calculate it with modulo m. I tried it but it results in wrong answer.This is my code in case you find any error `Arr[0] = 1; for(i=1;i<=max;i++){ Arr[i] = ((Arr[i-1]*(range-i+1))/i)%MOD; } for(i=0;i<=max;i++){ sum += Arr[i]; sum%=MOD; }` – Mayur Kharche Jun 09 '16 at 02:36
  • 1
    No, this doesn't work. It's true that `(a.b)%m = ((a%m).(b%m))%m` but there is no guarantee that `( a.(b/c) )%m = ((a%m).( (b/c) %m))%m`. As explained in [this](https://www.quora.com/I-need-to-find-nCr-mod-p-where-n-and-r-are-of-order-10-6-and-p-is-10-9-+-7-Can-anyone-help?no_redirect=1) link you can find ncrmodm really fast after storing `i! % m` for all `i<=n`. If you do not understand that solution leave a comment below, I would edit my answer and post the detailed solution. I assume `m is prime`. – 2rd_7 Jun 09 '16 at 05:09
1

For large N, the binomial coefficients behave like the Gaussian curve (at least for the centermost values). This can be derived from the Stirling formula and is supported by the Central Limit theorem.

Then the partial sum can be approximated by the Error function.