Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Donβt start by thinking about all subarrays. First ask a much meaner question: how long can a good subarray even be? Let of the subarray, , , and use the condition .
Since is the gcd, it divides every element of the subarray. In particular, and . But also . So how many multiples of can exist in the closed interval ?
Exactly: the only multiples of in are and . So every element of a good subarray must be either or . But the array is a permutation, so all values are distinct. That kills almost everything.
A length- subarray is never good because , while . Therefore every good subarray must have length exactly . So now the whole problem is just counting adjacent pairs such that
If you want an equivalent number-theory form, let and . Then So the pair is good iff , i.e. iff . In practice, though, just check for each adjacent pair and count them. Done. Nice little trap problem.
The main trick is realizing this problem is way less about subarrays and way more about a brutal structural constraint.
Take any good subarray, and let
The definition says
But because is the gcd, it divides every element of the subarray. In particular,
Since , both endpoints are multiples of .
Now look at the interval . How many multiples of can lie inside an interval of length exactly ?
Only two: the endpoints themselves.
So every element in the subarray, being divisible by and lying between and , must be either
That already means a good subarray contains at most two distinct values.
But the array is a permutation, so all values are distinct. Therefore a good subarray can have at most elements.
A subarray of length has
while
So length is never good.
Therefore:
Every good subarray has length exactly .
Thatβs the whole damn problem, honestly.
Now we only need to check adjacent pairs . A pair is good iff
So the answer is simply
If you like the number-theory flavor, let and . Then
Hence the pair is good iff
So good pairs are exactly pairs of the form
for some integers . Examples: , , , , etc.
But for implementation, just checking gcd directly is cleaner and more idiot-proof.
For each test case:
ans = 0.ans.ans.We proved:
So counting exactly those adjacent pairs gives the exact answer.
No hidden gremlins. No segment trees. No black magic. Just a clean observation and a loop.
For each test case we do gcd computations, so the complexity is
where is the value size. Since the total over all test cases is at most , this is easily fast enough.
Memory usage is
for storing the permutation, or even extra beyond the array.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> p(n);
for (int i = 0; i < n; ++i) cin >> p[i];
ll ans = 0;
for (int i = 0; i + 1 < n; ++i) {
if (std::gcd(p[i], p[i + 1]) == abs(p[i] - p[i + 1])) {
++ans;
}
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> p(n);
for (int i = 0; i < n; ++i) cin >> p[i];
ll ans = 0;
for (int i = 0; i + 1 < n; ++i) {
if (std::gcd(p[i], p[i + 1]) == abs(p[i] - p[i + 1])) {
++ans;
}
}
cout << ans << '\n';
}
return 0;
}