2999. Function-10

 

Let the function be defined as follows:

Compute the value of this function.

 

Input. Two integers n and m (0 ≤ n, m ≤ 20).

 

Output. Print the value of the function f(m, n).

 

Sample input

Sample output

4 2

6

 

 

SOLUTION

recursion

 

Algorithm analysis

In this problem, you need to implement the given recursive function.

Note that in the input, the variables are given in the order n, m, whereas in the function they are passed in the reverse order: f(m, n).

 

Algorithm implementation

Here is a recursive implementation of the function f.

 

int f(int m, int n)

{

  if (m == 0) return 1;

  if (m == n) return 1;

  return f(m-1,n-1) + f(m,n-1);

}

 

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

 

scanf("%d %d",&n,&m);

 

Compute and print the answer.

 

res = f(m,n);

printf("%d\n",res);

 

Java implementation

 

import java.util.*;

 

public class Main

{

  static int f(int m, int n)

  {

    if (m == 0) return 1;

    if (m == n) return 1;

    return f(m-1,n-1) + f(m,n-1);

  }

   

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    int m = con.nextInt();

   

    int res = f(m,n);

 

    System.out.println(res);

    con.close();

  }

}

 

Python implementation

Here is a recursive implementation of the function f.

 

def f(m, n):

  if m == n or m == 0:

    return 1

  return f(m-1, n-1) + f(m, n - 1)

 

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

 

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

 

Compute and print the answer.

 

print(f(b, a))