公式

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

claude5.5opus-high

Overview

When three ropes are chosen, the length of each cut piece becomes the greatest common divisor \(g\) of the three lengths. The problem asks us to count the number of triplets such that the total number of pieces \(\frac{a+b+c}{g}\) is even. This can be determined solely by how many times each length is divisible by 2, and we can count the combinations based on this.

Analysis

Number of Pieces

In order to cut ropes of lengths \(a, b, c\) into pieces of equal length without any remainder, the piece length must be a common divisor of \(a, b\), and \(c\). The maximum such length is \(g = \gcd(a, b, c)\).

In this case, the total number of pieces is: $\(\frac{a}{g} + \frac{b}{g} + \frac{c}{g}\)$ We only need to determine the parity of this value.

Parity Depends Only on Divisibility by 2

Let \(v(x)\) denote the number of times an integer \(x\) is divisible by 2. For example, \(v(12) = 2\) and \(v(7) = 0\).

  • The number of times \(g\) is divisible by 2 is the minimum among the three values. That is, \(m = \min(v(a), v(b), v(c))\).
  • \(\frac{a}{g}\) is odd if and only if \(v(a) = m\), which means \(a\) has the minimum count.
    • Dividing by the odd part of \(g\) does not change the parity. Therefore, it is sufficient to consider only the factors of 2.

From the above, among the three terms, only the terms corresponding to the ropes with the minimum count \(m\) become odd.

Condition for the Sum to be Even

The number of odd terms is the number of ropes whose \(v\) equals the minimum value \(m\). This count can be 1, 2, or 3. The sum is even if and only if the number of odd terms is even. Therefore, the condition is as follows:

Exactly two ropes share the minimum \(v\), and the remaining rope has a strictly greater \(v\).

Examples - \((2, 6, 4)\): \(v = (1, 1, 2)\). Here, \(g = 2\), and the number of pieces is \(1 + 3 + 2 = 6\), which is even. This satisfies the condition. - \((2, 6, 10)\): \(v = (1, 1, 1)\). Here, \(g = 2\), and the number of pieces is \(1 + 3 + 5 = 9\), which is odd. All three ropes have the minimum, so this does not satisfy the condition. - \((1, 2, 4)\): \(v = (0, 1, 2)\). Here, \(g = 1\), and the number of pieces is \(7\), which is odd. Only one rope has the minimum, so this does not satisfy the condition.

Problem with the Naive Approach

Checking all triplets takes \(O(N^3)\) time. For \(N = 200000\), this will not finish in time.

Algorithm

  1. For each \(A_i\), find \(v(A_i)\) and count the frequency \(\text{cnt}[v]\) of each value. Since \(A_i \le 10^6\), we have \(v \le 19\).
  2. Fix \(v\) as the minimum value and count. Since we choose 2 ropes with valuation \(v\) and 1 rope with a valuation greater than \(v\), the number of combinations to add is: $\(\binom{\text{cnt}[v]}{2} \times \left(\sum_{w > v} \text{cnt}[w]\right)\)$
  3. By iterating \(v\) from largest to smallest, we can maintain and update \(\sum_{w>v}\text{cnt}[w]\) as a running sum suffix.

Complexity

  • Time Complexity: \(O(N + \log \max A)\)
  • Space Complexity: \(O(\log \max A)\) (excluding input storage)

Implementation Notes

  • a & -a isolates the least significant set bit of \(a\). Taking its bit_length() - 1 yields the number of times \(a\) is divisible by 2, \(v(a)\).

  • Add cnt[v] to suffix after adding the combinations to the answer. This ensures that only values strictly greater than \(v\) are counted.

  • The answer can be as large as \(\binom{200000}{3} \approx 1.3 \times 10^{15}\). In Python, integer overflow is not an issue, but in other languages, make sure to use 64-bit integers.

    Source Code

import sys

def main():
    data = sys.stdin.buffer.read().split()
    n = int(data[0])
    cnt = [0] * 32
    for i in range(1, n + 1):
        a = int(data[i])
        v = (a & -a).bit_length() - 1
        cnt[v] += 1
    total = 0
    suffix = 0
    for v in range(31, -1, -1):
        c = cnt[v]
        total += c * (c - 1) // 2 * suffix
        suffix += c
    print(total)

main()

This editorial was generated by claude5.5opus-high.

投稿日時:
最終更新: