#P9317. [EGOI 2022] SubsetMex / 子集 mex

[EGOI 2022] SubsetMex / 子集 mex

Background

Day 1 Problem A.

The statement is translated from EGOI2022 subsetmex

CC BY-SA 3.0

Problem Description

A multiset is a set in which elements may appear multiple times. For example, {0,0,1,2,2,5,5,5,8}\{0,0,1,2,2,5,5,5,8\} is a multiset.

You are given a multiset SS with values in N\N, and a target natural number nn (nSn\notin S). Your goal is to make nSn\in S by repeatedly performing the following operation some number of times:

  1. Choose a (possibly empty) subset TST\subseteq S, where TT contains no duplicate elements.
  2. Delete the elements of TT from SS. (If an element appears multiple times, remove only one copy.)
  3. Insert mex(T)\operatorname{mex}(T) into SS, where mex(T)\operatorname{mex}(T) is the smallest natural number that is not in TT. mex\operatorname{mex} stands for “minimum excluded value”.

You need to find the minimum number of operations needed to make nSn\in S.

Since S|S| can be very large, it is given as a list of length nn, (f0,fn1)(f_0,\ldots f_{n-1}), where fif_i denotes how many times ii appears in SS. (Recall that nn is the element we want to insert into SS.)

Input Format

The first line contains an integer tt, the number of test cases. Then each test case is described in two lines:

  • The first line of each test case contains an integer nn, the element to be inserted into SS.
  • The second line contains nn integers f0,f1,,fn1f_0,f_1,\ldots,f_{n-1}, describing the multiset SS as above.

Output Format

For each test case, output one line containing one integer, the minimum number of operations.

2
4
0 3 0 3
5
4 1 0 2 0
4
10

Hint

Explanation for Sample 11.

Initially, S={1,1,1,3,3,3}S=\{1,1,1,3,3,3\}, and the goal is to make 4S4\in S. We can do the following operations:

  1. Let T=T=\varnothing, then S={0,1,1,1,3,3,3}S=\{0,1,1,1,3,3,3\}.
  2. Let T={0,1,3}T=\{0,1,3\}, then S={1,1,2,3,3}S=\{1,1,2,3,3\}.
  3. Let T={1}T=\{1\}, then S={0,1,2,3,3}S=\{0,1,2,3,3\}.
  4. Let T={0,1,2,3}T=\{0,1,2,3\}, then S={3,4}S=\{3,4\}.

Constraints

For all testdata, 1t2001\le t\le 200, 1n501\le n\le 50, 0fi10160\le f_i\le 10^{16}.

  • Subtask 1 (55 points): n2n\le 2.
  • Subtask 2 (1717 points): n20n\le 20.
  • Subtask 3 (77 points): fi=0f_i=0.
  • Subtask 4 (99 points): fi1f_i\le 1.
  • Subtask 5 (2020 points): fi2×103f_i\le 2\times 10^3.
  • Subtask 6 (99 points): f01016f_0\le 10^{16} and j0,fj=0\forall j\ne 0,f_j=0.
  • Subtask 7 (1010 points): i,fi1016\exists i,f_i\le 10^{16} and ji,fj=0\forall j\ne i,f_j=0.
  • Subtask 8 (2323 points): No special constraints.

Input Format

Output Format

Translated by ChatGPT 5