Counting permutations

2.8

6 votes
Special Numbers, Counting, Mathamatics, Math, C++
Problem

You are given two integers \(n\) and \(m\). You are required to find the number of permutations of \(n\) such that for each \(i\) the \(|a_i - i| ≠ m\) where \(|x - y|\) denotes the absolute difference between \(x\) and \(y\).

Example:

Here, \(n = 4\) and \(m = 3\), therefore, the valid permutations are \([2\ 4\ 1\ 3]\), \([1\ 3\ 4\ 2]\), and so on. The invalid permutations for the same are \([4\ 1\ 3\ 2]\), \([3\ 2\ 4\ 1]\), and so on.

Note: As the number can be large, print answer modulo \(1000000007\).

Input format

The first line contains two integers \(n\) and \(m\).

Output format

Print one integer denoting the number of permutations that are valid. As the number can be very large, print answer modulo \(1000000007\).

Constraints

 \(2 ≤ n ≤ 1e3\\ 1 ≤ m < n\)

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Valid Permutations:

1 2 3 4 
1 2 4 3 
1 3 2 4 
1 3 4 2 
1 4 2 3 
1 4 3 2 
2 1 3 4 
2 1 4 3 
2 3 1 4 
2 4 1 3 
3 1 2 4 
3 1 4 2 
3 2 1 4 
3 4 1 2 
 

Invalid Permutations

2 3 4 1 
2 4 3 1 
3 2 4 1 
3 4 2 1 
4 1 2 3 
4 1 3 2 
4 2 1 3 
4 2 3 1 
4 3 1 2 
4 3 2 1 

Editor Image

?