Given two numbers and . The task is to find the sum of the sequence given below.
(1*2*3*…*k) + (2*3*…*k*(k+1)) + (3*4*..*(k+1)*(k+2)) +…..+((n-k+1)*(n-k+2)*…*(n-k+k)).
Since the output can be large, print the answer under modulo 10^9+7.
Input : N = 3, K = 2 Output : 8 Input : N = 4, K = 2 Output : 20
Let us take the given example and try to reduce it to a general formula.
In the given example for n = 3 and k=2,
Sum = 1*2 + 2*3
We know that:
So each term is of the form:
If we multiply and divide by , it becomes
Which is nothing but,
But since n is so large we can not calculate it directly, we have to simplify the above expression.
On Simplifying we get,
Below is the implementation of the above idea:
$n – $k; $i–)
$ans = $ans * $i % $MOD;
$ans = $ans * modInv($k + 1) % $MOD;
// Driver code
$n = 3; $k = 2;
echo getSum($n, $k);
// This code is contributed
// by Akanksha Rai
- Program to print Collatz Sequence
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Find Recurring Sequence in a Fraction
- Find nth term of the Dragon Curve Sequence
- Find if the given number is present in the infinite sequence or not
- Find element position in given monotonic sequence
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Program to find HCF iteratively
- Program to Find the value of cos(nΘ)
- Program to find LCM of 2 numbers without using GCD
- Program to find LCM of two numbers
- Program to find GCD or HCF of two numbers
- Program to find greater value between a^n and b^n
- Program to find the value of sin(nΘ)
- Program to find Sum of the series 1*3 + 3*5 + ....
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.