比较有趣的题。
Sol.1 排列 DP
的做法是平凡的,我们先研究有效询问的性质。
容易发现,一个询问是有效询问,当且仅当它缩小了上界或者下界。而上界必然 ,下界必然 ,这启示我们对上界、下界分别进行研究。
继续观察,发现所有出现过的上界组成了一个单调下降的序列。同理,所有出现过的下界也组成了一个单调上升的序列。
由于这是个排列,我们可以使用排列 DP 的经典套路:插入法。具体而言,定义 表示排列中已经出现了 个 的元素, 个 的元素,且上一次有效询问缩小的是上界还是下界。
在转移的过程中,我们先枚举它是大于 还是小于等于 ,然后再确定它在 或 中已插入元素的相对排名。如果相对排名最大 / 最小,则根据题目要求进行特殊转移即可。
转移的过程中需要分别记录方案数与计数总和。
时间复杂度 。
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define fi first
#define se second
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define lc(x) (tr[x].ls)
#define rc(x) (tr[x].rs)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ldb;
typedef __int128 i128;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
const int N = 5e3 + 10;
const ll mod = 1e9 + 7;
ll n, x;
inline void add(ll &a, ll b)
{
a += b;
if(a >= mod) a -= mod;
}
inline void upd(pl &a, pl b, int typ)
{
add(a.fi, b.fi);
if(typ == 2) add(b.se, b.fi);
add(a.se, b.se);
}
pl mul(pl a, ll b)
{
return {(a.fi * b) % mod, (a.se * b) % mod};
}
pl dp[N][N][2];
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> x;
dp[0][0][0] = {1, 0};
for(int i = 0; i <= n - x; i++)
{
for(int j = 0; j <= x; j++)
{
upd(dp[i + 1][j][1], dp[i][j][0], 1);
upd(dp[i][j + 1][0], dp[i][j][0], 1);
upd(dp[i + 1][j][1], dp[i][j][1], 1);
upd(dp[i][j + 1][0], dp[i][j][1], 2);
upd(dp[i + 1][j][0], mul(dp[i][j][0], i), 1);
upd(dp[i][j + 1][0], mul(dp[i][j][0], j), 1);
upd(dp[i + 1][j][1], mul(dp[i][j][1], i), 1);
upd(dp[i][j + 1][1], mul(dp[i][j][1], j), 1);
}
}
cout << (dp[n - x][x][0].se + dp[n - x][x][1].se) % mod;
return 0;
}Sol.2 贡献法
考虑计算值为 的数对的贡献。假设 为排在 前面的,最后一个 的数。如果 要有贡献,必然满足 有 。其中 表示 在排列里的位置。
注意到当 固定时, 的取值其实并不影响方案。故我们枚举 ,然后直接计算 的方案数。
因此我们先钦定 内元素在排列中的位置,这里的方案数为 。然后剩下的位置里面, 必须直接被放在前三个空位中,此时我们只需要确定 的值即可,显然有 个可能的取值。最后把其余值域在 内的元素放入空位里,方案数为 。
最后的答案可以表示为:
为了方便,我们在 的时候暴力求解,这样就可以丢掉那个丑陋的 了。开始推式子:
注意到最内层仅与 有关,我们考虑变量代换的技巧:令 。
接下来就是好做的了,我们枚举 ,最内层的求和可以直接通过等差数列求和公式算。时间复杂度 。
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define fi first
#define se second
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define lc(x) (tr[x].ls)
#define rc(x) (tr[x].rs)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ldb;
typedef __int128 i128;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
const int N = 5e3 + 10;
const ll mod = 1e9 + 7;
ll n, x, ans1 = 0, ans2 = 0, inv[N], fac[N];
void init()
{
inv[1] = 1;
for(int i = 2; i < N; i++)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
fac[0] = 1;
for(int i = 1; i < N; i++)
fac[i] = (fac[i - 1] * i) % mod;
}
ll calc(ll l, ll r)
{
return (((x * (r - l + 1) % mod) -
((l + r) * (r - l + 1) / 2) % mod) % mod + mod) % mod;
}
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
init();
cin >> n >> x;
for(int i = x + 1; i <= n; i++)
ans1 = (ans1 + inv[i] * inv[i - 1] % mod) % mod;
ans1 = ans1 * fac[n] % mod * x % mod;
for(int g = 2; g <= n - 1; g++)
{
ans2 = (ans2 + inv[g + 1] * inv[g] % mod * inv[g - 1] % mod
* calc(max(1ll, x + 1 - g), min(x - 1, n - g)) % mod) % mod;
}
ans2 = ans2 * fac[n] % mod;
cout << (ans1 + ans2) % mod;
return 0;
}