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 cut count is not the clean variable. If you make cuts along one dimension, you create equal parts along that axis.
Let be the number of parts along . Then you need , , , and . The answer is .
Do not simulate slicing. This is just factor distribution: every prime factor of must be assigned to a dimension whose length contains that factor.
For one dimension, the largest useful piece-count you can take from the remaining is . Taking it greedily is safe because it only removes factors this dimension can actually support.
Do and divide by , then repeat for and . If the leftover is , output ; otherwise some factor fits nowhere, so print .
The trap here is that the output asks for cuts, but the math wants parts. If you make cuts along one dimension, that dimension is split into equal segments. So define:
For the segment lengths to be integers, we need , , and . The total number of small boxes is , so we need .
That is the whole problem. No geometry boss fight, just divisors wearing a cake costume.
Key Idea
We need to distribute the factors of among the three dimensions. A dimension can only receive factors that divide its length.
Suppose the remaining number of pieces we still need is . For a dimension of length , the biggest factor of that can safely go into this dimension is . Call it .
Set that dimension's part count to , then divide by .
A tempting thought is to multiply the full volume and test divisibility. Mathematically, that condition is equivalent, but as an implementation plan it is weaker: you still need the actual split, and can blast past 64-bit. The gcd construction gives both the proof and the answer.
Why is this greedy okay? Look at one prime . If appears times in the current remaining value , and the current dimension contains it times, this dimension can contribute at most copies of . The gcd takes exactly that many. The remaining copies, if any, literally cannot fit in this dimension, so they must be handled later. There is no downside to taking everything available now; factors are not a budget with weird cross-prime drama.
After all three dimensions, if , every factor of the original was assigned. If , some prime factor demand was larger than what collectively could provide, so it is impossible.
Algorithm
The sample uses , but our greedy might output for the same input. That is also valid. Codeforces does not care about aesthetic cake slicing, thank god.
Correctness Proof
Let be the remaining number of parts that still must be produced before processing some dimension of length .
The algorithm chooses . Therefore divides , so using parts along this dimension gives integer segment length. Also divides , so after setting this part count, the new remaining value is still an integer.
For any prime , let be its exponent in and its exponent in . Any valid choice for this dimension can use at most copies of . The gcd uses exactly copies. Therefore the algorithm leaves exactly the copies of that cannot be placed in this dimension. This is true independently for every prime.
After processing , if the remaining value is , then the chosen values multiply to the original required , and each divides its corresponding dimension. Thus printing gives valid cut counts.
If the remaining value is greater than , then at least one prime factor of the original still has unassigned exponent after all three dimensions have contributed as much as they possibly can. No valid triple can assign that factor, so the answer is impossible.
So the algorithm is correct.
Complexity
We compute three gcds. Time is , memory is .
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
ll w, h, d, n;
cin >> w >> h >> d;
cin >> n;
ll x = gcd(w, n);
n /= x;
ll y = gcd(h, n);
n /= y;
ll z = gcd(d, n);
n /= z;
if (n != 1) {
cout << -1 << '\n';
} else {
cout << x - 1 << ' ' << y - 1 << ' ' << z - 1 << '\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();
ll w, h, d, n;
cin >> w >> h >> d;
cin >> n;
ll x = gcd(w, n);
n /= x;
ll y = gcd(h, n);
n /= y;
ll z = gcd(d, n);
n /= z;
if (n != 1) {
cout << -1 << '\n';
} else {
cout << x - 1 << ' ' << y - 1 << ' ' << z - 1 << '\n';
}
return 0;
}