公式

D - 三本のひもと偶数個の切れ端 / Three Strings and an Even Number of Pieces 解説 by admin 印刷ページ

gpt-6-astra-medium

Overview

We rephrase the condition that the total number of pieces obtained from three ropes is even into “how many times each rope’s length is divisible by \(2\)”. By classifying the ropes based on this count, we can count the satisfying combinations in bulk.

Analysis

The Length of a Piece is the Greatest Common Divisor

Let the lengths of the chosen ropes be \(a,b,c\).

To cut them into pieces of the same length without any remainder, the piece length must be a common divisor of all of \(a,b,c\). The maximum possible length is their greatest common divisor, \(g=\gcd(a,b,c)\).

Therefore, the total number of pieces is

\(\displaystyle \frac{a}{g}+\frac{b}{g}+\frac{c}{g}\).

A method that computes the greatest common divisor for all triplets of ropes would require checking \(\binom{N}{3}\) combinations, which will not run in time for \(N\leq 200000\). Instead of the greatest common divisor itself, we focus only on the information that determines the parity of the total number of pieces.

Focusing on the Number of Times Divisible by \(2\)

For a positive integer \(x\), let \(v_2(x)\) denote the number of times \(x\) is divisible by \(2\). For example:

  • Since \(12=2^2\times 3\), \(v_2(12)=2\)
  • Since \(40=2^3\times 5\), \(v_2(40)=3\)
  • Since \(7\) is odd, \(v_2(7)=0\)

For the three chosen ropes, let

\(m=\min(v_2(a),v_2(b),v_2(c))\).

Then, the number of factors of \(2\) contained in the greatest common divisor \(g\) is also \(m\).

Thus, the parity of \(a/g\) is determined as follows:

  • If \(v_2(a)=m\), all factors of \(2\) are cancelled out, so it is odd
  • If \(v_2(a)>m\), a factor of \(2\) remains, so it is even

The same holds for \(b/g,c/g\). In other words, the ropes that yield an odd number of pieces are precisely those that achieve the minimum value of \(v_2\).

Exactly Two Ropes Must Attain the Minimum Value

The sum of three integers is even if and only if the number of odd integers is either \(0\) or \(2\).

However, since at least one rope must attain the minimum value, there is always at least one odd term, meaning there can never be \(0\) odd numbers. Therefore, the required condition can be rephrased as follows:

Exactly two ropes attain the minimum value of \(v_2\), and the remaining one has a strictly greater value.

For example, if the lengths are \(12,20,40\), their \(v_2\) values are \(2,2,3\), respectively. The greatest common divisor is \(4\), so the total number of pieces is

\(3+5+10=18\),

which is indeed even.

Note that this cannot be determined solely by the parity of the original lengths. Even if all lengths are even, the parities after dividing by the greatest common divisor depend on how many times each is divisible by \(2\).

Algorithm

Let counts[k] be the number of ropes such that \(v_2(A_i)=k\).

A valid combination where the minimum value is \(k\) can be formed by:

  • Choosing 2 ropes from the counts[k] ropes
  • Choosing 1 rope from the ropes with \(v_2\) strictly greater than \(k\)

Letting higher be the number of ropes with \(v_2\) greater than \(k\), the number of such combinations is

\(\displaystyle \binom{\text{counts}[k]}{2}\times \text{higher}\).

To compute this efficiently, we process \(k\) in descending order:

  1. For each \(A_i\), compute \(v_2(A_i)\) and count the occurrences in counts.
  2. Initialize the answer answer and the count of processed ropes higher to \(0\).
  3. Iterate through counts in descending order of indices. Let \(c\) be the current count, and perform the following:
    • Add \(c(c-1)/2\times\text{higher}\) to answer.
    • Add \(c\) to higher.
  4. Output answer.

Since the minimum value \(k\) is unique for each valid combination, all valid combinations are counted without duplicates.

Complexity

Let \(M=\max A_i\), and assume arithmetic operations on integers within the constraints take constant time.

  • Time Complexity: \(O(N+\log M)\)
  • Space Complexity: \(O(N+\log M)\)
    Using only the array for counting would require \(O(\log M)\), but the provided code also uses \(O(N)\) space to read all input at once and store the split data.

Implementation Details

  • Computing \(v_2(a)\) with bitwise operations

a & -a isolates the greatest power of \(2\) dividing \(a\), which is \(2^{v_2(a)}\).

Since the bit_length() of \(2^k\) is \(k+1\),

  (a & -a).bit_length() - 1

computes \(v_2(a)\). Since \(A_i\geq 1\), we do not need to consider the case of \(0\).

  • Array size

Since \(A_i\leq 10^6<2^{20}\), \(v_2(A_i)\) is at most \(19\). The code prepares an array of length \(21\) with some margin.

  • Order of updating higher

Perform higher += c after adding to the answer. If updated beforehand, ropes with \(v_2\) equal to the current value would mistakenly be counted as “ropes with a greater value”.

  • Integer type for the answer

The answer can be up to around \(\binom{N}{3}\), which exceeds the range of a 32-bit integer. In Python, integers have arbitrary precision, so they can be handled directly.

Source Code

import sys


def main():
    data = map(int, sys.stdin.buffer.read().split())
    next(data)

    counts = [0] * 21
    for a in data:
        counts[(a & -a).bit_length() - 1] += 1

    answer = 0
    higher = 0
    for c in reversed(counts):
        answer += c * (c - 1) // 2 * higher
        higher += c

    print(answer)


if __name__ == "__main__":
    main()

This editorial was generated by gpt-6-astra-medium.

投稿日時:
最終更新: