D - 三本のひもと偶数個の切れ端 / Three Strings and an Even Number of Pieces 解説 by admin 印刷ページ
gpt-6-astra-mediumSummary
The length to which the chosen ropes are cut is the greatest common divisor of their three lengths. By classifying each length based on “how many times it is divisible by \(2\)”, we can count the valid combinations efficiently.
Analysis
Expressing the Total Number of Pieces as a Formula
Let the lengths of the chosen three ropes be \(a,b,c\).
To cut them into equal lengths without any remainder, the piece length must be a common divisor of \(a,b,c\). Since we use the maximum possible length, the piece length is \(g=\gcd(a,b,c)\).
Therefore, the total number of pieces is:
\(\displaystyle \frac{a}{g}+\frac{b}{g}+\frac{c}{g}\)
If we were to find the greatest common divisor for all triples, there would be \(O(N^3)\) combinations to examine, which will not run in time for \(N\leq 200000\).
Hence, instead of the total number itself, we focus only on its parity (even or odd).
Parity is Determined by the “Number of Times Divisible by \(2\)”
For a positive integer \(x\), let \(v_2(x)\) be the number of times \(x\) is divisible by \(2\). For example:
- Since \(12=2^2\times 3\), \(v_2(12)=2\)
- Since \(18=2\times 9\), \(v_2(18)=1\)
- Since \(7\) is odd, \(v_2(7)=0\)
Now, let \(k=\min(v_2(a),v_2(b),v_2(c))\). Since the number of factors of \(2\) in the greatest common divisor is also equal to this minimum, \(v_2(g)=k\).
Therefore, the parity of each length after dividing by the greatest common divisor is determined as follows:
- If \(v_2(a)=k\), then \(a/g\) is odd
- If \(v_2(a)>k\), then \(a/g\) is even
The same applies to \(b,c\). In other words, the number of ropes that become odd after division is equal to the number of ropes that achieve the minimum value of \(v_2\).
It Suffices to Have Exactly 2 Ropes Achieving the Minimum Value
The sum of three integers is even if and only if the number of odd integers is \(0\) or \(2\).
However, because there is always at least one rope achieving the minimum value of \(v_2\), the number of odd pieces after division can never be \(0\).
Therefore, the required condition can be rephrased as follows:
Among the three ropes, exactly two ropes achieve the minimum value of \(v_2\).
For example, when the lengths are \(6,10,12\), their \(v_2\) values are \(1,1,2\), respectively. Since exactly two ropes achieve the minimum value, this satisfies the condition. Indeed, the greatest common divisor is \(2\), and the total number of pieces is \(3+5+6=14\).
Note that this cannot be determined solely by the parity of the sum of the original lengths. For example, for \(2,2,2\), the sum of the lengths is even, but dividing by their greatest common divisor yields a total of \(3\) pieces.
Algorithm
Let \(\mathrm{cnt}[k]\) be the number of ropes such that \(v_2(A_i)=k\).
In a valid choice where the minimum value is \(k\), we choose:
- 2 ropes with \(v_2\) equal to \(k\)
- 1 rope with \(v_2\) strictly greater than \(k\)
Letting \(\mathrm{higher}\) be the number of ropes with \(v_2\) strictly greater than \(k\), the number of ways to choose them is:
\(\displaystyle \binom{\mathrm{cnt}[k]}{2}\times \mathrm{higher} =\frac{\mathrm{cnt}[k](\mathrm{cnt}[k]-1)}{2}\times \mathrm{higher}\)
The detailed procedure is as follows:
- For each \(A_i\), compute \(v_2(A_i)\) and count it in \(\mathrm{cnt}\).
- Initialize the answer to \(0\) and \(\mathrm{higher}\) to \(N\).
- Process \(k\) in ascending order:
- Subtract \(\mathrm{cnt}[k]\) from \(\mathrm{higher}\). This makes \(\mathrm{higher}\) equal to the number of ropes with \(v_2\) strictly greater than \(k\).
- Add \(\mathrm{cnt}[k](\mathrm{cnt}[k]-1)/2\times \mathrm{higher}\) to the answer.
- Output the answer.
Since the minimum value \(k\) is uniquely determined for every valid combination, we can count without duplicates or omissions.
Complexity
In the provided code, \(v_2(A_i)\) is computed in constant time using a built-in function, and 32 categories are scanned.
- Time complexity: \(O(N+32)=O(N)\)
- Space complexity: \(O(32)=O(1)\)
The input lengths do not need to be stored because they can be counted on the fly.
Implementation Notes
Computing \(v_2(a)\) with
__builtin_ctz(a)
This function returns the number of consecutive trailing zeros in the binary representation of an integer. This matches the number of times it is divisible by \(2\). Passing \(0\) results in undefined behavior, but here \(A_i\geq 1\), so there is no issue.Using 64-bit Integers
The answer can be as large as \(\binom{N}{3}\), which does not fit in a 32-bit integer. In the code,cnt,higher, andansare defined aslong long, ensuring that intermediate multiplications are also performed using 64-bit integers.Updating
higherBefore Adding to the Answer
Choosing 3 ropes from the same category does not satisfy the condition. By performinghigher -= cnt[k]first, we guarantee that the third rope is chosen from a category with a strictly larger \(v_2\).Ropes of the Same Length are Counted Distinctly
\(\binom{\mathrm{cnt}[k]}{2}\) is the number of ways to pick two ropes with distinct indices belonging to that category. Even if ropes have the same length, they are properly distinguished.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
array<long long, 32> cnt{};
for (int i = 0; i < N; ++i) {
unsigned int a;
cin >> a;
++cnt[__builtin_ctz(a)];
}
long long ans = 0;
long long higher = N;
for (int k = 0; k < 32; ++k) {
higher -= cnt[k];
ans += cnt[k] * (cnt[k] - 1) / 2 * higher;
}
cout << ans << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: