链接 : http://codeforces.com/problemset/problem/1436/C
标签 二分 *1500
写了之后觉得是一道很好的用来理解二分过程的一道题.
实际上就是模拟二分的过程, 想在乱序的序列中找到某个x, 实际上只要控制每次二分的mid值.
分别用a, b来表示用来控制二分过程的比x小的值与比x大的值.
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 1010, p = 1e9 + 7;
int C[N][N];
void init() {
for (int i = 0; i < N; ++i)
for (int j = 0; j <= i; ++j) {
if (!j) C[i][j] = 1;
else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % p;
}
}
int main() {
IO;
init();
int n, x, pos, a = 0, b = 0;
cin >> n >> x >> pos;
int l = 0, r = n;
while (l < r) {
int mid = l + r >> 1;
//cout << mid << endl;
if (mid > pos) {
b++;
r = mid;
} else {
if (mid != pos) a++;
l = mid + 1;
}
}
//cout << x - 1 << " " << a << " " << n - x << " " << b << endl;
if (x - 1 < a || n - x < b) {
cout << "0" << endl;
return 0;
}
ll ans = 1;
for (int i = x - 1; i > x - 1 - a; --i) ans = ans * i % p;
for (int i = n - x; i > n - x - b; --i) ans = ans * i % p;
if (x == 1 || x == n) {
a = b = 0;
ans = 1;
}
for (int i = 1; i <= n - a - b - 1; ++i) ans = ans * i % p;
cout << ans << endl;
return 0;
}
原文:https://www.cnblogs.com/phr2000/p/13942697.html