Codeforces Round #678 (Div. 2) C

题目链接

思路

按照题目所给程序,将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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值