codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1436C.md (2107B)


      1 Source: https://codeforces.com/problemset/problem/1436/C
      2 
      3 C. Binary Search
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Andrey thinks he is truly a successful developer, but in reality he didn't know about the binary search algorithm until recently. After reading some literature Andrey understood that this algorithm allows to quickly find a certain number $x$ in an array. For an array $a$ indexed from zero, and an integer $x$ the pseudocode of the algorithm is as follows:
     22 
     23  ![](https://espresso.codeforces.com/a63fab4a2aeadaecbf4d75db5fbea7d101fd2cee.png) 
     24 
     25 Note that the elements of the array are indexed from zero, and the division is done in integers (rounding down).
     26 
     27 Andrey read that the algorithm only works if the array is sorted. However, he found this statement untrue, because there certainly exist unsorted arrays for which the algorithm find $x$!
     28 
     29 Andrey wants to write a letter to the book authors, but before doing that he must consider the permutations of size $n$ such that the algorithm finds $x$ in them. A permutation of size $n$ is an array consisting of $n$ distinct integers between $1$ and $n$ in arbitrary order.
     30 
     31 Help Andrey and find the number of permutations of size $n$ which contain $x$ at position $pos$ and for which the given implementation of the binary search algorithm finds $x$ (returns true). As the result may be extremely large, print the remainder of its division by $10^9+7$.
     32 
     33 Input
     34 
     35 The only line of input contains integers $n$, $x$ and $pos$ ($1 \le x \le n \le 1000$, $0 \le pos \le n - 1$) — the required length of the permutation, the number to search, and the required position of that number, respectively.
     36 
     37 Output
     38 
     39 Print a single number — the remainder of the division of the number of valid permutations by $10^9+7$.
     40 
     41 Examples
     42 
     43 Input
     44 
     45 ```
     46 4 1 2
     47 ```
     48 
     49 Output
     50 
     51 ```
     52 6
     53 ```
     54 
     55 Input
     56 
     57 ```
     58 123 42 24
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 824071958
     65 ```
     66 
     67 Note
     68 
     69 All possible permutations in the first test case: $(2, 3, 1, 4)$, $(2, 4, 1, 3)$, $(3, 2, 1, 4)$, $(3, 4, 1, 2)$, $(4, 2, 1, 3)$, $(4, 3, 1, 2)$.