思路:通过题目所给的二分查找,判断pos位置两边的元素应该比它大的数和比它小的数,然后通过排列组合求解
题目链接
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+5;
int Jc[N];
#define int long long
int mod = 1e9+7;
void calJc() //求maxn以内的数的阶乘
{
Jc[0] = Jc[1] = 1;
for(int i = 2; i < N; i++)
Jc[i] = Jc[i - 1] * i % mod;
}
int pow(int a, int n) //快速幂 a^n % p
{
int ans = 1;
while(n)
{
if(n & 1) ans = ans * a % mod;
a = a * a % mod;
n >>= 1;
}
return ans;
}
int niYuan(int a) //费马小定理求逆元
{
return pow(a, mod - 2);
}
int C(int a, int b) //计算C(a, b)
{
return Jc[a] * niYuan(Jc[b]) % mod* niYuan(Jc[a - b]) % mod;
}
int A(int a){
int res = 1;
for(int i =a;i>=1;--i){
res = res*i%mod;
}
return res%mod;
}
void solve(){
int n ,pos,x;
cin>>n>>x>>pos;
int l =0,r = n;
int mid;
int c1 =0,c2=0 ;
while(l<r){
mid = (r+l)/2;
if(mid>pos){
r= mid;
c2++;
}
else{
l = mid+1;
if(mid!=pos)
c1++;
}
}
cout<<C(x-1, c1)*C(n-x, c2)%mod*A(c1)*A(c2)%mod*A(n-c1-c2-1)%mod<<endl;
}
signed main()
{
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int __ = 1;
//cin>>__;
calJc();
while (__--) {
// init();
solve();
}
}