思路
按照题目所给程序,将a[mid]<=x等价转化为mid<=pos(a[mid]>x同理),即可求出需要小于和大于x的数量。则答案转化为在x-1个数中任意选取small个小于x的数×在n-x个数中任意选取big个大于x的数×剩余的(n-big-small-1)个数任意组合。
代码
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
const int maxn=1e6+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll l,r,m,n,t,x,big,pos,small,sum,a[10],b[maxn];
ll jie(ll a)
{
ll ans=1;
for(int i=1;i<=a;i++)
ans=ans*i%mod;
return ans;
}
ll quick_pow(ll a,ll b)
{
ll base=a%mod;
ll ans=1;
while(b>0)
{
if(b&1) ans=ans*base%mod;
base=base*base%mod;
b>>=1;
}
return ans;
}
ll A(ll m,ll n)
{
return jie(n)*quick_pow(jie(n-m),mod-2)%mod;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>x>>pos;//3,3,1
l=0;
r=n;
while(l<r)
{
ll mid=floor((l+r)/2);
if(mid==pos)
l=mid+1;//注意此处根据题意不应跳出
else if(mid>pos)
{
r=mid;
big++;
}
else if(mid<pos)
{
l=mid+1;
small++;
}
}
if(small>x-1||big>n-x)
{
cout<<"0"<<endl;
return 0;
}//无法选取的情况
cout<<((A(big,n-x)%mod*A(small,x-1)%mod)%mod*jie(n-big-small-1)%mod)%mod<<endl;;
return 0;
}