Mixed Up Cows
InterviewTime limit1sMemory limit128 MB
Count permutations of N serial numbers (N at most 16) where every adjacent pair differs by more than K.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Bit manipulation, Combinatorics, Brute force
- Solved
- No attempts yet
Problem
Each of Farmer John's cows () has a unique serial number ().
The cows line up to be milked in an order called 'Mixed Up'. A lineup is 'Mixed Up' if, in the sequence of serial numbers formed by the line, every pair of consecutive cows has serial numbers that differ by more than (). For example, if and , then the order is 'Mixed Up', but is not, because the consecutive numbers and differ by only .
How many different ways can the cows be lined up 'Mixed Up'?
Input
- Line 1: Two space-separated integers and .
- Lines 2 to : Line contains a single integer , the serial number of cow .
Output
- Line 1: A single integer, the number of ways the cows can be lined up 'Mixed Up'. The answer is guaranteed to fit in a 64-bit integer.
Hint
For the sample, the possible 'Mixed Up' lineups are shown below, where each line lists the cows' serial numbers in order.
3 1 4 2
2 4 1 3