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.
Do not try to understand every possible pair first. Flip the question: what would a sequence with no valid pair have to look like?
Two even numbers immediately work. If is even and is even, then is even, because subtracting multiples of an even number keeps parity unchanged.
For two odd numbers , write . Since and are odd, the remainder is even exactly when is odd.
So if no valid pair exists, every larger odd number after an odd must have even. In particular, it cannot be , so the odd numbers must more than double each time.
A no-answer sequence has at most one even number, and its odd numbers grow like . Since , there can be at most odd numbers, so any elements force a valid pair. Just brute-force the first elements.
The obvious check is too big if you do it over the whole array. But this problem has a nice little trapdoor: if a sequence has no valid pair, it must be tiny.
We want and:
Let's understand what a sequence with no such pair would need to satisfy.
Even numbers are dangerous
Suppose there are two even elements .
Because is even, every multiple of is even. So when computing , we subtract an even number from .
Since is also even, the remainder is even.
So two even numbers always form a valid pair. Therefore, in any sequence with no answer, there can be at most one even number.
Also, if appears, then for any later element :
which is even. So a no-answer sequence of length at least cannot contain .
Odd numbers must grow stupidly fast
Now take two odd elements . Write:
where .
Since and are odd:
So for odd and odd , the pair is valid exactly when:
is odd.
If there is no valid pair, then for every earlier odd and later odd , this quotient must be even.
Since , the quotient is at least . It cannot be , because is odd. So it must be at least .
That means every later odd number must satisfy:
But is odd and is even, so actually:
So the odd elements in a no-answer sequence grow at least like this:
More formally, if the odd elements are , then and:
This gives:
Now use the constraint .
For :
So there can be at most odd numbers in a no-answer sequence.
And there can be at most one even number.
Therefore, any no-answer sequence has length at most:
That is the whole trick. If you look at any elements, there must be a valid pair. No segment tree. No weird DP. Just parity doing its job for once.
Algorithm
For each test case:
-1.Why is this enough?
So the brute force is tiny but complete.
Complexity
We check at most:
pairs per test case.
So the complexity is:
which is effectively constant per test case and easily fits.
#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<ll> a(n);
for (ll &x : a) cin >> x;
int m = min(n, 30);
bool found = false;
for (int i = 0; i < m && !found; i++) {
for (int j = i + 1; j < m; j++) {
if ((a[j] % a[i]) % 2 == 0) {
cout << a[i] << ' ' << a[j] << '\n';
found = true;
break;
}
}
}
if (!found) cout << -1 << '\n';
}
}#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<ll> a(n);
for (ll &x : a) cin >> x;
int m = min(n, 30);
bool found = false;
for (int i = 0; i < m && !found; i++) {
for (int j = i + 1; j < m; j++) {
if ((a[j] % a[i]) % 2 == 0) {
cout << a[i] << ' ' << a[j] << '\n';
found = true;
break;
}
}
}
if (!found) cout << -1 << '\n';
}
}