Farmer John is calling his N
(1 ≤ N
≤ 10) cows to a very important
meeting at a round table.
The cows are rather anxious about this meeting since they want to
put forth their very best image. To make the table arrangement
attractive, they want to ensure that any two cows sitting next to
each other to differ in height by no more than K
(1 ≤ K
millimeters (cow i
has integer height Hi
(1 ≤ Hi
Help calculate the number of ways the cows can sit down at the round
table such that no two adjacent cows differ in height by more than
millimeters. Two ways are different if there is at least one cow
with a different cow to its left in each arrangement.
The answer is guaranteed to fit in a 32-bit signed integer.
* Line 1: Two space-separated integers, N
* Lines 2..N
+ 1: Line i
+1 contains height Hi
* Line 1: The number of ways that the cows can sit down at the round
table such that two adjacent cows do not differ in height by
more than K
There are 4 cows, with heights 2, 16, 6, and 10. Two cows whose heights
differ by more than 10 do not want to sit next to each other.
Clearly the cows with heights 2 and 16 must be opposite each other, so
there are 2 ways to place the cows of height 6 and 10.