1995G - Ultra-Meow - [rating 2000]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
K1o0n gave you an array 𝑎 of length 𝑛, consisting of numbers 1,2,…,𝑛. Accept it? Of course! But what to do with it? Of course, calculate MEOW(𝑎).

Let MEX(𝑆,𝑘) be the 𝑘-th **positive** (strictly greater than zero) integer in ascending order that is not present in the set 𝑆. Denote MEOW(𝑎) as the sum of MEX(𝑏,|𝑏|+1), over all **distinct** subsets 𝑏 of the array 𝑎.

Examples of MEX(𝑆,𝑘) values for sets:

- MEX({3,2},1)=1, because 1 is the first positive integer not present in the set;
- MEX({4,2,1},2)=5, because the first two positive integers not present in the set are 3 and 5;
- MEX({},4)=4, because there are no numbers in the empty set, so the first 4 positive integers not present in it are 1,2,3,4.

Input

The first line contains a single integer 𝑡𝑡 (1≤𝑡≤10^4) — the number of test cases.

In a single line of each test case, an integer 𝑛 (1≤𝑛≤5000) is entered, the size of the array of gifted numbers.

It is guaranteed that the sum of 𝑛^2 over all test cases does not exceed 25⋅10^6.

Output

For each test case, output a single number — MEOW(𝑎). Since it may be very large, output it modulo 10^9+7.

题意

函数 MEX(S,k)MEX(S,k) 代表升序正整数列中第 kk 个不属于集合 SS 的数。

例:MEX([1,2,4],2)=5MEX([1,2,4],2)=5​,MEX([2,3],1)=1MEX([2,3],1)=1​,MEX([],1)=1MEX([],1)=1​​

现在给你一个正整数列 S=1,2,,nS=1,2,\dots,n ,求出 sSMEX(s,s+1)\sum_{\substack{s\in S}} MEX(s,\vert s\vert+1), 其中 s\vert s\vertss 的大小。

解法

SS 的长度为 nnss 的长度为 ll ,前 l+1l+1 个不属于集合 ss 的升序正整数列为 ttm=MEX(s,s+1)=MEX(s,l+1)=max(x)xtm = MEX(s,\vert s\vert+1) = MEX(s,l+1) = \max(x)_{x\in t} ,以下分情况讨论:

  1. 对于长度满足 2l+1n2*l+1 \le n 的子序列,其 mm 值不固定,对于给定的 ll 中每个可能的值 m[l+1,2l+1]m\in [l+1,2*l+1]mm 即为 tt 序列的最大值mm 左侧有 m1m-1 个空位,在其中放入长度为 lltt 剩余序列长度为 m1lm-1-lss 序列,排列个数为 (lm1)l \choose m-1mm 右侧有 nmn-m 个空位,在其中放入长度为 l(m1l)l-(m-1-l)ss 剩余序列,排列个数为 (l(m1l)nm)l - (m - 1 - l) \choose n - m,故每个 mm 值的排列个数为 (lm1)(l(m1l)nm){l \choose m-1} * {l - (m - 1 - l) \choose n - m },这部分的 mm 总和为 l=02l+1nm=l+1m2l+1m(lm1)(l(m1l)nm)\sum_{l=0}^{2*l+1\le n} \sum_{m=l+1}^{m\le 2*l+1} m * {l \choose m-1} * {l - (m - 1 - l) \choose n - m }
  2. 对于长度满足 2l+1>n2*l+1 \gt n 的子序列,序列 ss 和序列 tt 的总长度超过 nn , 则其 mm 值固定为 2l+12*l+1 ,长度为 ll 的子序列的个数为 (ln){l \choose n} ,这部分的 mm 总和为 2l+1>nln(2l+1)(ln)\sum_{2*l+1\gt n}^{l\le n} (2*l+1) * {l \choose n}

sSMEX(s,s+1)=l=02l+1nm=l+1m2l+1m(lm1)(l(m1l)nm)+2l+1>nln(2l+1)(ln)\sum_{\substack{s\in S}} MEX(s,\vert s\vert+1) = \sum_{l=0}^{2*l+1\le n} \sum_{m=l+1}^{m\le 2*l+1} m * {l \choose m-1} * {l - (m - 1 - l) \choose n - m } + \sum_{2*l+1\gt n}^{l\le n} (2*l+1) * {l \choose n}

https://codeforces.com/contest/1992/submission/271131606