Codeforces Round #678 (Div. 2) C. Binary Search

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).

题意 求有多少序列能够在指定位子二分查找这个确定的数的个数。

思路
求他的二分程序加上求在pos点左的mid的个数lsum和右边的个数rsum
结果自然可求出位C(x-1,lsum) * C(n-x,rsum) * (lsum)! * (rsum)! * (n-lsum-rsum-1)!
这里lsum在统计后要减一,原因为他写的二分查找程序是这样的。

代码

#include <bits/stdc++.h>
typedef unsigned long long ll;
const ll mod = 1e9+7;
using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 1e3+5;
const double esp = 1e-5;
int n,x,pos,lsum=-1,rsum;
ll f[N],a[N];
void init(){
	f[0]=1;
	for(int i=1;i<=1000;i++)
		f[i]=f[i-1]*i%mod,a[i-1]=i-1;
}
ll cal(ll n,ll m){
	return 1ll*(f[n]*qpow(f[m],mod-2)%mod*qpow(f[n-m],mod-2)%mod);
}
void find(){
	int l=0;
	int r=n;
	while(l<r){
		int mid=(l+r)>>1;
		//cout<<l<<" "<<r<<endl;
		if(mid<=pos) {
			l=mid+1;
			lsum++;
		}
		else{
			r=mid; 
			rsum++;
		}
	}
}
int main(){
	init();
    input(n),input(x),input(pos);
    find();
    ll ans=f[rsum]*cal(n-x,rsum)%mod*f[lsum]%mod*cal(x-1,lsum)%mod*f[n-lsum-rsum-1]%mod;
    printf("%lld",ans%mod);
    return 0;
}
/*
123 42 24
824071958
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值