本篇文章翻译自官方题解。由于笔者考场上没有通过此题,如有不当之处,还请在评论区指出。
还有这里因该比 Gold 那题难不少吧,建议评紫(?。
Suppose that we have already constructed some prefix of the permutation, and there are remaining non-redundant values below Bessie’s number and remaining non-redundant values above Bessie’s number (we define non-redundant numbers as numbers that Elsie could guess that would give her more information). Also, consider the two cases where Bessie has most recently said either “HI” or “LO”. Let be the case where Bessie has most recently said “LO” and let be the case where Bessie has most recently said “HI”. Then, the expected number of “HILO”s for the original problem is just the case where .
考虑 dp 状态,假设我们已经算到了某个位置,并且剩余了 个有效的小于 的值和 个有效的大于 的值(在这里有效指之后还可以被猜到的数)。此时,我们令状态中的 若前一次是LO, 若前一次是HI。这样的话,HILO出现的期望次数就是 对应的状态。
Thus, it is sufficient to count the expected number of “HILO”s for each and or . Call this quantity . Now, note that all redundant values do not affect the expected number of “HILOs”, so we ignore all of them. Then, there are possible values for the next number in the permutation, each of which occurs with probability .
因而,我们只需对每个 计算HILO出现的期望次数。定义状态为 。因为所有这些无效的数对答案都没有影响,所以说排列中的下一个数只有 种可能的值,每种数出现的概率都为 。
We will first consider the case where . Suppose that the next value in th.e permutation is less than of which there are possibilities. Denote these values as such that .
首先考虑 的边界情况,假设排列中的下一个数是比 小的 种情况之一,记这样的数为 ,满足 。
Suppose the next value in the permutation is for some . Then, Bessie says “LO” (because ) and there are remaining nonredundant values below Bessie’s number. The initial nonredundant values above Bessie’s number are still nonredundant, so the expected number of HILOs in the remaining sequence of values is .
设再往下的一个数为 且 ,那么 Bessie 将会说LO因为),然后将会剩下 个有效的值小于 ,而原本的 个大于 的有效的值将会保持不变,那期望下剩下的HILO的出现次数就是 。
Similarly, if we consider the values greater than which we label if we choose one of these values , Bessie says “HI” and there are remaining nonredundant values above Bessie’s number, so the expected number of HILOs in the remaining sequence of values is .
相似地,如果我们考虑的值是比 大的,即 中的一个数 ,Bessie 就会说HI,并剩下 个有效的值大于于 ,那期望下之后HILO的出现次数就是 。
So, we have the recurrence .
因此,我们得到转移方程 。
For computing we can apply the same technique. However, since Bessie has just said “HI”, if we now choose a value Bessie will then say “LO” and the number of “HILO”s increases by one. We choose a value less than with probability so the expected number of “HILO”s increases by
在计算 时,我们可以使用相似的技巧。不过由于Bessie可能刚说了HI,如果我们现在选的数 则 Bessie 就会说LO那么HILO的出现次数就会加一。因为选一个小于 的值的概率为 ,所以HILO的期望出现次数会增加 。
So, we have the recurrence
这样,我们得到转移方程 。
Now, we can compute every value of by iterating over all and using the above recurrences. Since there are dp states and we sum over smaller states, this leads to a time complexity of which is enough for the first two subtasks.
至此,我们可以通过遍历一遍 来计算得到 。因为有 个状态,每次转移 ,因此我们得到一个时间复杂度 的算法,可以通过前两个 subtask。
To speed this up, note that the bottleneck of the runtime comes from computing the terms like Now, suppose we are computing the value and want to find the value without using a loop. To do this, note that we have already computed the value of which required the value of Since we have already computed we can use the fact that which takes time instead of time to compute.
考虑优化转移,注意到算法的瓶颈在于计算像 的东西。我们计算 时希望不用遍历就得到 的值。注意到我们计算 时就已经用到 ,我们注意到 来把转移的时间复杂度由 优化到 。
Since we can now compute the terms of the DP in time each, our overall time complexity is now
这样我们就可以以 复杂度计算 个状态,最终时间复杂度为 ,可以通过此题。
翻译到此结束,以下提供优质 500B 代码。
// Nothing is Given, Everything is Earned.
#include<bits/stdc++.h>
using namespace std;
using L=long long;
const int N=5.1e3,P=1e9+7;
int dp[2][N][N],sum1[N],sum0[N],inv[N];
int main()
{
int n,x,y; cin>>n>>x; y=n-x;
inv[1]=1;
for(int i=2;i<=n;i++) inv[i]=1ll*(P-P/i)*inv[P%i]%P;
for(int j=0;j<=x;j++)
for(int k=0;k<=y;k++)
{
dp[0][j][k]=1ll*(sum1[j]+sum0[k])*inv[j+k]%P;
dp[1][j][k]=1ll*(sum1[j]+sum0[k]+j)*inv[j+k]%P;
(sum1[j]+=dp[1][j][k])%=P;
(sum0[k]+=dp[0][j][k])%=P;
}
L ans=dp[0][x][y];
for(L i=1;i<=n;i++) ans=1ll*ans*i%P;
cout<<ans<<endl;
return 0;
}