#C9304. Count Book Pairs

    ID: 53383 Type: Default 1000ms 256MiB

Count Book Pairs

Count Book Pairs

You are given n books with distinct heights labeled from 1 to n (i.e., the heights are exactly 1, 2, \ldots, n). You are also given an integer d representing the desired height difference.

A valid pair of books is defined as a pair (i, j) such that j = i + d and both books exist (i.e., 1 \leq i, j \leq n).

Your task is to compute the number of valid pairs.

Note: It is guaranteed that d is a non-negative integer. When d = 0, every book forms a pair with itself.

Examples:

  • For n = 5, d = 3: The valid pairs are (1, 4) and (2, 5), so the answer is 2.
  • For n = 8, d = 2: The valid pairs are (1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8), so the answer is 6.
  • For n = 10, d = 0: Every book forms a pair with itself, so the answer is 10.

inputFormat

The input consists of two space-separated integers:

  • n: the number of books (1 ≤ n ≤ 106).
  • d: the desired height difference (0 ≤ d ≤ 106).

These values are provided on a single line via standard input.

outputFormat

Output a single integer representing the number of valid pairs of books that have a height difference of d. The output should be written to standard output.

## sample
5 3
2

</p>