CTOI10D3 - A HUGE TOWER

no tags 

The ancient Babylonians decided to build a huge tower. The tower consists of N cubic building blocks that are stacked one onto another. The Babylonians gathered many building blocks of various sizes from all over the country. From their last unsuccessful attempt they have learned that if they put a large block directly onto a much smaller block, the tower will fall.

Task specification

Each two building blocks are different, even if they have the same size. For each building block you are given its side length. You are also given an integer D with the following meaning: you are not allowed to put block A directly onto block B if the side length of A is strictly larger than D plus the side length of B.

Compute the number of different ways in which it is possible to build the tower using all the building blocks. Since this number can be very large, output the result modulo 109 + 9.

Input specification

The first line of the input contains two positive integers N and D: the number of building blocks and the tolerance respectively.

The second line contains N space-separated integers; each represents the size of one building block.

Constraints

All numbers in the input files are positive integers not exceeding 109.

N is always at least 2.

In test cases worth 70 points N will be at most 70.

Out of those, in test cases worth 45 points, N will be at most 20.

Out of those, in test cases worth 10 points, N will be at most 10.

For some of the test cases the total number of valid towers will not exceed 1\,000\,000. These test cases are worth 30 points in total.

For the last six test cases (worth 30 points) the value of N is larger than 70. No upper bound on N is given for these test cases.

Output specification

Output a single line containing a single integer: the number of towers that can be built, modulo 1\,000\,000\,009.

Examples

input:

4 1
1 2 3 100

output:

4

We can arrange the first three blocks in any order, except for 2,1,3 or 1,3,2. The last block has to be at the bottom.

input:

6 9
10 20 20 10 10 20

output:

36

We are not allowed to put a cube of size 20 onto a cube of size 10. There are six ways to order the cubes of size 10, and six ways to order the cubes of size 20.


hide comments
lionblaze_16: 2017-05-10 22:48:20

Hello,
The exact same C++ code that gets TLE on SPOJ gets AC (100/100) on the official CEOI website. Can a mod please look at my code to see what I can do for a speedup?
Thanks,
lionblaze_16

EDIT: scanf is your friend in C++, and BufferedReader in Java

Last edit: 2017-05-10 23:07:08
Ajey Golsangi: 2012-02-03 17:29:58

Brute force does not work.


Added by:Phan Công Minh
Date:2010-07-17
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC PERL6 SQLITE VB.NET
Resource:CEOI 2010