Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of storing hay simpler and more convenient to use. He collected the hay into cubical hay blocks of the same size. Then he stored the blocks in his barn. After a summer spent in hard toil Sam stored A·B·C hay blocks and stored them in a barn as a rectangular parallelepiped A layers high. Each layer had B rows and each row had C blocks.
At the end of the autumn Sam came into the barn to admire one more time the hay he'd been stacking during this hard summer. Unfortunately, Sam was horrified to see that the hay blocks had been carelessly scattered around the barn. The place was a complete mess. As it turned out, thieves had sneaked into the barn. They completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides. As a result, the barn only had a parallelepiped containing (A - 1) × (B - 2) × (C - 2) hay blocks. To hide the evidence of the crime, the thieves had dissembled the parallelepiped into single 1 × 1 × 1 blocks and scattered them around the barn. After the theft Sam counted n hay blocks in the barn but he forgot numbers A, B и C.
Given number n, find the minimally possible and maximally possible number of stolen hay blocks.
Input
The only line contains integer n from the problem's statement (1 ≤ n ≤ 109).
Output
Print space-separated minimum and maximum number of hay blocks that could have been stolen by the thieves.
Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
Examples
Input
4
Output
28 41
Input
7
Output
47 65
Input
12
Output
48 105
Note
Let's consider the first sample test. If initially Sam has a parallelepiped consisting of 32 = 2 × 4 × 4 hay blocks in his barn, then after the theft the barn has 4 = (2 - 1) × (4 - 2) × (4 - 2) hay blocks left. Thus, the thieves could have stolen 32 - 4 = 28 hay blocks. If Sam initially had a parallelepiped consisting of 45 = 5 × 3 × 3 hay blocks in his barn, then after the theft the barn has 4 = (5 - 1) × (3 - 2) × (3 - 2) hay blocks left. Thus, the thieves could have stolen 45 - 4 = 41 hay blocks. No other variants of the blocks' initial arrangement (that leave Sam with exactly 4 blocks after the theft) can permit the thieves to steal less than 28 or more than 41 blocks.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sam originally had a rectangular parallelepiped (cuboid) of dimensions A, B, C (positive integers, presumably at least something) made of unit cubes (hay blocks). So total blocks originally = A * B * C.
After theft: the thieves removed a layer from each side: front, back, top, sides. Actually they "took away a layer of blocks from the parallelepiped's front, back, top and sides." So they removed one layer from each face? But note: "They completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides." That suggests they took away the outer layer on all six faces? But then the remaining is a smaller parallelepiped? Let's read: "As a result, the barn only had a parallelepiped containing (A - 1) × (B - 2) × (C - 2) hay blocks." That's interesting: they subtract 1 from A, but subtract 2 from B and 2 from C. That indicates they removed a layer from front and back? Possibly meaning: they removed one layer from the top? Wait, let's interpret.
Original dimensions: A layers high (vertical), B rows (maybe depth?), C blocks per row (width?). They say: A layers high, each layer had B rows, each row had C blocks. So the shape is: height = A, depth = B, width = C? Or any ordering.
After theft: the remaining is (A-1) × (B-2) × (C-2). So reduction: one from height dimension, two from depth dimension, two from width dimension. That suggests the thieves removed a layer from top (only from top? not bottom) and from front and back (so two layers from depth? because front and back are opposite faces along B dimension), and also from left and right sides (two layers from width C). That matches: they took away a layer from the top (only top, not bottom) and from front/back (two opposite faces) and sides (left and right). But why not bottom? Perhaps because the bottom layer was on the ground? Actually the barn floor maybe they didn't take? Or the description: "from the parallelepiped's front, back, top and sides." It says top and sides, not bottom. So they took from front, back, top, left, right — that totals 5 faces. They didn't take from the bottom (maybe it's on the ground). So they removed a 1-block-thick layer from each of those five faces. That would reduce the dimensions: the height: they removed the top layer only, so height reduces by 1 (A->A-1). The depth (B): they removed front and back layers, so depth reduces by 2 (B->B-2). The width (C): they removed left and right layers, so width reduces by 2 (C->C-2). That matches the remaining dimensions. Good.
So the thieves took the blocks from those outer layers. They also "dissembled the parallelepiped into single 1x1x1 blocks and scattered them around the barn." So after theft, the barn contains the remaining (A-1)(B-2)(C-2) blocks (maybe still assembled? Actually "the barn only had a parallelepiped containing (A - 1) × (B - 2) × (C - 2) hay blocks" suggests the remaining blocks were left as a solid smaller parallelepiped. But then it says "To hide the evidence of the crime, the thieves had dissembled the parallelepiped into single 1 × 1 × 1 blocks and scattered them around the barn." So they also took the remaining smaller parallelepiped and broke it apart into unit cubes and scattered them. So after everything, the barn just contains n blocks (scattered). But Sam counted n hay blocks in the barn. So n = the number of blocks left after theft, which is (A-1)(B-2)(C-2). But careful: Actually the thieves took away the outer layers. Then they also dissembled the remaining inner smaller parallelepiped into single cubes and scattered those cubes. So the total number of blocks present in the barn after theft is exactly the number of blocks that were originally inside (i.e., the inner core). Those blocks were disassembled and scattered, but still count as n. So n = (A-1)(B-2)(C-2). The thieves stole the outer layers, which amount to total original minus n. So stolen = ABC - n.
We are given n, and we need to find the minimum possible stolen and maximum possible stolen, among all positive integer triples (A, B, C) that satisfy n = (A-1)(B-2)(C-2) and also certain constraints: The original dimensions A, B, C must be such that the outer layers exist: A >= ? For top layer removal, need A >= 2? Actually to have a top layer to remove, A must be at least 2? But if A=1, then height is 1, there is a top but also bottom? Actually if A=1, the shape is just one layer high. If thieves take top layer, they'd take that only layer, leaving no inner core? But the resulting inner core would be (A-1)(B-2)(C-2) = 0 * (B-2)*(C-2) = 0, which would give n=0. But n >=1, so A>=2. Similarly for B and C: To remove two layers from B dimension (front and back), need B >= 3? Because you remove one from front and one from back, leaving B-2 >= 1 => B>=3. Similarly C>=3. So constraints: A >= 2, B >= 3, C >= 3. Additionally all integers.
Given n, we need to consider all factorizations of n as product of three positive integers: n = x * y * z, where x = A-1, y = B-2, z = C-2, with A>=2 => x>=1, B>=3 => y>=1, C>=3 => z>=1. So x, y, z >= 1 integer.
Thus any triple (x,y,z) of positive integers such that xyz = n corresponds to original dimensions: A = x+1, B = y+2, C = z+2. Then total original blocks = (x+1)(y+2)(z+2). Stolen = (x+1)(y+2)(z+2) - n.
We need to find min and max of stolen over all ordered triples (x,y,z) of positive integers with product n. Are x,y,z considered ordered? Since A, B, C are distinct dimensions? Probably yes, but they are not symmetric because the reductions are different: A loses 1, B loses 2, C loses 2. So ordering matters. That is, the assignment of dimensions to (A, B, C) is fixed: the dimension from which we subtract 1 is the height (A), the ones we subtract 2 are the other two. So when we factor n = xyz, we must assign x = A-1, y = B-2, z = C-2. So the triple (x,y,z) is ordered: x corresponds to the dimension with -1, y and z with -2. So different permutations of (x,y,z) will yield different stolen amounts, because (y+2) and (z+2) are symmetric (both have +2) but x+1 is distinct. However swapping y and z does not change original total (since multiplication is commutative). Indeed (x+1)(y+2)(z+2) is symmetric in y and z. So the stolen value is symmetric in y and z, but not with x. So we can consider x as any positive divisor, and y,z as any positive divisor pair such that product = n/x. But since y and z can be swapped, it's effectively the same, so we can treat (y,z) as unordered but consider both possibilities? Actually because (x,y,z) ordered but (y,z) symmetric, the stolen value depends only on the multiset {x, y, z} but with the special role of x. So we must consider all distinct assignments: which factor gets the -1 treatment. So we need to consider all ways to choose x, y, z positive integers with product n, where x corresponds to the factor assigned to the dimension with -1. That means: for each factorization n = x * d, where d = y * z, we need to consider all possible factor pairs (y,z) of d. But that is essentially enumerating all ordered triples (x,y,z). So the min/max stolen values are over all ordered triples (x,y,z) positive integers such that xyz = n.
But n can be up to 1e9, so the number of divisors is limited (max ~134k for 1e9? Actually max divisors for numbers <=1e9 is around 1344? Let's think: 1e9 ~ 2^9 * 5^9? Actually 2^30 is >1e9, but typical highly composite numbers: 73513440 has 1344 divisors? Actually maximum number of divisors for n <= 10^9 is 1536? Let's check: 73513440 has 1344 divisors? Possibly around 1.5k. For 1e9, maybe ~1344. So we can iterate over divisors of n, and for each divisor x, factor d = n/x, then iterate over divisors of d to get y, and set z = d / y. That would be O(divisors(d) * number of x). In worst case, if we iterate over all divisors of n and for each all divisors of n/x, total sum of divisor counts over all x could be large but still manageable because sum_{d|n} tau(d) = O(tau(n)^2)? Actually sum_{d|n} tau(d) = sum_{d|n} number of divisors of d. That is the sum of divisor function over divisors of n. For n with many divisors, this sum can be bigger. But max tau(n) for n <= 1e9 is about 1536? Let's confirm. I recall that maximum number of divisors for numbers up to 10^9 is 1344 for 735134400? Actually I need to check: Number 963761198400 (that's >1e9). For 1e9, let's compute roughly: The product of first few primes: 23571113171923 = 223092870 >1e9? Actually 23571113 = 30030; 17 = 510510; 19=9699690; 23=223092870; 29=6469693230 > 1e9, so up to 23 product is 223 million. 29 exceeds 1e9. So the maximum exponent combination that stays <= 1e9. The number of divisors function for number with prime factorization ∏ p_i^{e_i} is ∏ (e_i+1). To maximize tau(n) given n <= 1e9, we want small primes with exponents. Usually the champion numbers are highly composite. I think the maximum tau(n) for n <= 1e9 is 1536. Actually I've seen that for 10^9, maximum is 1344? Let's quickly approximate: 2^9 * 3^4 * 5^2 * 7 * 11 * 13 * 17 * 19 =? 2^9=512; 3^4=81; 5^2=25; product so far: 51281=41472; 25=1,036,800; times 7=7,257,600; times 11=79,833,600; times 13=1,037,836,800 which exceeds 1e9 (1.037e9 > 1e9). So maybe 2^8 * 3^4 * 5^2 * 7 * 11 * 13 * 17 * 19 = 2568125711131719 = 25681=20736; 25=518400; 7=3,628,800; 11=39,916,800; 13=518,918,400; 17=8,821,612,800 >1e9? Wait 518,918,400 * 17 = 8,821,612,800, that's >1e9. So my product already >1e9 before 17. Actually let's compute stepwise: 2^83^45^271113 = 256 * 81 = 20736; 20736 25 = 518400; 518400 7 = 3,628,800; 11 = 39,916,800; 13 = 518,918,400. This is 5.189e8. Multiply by 17 gives 8.821e9 >1e9, so stop at 13. So 2^83^45^271113 = 518,918,400, tau = (8+1)(4+1)(2+1)(1+1)(1+1)(1+1) = 953222 = 95=45; 11 = 5122437243=124416; 26=60; 781781=41472; 25=50; 72437243=248832; 26=66; 71381=5184; (1+1) = 7425=35; 1117(3+1)(1+1)(1+1)4224=28; *3=84; *2=168; *2=336; *2=672; 27=1728; *25=43200; *7=302400; *11=3,326,400; *13=43,243,200; *17=735,134,400. Yes that's under 1e9. So tau=1344 is achievable. So maximum divisor count about 1344.
Now, enumerating all ordered triples (x,y,z) of positive integers with product n would be number of ways to assign factors. That's essentially the number of ordered factorizations of n into three positive integers, which is the sum over divisors of n of tau(d) where d = n/x? Actually number of ordered triples (x,y,z) with xyz=n is equal to sum_{d|n} tau(d) where for each divisor x of n, the number of (y,z) such that y*z = n/x is tau(n/x). So total number = sum_{x|n} tau(n/x) = sum_{d|n} tau(d) (by symmetry). So total ordered triples = sum_{d|n} tau(d). For n with tau(n)=k, the sum of divisor function over divisors can be larger. For n=735134400 with tau=1344, what is sum_{d|n} tau(d)? This sum equals ∑{d|n} τ(d) = ∏ over primes ( (e_i+2 choose 2)? ) Actually there is formula: ∑{d|n} τ(d) = ∏ (1 + τ(p) + τ(p^2) + ... + τ(p^e))? Not exactly. Alternatively, it's the Dirichlet convolution τ * 1 evaluated at n, which is the divisor function of order 2: number of pairs (a,b) such that ab divides n? Actually number of ordered triples (x,y,z) with xyz=n is the 3-fold divisor function. That is d_3(n). The number of ordered factorizations into 3 positive integers is d_3(n) = ∑{d|n} τ(d). This can be computed combinatorially: if n = ∏ p_i^{e_i}, then d_3(n) = ∏ C(e_i+2, 2)?? Wait, the number of ordered triples (x,y,z) such that xyz=n is equal to ∏{i} binomial(e_i+2, 2)?? Let's derive: For each prime p, we need to distribute its exponent e among x, y, z (allow 0). Number of nonnegative integer solutions to a+b+c = e is C(e+2,2). So indeed d_3(n) = ∏ (e_i+2)(e_i+1)/2. For large e_i, this can be much larger than τ(n). For example, if n is prime: e=1, then d_3(n) = C(3,2)=3? Actually C(1+2,2)=C(3,2)=3. Indeed ordered triples: (1,1,p), (1,p,1), (p,1,1). So d_3(n)=3. But sum_{d|n} τ(d) for n=p: divisors:1,p. τ(1)=1, τ(p)=2 -> sum=3. Correct.
For n=735134400, with exponents: 2^6 (e=6), 3^3 (3), 5^2 (2), and 1 for primes 7,11,13,17. So d_3 = ∏ (e_i+2)(e_i+1)/2. For 2: (87)/2=28; for 3: (54)/2=10; for 5: (43)/2=6; for each e=1: (32)/2=3. So overall: 28 10=280; 6=1680; 3=5040; 3=15120; 3=45360; 3=136080. So d_3 = 136080, which is about 100 times tau. So enumerating all ordered triples (136k) might be acceptable for a single test? n is up to 1e9, but d_3 can be up to maybe a few hundred thousand? Actually 136k is fine. But we need to consider that we only have one test case? The problem seems to have just one n. So we could generate all ordered triples via loops over divisors, but we need to be careful about time. 136k is okay. But could d_3 be larger? Let's check the maximum possible for n <= 1e9. We need to maximize ∏ (e_i+2)(e_i+1)/2 subject to ∏ p_i^{e_i} ≤ 1e9. This is like maximizing the number of ordered factorizations. That would favor many small primes with moderate exponents? But note that (e_i+2)(e_i+1)/2 grows quadratically with e_i, while p_i^{e_i} grows exponentially. So to maximize product of these combinatorial factors while staying under 1e9, we likely use small primes and moderate exponents. The example 735134400 gave 136k. Could there be a number with even larger d_3? Possibly around 200k? Let's explore. Consider using exponent distributions: using primes 2,3,5,7,11,13,17? Already used. Could we increase exponent on 2? If we increase 2 to 7, product would be 2^7 * 3^3 * 5^2 * 7 * 11 * 13 * 17? 2^7=128, earlier product with 2^6 was 735,134,400. Multiply by 2 gives 1,470,268,800 > 1e9. So can't increase exponent on 2. Could we include prime 19? If we include 19, we would need to reduce elsewhere to keep product <=1e9. For instance, 2^6 * 3^3 * 5^2 * 7 * 11 * 13 * 19? Multiply 735,134,400 / 17 * 19 = (735,134,400 / 17 ≈ 43,243,200) 19 = 821,620,800? Actually compute: 2^63^35^271113 = from earlier: 2^6=64; 3^3=27; 5^2=25; 7=7; 11=11; 13=13: product = 6427=1728; 25=43200; 7=302400; 11=3,326,400; 13=43,243,200. So that's the product of those primes. Then we can multiply by 17 to get 735,134,400 or by 19 to get 821,620,800 (since 43,243,20019=821,620,800). That's still <1e9. Actually 821,620,800 < 1e9. So we could replace 17 with 19, and maybe even include both? Let's try 2^63^35^2711131719? That would be 43,243,200 * 17 * 19 = 43,243,200 * 323 = about 13.96e9, >1e9. So cannot have both 17 and 19. So we have at most 7 distinct primes if we use exponents? Actually the product of 2^63^35^271113 is 43,243,200. We could multiply by 17 (giving 735M) or by 19 (821M). Could also multiply by 23? 43,243,20023=994,593,600, still <1e9. Actually 43,243,20023 = 994,593,600 < 1e9. So we could include 23 instead of 17/19. Could we include both 17 and 23? 43,243,2001723 = 43,243,200*391 = about 16.9e9 >1e9. So only one extra prime beyond 13? Actually we have primes: 2,3,5,7,11,13 are 6 primes. Then we could add at most one more prime (like 17,19,23,29?). But we could also increase exponents on smaller primes while reducing number of primes. The d_3 factor for exponent e is quadratic, while adding a prime with exponent 1 adds factor 3, and uses up some product budget. So perhaps the maximum d_3 is achieved with the highly composite number with many divisors, which is also likely to maximize d_3? But need to compute.
But for n up to 1e9, d_3 can be at most maybe around 200,000. That's acceptable. But note: we don't need to enumerate all ordered triples? We can compute min and max of stolen analytically by considering the function f(x,y,z) = (x+1)(y+2)(z+2) - n = (x+1)(y+2)(z+2) - xyz. Since n = xyz.
We need to minimize and maximize this over positive integers x,y,z with product n.
Alternatively, we can think directly: Stolen = (x+1)(y+2)(z+2) - xyz.
Now, for fixed n = xyz, we can think of optimizing over factor triples.
But maybe we can find min and max by considering extreme shapes: When dimensions are as unbalanced as possible? Because stolen seems to increase with the surface area? Let's test.
Let’s consider that stolen = original - inner = (ABC) - ((A-1)(B-2)(C-2)). Original includes outer layers. So stolen is essentially volume of outer layers taken. That volume can be expressed as: total - core = ABC - (A-1)(B-2)(C-2). Expand: ABC - ( (A-1)(B-2)(C-2) ) = ... Let's compute directly:
Then stolen = ABC - [(A-1)BC - 2(A-1)B - 2(A-1)C + 4(A-1)] = ABC - (A-1)BC + 2(A-1)B + 2(A-1)C - 4(A-1) = [ABC - (A-1)BC] + 2(A-1)(B+C) -4(A-1) = BC + 2(A-1)(B+C) -4(A-1) = BC + 2(A-1)(B+C-2). That is a nice form.
Since n = (A-1)(B-2)(C-2) = (x)(y)(z) with x = A-1, y = B-2, z = C-2. So B = y+2, C = z+2, A = x+1. Then stolen = (y+2)(z+2) + 2x((y+2)+(z+2)-2) = (y+2)(z+2) + 2x(y+z+2). But previous expression gave stolen = yz + 2(x+1)(y+z) + 4(x+1). Let's verify if they match: (y+2)(z+2) = yz+2y+2z+4. 2x(y+z+2) = 2x(y+z)+4x. Sum = yz+2y+2z+4 + 2x(y+z)+4x = yz + 2(x+1)(y+z) + 4(x+1). Yes.
So stolen = yz + 2(x+1)(y+z) + 4(x+1).
Given that n=xyz, we need min and max.
One naive approach: iterate over x (which corresponds to divisor of n), then for each x, iterate over y (divisor of n/x), set z = (n/x)/y. Compute stolen and track min and max. Complexity: sum_{x|n} tau(n/x) = d_3(n). For n <= 1e9, d_3(n) <= maybe a few hundred thousand. That's fine for one test.
But we must ensure that we treat ordered triples (x,y,z) distinct, but as we said swapping y and z yields same stolen, so we could restrict y <= z to avoid duplicates if we want, but it's okay to just iterate over all divisors for y (which includes both orders) and compute stolen; min/max will be same. However careful: we need to consider all ordered triples, not only unordered? Since (x,y,z) is ordered with x special, but y and z symmetric, it's safe to iterate over all y divisors (ordered) because that will cover both y and z permutations, but they produce same stolen? Actually if we iterate over all y divisors (including y and its complementary divisor z), then for each x we have pairs (y,z) and (z,y) as separate iterations. But stolen depends symmetrically on y and z, so they give same stolen value. So min and max won't change if we consider duplicates. So we can simply do for each divisor x of n, for each divisor y of n/x, set z = (n/x)/y, compute stolen. That's straightforward.
But we must consider constraints that A >=2, B>=3, C>=3 => x>=1, y>=1, z>=1. So all positive.
Thus algorithm:
Input n.
Compute all divisors of n? Actually we need to iterate over all positive divisor pairs. Could generate all divisors of n, store in list. Then for each divisor x in that list, compute d = n / x. Then generate divisors of d. Generating divisors of d for each x by scanning the list of all divisors and checking if they divide d? Since we have list of all divisors of n, we could filter those that divide d. But that may be O(τ(n)^2) which for τ up to 1344 gives about 1.8 million, still fine. But we can also generate divisors of each d on the fly by factoring n and using recursion? But simpler: get all divisors of n. Then for each divisor x, we loop through all divisors y of d? To get divisors of d, we can again use the global divisor list but filter those that divide d: if y divides d then it's a divisor of d? Not necessarily: divisors of d may be a subset of divisors of n? Since d = n/x, any divisor of d is also a divisor of n? Actually if y | d and d | n? d = n/x, so d divides n? Not necessarily: d divides n? Because n = x * d, so d is a divisor of n? Yes, d is a divisor of n because n/d = x integer. So any divisor of d also divides n? Because if y|d, then d = yk, then n = xy*k, so y|n. So divisors of d are a subset of divisors of n. So we can precompute all divisors of n into a sorted list D. Then for each x in D, let d = n/x. Then we need to iterate over y in D such that y divides d. So we can simply iterate over y in D and if d % y == 0 then proceed. That yields O(τ(n)^2) checks. τ(n) <= ~1344 => ~1.8M checks, trivial.
But note: d = n/x, and x and y are divisors from D. So for each x, we loop over all y in D and check if y divides d. This might be okay, but we can also generate divisors of d more efficiently by factoring n and using recursion for each x? However, given constraints, O(τ(n)^2) is fine.
Alternatively, we could generate all factor triples by three nested loops over prime exponents via recursion, which might be even faster but more complex. Simpler to do divisor squared method.
But careful: For each x, we might also need to consider y such that y <=? Not necessary.
Thus compute stolen for each triple, keep min and max.
But we must ensure using 64-bit integers (long long). n up to 1e9, but stolen can be large. Let's bound stolen: A, B, C could be large. Suppose n is prime: n = p. Then possible triples (x,y,z) with xyz = p. Since p prime, the only factor triples (ordered) are (1,1,p), (1,p,1), (p,1,1). For (x=1,y=1,z=p): A=2, B=3, C=p+2. Stolen = (2)(3)(p+2) - p = 6(p+2) - p = 5p+12. For p=1e9? Actually prime up to 1e9, stolen approx 5e9+12, fits 64-bit. For (x=p,y=1,z=1): A=p+1, B=3, C=3, stolen = (p+1)*9 - p = 9p+9 - p = 8p+9. So max is 8p+9 ~ 8e9. So stolen <= about 8n + something, which for n=1e9 is 8e9, still fits 64-bit. However could stolen be larger? Suppose n has factors, maybe more extreme? If x is small, y and z large, stolen = yz + 2(x+1)(y+z) + 4(x+1). With x=1, y and z can be up to sqrt(n) roughly? Actually if x=1, then yz=n, y and z up to n. So yz = n. Then stolen = n + 2(2)(y+z) + 8 = n + 4(y+z) + 8. Since y+z can be as large as n+1 (if y=1, z=n => sum = n+1) or as small as 2 sqrt(n). So max sum when one of y,z is n and the other 1 gives y+z = n+1, stolen = n + 4(n+1) + 8 = 5n+12, same as above. So max for x=1 is ~5n.
If x is large, say x=n, y=1,z=1, stolen = (1*1) + 2(n+1)(1+1) + 4(n+1) = 1 + 4(n+1) + 4(n+1) = 1 + 8(n+1) = 8n+9. So maximum stolen could be around 8n. So within 64-bit.
Thus we can compute stolen as long long.
Now, the sample tests:
Input 4 -> Output 28 41.
n=4.
We need to find all triples (x,y,z) positive integers with product 4.
List all ordered triples:
x=1,y=1,z=4 => stolen = (1+1)(1+2)(4+2) - 4 = 236 -4 = 36-4=32? Wait 236=36, minus4=32, but sample says 28? That's not matching. Let's compute: (A-1)(B-2)(C-2)=n=4. With x=1,y=1,z=4 => A=2, B=3, C=6. Original = 236=36, stolen=36-4=32. But sample output min is 28. So maybe my assumption about constraints wrong: Maybe B and C must be >= 2? Or maybe they removed one layer from top, bottom, front, back, left, right? But then remaining would be (A-2)(B-2)(C-2)? Let's re-read problem statement: "They completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides. As a result, the barn only had a parallelepiped containing (A - 1) × (B - 2) × (C - 2) hay blocks."
It says front, back, top and sides. That's front, back, top, sides. Sides could be left and right. So that's five faces, as we assumed. So why does sample not include (2,3,6)? Let's test with n=4. If A=2, B=3, C=6: inner core = (2-1)(3-2)(6-2) = 114=4. That works. Then stolen = total - 4 = 36-4=32. But sample min is 28. So 32 is not minimal? They claim min is 28, which corresponds to A=2,B=4,C=4: original 244=32, stolen 32-4=28. That triple: x = A-1 =1, y = B-2=2, z = C-2=2, product = 122=4. So (x,y,z) = (1,2,2) gives stolen= (2)(4)(4) -4 = 32-4=28. So why is (1,1,4) not considered? Did they impose that B and C >=? Actually B and C are number of rows and number of blocks per row. After removing front and back layers (two layers) we get B-2 rows, which must be at least 1? So B-2 >=1 => B>=3. That's satisfied: B=3. After removing left and right layers, we get C-2, must be >=1 => C>=3. C=6 >=3. So why not allowed? Perhaps because they took a layer from top, but not from bottom? But then after removal, the remaining shape is (A-1)×(B-2)×(C-2). For A=2, after removing top layer, height reduces to A-1=1. So the remaining shape is 1 layer high, B-2=1 row, C-2=4 blocks per row. That's 1x1x4 = 4 blocks. That's okay. So why is that not considered in sample? Maybe because the thieves "dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides." That might imply they took a layer from all six faces? But they didn't mention bottom. However maybe "top and sides" includes all four vertical sides? Actually "front, back, top and sides" could be interpreted as: front, back, top, bottom? But they said sides. Might be ambiguous. Let's re-read: "They completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides." It mentions front, back, top, and sides. "Sides" might refer to left and right? But then they already said front and back; sides could be left and right. So that's five faces. But maybe they consider that they took a layer from all six faces: front, back, top, bottom, left, right. However the description says "front, back, top and sides." Could be that "sides" collectively includes left and right, but also maybe includes bottom? Unclear.
If they took from all six faces, then the remaining would be (A-2)(B-2)(C-2). That would yield (A-2)(B-2)(C-2) = n. Then x = A-2, y = B-2, z = C-2 all >=1 => A,B,C >=3. Then stolen = ABC - n. For n=4, possible triples: (x,y,z) positive with product 4. (1,1,4): A=3,B=3,C=6: original=54, stolen=50; (1,2,2): A=3,B=4,C=4: original=48, stolen=44; (2,1,2): A=4,B=3,C=4: original=48, stolen=44; (2,2,1): A=4,B=4,C=3: 48, 44; (4,1,1): A=6,B=3,C=3: 54,50. That gives min 44, max 50. Not matching sample 28 and 41.
So the sample suggests they removed layers from five faces, not six.
Then why (1,1,4) not considered? Let's check constraints again: maybe they require that the remaining parallelepiped after removing outer layers is still a proper parallelepiped and also that the thieves dissembled it into single blocks and scattered them. But that doesn't rule out (1,1,4). Perhaps there's an additional requirement: The original shape had to be such that after removing a layer from the front, back, top, and sides, the remaining shape is (A-1)×(B-2)×(C-2). That's correct for A=2, B=3, C=6? Let's compute: remove top layer: height A -> A-1 = 1. Remove front and back layers: B -> B-2 = 1. Remove left and right: C -> C-2 = 4. So remaining: 1x1x4. That's fine. So stolen would be outer blocks: those on top, front, back, left, right. That yields stolen = total - 4 = 32. So why is min 28? Let's compute stolen for (1,2,2) we got 28. So 28 < 32. So (1,2,2) yields less stolen. So maybe (1,1,4) is not allowed because B=3, C=6 but then after removal, the remaining has dimensions (A-1)=1, (B-2)=1, (C-2)=4. But the problem statement might require that the remaining is a parallelepiped, i.e., all three dimensions positive. Yes, that's satisfied. So it should be allowed. So why not considered in sample explanation? The sample note says: "If initially Sam has a parallelepiped consisting of 32 = 2 × 4 × 4 hay blocks in his barn, then after the theft the barn has 4 = (2 - 1) × (4 - 2) × (4 - 2) hay blocks left." That corresponds to (A=2,B=4,C=4). Then they mention another initial parallelepiped 45 = 5 × 3 × 3, giving 4 = (5-1)×(3-2)×(3-2). So these two correspond to (x=1,y=2,z=2) and (x=4,y=1,z=1). They do not mention (A=2,B=3,C=6). So maybe they are considering only those triples where the remaining dimensions are all at least 2? But the problem statement says the barn only had a parallelepiped containing (A-1)×(B-2)×(C-2) hay blocks. It doesn't say each dimension must be >=2; only that it is a parallelepiped, which can have dimension 1 (a degenerate parallelepiped still is technically a rectangular prism). So no.
Alternatively, perhaps the thieves also dissembled the remaining parallelepiped into single blocks, so the number n is the number of blocks after scattering. That's fine.
Maybe there is an additional condition: Sam stored hay blocks as a rectangular parallelepiped A layers high, each layer had B rows and each row had C blocks. So it's a standard shape. Perhaps there is an implicit assumption that B and C are at least 2? Because a row of C blocks: C is number of blocks in a row; if C=1, then row has 1 block. But that's allowed. But perhaps B and C are the numbers of rows and columns; they can be 1? Usually rows could be 1. But maybe they assume B and C are at least 2 because otherwise the notion of front/back or left/right layers would be ambiguous? However the problem says thieves took a layer from front, back, top, and sides. If B=3, then front and back layers exist (since B>=3, yes). If C=6, sides exist. So that's okay.
But maybe the requirement is that after taking away a layer from front/back/top/sides, the remaining parallelepiped is not empty and maybe also its dimensions are at least 1. That's satisfied.
So why does sample output not include 32 as possible stolen? Let's compute stolen for (1,1,4) = 32. But sample says minimum stolen is 28 and maximum is 41. So 32 is between these. So there must be some reason that (A=2,B=3,C=6) is not allowed. Let's check the product: (A-1)(B-2)(C-2) = (2-1)(3-2)(6-2) = 114 = 4. That matches n. So mathematically it's a solution. So why would it be excluded? Possibly because the thieves took a layer from the front, back, top and sides, but they might have taken exactly one layer from each of those faces, and the thickness of the layer is one block. However, if B=3, then the front and back layers each consist of a 2D face: For a cuboid of dimensions A=2, B=3, C=6, the front face is of size A x C = 2x6. Removing a layer of thickness 1 from the front means we remove the entire face? Actually the wording: "took away a layer of blocks from the parallelepiped's front, back, top and sides." A layer likely means a set of blocks that form a slab of thickness 1 adjacent to that face. For the front face, the slab would be the blocks with coordinate in B direction = 1 (assuming front). For B=3, the front layer is the set of blocks with B=1 (or B=1..1). That slab consists of AC = 26 = 12 blocks. Similarly, back layer is blocks with B=3, also 12 blocks. Top layer is blocks with A=2 (top), size 1BC = 136 = 18? Actually top: A=2, size 1 x B x C = 136=18. Sides: left and right: each is A x B x 1 = 231=6 per side, total 12. So total outer layers (excluding bottom) = front 12 + back 12 + top 18 + left 6 + right 6 = 54? But that sum exceeds original 36? That can't be because these layers overlap at edges/corners. Actually when you take the union of these five slabs, there is overlap: edges and corners are counted multiple times if we sum separately. The actual number of blocks removed is total original minus inner core (the blocks not in any of those slabs). For this shape, the inner core is the set of blocks with A in [1,1] (since top removed, A from 1 to A-1? Actually after removal, the remaining is A-1=1, B-2=1, C-2=4: the blocks with A=1 (bottom), B=2 (middle), C from 3 to ? Actually careful: If we remove front (B=1), back (B=3), left (C=1), right (C=6?), Wait C=6, so rightmost is C=6? Actually index: C goes 1..C. Removing left side (C=1) and right side (C=C). So the inner core is blocks with A from 1 to A-1? Since top removed, A=2 removed, bottom remains A=1. So inner core A=1, B=2 (since front B=1 and back B=3 removed), C from 2 to 5? Actually left C=1 removed, right C=6 removed, so C=2..5, which is 4 blocks. So inner core volume = 114? Wait B: only B=2 remains, so B-2=1 indeed. So that's correct. So stolen = total 36 - 4 = 32. That's plausible.
So why excluded? Maybe there is additional requirement that the original shape must be such that the thieves could have taken away a layer from front, back, top, and sides without also taking from bottom. But that's fine.
Maybe the condition is that the remaining parallelepiped must be at least 2 in each dimension? But not stated.
Let's examine sample output for n=4: min 28, max 41. That corresponds to (x=1,y=2,z=2) and (x=4,y=1,z=1) respectively. For (x=1,y=1,z=4) stolen=32 is between. So if the problem allowed all triples, the min would be 28 (since 28 < 32) and max would be 41 (since 41 > 32). Actually 32 is between, so it wouldn't affect min or max. So the sample output min=28 and max=41 are consistent with (1,1,4) being allowed or not, because 32 is not less than 28 nor greater than 41. So the sample does not disprove its allowance. However the sample explanation only mentions two specific configurations that yield the extreme values. They didn't need to mention (2,3,6). So it's possible that (2,3,6) is allowed, but just not extremal. So maybe our enumeration will yield min = 28, max = 41 for n=4. Let's compute stolen for (1,1,4) = 32, which is not min or max. So fine.
But we need to check the second sample: n=7. Output 47 65. Let's test some triples. For n=7, prime. Possible ordered triples: (1,1,7): A=2,B=3,C=9 -> original=239=54, stolen=54-7=47. (1,7,1): A=2,B=9,C=3 -> original=54, stolen=47? Actually symmetric yields same 47. (7,1,1): A=8,B=3,C=3 -> original=72, stolen=65. So min = 47, max = 65. That matches sample. Good. So our interpretation matches sample 2.
Third sample: n=12. Output 48 105. Let's test with our method. Compute all triples for n=12. Need to see if min=48, max=105.
We can do quick enumeration mentally: factorizations of 12 into three positive integers (ordered). List possibilities:
y=?? y=?? Also y=2,z=1.5 not integer. Actually 42? =12 => ?=1.5 not integer. So only (1,3) and (3,1).
x=6:
y=1,z=2 -> (7,3,4) => 734=84, stolen=72.
y=2,z=1 -> (7,4,3) => 84, stolen=72.
x=12:
y=1,z=1 -> (13,3,3) => 1333=117, stolen=105.
So stolen values we got: 72,52,48,48,52,72,60,48,48,60,60,52,60,63,63,72,72,105. The min appears to be 48, max 105. That matches sample output 48 105. So our interpretation yields correct results for samples.
Therefore our approach is correct: we must consider all ordered triples (x,y,z) of positive integers with product n.
Now the challenge: For n up to 1e9, we need to compute min and max of stolen efficiently.
We could do a triple loop over divisors. Since d_3(n) can be as large as maybe 200k or 300k, it's fine. But we need to be careful: generating all divisors of n up to 1e9 is easy using factorization. We can factor n by trial division up to sqrt(n) ~ 31623. That's acceptable for a single test (31623 iterations). But n can be up to 1e9, sqrt(1e9) ≈ 31623, trial division by primes up to 31623 would be about 3400 primes if we pregenerate primes. We could just trial divide by i from 2 to sqrt, but that would be up to 31623 steps, which is fine in C++ (since 3e4 operations is trivial). However we need to be careful: if n is prime, we only need to check up to sqrt, which is 31623, still fine.
So plan:
Read n.
Factor n: get list of (prime, exponent).
Generate all divisors of n: using recursion/backtracking to generate all combinations of prime exponents. The number of divisors tau(n) is at most about 1344, so generating them is fine.
Sort the list of divisors (maybe not necessary but convenient for iteration? Actually we can keep unsorted, but we might want to use for iterating in order? Not necessary. For each divisor x in D, we will loop through all y in D and check if y divides n/x. That's O(|D|^2). But if we have sorted list, we could break early if y > d? But we still need to check divisibility; we could filter by checking if y <= d and d % y == 0. That's okay. Complexity: For each x, we loop over all y (|D|). So total operations = |D|^2. Max |D| ≈ 1344 => 1.8 million, which is fine.
But we could also generate for each x the list of divisors of d = n/x by using the factor combination of the remaining exponents. Since we already have factorization, we could recursively generate divisors of d on the fly, but that might be more code. Simpler to just double loop.
But we must be careful: d = n/x might be large, but we just need to check d % y == 0. That's cheap.
However, we need to compute stolen for each triple. In the double loop, for each x and y that divides d, we compute z = d / y, then stolen = (x+1)(y+2)(z+2) - n. But we have to be careful about overflow? Use long long (64-bit). n up to 1e9, x,y,z up to 1e9? Actually x can be as large as n (if y=z=1). So (x+1) up to n+1, (y+2) up to n+2? If y=n? But note: y and z are divisors, and since product is n, if x=1, y can be up to n, z=1. So (y+2) could be up to n+2, (z+2) up to 3? Actually when y=n, z=1, so (y+2) ~ n+2, (z+2)=3. So product (x+1)(y+2)(z+2) roughly (2)*(n+2)*3 ~ 6n, within 64-bit. For n=1e9, that's 6e9, fine. For x=n, y=1, z=1, product ~ (n+1)33 = 9n, about 9e9, still fine. So 64-bit signed can handle up to 9e9, which is safe (max 9e9 < 2^63-1). So okay.
For each x in divs:
d = n / x; // integer
for each y in divs:
if (d % y != 0) continue;
z = d / y;
// compute stolen = (x+1)(y+2)(z+2) - n
stolen = (long long)(x+1) * (y+2) * (z+2) - n;
update min and max.
Print min_stolen and max_stolen.
But is there any possibility that the same triple is generated multiple times? Possibly due to ordering of y and z? But that's okay; we may compute same stolen multiple times but that's fine.
Edge cases: n=1. Then n=1. Then find x,y,z positive integers with product 1 => only triple (1,1,1). A=2, B=3, C=3 => original=18, stolen=17. But constraints: 1 ≤ n ≤ 1e9. So n=1 yields min=max=17. Let's quickly verify if any other triple? Only (1,1,1). So output 17 17. That seems plausible. But need to check if there are any constraints that B and C must be >=3? That holds: B=3, C=3. So okay.
But we should also consider if A, B, C must be integers, obviously.
Now, we must confirm that the triple (x,y,z) where x, y, z are all positive integers (≥1) is sufficient. Yes.
Thus solution is straightforward.
But we could also solve analytically: For given n, to minimize stolen, we want original volume as small as possible given n = (A-1)(B-2)(C-2). Since stolen = original - n, minimizing stolen is equivalent to minimizing original. So we need to minimize (x+1)(y+2)(z+2) subject to xyz=n. Similarly maximize stolen is maximize original.
But we can just brute force divisors.
However, careful: The double loop approach might be O(tau(n)^2) which for tau(n) up to maybe 2000, is 4 million, still fine. But I estimated tau(n) max 1344, but d_3 can be large but we only need to consider each triple once; the double loop might generate each triple many times? Let's check: In double loop, for each x in D, we loop over all y in D, and if y divides d, we compute z = d/y. This will generate each ordered triple (x,y,z) exactly once? Because x is chosen from D (all divisors), y is chosen from D such that y|n/x, and then z determined. Does this generate each ordered triple exactly once? For a given ordered triple (x,y,z) with xyz=n, x is a divisor, y is a divisor, and indeed y divides n/x = y*z, so condition holds. And for each x and y, we get a unique z. So each ordered triple is generated exactly once. However, note that the double loop iterates over all y in D, not just divisors of d. But for those y that do not divide d, they are skipped. So total number of iterations is tau(n)^2, but the number of valid (x,y) pairs is exactly the number of ordered triples d_3(n). So we are not generating duplicates; we are generating each ordered triple exactly once. So it's fine.
But we can possibly reduce loops by for each x, generating only divisors of d. However, generating divisors of d from the list D by scanning all D is acceptable. So we proceed.
Implementation details:
Factor n: for i=2; i*i <= n; i++ while n%i==0, add exponent.
After loop, if n>1, it's a prime factor.
Generate divisors: we can use recursion: vector divs; function dfs(index, current). Start with current=1; for each prime factor with exponent e, multiply current by p^0..p^e.
Sort divs maybe for no reason.
But careful: n up to 1e9 fits in 32-bit int, but for multiplication we use 64-bit.
Now, double loop: Use long long for variables.
Potential pitfalls: We must ensure that when computing stolen, we use 64-bit multiplication to avoid overflow. In C++, we can cast to long long.
Time complexity: factoring O(sqrt(n)) ~ 31623; generating divisors O(tau(n)) ~ 1344; double loop O(tau(n)^2) ~ 1.8M. All trivial.
But wait: tau(n) can be as high as 1344, then tau^2 = 1.8M. That's fine. However, d_3(n) could be up to 136080, which is the number of valid (x,y) pairs. So the double loop will perform about tau(n)^2 = 1.8M checks, which is less than d_3? Actually d_3(n) = sum_{d|n} tau(d). That is less than tau(n)*max tau(d) but could be more than tau(n)^2? Let's check: tau(n)=1344, sum_{d|n} tau(d) could be larger than 1344^2? No, because sum_{d|n} tau(d) is at most tau(n) * max tau(d) but max tau(d) could be also around tau(n). Actually tau(d) for d dividing n can be up to tau(n) if d = n. So sum_{d|n} tau(d) ≤ tau(n) * tau(n) = tau(n)^2? Because there are tau(n) divisors, each with tau(d) ≤ tau(n). So d_3(n) ≤ tau(n)^2. In fact, d_3(n) = sum_{d|n} tau(d) ≤ tau(n) * tau(n) = tau(n)^2. But equality is approached if all divisors have tau(d)=tau(n)? Not possible but bound is fine. So d_3(n) ≤ tau(n)^2. Actually tau(n)^2 is an upper bound, but d_3(n) could be less. For our example, tau(735134400)=1344, tau(n)^2 = 1,806,336. d_3(n) = 136,080, which is less. So double loop over all divisors (x) and for each over all divisors (y) yields tau(n)^2 iterations, which is about 1.8M worst-case, which is fine. But we could do a more efficient generation: for each x, only iterate over y that are divisors of n/x. But to get those efficiently, we could have a map from divisor to its index, and for each x, we factor n/x? Or we can simply use the scanning method. That's simplest.
But note: the scanning method uses condition d % y == 0, which involves modulus operation. 1.8M mod operations are fine.
One more nuance: The divisors list includes all divisors of n, but x and y must be positive. That's fine.
Thus the solution is straightforward.
But is there any scenario where (x,y,z) yields A, B, C such that the dimensions are not valid? As we argued, A>=2,B>=3,C>=3 => x>=1,y>=1,z>=1. So all positive. So fine.
Now, about the sample: For n=4, we got min 28, max 41. Good.
Thus we can implement.
But wait, is there any possibility that the minimum or maximum is achieved at some triple where some of y or z are 0? No, they must be positive.
Thus solution accepted.
But before finalizing, we should consider if there is a more mathematical approach that could be simpler, but the brute force is fine.
Now, we need to ensure that we use 64-bit output and not overflow.
Edge Cases:
n=1: only triple (1,1,1). Then stolen = (2)(3)(3) -1 = 18-1=17.
n=3: prime => (1,1,3): 235 -3 =30-3=27; (3,1,1): 433 -3=36-3=33. So min=27, max=33.
n=4: as above min 28 max 41.
n=5: prime: (1,1,5): 237=42-5=37; (5,1,1): 633=54-5=49 => min 37 max 49.
n=6: factorizations: (1,1,6): 238=48-6=42; (1,2,3): 245=40-6=34? Wait (1,2,3): (2,4,5)=245=40-6=34; (1,3,2): same 34; (1,6,1): 283=48-6=42; (2,1,3): 335=45-6=39; (2,3,1): 353=45-6=39; (3,1,2): 434=48-6=42; (3,2,1): 443=48-6=42; (6,1,1): 733=63-6=57. So min=34, max=57. Let's compute also (2,2,?) 22? =6 => ?=1.5 not integer. So correct. So min 34, max 57. Would be interesting to test our program later.
Thus algorithm seems correct.
Now, we must ensure to generate divisors correctly. We'll use recursion:
vector divs;
function<void(int idx, long long cur)> = for each prime factor at idx, for exponent from 0 to exp[idx], cur *= p^exp and recurse.
After generating, we can sort divs if needed but not required.
Then double loop:
long long min_stolen = LLONG_MAX, max_stolen = 0;
for (long long x : divs) {
long long d = n / x; // n is int, but use long long.
for (long long y : divs) {
if (d % y != 0) continue;
long long z = d / y;
long long stolen = (x+1) * (y+2) * (z+2) - (long long)n;
// Note: (x+1)(y+2)(z+2) may be large but within 64-bit.
if (stolen < min_stolen) min_stolen = stolen;
if (stolen > max_stolen) max_stolen = stolen;
}
}
Then output min_stolen and max_stolen.
But we need to consider if the triple (x,y,z) might be repeated due to same values? Not a problem.
Now, one more nuance: The expression (x+1)(y+2)(z+2) might overflow 32-bit if we use ints. We'll use long long for all variables.
Now, we might consider that d = n/x could be computed as integer. Since n and x are both ints up to 1e9, n/x is integer.
Implementation Steps:
Read integer n (int).
Factor n: Use int temp = n; vector<pair<int,int>> factors; for i=2; i*i <= temp; i++) { if temp%i==0 { int cnt=0; while temp%i==0 { temp/=i; cnt++; } factors.push_back({i,cnt}); } } if temp>1 factors.push_back({temp,1}).
Generate divisors: vector divs; function dfs(int idx, long long cur) { if (idx == factors.size()) { divs.push_back(cur); return; } int p = factors[idx].first; int exp = factors[idx].second; long long mult = 1; for (int e=0; e<=exp; e++) { dfs(idx+1, cur*mult); mult *= p; } }. Start with cur=1.
After generating, we can optionally sort divs (not needed). But for potential optimization in inner loop, we could sort to break early if y > d? But we need to check divisibility, so not helpful.
Alternative: we could generate divisors of d by using the prime factorization and only iterating over divisors of d. But scanning all divs is okay.
But note: In double loop, we might compute the same stolen for different permutations of y and z, but that's okay.
One more optimization: We could notice that stolen depends only on x and the sum y+z and product yz (which is d/x? Actually yz = d). But we already have d. So we could compute stolen as yz + 2(x+1)(y+z) + 4(x+1). Since yz = d. So stolen = d + 2(x+1)(y+z) + 4(x+1). That might be slightly simpler, but we still need y+z. For given x and d, we need to consider all factor pairs (y,z) of d, i.e., all y dividing d, then z = d/y, and then compute y+z. So still need to iterate over divisors of d. No simplification.
But we could possibly find min and max of stolen analytically for fixed x by examining factor pairs of d: stolen = d + 2(x+1)(y+z) + 4(x+1). Since d and x are fixed, stolen is monotonic in (y+z). So for fixed x, the minimum stolen occurs when y+z is minimized, and maximum when y+z is maximized, over positive factor pairs (y,z) of d. And y+z is minimized when y and z are as close as possible (i.e., around sqrt(d)), and maximized when one is 1 and the other is d. So we could for each x compute min and max stolen by only considering the factor pairs that minimize and maximize y+z. Then we wouldn't need to iterate over all y, just find the two extreme sums. That would reduce complexity significantly, especially if we have many divisors. Since tau(n) is small anyway, but could be used. However, we must ensure we consider both orders? Since y and z are symmetric, the sum is the same for (y,z) and (z,y). So we can treat unordered pairs. So for each x, we need to find among divisors y of d, the minimal and maximal of y + d/y. Since y can be any divisor, and note that as y runs over divisors, the function f(y)= y + d/y is symmetric and convex? Actually it's minimized when y is close to sqrt(d), and maximized at extremes y=1 or y=d. So we can find the divisor y that gives smallest sum (by scanning divisors of d) and largest sum (by taking y=1 or y=d, if they are divisors). But note: y must be a divisor of d. d = n/x. Since we have list of divisors of n, we could find divisors of d by checking which divisors of n divide d. But we can also generate divisors of d directly using factorization of d? Instead of scanning all divs each time, we could precompute something? But scanning all divs for each x is fine. However, we can optimize by for each x, we only need to find among divisors of d, the min and max sum. So we can compute the divisors of d by scanning the full list of divisors and filter. But to find min and max sum we still need to examine all divisors y of d. That's essentially the same as before, but without computing stolen for each y, we could just compute sums. But we still need to iterate over y that divide d. So we can still do double loop but compute min and max per x without storing all, but still O(tau(n)^2). That's acceptable.
Given simplicity, I'll stick to double loop.
But we must ensure we don't exceed time limit. 2 million operations is fine. There's only one test case.
Thus solution is straightforward.
Now, confirm that the problem expects us to consider all ordered triples. I'll test with n=12 we already did and got correct.
Now, let's double-check sample n=4: Our double loop would produce many triples, but we need to confirm that min is 28, max 41. Let's compute quickly with our formula: For (1,1,4): stolen = 32; (1,2,2): 28; (1,4,1): 32; (2,1,2): (3,3,4?) Wait x=2,y=1,z=2: (3,3,4) =36? Actually (x+1)=3, (y+2)=3, (z+2)=4 => 36, -4=32? Let's compute: 334=36-4=32. (2,2,1): 343=36-4=32. (4,1,1): 533=45-4=41. So min 28 max 41. So correct.
Thus answer.
Now consider potential pitfalls: n=1e9, which is 1000000000. Factorization up to sqrt(1e9)=31623. We'll loop i from 2 to sqrt, but we can optimize by incrementing i by 1? That's 31623 iterations, which is fine. But if we do while (i*i <= n) and i++ each time, that's 31623 iterations, each with a modulus check. That's okay.
However, for a prime close to 1e9, we will test division by all odd numbers? Actually we loop i=2 to sqrt, but we can improve by checking 2 separately, then odd numbers from 3. But it's still fine.
Thus code is simple.
But let's double-check the condition: The original shape had A layers high, each layer had B rows and each row had C blocks. After theft, they took away a layer from front, back, top and sides. So the remaining is (A-1) × (B-2) × (C-2). That is correct. So our variables mapping is correct.
One more nuance: Could the thieves have taken the layers in a different order? No.
Now, we must note that the answer could be large, we need 64-bit output.
Implementation details in C++:
Use long long for variables that might exceed int range.
For input and output, we can use cin, cout with long long.
For generating divisors, use vector and recursion.
Potential integer overflow during divisor generation: cur * mult may exceed 1e9? Actually cur and mult are up to n (1e9). However cur is product of powers; at most n, so fine. But we might have intermediate multiplication up to n, which fits in 64-bit. We'll use long long.
Now, we could also try to reduce loops: Since stolen = (x+1)(y+2)(z+2) - n, and n = xyz. For fixed x, we want to minimize and maximize f(y,z) = (x+1)(y+2)(z+2). Because n constant, we could also compute as earlier. But double loop fine.
Let's test with some random n to ensure correctness.
Also, note that the problem note says: "Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator." So we can just use cin/cout with long long.
Now, we should write the final solution.
But before finalizing, I want to consider if there's any possibility that the minimum stolen is achieved when x, y, z are not divisors? Obviously they must be integers dividing n. So yes.
Now, edge case: n=0? Not allowed, as n >=1.
Now, potential pitfalls: In the double loop, d = n / x; but x is a divisor, so integer division exact.
Now, let's think about possible performance: 1.8M modulus operations, each on 64-bit integers, is trivial.
Thus solution is ready.
But there might be an even simpler mathematical insight: The stolen amount can be expressed as (y+2)(z+2) + 2(x+1)(y+z) + 4(x+1). Since yz = d. So stolen = d + 2(x+1)(y+z) + 4(x+1). For fixed x, as we vary factor pairs (y,z) of d, the sum y+z ranges between minSum and maxSum. The min sum occurs when y and z are as close as possible. The max sum when one is 1 and other is d, if d has divisor 1 (always) and d (since d divides itself). So max sum = 1 + d. So for each x, the maximum stolen is d + 2(x+1)(1 + d) + 4(x+1) = d + 2(x+1)(d+1) + 4(x+1) = d + 2(x+1)d + 2(x+1) + 4(x+1) = d(1+2(x+1)) + 6(x+1) = d(2x+3) + 6(x+1). Wait, check: 2(x+1)(d+1) = 2(x+1)d + 2(x+1). So stolen_max_x = d + 2(x+1)d + 2(x+1) + 4(x+1) = d(1+2(x+1)) + 6(x+1) = d(2x+3) + 6(x+1). Since d = n/x, stolen_max_x = (n/x)(2x+3) + 6(x+1) = 2n + 3n/x + 6x + 6? Actually compute: d(2x+3) = (n/x)(2x+3) = 2n + (3n)/x. Then plus 6(x+1) = 6x+6. So stolen_max_x = 2n + (3n)/x + 6x + 6. Check with sample: n=4, x=4 (since max from x=4 gives stolen 41). Compute: stolen_max_x = 24 + (34)/4 + 64 + 6 = 8 + 12/4? Wait (3n)/x = (12)/4 = 3. So = 8+3+24+6 = 41. Yes matches. For x=1, max would be: 24 + (12)/1 + 61+6 = 8+12+6+6=32, which matches (1,1,4) gives 32? Actually (1,1,4) is 32, yes. So that formula works for max per x, given that d has divisor 1 (always) and d (always), so indeed the pair (1,d) is always a factor pair? y and z must be positive integers, so y=1, z=d works because 1d = d, and 1 is a divisor of d? Since d is integer, 1 always divides d. So yes, (1,d) is always a factor pair. So for each x, there exists y=1, z=d (or y=d,z=1). So maximum sum y+z = 1+d. So we can compute max stolen per x without iterating.
Similarly, minimum stolen per x: We need the factor pair (y,z) of d that minimizes y+z. Since y and z are positive integers with product d, the sum is minimized when y and z are as close as possible to sqrt(d). Among divisors of d, the pair with y being the largest divisor ≤ sqrt(d) and the complementary divisor z = d / y yields minimal sum. So we need to find for each d, the divisor y closest to sqrt(d). This can be done by iterating over divisors of d. But we could also compute this by scanning the list of divisors of n? But we can compute it per x. To find min, we still need to scan divisors of d to find the one that minimizes y + d/y. That's essentially what the double loop would do. However, we could compute min and max per x by just scanning divisors of d (subset of all divisors of n). Since we have all divisors of n, we could for each x, filter the divisors of d and compute min and max sum. That is similar to double loop but with early break? But we might as well just double loop and compute stolen for each, since that gives both min and max anyway. But note: For max per x, we could compute directly without scanning all y, but that doesn't reduce complexity much because scanning all y is already done. So it's okay.
But there might be a more global optimization: The overall min stolen might be achieved when x is as large as possible? Let's test: For n fixed, stolen as function of (x,y,z). There might be monotonicities: For fixed product xyz=n, the expression (x+1)(y+2)(z+2) seems larger when x is larger? Not necessarily; we saw for n=4, min occurs at x=1 (with y,z balanced), max at x=4. So we can't just pick extremes without scanning.
Given that the divisor enumeration is simple and safe, we'll proceed.
But we need to be careful about generating divisors for n up to 1e9; using recursion is fine.
Now, let's think about potential overflow in divisor generation: cur * mult may exceed 1e9 before checking? Actually we only multiply by primes up to exponents; the product stays ≤ n (1e9) because we are generating divisors. But if we multiply cur by p^e in loops, we may exceed n if we do not bound. In recursion we always multiply cur by p^k for k ≤ exponent, but cur could already be some product, and the product should be ≤ n. Since we are generating divisors, it's fine. But we need to ensure we use long long for cur to avoid overflow during intermediate multiplication before checking? But the product will not exceed n, but if we accidentally multiply p beyond exponent? We'll code correctly.
Implementation steps:
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n; cin >> n;
// factor n
int temp = n;
vector<pair<int,int>> factors;
for (int i = 2; i * i <= temp; ++i) {
if (temp % i == 0) {
int cnt = 0;
while (temp % i == 0) {
temp /= i;
cnt++;
}
factors.push_back({i, cnt});
}
}
if (temp > 1) factors.push_back({temp, 1});
// generate divisors
vector<ll> divs;
function<void(int, ll)> dfs = [&](int idx, ll cur) {
if (idx == factors.size()) {
divs.push_back(cur);
return;
}
int p = factors[idx].first;
int exp = factors[idx].second;
ll mult = 1;
for (int e = 0; e <= exp; ++e) {
dfs(idx+1, cur * mult);
mult *= p;
}
};
dfs(0, 1);
// now double loop
ll min_stolen = LLONG_MAX, max_stolen = 0;
for (ll x : divs) {
ll d = n / x; // d = y * z
for (ll y : divs) {
if (d % y != 0) continue;
ll z = d / y;
ll stolen = (x + 1) * (y + 2) * (z + 2) - (ll)n;
if (stolen < min_stolen) min_stolen = stolen;
if (stolen > max_stolen) max_stolen = stolen;
}
}
cout << min_stolen << " " << max_stolen << endl;
return 0;
}
Check for n=1: factors: empty? Since for loop i=2... sqrt(1) -> ii<=1 false, then temp=1, no remainder. So factors remains empty. Then dfs with idx=0 and size=0, pushes cur=1. divs = [1]. Then double loop: x=1, d=1/1=1. Loop y in divs: y=1, d%1==0, z=1/1=1. stolen = (1+1)(1+2)(1+2) - 1 = 23*3-1=18-1=17. min=17, max=17. Output correct.
But for n=0? Not possible.
Now, we must be careful about integer types: n is int, but when computing d = n / x, we need to ensure n is cast to ll? Actually x is ll, n is int, but division yields integer. It's safe to do ll d = n / x; because n will be promoted to ll. But better to store n as ll.
Also, we need to ensure that stolen calculation uses ll.
Third sample 12: we already manually did and got min 48, max 105.
So solution works.
Now, confirm that we are not missing any triple because our divisors list only contains divisors of n, but y must be a divisor of d, and d divides n, so y is a divisor of n? Yes, because y divides d and d divides n, so y divides n. So y is in the set of divisors of n. So scanning all divisors of n is correct.
Now, potential performance: For n=735134400, tau(n) ~ 1344. Then inner loop will be about 1344^2 ≈ 1.8M. That's fine.
But wait: d = n/x may be a number, and d % y check is O(1). So total operations ~2 million, trivial.
Now, memory: divisors list size ~1.3k.
Thus solution is efficient.
But we might also consider using 64-bit integer for n. I'll store n as long long.
Now, consider potential pitfalls: The recursion for generating divisors might cause deep recursion but depth = number of distinct primes <= about 9 (since product of first 9 primes exceeds 1e9? Actually 23571113171923 = 223092870? Wait that product is 223092870? Actually earlier we computed 2357111317*19 = 9699690, times 23 = 223092870, still less than 1e9; times 29 = 6.47e9 >1e9, so at most 9 distinct primes. So depth <=9, fine.
Now, we should ensure that we handle the case when n=1 correctly (factors empty). The DFS function with empty factors will call dfs(0,1) and immediately push 1. Works.
Now, note: In some judges, using recursion might cause stack overflow? Depth <= 9, fine.
Now, final code.
But there is a potential for speed improvement: we could pre-sort divisors to break early in inner loop? Not needed.
Thus final solution is ready.
Now, think about alternative approach: directly iterate over all factor triples via loops over exponents? Might be even simpler: For each exponent combination? But generating divisors is easier.
Let's produce final answer.
Now, also consider that the problem might have input multiple test cases? The statement says "The only line contains integer n". So single test case.
Thus final solution.
But before finalizing, I'll test with some random values to ensure we don't get unexpected results.
Now, check with n=2 we earlier computed min=22 max=25.
Now, test with large n random to ensure no overflow.
Now, double-check that we are not missing cases where x,y,z are not divisors because they might be larger than n? They are divisors because product is n, each factor <= n. So yes.
Now, ensure we treat x,y,z as positive integers. Good.
Now, final code.
But wait, one more nuance: In the problem statement, after theft, the barn only had a parallelepiped containing (A-1)×(B-2)×(C-2) hay blocks. But then thieves dissembled that into single blocks. So n = (A-1)(B-2)(C-2). So that's what we used. So yes.
Now, confirm that the sample note uses A=2,B=4,C=4 gives (2-1)×(4-2)×(4-2)=1×2×2=4, matches. For A=5,B=3,C=3 gives (5-1)×(3-2)×(3-2)=4×1×1=4. So both are valid. So there is no additional constraint like B and C must be >=4? B=3 is allowed; they have B=3 in sample. So B and C can be 3. Good.
Thus final solution.
Now, we might consider the possibility that the answer is symmetrical: min and max can be computed by considering extremes of x and y+z. But we already have code.
I'll produce final answer.
But I should also think about potential edge cases: n is very large, like 1e9 = 2^9 * 5^9? Actually 2^9=512, 5^9=1953125, product = 1e9 exactly? 5121953125 = 1,000,000,000. So that factorization yields exponents: 2^9,5^9. Then tau(n) = (9+1)(9+1)=100. So divisors = 100, which is small. d_3(n) = product of binomial(e_i+2,2) = C(11,2)=55 for 2? Actually for each prime exponent e=9, C(e+2,2)=C(11,2)=55. So total d_3 = 55*55=3025. So still small.
Thus our code is safe.
Now, implement and test.
I'll write the code accordingly.
But I note: In the inner loop, we compute stolen = (x+1)(y+2)(z+2) - n. Since we are using long long, fine.
Potential micro-optimizations: pre-compute y+2 and maybe but not needed.
Now, final solution in C++.
But the problem statement says: "Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator." So we will use cin and cout with long long.
Now, final code:
code
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <climits>
using namespace std;
typedef long long ll;
int main() {
int n_int; cin >> n_int;
ll n = n_int;
// factor n
int temp = n_int;
vector<pair<int,int>> factors;
for (int i = 2; i * i <= temp; ++i) {
if (temp % i == 0) {
int cnt = 0;
while (temp % i == 0) {
temp /= i;
cnt++;
}
factors.push_back({i, cnt});
}
}
if (temp > 1) factors.push_back({temp, 1});
vector<ll> divs;
function<void(int, ll)> dfs = [&](int idx, ll cur) {
if (idx == (int)factors.size()) {
divs.push_back(cur);
return;
}
int p = factors[idx].first;
int e = factors[idx].second;
ll mult = 1;
for (int i = 0; i <= e; ++i) {
dfs(idx+1, cur * mult);
mult *= p;
}
};
dfs(0, 1);
ll min_stolen = LLONG_MAX, max_stolen = 0;
for (ll x : divs) {
ll d = n / x;
for (ll y : divs) {
if (d % y != 0) continue;
ll z = d / y;
ll stolen = (x + 1) * (y + 2) * (z + 2) - n;
if (stolen < min_stolen) min_stolen = stolen;
if (stolen > max_stolen) max_stolen = stolen;
}
}
cout << min_stolen << " " << max_stolen << endl;
return 0;
}
But note: We used n_int and n (long long). Could just use long long n from the start.
Let's adjust: read into long long n directly. That's fine.
Potential optimization: we can also compute the divisors without recursion using iterative method. But recursion is fine.
Now, test on sample:
Input: 4 -> output 28 41.
Works.
Now, note: For n large, divisors may not be sorted; but that's okay.
Now, I will double-check the handling of n when it's 0? Not needed.
Now, ensure that we include ? Not needed.
Now, potential bug: In inner loop, we use d % y. d and y are long long. That's fine.
Now, consider performance: The inner loop loops over all divisors for each x. That includes many checks where d % y != 0. For typical n with many divisors, that's okay.
Now, one more subtlety: The thieves took away a layer from front, back, top, and sides. That means they removed blocks from five faces. The remaining core is (A-1)(B-2)(C-2). However, does the remaining core include the bottom layer? Yes, bottom is not removed. So the core includes the bottom layer, which is at A=1 (if we index from bottom). So that's consistent.
Now, about the note: "they completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides." It might also mean they took a layer from each of those faces, but they also "completely dissembled" the remaining? No, they dissembled the remaining and scattered. So the barn contains n blocks (the core). That is what we used.
Thus solution is correct.
I'll now produce final answer with explanation.
But there might be a more clever way: We could compute min and max stolen directly via formulas using divisors, but as described, our approach is simple.
Now, consider memory and speed: fine.
Now, final answer.
But before finalizing, I'll consider if there's any trick: Could there be triple where some of y or z is 0? Not allowed. So ok.
Now, produce final solution.
But I think there is a nuance: The problem says: "the barn only had a parallelepiped containing (A - 1) × (B - 2) × (C - 2) hay blocks." Then "To hide the evidence of the crime, the thieves had dissembled the parallelepiped into single 1 × 1 × 1 blocks and scattered them around the barn." So the number of blocks after scattering is exactly that product. So n is that product. So our mapping is correct.
Now, answer.
Now, final code: I'll present as final. We need to find the minimum and maximum possible number of stolen hay blocks given that after the theft exactly n blocks remain.
Originally the hay formed a rectangular parallelepiped of dimensions (A), (B), (C) (all integers).
The thieves removed one layer from the top, front, back, left, and right faces (five faces).
Hence the inner untouched parallelepiped has dimensions ((A-1)), ((B-2)), ((C-2)), and its volume is (n = (A-1)(B-2)(C-2)).
The stolen amount is (A B C - n).
Let
(x = A-1\ge 1), (y = B-2\ge 1), (z = C-2\ge 1).
Then (n = x y z) and stolen = ((x+1)(y+2)(z+2) - n).
Thus for a given (n) we must consider all ordered triples ((x,y,z)) of positive integers with product (n), compute the stolen value, and output the minimum and maximum.
Since (n \le 10^9), its number of divisors (\tau(n)) is at most about 1344.
We can factor (n), generate all its divisors, and then for every divisor (x) and for every divisor (y) that also divides (n/x) we set (z = (n/x)/y).
This enumerates each ordered triple exactly once, and the total work is (O(\tau(n)^2)) which is well within limits.
All intermediate values fit in 64‑bit integers (stolen (\le 9n)).
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
// factorize n
vector<pair<int, int>> factors;
ll temp = n;
for (int i = 2; i * i <= temp; ++i) {
if (temp % i == 0) {
int cnt = 0;
while (temp % i == 0) {
temp /= i;
++cnt;
}
factors.push_back({i, cnt});
}
}
if (temp > 1)
factors.push_back({(int)temp, 1});
// generate all divisors of n
vector<ll> divs;
function<void(int, ll)> dfs = [&](int idx, ll cur) {
if (idx == (int)factors.size()) {
divs.push_back(cur);
return;
}
int p = factors[idx].first;
int e = factors[idx].second;
ll mult = 1;
for (int i = 0; i <= e; ++i) {
dfs(idx + 1, cur * mult);
mult *= p;
}
};
dfs(0, 1);
// enumerate all ordered triples (x, y, z) with xyz = n
ll minStolen = LLONG_MAX, maxStolen = 0;
for (ll x : divs) {
ll d = n / x; // d = y * z
for (ll y : divs) {
if (d % y != 0) continue;
ll z = d / y;
ll stolen = (x + 1) * (y + 2) * (z + 2) - n;
if (stolen < minStolen) minStolen = stolen;
if (stolen > maxStolen) maxStolen = stolen;
}
}
cout << minStolen << " " << maxStolen << endl;
return 0;
}
3=135; 2=270; 2=540; 2=1080. So tau = 1080. That's a candidate. Could we increase tau further? Possibly using larger exponents on 2 and less primes? 2^93^55^27
25
11 = 512
25=3,110,400; 7=21,772,800; 11=239,500,800; tau = 1063
2 = 10
3=180; 2=360; 2=720. That's lower. 2^93^45^3
11 = 512
125
11 = 512
125=5,184,000; 7=36,288,000; 11=399,168,000; tau = 1054
2 = 10
4=200; 2=400; 2=800. So 800. 2^103^55^2
11 = 1024
25
11 = ... 1024
25=6,220,800; 7=43,545,600; 11=479,001,600; still under 1e9? 479M, okay; tau = 1163
2 = 11
3=198; 2=396; 2=792. So 792. 2^63^45^3
11
17 = maybe bigger tau? Let's compute product: 64
125=648,000; 7=4,536,000; 11=49,896,000; 13=648,648,000; 17=11,027,016,000 >1e9. So stop at 13: 648,648,000, tau = (6+1)(4+1)(3+1)(1+1)(1+1)
5
2
2 = 7
4=140; 2=280; 2=560; 2=1120. So 1120. That's higher than 1080. Could we get 1200? Try 2^53^35^27
13
19? That would be many small primes, but exponents small. Let's calculate approximate product: start from product of primes up to 19 = 9699690. 2^5=32, 3^3=27, 5^2=25. So product = 9699690 * 32 27 25. 3227=864; 86425=21600; product = 9699690 * 21600 = about 209.5e9, far above 1e9. So too large. So the maximum tau likely around 1344? I've seen references: max number of divisors for numbers <= 10^9 is 1536? Actually I recall that for n <= 1,000,000,000, the maximum number of divisors is 1344 for 735134400? Let's check: 735134400 factorization? 735134400 = 2^6 * 3^3 * 5^2 * 7 * 11 * 13 * 17? Possibly. tau = (6+1)
(2+1)
(1+1)
(1+1) = 7
3
2
2 = 7
2=1344. So 1344. That's plausible. And product: 2^6=64, 3^3=27, 5^2=25, 7=7, 11=11, 13=13, 17=17. Multiply: 64