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.
A deal always buys a power of : . So the problem is really: write as a sum of powers of using as few terms as possible.
If you ever use three deals of size , you can replace them with one deal of size . That buys the same number of watermelons and uses fewer deals. So in an optimal least-deal representation, no power is used or more times.
“No power used or more times” should scream base . The counts of deals are exactly the ternary digits of .
Once the least number of deals is forced by the ternary digits, there is no extra greedy choice left. If the digit at position is , you buy deals of size .
Precompute powers of . For each ternary digit , add to the answer, with the cost handled as .
We need buy exactly watermelons. One deal can buy watermelons, and its cost is
For , this is just the special-looking but obvious deal from the statement: watermelon costs coins.
The Main Constraint Is Not Cost First
The buyer must use the least possible number of deals. Only after that do we compute what he pays. This matters: we are not free to use more smaller deals just because they might look cheaper. The hurry-up constraint comes first.
So first ask: what is the minimum number of powers of needed to sum to ?
A deal is one power of :
Suppose some representation uses three deals of size . Then those three deals buy
watermelons total. But that is exactly one bigger deal. Replacing those three deals by one deal buys the same number of watermelons and reduces the deal count from to .
So any least-deal solution cannot use a power or more times. If it did, it was leaving an easy compression on the table. Rookie mistake. Seller would love that nonsense.
Therefore, each power is used only , , or times.
That is exactly the ternary representation of .
If
where each , then the least possible number of deals is
And the actual deals are forced: use deals of size .
Why This Is Optimal
Take any way to buy exactly watermelons using powers of . If some size appears at least three times, combine three of them into one deal. This preserves the total number of watermelons and decreases the number of deals.
Repeat this until every count is , , or . Now the counts form a valid base- representation of . Base representation is unique, so these counts must be the ternary digits of .
That means no representation can beat the ternary digit sum, and the ternary representation achieves it. Done.
Computing The Cost
For each ternary digit , we pay for deals of size .
The cost of one such deal is:
So the answer is
Since , we only need powers up to around , and the answer fits safely in long long.
Algorithm
n % 3.digit * cost(x).n by .Complexity
Each test case processes digits.
So the total complexity is
which is tiny for the constraints.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
vector<ll> pow3(25, 1);
for (int i = 1; i < 25; i++) pow3[i] = pow3[i - 1] * 3;
int t;
cin >> t;
while (t--) {
ll n;
cin >> n;
ll ans = 0;
int x = 0;
while (n > 0) {
ll digit = n % 3;
ll cost;
if (x == 0) cost = 3;
else cost = pow3[x + 1] + 1LL * x * pow3[x - 1];
ans += digit * cost;
n /= 3;
x++;
}
cout << ans << '\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();
vector<ll> pow3(25, 1);
for (int i = 1; i < 25; i++) pow3[i] = pow3[i - 1] * 3;
int t;
cin >> t;
while (t--) {
ll n;
cin >> n;
ll ans = 0;
int x = 0;
while (n > 0) {
ll digit = n % 3;
ll cost;
if (x == 0) cost = 3;
else cost = pow3[x + 1] + 1LL * x * pow3[x - 1];
ans += digit * cost;
n /= 3;
x++;
}
cout << ans << '\n';
}
}