公式

D - カードの積合わせ / Card Stacking 解説 by admin

Claude 4.6 Opus (Thinking)

Overview

Given \(N\) cards, determine whether there exists a combination of selecting 2 or more cards such that the product of the front values equals the product of the back values. If such a combination exists, find the minimum number of cards.

Analysis

Rephrasing the Problem

For each card \(i\), consider the “ratio” \(\frac{P_i}{Q_i}\) of the front value \(P_i\) to the back value \(Q_i\). The condition that the product of front values equals the product of back values for selected cards can be rephrased as:

\[\prod_{i \in S} P_i = \prod_{i \in S} Q_i \iff \prod_{i \in S} \frac{P_i}{Q_i} = 1\]

In other words, the problem reduces to finding a subset whose product of ratios equals 1.

Checking Constraints

Note the constraint \(N \leq 22\). Enumerating all subsets gives \(2^{22} \approx 4 \times 10^6\) possibilities, which can be processed sufficiently fast.

However, in the code presented here, we search “in order of increasing size,” so we terminate immediately upon finding the smallest \(|S|\) that satisfies the condition. In the worst case, all subsets are examined, but this is still fast enough even for \(N = 22\).

About Product Overflow

Since \(P_i, Q_i \leq 100\) and at most 22 cards can be selected, the product can reach up to \(100^{22} = 10^{44}\). However, Python natively supports arbitrary-precision integers, so there is no concern about overflow.

Algorithm

  1. Try card subset sizes \(k\) in increasing order: \(2, 3, \ldots, N\).
  2. For each size \(k\), enumerate all combinations of choosing \(k\) cards from \(N\).
  3. For each combination, compute the product of front values \(\prod P_i\) and the product of back values \(\prod Q_i\), and check whether they are equal.
  4. As soon as an equal combination is found, output \(k\) and terminate.
  5. If no valid combination is found after trying all sizes, output -1.

Example: If the cards are \((P_1, Q_1) = (2, 3)\) and \((P_2, Q_2) = (3, 2)\), selecting both cards gives a front product of \(2 \times 3 = 6\) and a back product of \(3 \times 2 = 6\), which are equal, so the answer is \(2\).

Complexity

  • Time complexity: \(O\left(\sum_{k=2}^{N} \binom{N}{k} \cdot k\right) = O(N \cdot 2^N)\)
    • We enumerate all subsets of size 2 or more, performing \(k\) multiplications for each combination. For \(N = 22\), this amounts to roughly \(9 \times 10^7\) operations, which is well within the time limit.
  • Space complexity: \(O(N)\)
    • Only the space for storing card information and generating combinations is needed.

Implementation Notes

  • Searching from smallest size: By iterating with range(2, N+1) and checking from the smallest \(k\) first, the first solution found is automatically the minimum number of cards. We can immediately exit with return as soon as a solution is found.

  • Python’s itertools.combinations: This concisely enumerates all combinations of choosing \(k\) elements from \(N\), making the implementation very simple.

  • Python’s arbitrary-precision integers: Although the products can become extremely large, Python has no integer overflow, so no special handling is needed.

    Source Code

from itertools import combinations

def solve():
    N = int(input())
    cards = []
    for _ in range(N):
        p, q = map(int, input().split())
        cards.append((p, q))
    
    for size in range(2, N + 1):
        for combo in combinations(range(N), size):
            prod_p = 1
            prod_q = 1
            for i in combo:
                prod_p *= cards[i][0]
                prod_q *= cards[i][1]
            if prod_p == prod_q:
                print(size)
                return
    
    print(-1)

solve()

This editorial was generated by claude4.6opus-thinking.

投稿日時:
最終更新: