E - 最短の棒が二本ある区間 / Intervals with Two Shortest Sticks 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
The condition for a valid subarray can be rephrased as: “a subarray that contains exactly \(2\) elements equal to the greatest common divisor (GCD) of the subarray.”
By sweeping the right endpoint from left to right and grouping subarrays with the same GCD, we can count them efficiently.
Analysis
Rephrasing the Condition Using the Greatest Common Divisor
Let \(m\) be the minimum value in a subarray, and let \(g\) be the greatest common divisor of all elements in the subarray.
If all sticks can be cut into pieces of length \(m\) without remainder, every element is a multiple of \(m\). Furthermore, since the subarray contains an element of value \(m\), the GCD must be \(g=m\).
Conversely, if the subarray contains an element of value \(g\), by the properties of the GCD, every element is a positive multiple of \(g\). Therefore, the minimum value is \(g\), and all sticks can be cut into pieces of length \(g\) without remainder.
Therefore, the desired condition can be rephrased as follows:
When the GCD of the subarray is \(g\), there are exactly \(2\) elements with value \(g\).
For example, the subarray \([6,12,6]\) has a GCD of \(6\) and contains two \(6\)s, so it satisfies the condition. On the other hand, \([6,10,6]\) has a GCD of \(2\), but it does not satisfy the condition because there are no elements with value \(2\).
Checking All Subarrays Directly Exceeds the Time Limit
There are \(N(N+1)/2\) subarrays in total. Even if we maintain and update the GCD incrementally, enumerating subarrays one by one would require \(O(N^2)\) operations, which will not run in time for \(N\leq 200000\).
Therefore, for a fixed right endpoint, we group subarrays by their GCD.
Subarrays with the Same Right Endpoint Have Few Distinct GCDs
Fix the right endpoint and extend the subarray by moving the left endpoint from right to left.
The GCD after adding an element is a divisor of the previous GCD. Moreover, whenever the value changes, it becomes a proper divisor, so it is at most halved.
Therefore, letting \(V=\max_i A_i\), there are only \(O(\log V)\) distinct GCD values that appear among subarrays sharing the same right endpoint.
By managing these in groups, we only need to update a small number of states for each right endpoint.
Also Maintaining the Count of Elements Equal to the GCD
Even among subarrays with the same GCD, the number of occurrences of that value may differ. Therefore, for each GCD \(g\), we maintain the following:
| Variable | Meaning |
|---|---|
g |
GCD of the subarray |
total |
Total number of subarrays belonging to this state |
count0 |
Number of subarrays with \(0\) elements of value \(g\) |
count1 |
Number of subarrays with \(1\) element of value \(g\) |
count2 |
Number of subarrays with \(2\) elements of value \(g\) |
Subarrays with \(3\) or more elements of value \(g\) are not included in count0 through count2, but only in total.
However, we must not discard such subarrays entirely. This is because if the GCD changes later, the count of occurrences of the new GCD may decrease.
For example, appending \(3\) to \([6,6,6]\) changes the GCD to \(3\), which appears only \(1\) time. Appending another \(3\) will make it a valid subarray that satisfies the condition.
Algorithm
Let prev be the list of states where the right endpoint is at the previous position, and curr be the list of states where the right endpoint is at the current position.
Each time we process a new element \(x=A_i\), we perform the following steps:
1. Add a Subarray of Length \(1\)
The subarray \([x]\) has a GCD of \(x\), and there is \(1\) element of that value.
Therefore, we add the following state to curr:
g = xtotal = 1count0 = 0count1 = 1count2 = 0
2. Append \(x\) to Each State from the Previous Step
Let \(h\) be the GCD of a state in prev. After appending \(x\), the new GCD is \(g=\gcd(h,x)\).
The number of subarrays total does not change. Updating each count can be divided into the following cases:
Case 1: The GCD Changes (\(g\neq h\))
In this case, \(g<h\).
Since all elements in the previous subarray are positive multiples of \(h\), every element is at least \(h\). Therefore, none of the elements in the previous subarray can have value \(g\).
Thus, the number of occurrences of the new GCD \(g\) is determined solely by the newly appended \(x\).
- If \(g=x\), the count becomes \(1\) for all subarrays
→ Newcount1 = total - If \(g\neq x\), the count becomes \(0\) for all subarrays
→ Newcount0 = total
All other counters are set to \(0\).
By using total here, subarrays that previously contained \(3\) or more elements equal to the GCD are also handled correctly.
Case 2: The GCD Does Not Change, and \(x=g\)
The number of elements equal to the GCD increases by one.
- New
count0 = 0 - New
count1 = old count0 - New
count2 = old count1
Subarrays belonging to the old count2 now have \(3\) occurrences, so they are not included in the new count2. However, they remain in total.
Case 3: The GCD Does Not Change, and \(x\neq g\)
The number of elements equal to the GCD does not increase, so count0, count1, and count2 are carried over as they are.
3. Merge States with the Same GCD
If the updated GCD is the same as that of the last element in curr, merge them by adding up total and each counter. If it is different, append it to the end as a new state.
States are ordered from shorter subarrays to longer subarrays. Their GCDs maintain the property that each GCD divides the preceding one. Even after appending \(x\), this property is preserved, so states with the same GCD will always appear contiguously.
Therefore, we can merge them by comparing only with the last element; an associative array is not needed.
4. Add the Answer for Subarrays Ending at the Current Right Endpoint
For all states in curr, add count2 to the answer.
Then, swap prev and curr, and proceed to the next element.
Every subarray is considered exactly once when processing its right endpoint. Since count2 in each state represents the number of subarrays satisfying the condition, the sum of these values is the answer.
Complexity
Let \(V=\max_i A_i\).
- Time Complexity: \(O(N(\log V)^2)\)
- The number of states maintained for each right endpoint is \(O(\log V)\).
- For each state, we compute the GCD once, which takes \(O(\log V)\) time.
- The number of GCD computations themselves is \(O(N\log V)\).
- Space Complexity: \(O(\log V)\)
- We only need to store the previous and current state lists. There is no need to store the entire array.
Implementation Notes
Use a 64-bit integer for the answer.
The maximum possible number of subarrays, \(N(N+1)/2\), does not fit in a 32-bit integer. In C++, uselong long.Do not substitute
totalwithcount0 + count1 + count2.
Subarrays with \(3\) or more elements equal to the GCD will still be needed in future updates.When the GCD changes, do not carry over the old counts.
Since the old subarrays contain no elements equal to the new GCD, reset the counts usingtotal.Merge states with the same GCD immediately.
This keeps the number of states bounded by \(O(\log V)\).Source Code
#include <bits/stdc++.h>
using namespace std;
struct State {
int g;
int total;
int count0, count1, count2;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<State> prev, curr;
prev.reserve(64);
curr.reserve(64);
long long answer = 0;
for (int i = 0; i < N; ++i) {
int x;
cin >> x;
curr.clear();
curr.push_back({x, 1, 0, 1, 0});
for (const State& s : prev) {
int g = std::gcd(s.g, x);
State t{g, s.total, 0, 0, 0};
if (g != s.g) {
if (g == x) {
t.count1 = s.total;
} else {
t.count0 = s.total;
}
} else if (g == x) {
t.count1 = s.count0;
t.count2 = s.count1;
} else {
t.count0 = s.count0;
t.count1 = s.count1;
t.count2 = s.count2;
}
if (curr.back().g == g) {
State& back = curr.back();
back.total += t.total;
back.count0 += t.count0;
back.count1 += t.count1;
back.count2 += t.count2;
} else {
curr.push_back(t);
}
}
for (const State& s : curr) {
answer += s.count2;
}
prev.swap(curr);
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: