2999. Function-10

 

The function is given with nonnegative integer arguments m and n (mn):

Find the value of the function.

 

Input. Two nonnegative 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 should implement the given recursive function.

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

 

Algorithm realization

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, compute and print the result.

 

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

res = f(m,n);

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

 

Java realization

 

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 realization

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, compute and print the result.

 

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

print(f(b, a))