定义了一个函数,该函数将前N个自然数之和的两倍计算为sum(N) 。您的任务是将函数修改为sumX(N,M,K) ,以计算sum(K + sum(K + sum(K +…sum(K + N)…))) ,继续M个项。对于给定的N,M和K,计算sumX(N,M,K)的值。
注意:由于答案可能非常大,请以模数10 ^ 9 + 7打印答案。
例子:
Input: N = 1, M = 2, K = 3
Output: 552
For M = 2
sum(3 + sum(3 + 1)) = sum(3 + 20) = 552.
Input: N = 3, M =3, K = 2
Output: 1120422
For M = 3
sum(2 + sum(2 + sum(2 + 3))) = sum(2 + sum(2 + 30)) = sum(2 + 1056) = 1120422.
方法:
- 使用公式N *(N + 1)计算sum(N)的值。
- 运行M次循环,每次将K添加到上一个答案,然后应用sum(prev_ans + K) ,每次取模10 ^ 9 + 7。
- 最后打印sumX(N,M,K)的值。
下面是上述方法的实现:
C++
// C++ program to calculate the
// terms of summing of sum series
#include
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
Javascript
输出:
552