11249. Recursion – 2

 

The integers a, b, c are given. For a given integer n, compute the value of the function:

Input. Four integers a, b, c (|a|, |b|, |c| ≤ 1000), n (0 n 1000).

 

Output. Print the value of f(n).

 

Sample input

Sample output

4 2 -3 10

84

 

 

RECURSION

recursion

 

Algorithm analysis

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

 

Algorithm realization

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 realization

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)