Given three integers n, m, and k, define the function: sum(x) = x × (x + 1).
- Start with the value n + k and apply the function sum() exactly m times.
- Before each application of sum() after the first, add k to the current value.
Return the answer modulo 109 + 7.
Examples:Â Â
Input: n = 1, m = 2, k = 3Â
Output: 552Â
Explanation: For m = 2Â
sum(3 + sum(3 + 1)) = sum(3 + sum(4)) = sum(3 + 20) = sum(23) = 552Input: n = 2, m = 2, k = 2
Output: 506
Explanation: For m = 3Â
sum(2 + sum(2 + 2)) = sum(2 + sum(4)) = sum(2 + 20) = 506
Try It Yourself
Table of Content
[Naive Approach] Using Recursion - O(m) Time and O(m) Space
We can directly translate the math into a recursive function that adds k and applies the sum() formula until m reaches 0.
- Create a helper function that takes the current value, m, and k.
- If m is 0, return the current value.
- Otherwise, add k to the current value and take the modulo.
- Apply the sum formula: current = (current * (current + 1)) % mod.
- Recursively call the helper function with the new value and m - 1.
#include <iostream>
using namespace std;
long long helper(long long current, int m, int k, long long mod) {
// Base case: zero operations left
if (m == 0) {
return current;
}
long long val = (current + k) % mod;
long long nextVal = (val * (val + 1)) % mod;
// Recursive call for next iteration
return helper(nextVal, m - 1, k, mod);
}
int modifiedSum(int n, int m, int k) {
long long mod = 1000000007;
return (int)helper(n, m, k, mod);
}
int main() {
int n = 1, m = 2, k = 3;
cout << modifiedSum(n, m, k) << endl;
return 0;
}
class GFG {
public static long helper(long current, int m, int k, long mod) {
// Base case: zero operations left
if (m == 0) {
return current;
}
long val = (current + k) % mod;
long nextVal = (val * (val + 1)) % mod;
// Recursive call for next iteration
return helper(nextVal, m - 1, k, mod);
}
public static int modifiedSum(int n, int m, int k) {
long mod = 1000000007;
return (int)helper(n, m, k, mod);
}
public static void main(String[] args) {
int n = 1, m = 2, k = 3;
System.out.println(modifiedSum(n, m, k));
}
}
def helper(current, m, k, mod):
# Base case: zero operations left
if m == 0:
return current
val = (current + k) % mod
next_val = (val * (val + 1)) % mod
# Recursive call for next iteration
return helper(next_val, m - 1, k, mod)
def modifiedSum(n, m, k):
mod = 1000000007
return helper(n, m, k, mod)
if __name__ == "__main__":
n = 1
m = 2
k = 3
print(modifiedSum(n, m, k))
using System;
class GFG {
public static long Helper(long current, int m, int k, long mod) {
// Base case: zero operations left
if (m == 0) {
return current;
}
long val = (current + k) % mod;
long nextVal = (val * (val + 1)) % mod;
// Recursive call for next iteration
return Helper(nextVal, m - 1, k, mod);
}
public static int modifiedSum(int n, int m, int k) {
long mod = 1000000007;
return (int)Helper(n, m, k, mod);
}
public static void Main() {
int n = 1, m = 2, k = 3;
Console.WriteLine(modifiedSum(n, m, k));
}
}
function helper(current, m, bigK, mod) {
// Base case: zero operations left
if (m === 0) {
return current;
}
let val = (current + bigK) % mod;
let nextVal = (val * (val + 1n)) % mod;
// Recursive call for next iteration
return helper(nextVal, m - 1, bigK, mod);
}
function modifiedSum(n, m, k) {
let mod = 1000000007n;
let bigK = BigInt(k);
return Number(helper(BigInt(n), m, bigK, mod));
}
// Driver Code
let n = 1;
let m = 2;
let k = 3;
console.log(modifiedSum(n, m, k));
Output
552
[Expected Approach] Iterative Simulation - O(m) Time and O(1) Space
Instead of using recursion (which takes up too much memory and can crash for large m), we can just use a simple for loop to repeat the process m times.
- Start by setting a current variable to n. Define your modulo value as 1000000007.
- Run a loop exactly m times.
- Inside the loop, first add k to your current value: val = (current + k) % mod.
- Next, apply the sum formula to get the new current value: current = (val * (val + 1)) % mod.
- Once the loop finishes running m times, return the final current value.
For Example: n = 1, m = 2, k = 3
- Start: Initialize current = 1.
- Step 1: Add k to get val = 1 + 3 = 4. Apply the sum formula to update current = (4 * 5) % 1000000007 = 20.
- Step 2: Add k to get val = 20 + 3 = 23. Apply the sum formula to update current = (23 * 24) % 1000000007 = 552.
- Result: The loop finishes after 2 iterations, and we return 552.
#include <iostream>
using namespace std;
int modifiedSum(int n, int m, int k) {
long long mod = 1000000007;
long long current = n;
for (int i = 0; i < m; i++) {
// Add k before applying sum
long long val = (current + k) % mod;
// Apply sum(x) = x * (x + 1)
current = (val * (val + 1)) % mod;
}
return (int)current;
}
int main() {
int n = 1, m = 2, k = 3;
cout << modifiedSum(n, m, k) << endl;
return 0;
}
class GFG {
public static int modifiedSum(int n, int m, int k) {
long mod = 1000000007;
long current = n;
for (int i = 0; i < m; i++) {
// Add k before applying sum
long val = (current + k) % mod;
// Apply sum(x) = x * (x + 1)
current = (val * (val + 1)) % mod;
}
return (int)current;
}
public static void main(String[] args) {
int n = 1, m = 2, k = 3;
System.out.println(modifiedSum(n, m, k));
}
}
def modifiedSum(n, m, k):
mod = 1000000007
current = n
for _ in range(m):
# Add k before applying sum
val = (current + k) % mod
# Apply sum(x) = x * (x + 1)
current = (val * (val + 1)) % mod
return current
if __name__ == "__main__":
n = 1
m = 2
k = 3
print(modifiedSum(n, m, k))
using System;
class GFG {
public static int modifiedSum(int n, int m, int k) {
long mod = 1000000007;
long current = n;
for (int i = 0; i < m; i++) {
// Add k before applying sum
long val = (current + k) % mod;
// Apply sum(x) = x * (x + 1)
current = (val * (val + 1)) % mod;
}
return (int)current;
}
public static void Main() {
int n = 1, m = 2, k = 3;
Console.WriteLine(modifiedSum(n, m, k));
}
}
function modifiedSum(n, m, k) {
let mod = 1000000007n;
let current = BigInt(n);
let bigK = BigInt(k);
for (let i = 0; i < m; i++) {
// Add k before applying sum safely with BigInt
let val = (current + bigK) % mod;
// Apply sum(x) = x * (x + 1)
current = (val * (val + 1n)) % mod;
}
return Number(current);
}
// Driver Code
let n = 1;
let m = 2;
let k = 3;
console.log(modifiedSum(n, m, k));
Output
552