Codeforces1436 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 𝑥 in an array. For an array 𝑎 indexed from zero, and an integer 𝑥 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 𝑥!

Andrey wants to write a letter to the book authors, but before doing that he must consider the permutations of size 𝑛 such that the algorithm finds 𝑥 in them. A permutation of size 𝑛 is an array consisting of 𝑛 distinct integers between 1 and 𝑛 in arbitrary order.

Help Andrey and find the number of permutations of size 𝑛 which contain 𝑥 at position 𝑝𝑜𝑠 and for which the given implementation of the binary search algorithm finds 𝑥 (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 𝑛, 𝑥 and 𝑝𝑜𝑠 (1≤𝑥≤𝑛≤1000, 0≤𝑝𝑜𝑠≤𝑛−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。

思路:
就是按照这个二分算法跑一遍,二分到pos右边时,要求这个数一定要大于 a [ p o s ] a[pos] a[pos],到pos左边的时候,这个数一定要小于 a [ p o s ] a[pos] a[pos],就这样得到了一些限制条件。
根据限制条件退一下组合数公式就好了。

#include <cstdio>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
typedef long long ll;
const int maxn = 1e4 + 7;
const int mod = 1e9 + 7;
 
int vis[maxn];//1为大于n,-1位小于n
int x,n,pos;
 
ll fac[maxn],inv[maxn];
 
ll qpow(ll a,ll b)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)
        {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b = b >> 1;
    }
    return res % mod;
}
 
ll C(ll n,ll m)
{
    if(m > n || m < 0)
        return 0;
    return fac[n] * ((inv[n - m] * inv[m]) % mod) % mod;
}
 
void init()
{
    fac[0] = 1;
    inv[0] = 1;
    for(int i = 1;i <= maxn - 2;i++)
    {
        fac[i] = (fac[i - 1] * i) % mod;
        inv[i] = qpow(fac[i],mod - 2);
    }
}
 
 
void bin() {
    int l = 0,r = n;
    while(l < r) {
        int mid = (l + r) >> 1;
        if(mid == pos) {
            l = mid + 1;
        } else if(mid > pos) {
            vis[mid] = 1;
            r = mid;
        } else {
            vis[mid] = -1;
            l = mid + 1;
        }
    }
}
 
int main() {
    init();
    scanf("%d%d%d",&n,&x,&pos);
    bin();
    int cnt1 = 0,cnt0 = 0;
    for(int i = 0;i < n;i++) {
        if(vis[i] == 1) cnt1++;
        else if(vis[i] == -1) cnt0++;
    }
 
    ll ans = 0;
    ans = C(n - x,cnt1) * C(x - 1,cnt0) % mod * fac[n - cnt1 - cnt0 - 1] % mod * fac[cnt1] % mod * fac[cnt0] % mod;
    printf("%lld\n",ans);
    return 0;
}
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值