【Codeforces 1436 C】Binary Search,二分,构造排列,统计


C. Binary Search
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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.

Print a single number — the remainder of the division of the number of valid permutations by 109+7.

4 1 2
123 42 24
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).


+ 构造一个1~n的排列,其中x在pos位置,使得题给的二分查找程序能正确找到x。
+ 求有多少个这样的排列
+ 首先知道x在pos,所以二分的过程中,需要找几次,会经过哪些点就已经知道了(虽然排列不是单调的,但这个排列的下标是单调的)。
+ 直接对下标pos二分查找,在这个过程中记录比pos大的下标的个数以及比pos小的下标的个数,这实际上也就是在最终要构造出来的序列中二分转折点上比x大的数的个数以及比x小的数的个数。个数确定后就是排列组合问题了,对于这两部分先选再排,剩下的数求全排列,把这些乘起来即可。
using namespace std;
typedef long long LL;
const int mod = 1e9+7;
LL inv(LL x){return x==1?1:(LL)(mod-mod/x)*inv(mod%x)%mod;}
LL C(LL n, LL m){
	if(n<0 || n<m)return 0;
	LL up = 1, down = 1;
	for(LL i = 0; i < m; i++){
		up = up*(n-i)%mod;
		down = down*(i+1)%mod;
	return up*inv(down)%mod;
LL A(LL n, LL m){
	LL ans = 1;
	for(LL i = n; i >= n-m+1; i--)
		ans = ans*i%mod;
	return ans;
int main(){
	int n, x, pos;
	int l = 0, r = n;
	LL small = 0, big = 0;
	while(l < r){
		int mid = l+r>>1;
		if(mid > pos){r = mid; big++;}
		else if(mid<pos){l=mid+1; small++;}
		else l = mid+1;
	return 0;

已标记关键词 清除标记
<p> <strong><span style="font-size:20px;color:#FF0000;">本课程主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者</span></strong> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">1. 包含:<span style="color:#FFFF00;background-color:#FF0000;">项目源码、</span><span style="color:#FFFF00;background-color:#FF0000;">项目文档、数据库脚本、软件工具</span>等所有资料</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">2. 手把手的带你从零开始部署运行本套系统</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">3. 该项目附带的源码资料可作为毕设使用</span></strong></span> </p> <p> <span style="color:#FF0000;"><strong><span style="font-size:18px;">4. 提供技术答疑和远程协助指导</span></strong></span><strong><span style="font-size:18px;"></span></strong> </p> <p> <br /> </p> <p> <span style="font-size:18px;"><strong>项目运行截图:</strong></span> </p> <p> <strong><span style="font-size:18px;">1)系统登陆界面</span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015433522.png" alt="" /><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">2)学生模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241015575966.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">3)教师模块</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016127898.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">4)系统管理员</span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016281177.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><img src="https://img-bss.csdn.net/202002241016369884.png" alt="" /></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p> <p> <strong><span style="font-size:18px;"><strong><span style="font-size:18px;">更多Java毕设项目请关注我的毕设系列课程 <a href="https://edu.csdn.net/lecturer/2104">https://edu.csdn.net/lecturer/2104</a></span></strong></span></strong> </p> <p> <strong><span style="font-size:18px;"><br /> </span></strong> </p>
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页