Official

D - 三本のひもと偶数個の切れ端 / Three Strings and an Even Number of Pieces Editorial by admin Editorial for printing

imported

Editorial

Let the chosen lengths be \(a,b,c\). The maximum length of each piece is \(g=\gcd(a,b,c)\), and the number of pieces is \((a+b+c)/g\). One must not simply check whether \(a+b+c\) is even. For example, when \(a=b=c=2\), the sum is even, but the number of pieces is \(3\).

Let \(M=\max A_i\), and record the number of ropes of length \(x\) in \(f_x\). We compute the following two arrays in descending order of \(d\):

  • \(T_d\): The number of ways to choose \(3\) ropes such that the greatest common divisor of their lengths is exactly \(d\).
  • \(E_d\): The number of such ways in which the total number of pieces is even.

Among the ropes whose lengths are multiples of \(d\), let \(e\) be the count of ropes whose length divided by \(d\) is even, and \(o\) be the count of ropes whose length divided by \(d\) is odd. These can be counted by iterating over \(f_d,f_{2d},f_{3d},\ldots\).

From these ropes, there are \(\binom{e+o}{3}\) ways in total to choose \(3\) ropes. Moreover, the sum of lengths divided by \(d\) is even when we choose either \(0\) or \(2\) ropes with an odd quotient, so the number of such choices is

\[ B_d=\binom e3+e\binom o2. \]

Here, we define \(\binom xr=0\) when \(x<r\).

Excluding the triplets whose greatest common divisor is a strict multiple of \(d\), we have

\[ T_d=\binom{e+o}{3}-\sum_{k\geq2,\ kd\leq M}T_{kd}. \]

When computing \(E_d\), pay attention to the parity of the triplets being excluded. For a triplet whose greatest common divisor is \(kd\), if the actual number of pieces is \(q\), then the sum of lengths divided by \(d\) is \(kq\).

  • If \(k\) is even, \(kq\) is even regardless of the parity of \(q\). Since all such triplets are included in \(B_d\), we subtract \(T_{kd}\).
  • If \(k\) is odd, the parity of \(kq\) matches that of \(q\). Since \(E_{kd}\) such triplets are included in \(B_d\), we subtract \(E_{kd}\).

Therefore,

\[ E_d=B_d- \sum_{\substack{k\geq2,\ kd\leq M\\k\text{ is even}}}T_{kd} - \sum_{\substack{k\geq3,\ kd\leq M\\k\text{ is odd}}}E_{kd}. \]

Since all triplets are partitioned without overlap by their greatest common divisor, the answer is \(\sum_{d=1}^M E_d\).

Correctness

The positive integer lengths into which ropes can be cut without remainder correspond to the common divisors of the three chosen lengths, so the maximum length is their greatest common divisor. The multiple-based counting above includes each triplet whose greatest common divisor is a multiple of \(d\) exactly once.

Assume that \(T\) and \(E\) are correct for all indices greater than \(d\). The formula for \(T_d\) subtracts each triplet whose greatest common divisor is \(kd\) \((k\geq2)\) exactly once, so what remains are precisely the triplets whose greatest common divisor is exactly \(d\). The formula for \(E_d\) also subtracts the triplets with strict multiples included in \(B_d\) without omission or excess, based on the parity of \(kq\) described above. The remaining triplets have a greatest common divisor of \(d\), and the sum divided by \(d\) is even. Therefore, \(T_d\) and \(E_d\) are correct. By mathematical induction in descending order from \(d=M\), all elements are correct, and the final sum yields the desired answer.

Complexity and Integer Range

Since we perform \(O(M/d)\) operations for each \(d\), the overall time complexity is \(O(N+M\log M)\), and the space complexity is \(O(N+M)\). The implementation also retains the input array.

The answer and the number of triplets are at most \(\binom{200000}{3}<1.34\times10^{15}\). Even the product before division in combination calculations is at most \(200000^3=8\times10^{15}\), which fits within a signed \(64\)-bit integer. In the implementation, use long long for counts, combinations, arrays, and the answer.

posted:
last update: