#P12117. [NWRRC2024] Misère

[NWRRC2024] Misère

题目描述

Preˊfeˊrence\textit{Préférence} is a card game which is very popular in Eastern Europe. It is usually played with a 32-card deck, which consists of pip cards from 7 to 10, Jack, Queen, King, and Ace in each of the four suits: Spades, Clubs, Diamonds, and Hearts. In each round of the game, three players receive ten cards each, and two cards are left on the table as a talon. Then, a phase of auction happens, where players make their bids, which are obligations to take at least a certain number of tricks. A special case of a bid is a so-called miseˋre\textit{misère}, which is an obligation to take no tricks regardless of other players' moves.

In this task, we will consider a special modification of préférence which is played with a modified deck containing ABA\cdot B cards, where AA is a number of suits, and BB is the number of ranks in each suit. For example, the standard 32-card deck for the préférence game has A=4A = 4 suits and B=8B = 8 ranks. For convenience, we'll number the suits from 11 to AA, and the ranks from 11 to BB.

You need to solve a puzzle about this modification of préférence. In this modification, we'll say that a misère is guaranteed\textit{guaranteed} if for every suit, after we order the cards belonging to this suit in your hand by their rank as b1<b2<<bkb_1 < b_2 < \cdots < b_k (where kk is the number of cards of the suit in your hand), the following condition is satisfied: bi2i1b_i \le 2i - 1 for all ii from 11 to kk. If you don't have any cards of the suit (k=0k = 0), the condition is trivially satisfied.

You have nn cards in your hand, and you will be allowed to choose any xx cards you don't have and add them to your hand. Then, you must select any xx of your n+xn + x cards and drop them, leaving some nn cards in your hand. Your task is to find the smallest possible xx such that you can transform your hand to a guaranteed misère.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001 \le t \le 1000). The description of the test cases follows.

The first line of each test case contains three integers nn, AA, and BB, denoting the number of cards in your hand, the number of suits in the deck, and the number of ranks in the deck (1n50001 \le n \le 5000; 1A,B1091\le A, B\le 10^9).

The ii-th of the following nn lines contains two integers aia_i and bib_i and describes one card, where aia_i is the suit of the ii-th card, and bib_i is its rank (1aiA1 \le a_i \le A; 1biB1 \le b_i \le B). All the cards in your hand are distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

输出格式

For each test case, print the smallest non-negative integer value of xx such that you can transform your hand to a guaranteed misère by first adding xx cards that you don't have to your hand, and then dropping any xx cards from your hand.

It can be shown that such a value of xx always exists.

2
4 2 6
1 1
1 2
1 6
2 3
2 4 5
3 4
2 4
1
2