D - 三本のひもと偶数個の切れ端 / Three Strings and an Even Number of Pieces Editorial by admin Editorial for printing
claude5.5opus-highOverview
This problem asks us to count the number of triplets of ropes such that when the \(3\) ropes are cut into pieces of length equal to their greatest common divisor \(g\), the total number of pieces, \(\frac{a}{g}+\frac{b}{g}+\frac{c}{g}\), is even. We can determine this parity simply by checking how many times each length is divisible by \(2\).
Analysis
Length of the Pieces A piece length that cuts all \(3\) ropes without remainder must be a common divisor of the \(3\) lengths. The maximum such length is the greatest common divisor \(g=\gcd(a,b,c)\). Therefore, the total number of pieces is: $\(\frac{a}{g}+\frac{b}{g}+\frac{c}{g}\)$
Parity Depends Only on the “Number of Times Divisible by 2” Let \(v(x)\) denote the number of times an integer \(x\) is divisible by \(2\).
- Since \(g\) is a common divisor of the \(3\) ropes, \(v(g)=\min(v(a),v(b),v(c))\) holds.
- \(\frac{a}{g}\) is odd if and only if \(v(a)=v(g)\), which means \(v(a)\) is the minimum among the \(3\) ropes.
- Dividing by the odd part of \(g\) does not change the parity, so the odd part can be ignored.
Therefore, “the number of ropes whose \(v\) is equal to the minimum value” is the number of terms that become odd.
Condition for the Total to be Even The sum is even if and only if the number of odd terms is even. Since there is always at least \(1\) rope that attains the minimum value, we have the following cases:
- \(1\) rope attains the minimum \(\to\) \(1\) odd term \(\to\) the sum is odd
- \(2\) ropes attain the minimum \(\to\) \(2\) odd terms \(\to\) the sum is even
- \(3\) ropes attain the minimum \(\to\) \(3\) odd terms \(\to\) the sum is odd
Thus, the condition is that “exactly \(2\) ropes attain the minimum value of \(v\), and the remaining \(1\) rope has a \(v\) strictly greater than that.”
Concrete Example When \(A=(2,6,4)\), we have \(v=(1,1,2)\). Since exactly \(2\) ropes attain the minimum value of \(1\), this satisfies the condition. Indeed, \(g=2\), and the number of pieces is \(1+3+2=6\), which is even.
Issue with the Naive Approach Checking all triplets of ropes would take \(O(N^3)\) time. With \(N=200000\), this will not finish in time. Instead, we count the number of ropes for each value of \(v\) and count the combinations collectively using combinatorial formulas.
Algorithm
- For each \(A_i\), compute \(v(A_i)\) and count the frequency in \(\text{cnt}[v]\).
- Fixing \(v\) as the minimum value, the number of triplets for that \(v\) is:
- Ways to choose \(2\) ropes with valuation \(v\): \(\binom{\text{cnt}[v]}{2}\) ways
- Ways to choose \(1\) rope with valuation strictly greater than \(v\): \(\text{greater}\) ways
- In total, \(\binom{\text{cnt}[v]}{2}\times\text{greater}\) ways
- Iterate \(v\) from largest to smallest. While updating \(\text{greater}\) (the cumulative count of ropes with valuations strictly greater than \(v\)), sum up the values from step 2.
The answer is given by the following expression: $\(\sum_{v}\binom{\text{cnt}[v]}{2}\cdot\#\{i : v(A_i)>v\}\)$
Complexity
- Time complexity: \(O(N + \log \max A)\)
- Space complexity: \(O(\log \max A)\)
Implementation Notes
\(v(A_i)\) can be computed in \(O(1)\) using
__builtin_ctzll(the number of trailing zeros). Since \(A_i\ge 1\), there is no need to worry about undefined behavior from passing \(0\).The answer can be up to approximately \(\binom{200000}{3}\approx 1.3\times10^{15}\). It is necessary to use
long long.Add to the cumulative sum
greaterafter calculating the contribution of that \(v\). This correctly handles the “strictly greater” condition.Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
if(!(cin>>n))return 0;
vector<long long> cnt(64,0);
for(int i=0;i<n;i++){
long long a;cin>>a;
int v=__builtin_ctzll(a);
cnt[v]++;
}
long long ans=0;
long long greater=0;
for(int v=63;v>=0;v--){
ans+=cnt[v]*(cnt[v]-1)/2*greater;
greater+=cnt[v];
}
cout<<ans<<endl;
}
This editorial was generated by claude5.5opus-high.
posted:
last update: