cf-678div2-C.Binary Search

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 10^9+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 10^9+7.

Examples

input
4 1 2
output
6

input
123 42 24
output
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).

注意题目使用的二分搜索普通的二分搜索不太一样

#include <stdio.h>
#include <algorithm>
using namespace std;
const int mod=1000000000+7;
int flag[1000+10];
long long cntLarger=0,cntSmaller=0;
long long n,x,pos;
void BinarySearch(int a,int x) {
	int l=0;
	int r=a;
	while(l<r) {
		int mid=(l+r)/2;
		if(mid<=x) {
			l=mid+1;
			flag[mid]=-1;
		}
		else {
			r=mid;
			flag[mid]=1;
		}
	}
	for(int i=0;i<n;i++) {
		if(i==x) {
			continue;
		}
		if(flag[i]==1) {
			cntLarger++;
		}
		else if(flag[i]==-1) {
			cntSmaller++;
		} 
}
int main(void) {
	scanf("%lld%lld%lld",&n,&x,&pos);
	long long ans=1;
	BinarySearch(n,pos);
	long long smaller=x-1;
	long long larger=n-x;
	for(int i=0;i<cntLarger;i++) {
		ans=ans*larger%mod;
		larger--;
	}
	for(int i=0;i<cntSmaller;i++) {
		ans=ans*smaller%mod;
		smaller--;
	}
	n-=cntLarger+cntSmaller+1;
	while(n) {
		ans*=n--;
		ans%=mod;
	}
	printf("%lld",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ljw0925

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值