📜  程序查找给定序列的总和

📅  最后修改于: 2021-05-31 17:01:11             🧑  作者: Mango

给定两个数字N  K  。任务是找到以下给定序列的总和。

由于输出可能很大,因此请以模数10 ^ 9 + 7打印答案。
例子

Input : N = 3, K = 2
Output : 8

Input : N = 4, K = 2
Output : 20

让我们以给出的示例为例,尝试将其简化为一个通用公式。
在给定的示例中,对于n = 3k = 2

Sum = 1*2 + 2*3 

我们知道:
$1\times2=2!\times 0!$\\ $2\times3=3!\times1!$\\
因此,每个术语的形式为:

    $$\frac{x!}{\left(x-k\right)!}$$

如果我们乘以除以k!  , 它成为了

    $$k!\cdot\frac{x!}{\left(x-k\right)!\cdot k!}$$

没什么,
$k!\times$ ${N}\choose{k}$\\
所以,
sum = $k\times \sum_{x=k}^{n}$ $ {x}\choose{k}$ =$ {n+1}\choose{k+1}$ $\times k! $\\
但是由于n太大,我们不能直接计算它,所以我们必须简化上面的表达式。
在简化中,我们得到了

    $$\frac{\left(n\right) \cdot \left(n-1\right) \cdot \left(n-2\right) \dots \left(n-k+1\right)}{k+1}$$

下面是上述想法的实现:

C++
// CPP program to find the sum of the
// given sequence
 
#include 
using namespace std;
 
const long long MOD = 1000000007;
 
// function to find moudulo inverse
// under 10^9+7
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;
}
 
// Function to find the sum of the
// given sequence
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;
}
 
// Driver code
int main()
{
    long long n = 3, k = 2;
     
    cout<


Java
// Java program to find the sum of the
// given sequence
 
class GFG {
 
    static long MOD = 1000000007;
 
// function to find moudulo inverse
// under 10^9+7
    static long modInv(long x) {
        long n = MOD - 2;
        long result = 1;
        while (n > 0) {
            if ((n & 1) > 0) {
                result = result * x % MOD;
            }
            x = x * x % MOD;
            n = n / 2;
        }
 
        return result;
    }
 
// Function to find the sum of the
// given sequence
    static long getSum(long n, long k) {
        long ans = 1;
 
        for (long i = n + 1; i > n - k; i--) {
            ans = ans * i % MOD;
        }
        ans = ans * modInv(k + 1) % MOD;
 
        return ans;
    }
 
// Driver code
    public static void main(String[] args) {
        long n = 3, k = 2;
        System.out.println(getSum(n, k));
    }
}


Python3
# Python3 program to find the sum
# of the given sequence
 
MOD = 1000000007;
 
# function to find moudulo inverse
# under 10^9+7
def modInv(x):
 
    n = MOD - 2;
    result = 1;
    while (n):
        if (n&1):
            result = result * x % MOD;
        x = x * x % MOD;
        n = int(n / 2);
     
    return result;
 
# Function to find the sum of
# the given sequence
def getSum(n, k):
 
    ans = 1;
     
    for i in range(n + 1, n - k, -1):
        ans = ans * i % MOD;
    ans = ans * modInv(k + 1) % MOD;
     
    return ans;
 
# Driver code
n = 3;
k = 2;
     
print(getSum(n,k));
     
# This code is contributed by mits


C#
// C# program to find the sum of the
// given sequence
using System;
 
  
 
// function to find moudulo inverse
// under 10^9+7
class gfg
{
    public long MOD = 1000000007;
 public long modInv(long x)
 {
    long n = MOD - 2;
    long result = 1;
    while (n >0) {
        if ((n & 1) > 0)
            result = result * x % MOD;
        x = x * x % MOD;
        n = n / 2;
    }
     
    return result;
}
 
// Function to find the sum of the
// given sequence
  public long getSum(long n, long k)
  {
    long ans = 1;
     
    for (long i = n + 1; i > n - k; i--)
        ans = ans * i % MOD;
    ans = ans * modInv(k + 1) % MOD;
     
    return ans;
 }
}
 
// Driver code
class geek
{
  public static int Main()
 {
     gfg g = new gfg();
    long n = 3, k = 2;
     
    Console.WriteLine(g.getSum(n,k));
     
    return 0;
 }
}
//This code is contributed by SoumikMondal


PHP
 $n - $k; $i--)
        $ans = $ans * $i % $MOD;
    $ans = $ans * modInv($k + 1) % $MOD;
     
    return $ans;
}
 
// Driver code
$n = 3; $k = 2;
     
echo getSum($n, $k);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript


输出:
8