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.
Temporarily forbid the initial increments. For one number processed alone, write down how many subtractions and divisions are needed using its binary representation.
Define . The potential decreases by one for an odd subtraction and by the length of the divided prefix for an even-prefix operation. Can Bob force every future prefix to have length one once any odd element exists?
Yes: Bob keeps an odd element at position whenever at least two elements remain. Therefore, if , the first turns divide the whole array, and afterward every element is handled individually. The fixed-array value is
Bring the increments back and fix some . Require every final value to be divisible by . The choices then become independent: The answer is ; requiring the minimum trailing-zero count to equal is unnecessary.
Let and let be the smallest multiple of not below . Any costs strictly more than simply choosing , so only enumerate Checking is enough under the given bounds.
For , define
and define .
This is exactly the number of operations needed to erase one isolated number:
1. Thus decreases by exactly .For example, has
corresponding to
Now use the potential
An odd subtraction decreases by . If Alice divides an even prefix of length , every affected number loses one unit of , so decreases by exactly .
Therefore, every Alice turn decreases by at least one. The only way Bob can attain the maximum possible number of turns is to make every divided prefix have length one.
Suppose the array currently contains an odd number.
Whenever at least two elements remain, Bob can arrange for position to contain an odd number using one swap:
Afterward:
If gets erased, the stopper merely shifts to position $1`; Bob can reposition it on his next turn. Once only one element remains, every operation obviously affects one element.
Thus, from any state containing an odd value, Bob can force every turn to decrease by exactly one. The remaining number of Alice turns is consequently exactly .
That odd number is basically Bob's traffic cone: stupidly simple, annoyingly effective.
Let
where is the number of trailing zero bits of .
Every number is divisible by . Hence, during the first Alice turns, the entire array is even and Bob cannot prevent Alice from dividing every element. After those global divisions, at least one number is odd, so Bob's stopper strategy becomes available.
Because
the potential remaining after those divisions is
The total number of game turns is therefore
=\sum_iF(a_i)-(n-1)q.$$ Equivalently, the $q$ common divisions save $(n-1)q$ turns compared with processing every number independently. ## Adding Alice's initial increments Suppose Alice changes the original values $a_i$ into final values $b_i\ge a_i$. She spends $$\sum_i(b_i-a_i)$$ initial moves. Applying the fixed-array formula afterward gives the total $$\sum_i\bigl(b_i-a_i+F(b_i)\bigr) -(n-1)\min_i v_2(b_i).$$ Directly optimizing this expression looks coupled because of the minimum trailing-zero count. We remove that coupling by fixing a candidate number $m$ of common divisions. For one original value $a$, define $$G_m(a)=\min_{\substack{x\ge a\\2^m\mid x}} \bigl(x-a+F(x)\bigr).$$ All elements can now choose their targets independently, giving $$C_m=\sum_iG_m(a_i)-(n-1)m.$$ The answer is $$\min_m C_m.$$ There is a small subtlety: all independently selected targets might actually be divisible by $2^{m+1}$, so their minimum trailing-zero count could exceed $m$. That causes no problem. - For any particular final array, choose $m$ equal to its true minimum trailing-zero count. The independent minima in $C_m$ can only be cheaper, so $\min_m C_m$ is no larger than the optimum. - Conversely, take the targets selected for some $C_m$. If their true minimum trailing-zero count is $q\ge m$, their real game cost subtracts $(n-1)q$, which is no larger than the value calculated in $C_m$. Thus the actual optimum is no larger than every $C_m$. Both inequalities hold, so the formula is exact. ## Finding $G_m(a)$ without a fake magic bound Let $s=2^m$, and let $$p=\left\lceil\frac{a}{s}\right\rceil s$$ be the smallest legal target. Choosing $p$ costs $$p-a+F(p).$$ For any legal target $x\ge p+F(p)$, since $F(x)\ge1$, $$x-a+F(x) \ge p+F(p)-a+1,$$ which is already strictly worse than choosing $p$. Therefore, an optimal target must belong to the tiny list $$p,p+s,p+2s,\ldots <p+F(p).$$ This detail matters: always selecting the first multiple is wrong. Crossing a binary boundary can sharply reduce popcount; for instance, incrementing $15$ to $16$ can be worthwhile. The short search catches those cases exactly. ## Why only $m=0,1,\ldots,17$ At $m=0$, choosing every target unchanged gives an answer at most $$\sum_iF(a_i)\le33n,$$ because $a_i\le10^5<2^{17}$. For $m\ge18$, every positive multiple of $2^m$ is at least $2^m$, and its $F$ value is at least $m+1$. Therefore, $$C_m\ge n2^m-\sum_i a_i+n+m \ge n(2^m-100000+1)+m,$$ which is already vastly larger than $33n$ at $m=18$ and only increases afterward. Such an $m$ cannot be optimal. ## Algorithm For every $m$ from $0$ through $17$: 1. Set $s=2^m$ and initialize the total with $-(n-1)m$. 2. For every $a_i$: - Find the first multiple $p$ of $s$ with $p\ge a_i$. - Enumerate the multiples of $s$ in $[p,p+F(p))$. - Add the minimum value of $x-a_i+F(x)$. 3. Minimize the answer with this total. For `[2,2,2,2]`, leaving the values unchanged gives $q=1$. Their individual costs sum to $8$, while the common division saves $3$, producing $5$. For `[6,8,2,4,1]`, Alice can increment the last value to $2$. The increment costs one move, and the resulting array has one common division. Its game lasts $11$ turns, giving $12$ moves total. ## Correctness proof **Lemma 1.** Processing an isolated positive value $x$ takes exactly $F(x)$ turns. Every legal operation on an isolated value decreases $F$ by exactly one, and the process ends precisely when $F=0$. **Lemma 2.** If an array contains an odd value, Bob can force the remaining game to last exactly $\sum_iF(a_i)$ turns. Every Alice turn decreases this potential by at least one, giving an upper bound. Bob can keep an odd stopper at position $2$, forcing every division to affect only position $1`; odd operations already affect only position $1$. Thus every turn decreases the potential by exactly one, attaining the bound. **Lemma 3.** For a fixed array, its game value is $$\sum_iF(a_i)-(n-1)\min_i v_2(a_i).$$ The first $q=\min_i v_2(a_i)$ turns divide all $n$ elements. Afterward an odd value exists, and Lemma 2 applies. Combining the $q$ global turns with the remaining potential gives the stated expression. **Lemma 4.** For fixed $m$, checking only multiples $x$ in $[p,p+F(p))$ finds $G_m(a)$. The target $p$ is legal. Every $x\ge p+F(p)$ has cost at least one greater than the cost of $p$, so no excluded target can be optimal. **Theorem.** The algorithm outputs the minimum number of moves under optimal play. By Lemma 3, every choice of final values has the derived increment-plus-game cost. Fixing $m$ makes all element choices independent and produces $C_m`. The two-direction argument above proves that minimizing these relaxed values over $m$ equals the optimum over all final arrays. Lemma 4 computes every per-element minimum exactly, and no $m\ge18$ can be optimal. Therefore, the algorithm checks every potentially optimal strategy and returns the required answer. ## Complexity Across all $m$, only $O(\log A)$ tiny candidate targets are examined per element; a looser bound of $O(\log^2 A)$ also easily passes. The total time is $O(n\log A)$ with these constraints, and the memory usage is $O(n)$.#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int clearCost(int x) {
return __builtin_popcount((unsigned)x)
+ 31 - __builtin_clz((unsigned)x);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
ll answer = LLONG_MAX;
for (int m = 0; m <= 17; ++m) {
int step = 1 << m;
ll total = -1LL * (n - 1) * m;
for (int value : a) {
int first = (value + step - 1) / step * step;
int limit = first + clearCost(first);
int best = INT_MAX;
for (int x = first; x < limit; x += step) {
best = min(best, x - value + clearCost(x));
}
total += best;
}
answer = min(answer, total);
}
cout << answer << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int clearCost(int x) {
return __builtin_popcount((unsigned)x)
+ 31 - __builtin_clz((unsigned)x);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
ll answer = LLONG_MAX;
for (int m = 0; m <= 17; ++m) {
int step = 1 << m;
ll total = -1LL * (n - 1) * m;
for (int value : a) {
int first = (value + step - 1) / step * step;
int limit = first + clearCost(first);
int best = INT_MAX;
for (int x = first; x < limit; x += step) {
best = min(best, x - value + clearCost(x));
}
total += best;
}
answer = min(answer, total);
}
cout << answer << '\n';
}
}