Definierte eine Funktion, die das Doppelte der Summe der ersten N natürlichen Zahlen als sum(N) berechnet . Ihre Aufgabe besteht darin, die Funktion in sumX(N, M, K) zu ändern, die sum( K + sum( K + sum( K + …sum(K + N)…))) berechnet , und zwar für M Terme. Berechnen Sie für ein gegebenes N, M und K den Wert von sumX(N, M, K)
Hinweis: Da die Antwort sehr groß sein kann, geben Sie die Antwort in Modulo 10^9 + 7 aus .
Beispiele: 
 

Eingabe: N = 1, M = 2, K = 3 
Ausgabe: 552 
Für M = 2 
sum(3 + sum(3 + 1)) = sum(3 + 20) = 552.
Eingabe: N = 3, M =3 , K = 2 
Ausgabe: 1120422 
Für M = 3 
sum(2 + sum(2 + sum(2 + 3))) = sum(2 + sum(2 + 30)) = sum(2 + 1056) = 1120422. 
 

Sich nähern: 
 

  • Berechnen Sie den Wert von sum(N) mit der Formel N*(N + 1) .
  • Führen Sie M - mal eine Schleife aus, fügen Sie jedes Mal K zur vorherigen Antwort hinzu und wenden Sie jedes Mal sum(prev_ans + K) modulo 10^9 + 7 an.
  • Geben Sie am Ende den Wert von sumX(N, M, K) aus.

Unten ist die Implementierung des obigen Ansatzes: 
 

C++

// C++ program to calculate the 
// terms of summing of sum series
 
#include <iostream>
 
using namespace std;
# define MOD 1000000007
 
// Function to calculate
// twice of sum of first N natural numbers
long sum(long N){
     
    long val = N * (N+1);
    val = val % MOD;
     
    return val;
}
 
// Function to calculate the
// terms of summing of sum series
int sumX(int N, int M, int K){
     
    for (int i = 0; i < M; i++) {
        N = (int)sum(K + N);
    }
     
    N = N % MOD;
    return N;
}
 
// Driver Code
int main()
{
    int N = 1, M = 2, K = 3;
    cout << sumX(N, M, K) << endl;
    
    return 0;
}
 
// This code is contributed by Rituraj Jain

Java

// Java program to calculate the
// terms of summing of sum series
 
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG {
 
    static int MOD = 1000000007;
 
    // Function to calculate
    // twice of sum of first N natural numbers
    static long sum(long N)
    {
        long val = N * (N + 1);
 
        // taking modulo 10 ^ 9 + 7
        val = val % MOD;
 
        return val;
    }
 
    // Function to calculate the
    // terms of summing of sum series
    static int sumX(int N, int M, int K)
    {
        for (int i = 0; i < M; i++) {
            N = (int)sum(K + N);
        }
        N = N % MOD;
        return N;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int N = 1, M = 2, K = 3;
        System.out.println(sumX(N, M, K));
    }
}

Python3

# Python3 program to calculate the 
# terms of summing of sum series
   
MOD = 1000000007
   
# Function to calculate
# twice of sum of first N natural numbers
def Sum(N):
      
    val = N * (N + 1)
   
    # taking modulo 10 ^ 9 + 7
    val = val % MOD
   
    return val
   
# Function to calculate the
# terms of summing of sum series
def sumX(N, M, K):
      
    for i in range(M):
        N = int(Sum(K + N))
          
    N = N % MOD
    return N
   
if __name__ == "__main__":
      
    N, M, K = 1, 2, 3
    print(sumX(N, M, K))
 
# This code is contributed by Rituraj Jain

C#

// C# program to calculate the
// terms of summing of sum series
 
using System;
class GFG {
 
    static int MOD = 1000000007;
 
    // Function to calculate
    // twice of sum of first N natural numbers
    static long sum(long N)
    {
        long val = N * (N + 1);
 
        // taking modulo 10 ^ 9 + 7
        val = val % MOD;
 
        return val;
    }
 
    // Function to calculate the
    // terms of summing of sum series
    static int sumX(int N, int M, int K)
    {
        for (int i = 0; i < M; i++) {
            N = (int)sum(K + N);
        }
        N = N % MOD;
        return N;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 1, M = 2, K = 3;
        Console.WriteLine(sumX(N, M, K));
    }
}
 
// This code is contributed by anuj_67..

PHP

<?php
// PHP program to calculate the
// terms of summing of sum series
 
// Function to calculate twice of
// sum of first N natural numbers
function sum($N)
{
    $MOD = 1000000007;
    $val = $N * ($N + 1);
    $val = $val % $MOD;
     
    return $val;
}
 
// Function to calculate the terms
// of summing of sum series
function sumX($N, $M, $K)
{
    $MOD = 1000000007;
    for ($i = 0; $i < $M; $i++)
    {
        $N = sum($K + $N);
    }
     
    $N = $N % $MOD;
    return $N;
}
 
// Driver Code
$N = 1;
$M = 2;
$K = 3;
echo (sumX($N, $M, $K));
     
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript

<script>
 
// Javascript program to calculate the
// terms of summing of sum series
 
// Function to calculate twice of
// sum of first N natural numbers
function sum(N)
{
    let MOD = 1000000007;
    let val = N * (N + 1);
    val = val % MOD;
     
    return val;
}
 
// Function to calculate the terms
// of summing of sum series
function sumX(N, M, K)
{
    let MOD = 1000000007;
    for (let i = 0; i < M; i++)
    {
        N = sum(K + N);
    }
     
    N = N % MOD;
    return N;
}
 
// Driver Code
let N = 1;
let M = 2;
let K = 3;
document.write (sumX(N, M, K));
     
// This code is contributed
// by Sravan
 
</script>
Ausgabe: 
552

 

Falls Sie an Live-Kursen mit Experten teilnehmen möchten , beziehen Sie sich bitte auf DSA Live-Kurse für Berufstätige und Competitive Programming Live for Students .