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.
Fix a candidate beauty . Each original number is basically its own little problem: either it can be made compatible with , or you must spend an erase on it.
A number already divisible by is compatible for free. The only interesting case is a number that is not divisible by .
If one split immediately creates two kept multiples of , say and , then the discarded middle part must satisfy . Since the smallest positive multiple is , the tiny split only handles , which is already divisible by .
For a non-multiple below , you cannot fit kept multiples and because the middle must be at least , giving total at least . Kept multiples and only give exactly , also divisible. So non-multiples below are doomed.
The key test is: is good for iff or . Therefore bad numbers for are exactly values that are not . Use frequencies and prefix sums to check every in , scanning downward.
Fix some candidate answer . We want to know whether we can make every remaining number divisible by after using at most erases.
The important part: split operations never combine different original numbers. So each starting value can be judged independently. If it can be transformed into only multiples of , we keep it. If not, we erase it. Then is feasible iff at most values are bad.
Call a value good for if, using only split operations, it can end as a collection where every number is divisible by .
Obviously, if , then is good: do nothing.
Now suppose is not divisible by . When can a split help?
A split chooses
then keeps and while discarding .
If after this split both kept numbers are already multiples of , the smallest possible kept multiple is . The discarded middle must be at least the smaller kept value.
So if the kept numbers are and , the smallest possible total is
If the kept numbers are and , the total is forced to be
which is already divisible by , so it does not help with non-multiples.
That gives the bad lower range:
Any non-multiple is impossible to fix.
There is no room to split it into two useful kept multiples. And recursively splitting does not magically help: any smaller good non-multiple would need the same kind of first useful split, so this lower-bound argument blocks the whole chain.
Now we show the other direction. Let
If , then .
Split into
The sum is
The order condition also holds:
For , the right side is , and if ; if , it is even easier. For larger , there is obviously enough room.
The split keeps the smallest and largest values, which are
Both are divisible by . The annoying middle part gets thrown away. Nice. The operation is basically a very legal trash chute.
So the full characterization is:
A value is bad exactly when
Since all , we can keep a frequency array. For a fixed , count all values below , then subtract the divisible ones below .
The positive multiples of that are strictly below are only
So:
Use prefix sums over values to get in :
Then is feasible iff
Scan from down to , and the first feasible is the maximum possible beauty.
For each test case:
Total over all test cases is
which is comfortably within limits.
#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, k;
cin >> n >> k;
vector<int> freq(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
freq[x]++;
}
vector<int> pref(n + 1, 0);
for (int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + freq[i];
}
int ans = 1;
for (int d = n; d >= 1; d--) {
int upto = min(n, 4 * d - 1);
int bad = pref[upto];
bad -= freq[d];
if (2 * d <= n) bad -= freq[2 * d];
if (3 * d <= n) bad -= freq[3 * d];
if (bad <= k) {
ans = d;
break;
}
}
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, k;
cin >> n >> k;
vector<int> freq(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
freq[x]++;
}
vector<int> pref(n + 1, 0);
for (int i = 1; i <= n; i++) {
pref[i] = pref[i - 1] + freq[i];
}
int ans = 1;
for (int d = n; d >= 1; d--) {
int upto = min(n, 4 * d - 1);
int bad = pref[upto];
bad -= freq[d];
if (2 * d <= n) bad -= freq[2 * d];
if (3 * d <= n) bad -= freq[3 * d];
if (bad <= k) {
ans = d;
break;
}
}
cout << ans << '\n';
}
return 0;
}