Program to find sum of the given sequence | CPP program to find the sum of the given sequence ; function to find moudulo inverse under 10 ^ 9 + 7 ; Function to find the sum of the given sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const long long MOD = 1000000007 ; long long modInv ( long long x ) { long long n = MOD - 2 ; long long result = 1 ; while ( n ) { if ( n & 1 ) result = result * x % MOD ; x = x * x % MOD ; n = n / 2 ; } return result ; } long long getSum ( long long n , long long k ) { long long ans = 1 ; for ( long long i = n + 1 ; i > n - k ; i -- ) ans = ans * i % MOD ; ans = ans * modInv ( k + 1 ) % MOD ; return ans ; } int main ( ) { long long n = 3 , k = 2 ; cout << getSum ( n , k ) ; return 0 ; }