Recursion - 2#11249

Recursion - 2 (Editorial)

To solve the problem, the given recursive function should be implemented.

Algorithm implementation

The function f implements the recursive function given in the problem statement.

long long f(long long n)
{
  if (n == 0) return a;
  return f(n - 1) + b * n + c;
}

The main part of the program. Read the input data.

scanf("%lld %lld %lld %lld", &a, &b, &c, &n);

Compute and print the answer.

res = f(n);
printf("%lld\n", res);

Python implementation

The function f implements the recursive function given in the problem statement.

def f(n):
  if n == 0: return a
  return f(n - 1) + b * n + c

The main part of the program. Read the input data.

a, b, c, n = map(int,input().split())

Compute and print the answer.

res = f(n)
print(res)