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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Program to find Nth term of the Van Eck's Sequence
- Program to print Collatz Sequence
- Find F(n) when F(i) and F(j) of a sequence are given
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Find Recurring Sequence in a Fraction
- Find sum in range L to R in given sequence of integers
- Find the Kth position element of the given sequence
- Find element position in given monotonic sequence
- Find if the given number is present in the infinite sequence or not
- Find the K-th Permutation Sequence of first N natural numbers
- Find nth term of the Dragon Curve Sequence
- Find a sequence of N prime numbers whose sum is a composite number
- Find the final sequence of the array after performing given operations
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Find Nth number in a sequence which is not a multiple of a given number
- Find the sequence number of a triangular number
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Program to find value of 1^k + 2^k + 3^k + ... + n^k
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.