Codeforces Round #678 (Div. 2) C题 Binary Search(组合数学)

题目链接:https://codeforc.es/contest/1436/problem/C

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

题意

有 n 个数(1~n)的数列,x 值在位置 pos 上(数列索引是从0开始的),对这个数列进行二分查找使其能够顺利查找到 x,那么这个数列的排列顺序有几种(mod 1e9+7)。

分析

我们先按照 1~n 的顺序数列进行二分查找值为 pos+1 的数(因为数列索引是从0开始,所以在顺序数列 a[pos] 的值为 pos+1),这样我们就可以知道最终找到这个位置的要求,即要有几个数大于 x,要有几个数小于等于 x,再根据排列组合算出答案。
设有 A 个数大于 x,B个数小于等于 x,有 a 个位置大于 x,b 个位置小于 等于 x,则答案为 A(A, a) * A(B - 1, b - 1) * f[n - a - b] (取模,注意等于的位置已经确定了所以要减一)。


代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll mod = 1e9 + 7;
int n,x,pos;
int a[1007];
ll f[1007];

ll qp(ll xx,ll yy)
{
	ll res = 1;
	while(yy)
	{
		if(yy & 1) res = (res * xx) % mod;
		xx = (xx * xx) % mod;
		yy >>= 1;
	}
	return res % mod;
}

ll A(int n,int m)
{
	return f[n] * qp(f[n - m] % mod, mod - 2) % mod;
}

int main()
{
	f[0] = 1;
	for(int i=1;i<=1000;i++) f[i] = i * f[i - 1] % mod;
	scanf("%d%d%d",&n,&x,&pos);
	for(int i=0;i<n;i++)
		a[i] = i + 1;
	
	int smaller = 0;
	int bigger = 0;
	int l = 0;
	int r = n;
	while(l < r)
	{
		int mid = (l + r) / 2;
		if(a[mid] <= pos + 1)
		{
			l = mid + 1;
			smaller++;
		}
		else
		{
			r = mid;
			bigger++;
		}
	}
	int els = n - smaller - bigger;
	int B = n - x;
	int S = x;
	ll ans = A(B, bigger) * A(S - 1, smaller - 1) % mod * f[els] % mod;
	printf("%lld\n",ans);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值