# Binary Search Codeforces Round #678 SOLUTION

### C. Binary Search Codeforces Round #678 SOLUTION

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:
Note that the elements of the array are indexed from zero, and the division is done in integers (rounding down).
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!
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.
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 109+7.
Input
The only line of input contains integers n, x and pos (1≤x≤n≤1000, 0≤pos≤n−1) — the required length of the permutation, the number to search, and the required position of that number, respectively.
Output
Print a single number — the remainder of the division of the number of valid permutations by 109+7.
Examples
inputCopy
4 1 2
outputCopy
6
inputCopy
123 42 24
outputCopy
824071958
Note
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).
SOLUTION :

1. #include <bits/stdc++.h>
2. using namespace std;
3. typedef long long ll;
4. const int N_MAX = 1002;
5. const int MOD = 1e9+7;
6. int pwr (int a, int b)
7. {
8. if(b == 0)
9. return 1;
10. if(b & 1)
11. return 1LL * a * pwr(a, (b ^ 1)) % MOD;
12. int aux = pwr(a, (b >> 1));
13. return 1LL * aux * aux % MOD;
14. }
15. int n, x, pos;
16. int a, b;
17. int fact[N_MAX];
18. int ifact[N_MAX];
19. int comb (int n, int k)
20. {
21. if(k == 0)
22. return 1;
23. if(n < k)
24. return 0;
25. return 1LL * fact[n] * ifact[k] % MOD * ifact[n k] % MOD;
26. }
27. int main()
28. {
29. ios_base::sync_with_stdio(false);
30. cin.tie(0);
31. cout.tie(0);
32. cin >> n >> x >> pos;
33. fact = 1;
34. ifact = 1;
35. for(int i = 1; i <= n; i++)
36. {
37. fact[i] = 1LL * fact[i 1] * i % MOD;
38. ifact[i] = pwr(fact[i], MOD 2);
39. }
40. int l = 0, r = n;
41. while(l < r)
42. {
43. int mid = ((l + r) >> 1);
44. if(mid + 1 <= pos + 1)
45. {
46. if(mid != pos)
47. a++;
48. l = mid + 1;
49. }
50. else
51. {
52. if(mid != pos)
53. b++;
54. r = mid;
55. }
56. }
57. int A = 1LL * comb(x 1, a) * fact[a] % MOD;
58. int B = 1LL * comb(n x, b) * fact[b] % MOD;
59. int C = fact[n a b 1];
60. cout << 1LL * A * B % MOD * C % MOD << “n”;
61. return 0;
62. }