We are asked: given X, find a and b such that lcm(a,b) = X, and the maximum of a and b is minimized. Output any pair.
We need to understand: For a given X, we want to factor X into two numbers a and b, but not exactly multiplication; we require lcm(a,b)=X. The lcm of a and b is the product divided by gcd: lcm(a,b) = ab / gcd(a,b). So ab = X * gcd(a,b). That's not simply product = X. To minimize max(a,b), we want a and b as close as possible (since for given product, the sum/minimax is when numbers are close). But product isn't fixed; it's X * gcd. However, we can think: Since lcm(a,b)=X, both a and b must be divisors of X? Actually, lcm(a,b)=X implies that both a and b divide X? Not necessarily: For lcm to be X, both a and b must be divisors of X, because if lcm(a,b)=X, then X is a multiple of a and b. Indeed, lcm is the smallest common multiple, so X must be divisible by a and b. So a | X and b | X. Yes, because lcm(a,b) is multiple of both a and b. So a and b are divisors of X.
Thus we are to pick two divisors a,b of X such that lcm(a,b)=X. Minimize max(a,b). Among all such pairs, output any.
Observation: If a and b are divisors, lcm(a,b) = product / gcd(a,b). Alternatively, we can represent prime factorization: For each prime p, let exponent of X be e. For a, exponent is f (0<=f<=e), for b exponent is g (0<=g<=e). Then lcm exponent = max(f,g). For lcm to be X, we need for each p, max(f,g) = e. That means at least one of f,g equals e, and the other ≤ e. So for each prime, one of the two numbers takes the full exponent e, the other can be any from 0 to e.
We need to choose f and g for each prime such that the numbers a and b (with exponents f,g) have max as small as possible.
We need to minimize max(a,b). Since a and b are products over primes p^f and p^g, the max will be determined by these exponents.
We want to partition the primes into two sets? Actually, we can assign for each prime which number gets the full exponent e, and the other gets some lower exponent. The exponents of the lower one could be chosen to minimize the product? But note: To minimize the maximum, we want the two numbers to be as balanced as possible. The full exponent e on a prime makes that factor big. If we put many primes with high exponents into one number, that number will be large, and the other small. So we want to split the prime powers (or at least the "full contributions") as evenly as possible.
Essentially, we want to find two numbers a,b such that ab = X * d, where d = gcd(a,b). But maybe there's a simpler approach: Since a and b are divisors of X, and lcm(a,b)=X, then a and b are complementary divisors: In terms of prime factorization, if we define for each prime, we must have max(f,g)=e. This is equivalent to saying that the set of primes where a has exponent e (i.e., full) and b has less; and vice versa. Also possibly both have e? That's allowed but then both have full exponent; that would make both numbers equal to X? But if both have exponent e for all primes, then a=b=X, lcm=X, max=X. That's likely not minimal unless X=1? For X>1, we can do better: e.g., X=6=23: we can take a=2,b=3, both have exponents max? Actually 2 exponent 1 for 2, 0 for 3; b exponent 0 for 2, 1 for 3; lcm=2*3=6. So max=3, which is less than 6.
So we need to split the prime powers such that one number gets full exponent for some primes, the other gets full exponent for the others. The exponents for the other primes on each number can be chosen arbitrarily (0 to e). But to minimize the maximum, we likely want to set the lower exponents to be as small as possible, i.e., 0. Because if we give some exponent to the number that already has many full exponents, it would increase it further; if we give it to the other number, it might reduce the maximum if it balances? Actually, consider we have two numbers: a and b. For each prime p^e, we must assign at least one of a or b to have exponent e. The other can have exponent from 0 to e. If we assign the other exponent some value >0, that will increase that number's size, which may increase the maximum if that number is already the larger. But it might also balance if we increase the smaller number? But our goal is to minimize the maximum, not to balance per se; but balancing often minimizes the maximum for a given product of the two numbers? However, product is not fixed. Let's think: The product a*b = X * d, where d = gcd(a,b). If we increase the exponent on the "other" number, we increase both a and b? Not necessarily: If we increase the exponent on the number that does not have full exponent, it might also increase the gcd, since gcd takes min(f,g). Let's consider a simpler view: For each prime, we have two exponent choices (f,g) with 0 ≤ f,g ≤ e, and max(f,g)=e. For minimizing max(a,b), what's the best choice? Suppose we fix which number gets the full exponent e. That is, for each prime, we decide either f=e, g ≤ e, or g=e, f ≤ e. The other exponent can be chosen. The product of the two numbers can be written as p^{f+g}. Since max(f,g)=e, we have f+g >= e (since at least one is e, the other >=0). The sum of exponents for each prime contributes to the product's prime exponent. But the maximum of the two numbers depends on the product of p^f and p^g individually. So we want to shape the two numbers' sizes.
Intuition: The optimal solution is to set the other exponent to 0 for each prime. Because if we give a positive exponent to the number that doesn't have the full exponent, that will increase that number without reducing the other number. Could it ever help to give it a positive exponent? Suppose we have two primes: p^e and q^e. If we assign both full exponents to a, then a = p^e q^e = X, b = 1 (if we set other exponents to 0). Then max = X. If we assign p^e to a, q^e to b, and set other exponents 0, then a = p^e, b = q^e, max = max(p^e, q^e). That's smaller if p^e and q^e are both smaller than X. That's obviously better. Could there be a scenario where we assign full exponents unevenly, but we also give some extra exponents to the smaller number to balance? For example, X = 12 = 2^2 * 3^1. Options: a=3, b=4 -> lcm=12, max=4. a=2, b=6 -> lcm=12, max=6. a=12, b=1 -> max=12. a=12, b=12 -> 12. So best is max=4 (a=3,b=4). Here we assigned full exponent for 2^2 to b (4) and full exponent for 3 to a (3). Exponents other than full are 0. Could we do better with non-zero other exponent? Suppose we assign for prime 2: a exponent 2, b exponent 1 (so b gets exponent 1, a gets full). For prime 3: a exponent 0, b exponent 1 (so b gets full). Then a = 2^2=4, b=2^13=6 -> max=6. Worse. If we assign for 2: a exponent 2, b exponent 0; for 3: a exponent 1, b exponent 1 (so both get full? Actually max=1 => both full? That's allowed but then a=2^23=12, b=3? Wait if for prime 3 we set a exponent 1, b exponent 1 => both have exponent 1 which is e=1, max=1, okay. So a=12, b=3? Actually b=3? b exponent for 2: 0, for 3: 1 => b=3, a=12 => max=12. Not better. So 0 for the non-full seems best.
But is there any scenario where giving the non-full exponent something >0 actually reduces the maximum? Suppose the two numbers are very imbalanced: one is huge, the other is tiny. Giving extra exponent to the tiny number may increase it, making it larger, possibly increasing the maximum if it becomes larger than the huge one? That would increase the maximum, not reduce. However, if the huge number is significantly larger, and we increase the tiny number, the maximum remains the huge number, so the maximum doesn't change, but we might make them more balanced, but that doesn't lower the maximum. To lower the maximum, we need to reduce the larger one. So adding to the smaller doesn't reduce max, it could increase max if it overtakes. So adding extra exponent to either number cannot reduce the current maximum unless we simultaneously reduce the larger one; but we cannot reduce the larger one because it must have full exponent for some primes. So to minimize the maximum, we should avoid adding any extra factor to either number beyond the necessary full exponents. Thus optimal is to set, for each prime, exactly one of the numbers gets exponent e, and the other gets exponent 0. Then a and b become products of disjoint prime power factors of X, partitioning the set of prime power factors.
But is that always valid? Check condition: For each prime, if we set one exponent = e, the other = 0, then max = e, fine. lcm = product of primes with max exponents = e, indeed lcm = X because each prime appears with exponent e in at least one number. So yes, it works. This yields a and b such that a * b = X? Actually if we partition the prime powers, then a * b = product over all primes p^{e} = X. Because each prime power contributes fully to exactly one of a or b. So product = X. Indeed, if we split prime powers, then a * b = X. However, lcm(a,b) = X? Since a and b are coprime? They are not necessarily coprime because if we split prime powers, they are indeed coprime: they share no prime factors. Because each prime appears in only one of the numbers. So gcd(a,b)=1, lcm = a*b = X. Yes.
Thus the problem reduces to: Find a factorization X = a * b with a,b positive integers, and gcd(a,b)=1 (i.e., a and b coprime), such that max(a,b) is minimized. Because any solution with disjoint prime powers yields coprime factorization. And we argued that any optimal solution can be taken to have this property (since adding shared factors only increases max). But we need to verify that there isn't a better solution where the two numbers are not coprime but have a smaller max. Let's consider a counterexample. Suppose X = 72 = 2^3 * 3^2. Partition: a = 8, b = 9 => max = 9. Another possibility: a = 12 (2^23), b = 6 (23)? But check lcm: a=12 (2^2 * 3), b=6 (23). lcm = 2^23 = 12? Actually lcm(12,6)=12, not 72. So not valid. To get lcm=72, each prime's max exponent must be full. So if we set a = 24 (2^33), b = 3 (3^1) then lcm=24? Actually 24=2^33, 3=3, lcm=2^33=24, not 72, because exponent for 3 max is 1, but needed 2. So need exponent 2 for 3 somewhere. So if we set a=24, b=9 (2^33 and 3^2) then lcm = 2^33^2 = 72. But a=24, b=9 => max=24. That's worse. What about a=18 (23^2), b=8 (2^3) => lcm=72, max=18. That's also worse than 9? Actually 18 >9. So the best seems 9.
But could there be a non-coprime factorization with a smaller max? Suppose we try a=12, b=12? lcm(12,12)=12 not 72. Not valid. a=36, b=8? 36=2^23^2, 8=2^3 => lcm=2^33^2=72, max=36. Worse.
What about a=6, b=24? lcm=24? Not 72. So indeed partition of prime powers gives the best: a=8, b=9 => max=9. But could there be a scenario where making them not coprime reduces the maximum? Consider X = 30 = 235. Partition: a=2, b=15 => max=15; a=3,b=10 => max=10; a=5,b=6 => max=6. So best is 6 (5 and 6). Coprime? 5 and 6 are coprime. So max=6.
Now consider X = 16 = 2^4. Partition: only one prime power, so one number must get 2^4, the other must get 1 (since we need at least one to have full exponent; the other can be any divisor? If we set a=16, b=1, then lcm=16, max=16. Could we do better by not splitting? Since only one prime, we cannot split the exponent. The condition requires that at least one number has exponent 4. The other can have exponent k (0 ≤ k ≤ 4). But if we give the other exponent >0, say b=2, a=16? Then lcm=16? Actually a=16, b=2: lcm(16,2)=16, valid. max=16. If a=8, b=2? Then lcm(8,2)=8, not 16. If a=8, b=4? lcm=8. Not 16. If a=8, b=8? lcm=8. So the only way to get lcm=16 is that at least one number has exponent 4. So the other number could be any divisor. But what if we set a=8, b=16? Then max=16. So indeed the minimal max is 16? Could we set a=16, b=16? max=16. So minimal max = 16. But what about a=4, b=16? lcm(4,16)=16, max=16. So still 16. So minimal max = X when X is a prime power? Let's check X=4, example given: output 1 4 => max=4. So indeed minimal max = X. So for X being a single prime power, the answer is (1, X) (or (X,1) or (X, X) but max is X). So the minimal max equals X.
Now consider X = 36 = 2^2 * 3^2. Partition: a=4, b=9 => max=9. Can we get max smaller than 9? Suppose a=6, b=6? lcm(6,6)=6 not 36. a=12, b=9? 12=2^23, 9=3^2 => lcm=2^23^2=36, max=12. a=18, b=4 => max=18. a=36, b=1 => max=36. So 9 seems minimal. So partition works.
But what about X=72 we already did: 8 and 9 gives max=9. Could there be a pair with max=8? Suppose we want max=8, then both a and b ≤8. But one must have factor 2^3=8, so a=8 or b=8. The other must be ≤8 and contain 3^2=9? Not possible because 9>8. So max must be at least 9. So indeed partition is optimal.
Thus the problem reduces to: Given X, we want to split its prime factors into two groups, such that the product of the prime powers in each group (i.e., a and b) are as balanced as possible (i.e., minimize the maximum). And they must be coprime, which is natural if we don't repeat prime factors.
But note: The prime factorization may have exponents >1. For a prime p^e, it's a single "chunk" that must go entirely to one side if we set the other exponent to 0. We cannot split the exponent because that would give both numbers having some exponent but not full? Wait, can we split the exponent? Suppose we assign f=e for a, g = k where 0 ≤ k ≤ e. Then a includes p^e, b includes p^k. That would mean the prime power p^e is not entirely allocated to one side; it's split: the full exponent e goes to a, but b also gets p^k. This would make a and b not coprime. Could that lead to a smaller maximum? Let's examine if splitting the exponent could potentially reduce the max.
Consider X = p^e * q^f ... Suppose we have a prime with exponent e. If we give both numbers some exponent, then the number that gets the full exponent e is at least p^e, the other gets p^k. That extra p^k increases the other number, possibly making the max larger or maybe balancing if the larger number is much larger than the other? But if the larger number is huge, adding p^k to the smaller won't reduce the max; it might increase it if it becomes larger, but can't reduce the larger number. The only scenario where splitting might reduce the max is if we can reduce the exponent on the larger number by not giving it full exponent? But we need max(f,g)=e, so at least one must have exponent e. So the larger number must have at least p^e. So that's a lower bound for that number. However, we might decide to give the full exponent to the number that would otherwise be smaller, making the numbers more balanced. That is the decision of assignment: which number gets the full exponent. But giving extra exponent to the other number (non-full) only increases that number, making the larger possibly larger, or making the smaller larger but still smaller than the larger? It could increase the maximum if it makes the smaller surpass the previous larger? But that would increase max, not reduce. So the optimal is to give the non-full exponent 0.
But there is nuance: Suppose we have multiple primes. The assignment of which number gets full exponent for each prime determines the products a and b. The products are multiplicative. The goal is to minimize max(a,b). This is essentially the partition problem of multiset of numbers (the prime powers) into two subsets to minimize the maximum product. However, we have the option to also give some prime powers partially to both numbers. Could that ever produce a better balance than the partition of whole prime powers? Intuitively, because we can only increase numbers (by adding extra factors) but not decrease the one that already has the full exponent, it seems not beneficial.
Let's attempt to prove: For any valid pair (a,b) with lcm=X, define for each prime p, let f,g as before. Construct a' and b' by setting, for each prime, if f = e then set f' = e, g' = 0; if g = e then set f' = 0, g' = e; if both f=g=e, then assign arbitrarily (say f'=e, g'=0). This yields a' and b' such that a' divides a? Actually not necessarily: For primes where a had full exponent and b had something >0, we set b'=0 (i.e., remove that factor). So a' = product of p^e for primes where f=e (and maybe if both full, we put in one). b' = product of p^e for the other primes. Then a' ≤ a? Because a had p^e, maybe a also had extra factors from other primes? Actually a may have extra factors from primes where it didn't have full exponent? But if a didn't have full exponent, then it had exponent ≤ e, but b had full exponent. In constructing a', we only include full exponents from the set where a has full exponent. So a' might be less than or equal to a? Actually compare: For each prime, in the original a, if f=e, then a includes p^e; in a', we also include p^e. If f<e, then a includes p^f (with f<e), but a' would include either 0 (if we assigned that prime to b') or p^e (if we assigned to a'? But we assign based on who had full exponent. Since f<e, then we must have g=e. So we assign that prime to b' (full exponent to b). So for that prime, a' gets 0, while original a had p^f. So a' might be smaller than a for those primes. So a' ≤ a? Not necessarily: if a had f<e, a'=0 for that prime, so a' ≤ a. If a had f=e, a' includes p^e same as a. So a' ≤ a. Similarly b' ≤ b. Also lcm(a',b') = X because each prime's full exponent is assigned to exactly one of a', b'. Also max(a',b') ≤ max(a,b) because we decreased both numbers. Actually careful: if we decrease both numbers, the maximum could potentially increase? For example, if original a=100, b=10, max=100. After reduction, a' might become 50, b'=5, max=50 < 100. So yes, max decreases or stays same. However, we need to ensure that we didn't inadvertently increase one of them. Since we only removed factors (set exponents to 0 from positive), each number's product becomes less or equal. So max(a',b') ≤ max(a,b). So given any valid pair (a,b), we can construct a pair (a',b') that is of the form "disjoint prime power sets" (i.e., coprime factorization) with max no larger. Hence the minimal maximum will be achieved by such a disjoint assignment. So the problem reduces exactly to: split the multiset of prime power factors of X into two subsets to minimize the maximum of the products.
Thus we need to find a partition of the prime powers (p_i^{e_i}) into two groups such that the product of the numbers in each group is as balanced as possible. Then output the two products. Since X can be up to 1e12, the number of distinct prime factors is limited: the product of the first few primes grows quickly. For 1e12, the maximum number of distinct prime factors is at most something like: 23571113171923293137... Let's compute: product of primes up to 37: 23=6, 5=30, 7=210, 11=2310, 13=30030, 17=510510, 19=9699690, 23=223092870, 29≈6.5e9, 31≈2.0e11, 37≈7.4e12 >1e12. So up to 31 gives around 2e11, next 37 pushes over. So at most about 10-11 distinct prime factors. However, exponents can be >1. But we treat each prime power as a whole factor, not splitting the exponent further. So the number of items in the partition is the number of distinct prime factors (since p^e is considered as one item). That's at most ~12. So we can possibly enumerate all subsets? 2^d where d is number of distinct primes. d max maybe 12, 2^12=4096, which is fine. But 1e12 might have d up to maybe 12? Let's compute worst-case: product of the smallest primes: 235711131719232931 = about 2e11 as said, product of next prime 37 gives 7.4e12 > 1e12. So max distinct primes is 11 (if X is product of first 11 primes: 235711131719232931 = 200560490130? Actually let's compute: product of primes up to 31: 2*3=6, *5=30, *7=210, *11=2310, *13=30030, *17=510510, *19=9699690, *23=223092870, *29=6469693230, *31=200560490130. That's ~2e11? Actually 200560490130 = 2.0056e11. That's less than 1e12. Next prime 37: multiply gives ~7.42e12 >1e12. So indeed maximum distinct primes is 11, but 200560490130 < 1e12, so could also include another prime factor maybe 2 again? But that would be same prime, not distinct. So maximum distinct primes ≤ 11. Actually X can be as large as 1e12, so product of the first 11 primes is ~2e11, still less than 1e12, so it's possible to have 12 distinct primes? Let's check: multiply by 37 gives ~7.4e12 >1e12, so 12 distinct primes product >1e12 for smallest primes. So maximum distinct primes is 11, but wait maybe we can have 12 if we include 2^something? No, distinct primes count only distinct. So at most 11. However, X could be, say, 2^something * 3 * 5 * ... but that doesn't increase distinct count. So indeed d ≤ 11.
Thus we can simply factor X, get list of prime powers (p^e). Then we need to partition these factors into two groups to minimize max(product_group1, product_group2). Since d is small, we can iterate over all subsets? The number of subsets is 2^d, but we only need to consider assignments to group A (the rest to B). For each subset, compute product of powers in that subset (call it A), then B = X / A? Wait careful: If we assign each prime power entirely to one group, then the product of the two groups multiplied together equals product of all prime powers = X. Because each p^e is assigned to exactly one group. So if group A product = a, then group B product = X / a. Indeed a * b = X. So we can simply iterate over all possible a that are divisors of X that can be formed by product of some subset of prime powers (i.e., any divisor of X that is squarefree in exponent? Actually any divisor of X that is composed of full prime powers? Not exactly: any divisor of X that is product of some of the p^e factors. That means a must be a divisor of X that is "unitary"? Not necessarily; it's a divisor that, for each prime, either takes the whole exponent e or 0. So it's a divisor that is of the form ∏_{i in S} p_i^{e_i}. So it's a specific subset of divisors. So we can generate all such a by iterating over subsets of prime factors.
Then compute b = X / a. Then max(a,b). We want to minimize max(a,b). Since max(a,b) is minimized when a and b are as close as possible, i.e., a as close to sqrt(X) as possible, but only over such special divisors. So essentially we need to find the divisor a of X that is product of some prime powers, such that a ≤ sqrt(X) and as large as possible? Actually we want to minimize max(a,b). For given a, max = max(a, X/a). This is minimized when a is as close as possible to sqrt(X). Because if a ≤ sqrt(X), then max = X/a, which decreases as a increases. If a ≥ sqrt(X), then max = a, which decreases as a decreases. So the optimal is the divisor a (of this special type) that is closest to sqrt(X). And then we output (a, X/a). Note that a and X/a are coprime (since they partition prime powers). So that's valid.
Thus we can factor X, get list of prime powers v_i = p_i^{e_i}. Then compute all possible products from subsets. Since d ≤ ~11, we can generate all products via recursion or bitmask. Then for each product a, compute b = X / a, and compute max(a,b). Keep the pair with smallest max. Since we want any, we can output that pair.
We must be careful about overflow: X up to 1e12, products can be up to X. Use 64-bit integer (long long). Number of subsets up to 2^11=2048, well within limits.
Edge Cases: X=1. Then there are no prime factors. The only way? For X=1, lcm(a,b)=1 implies a and b must both be 1? Actually lcm(1,1)=1. So a=b=1. Our method: list of prime powers is empty. Subset products: only product = 1 (empty product). So a=1, b = 1/1 = 1. max=1. Output 1 1. Good.
For prime powers (single prime factor), d=1. Subsets: a=1 or a=p^e. sqrt(X) maybe not integer. The two possible a: 1 and X. max(1,X)=X; max(X,1)=X. So the minimal max is X. Both are same, but we could output (1,X) as per example. So algorithm would choose either? Compute both: for a=1, max=X; for a=X, max=X. Both equal. We can pick the one with smaller max, both equal, so either. But we need to ensure we pick any. We can just iterate and keep the one with smallest max; if tie, any. So that's fine.
Implementation: factor X. Since X ≤ 1e12, we can factor by trial division up to sqrt(X) (1e6). sqrt(1e12)=1e6, so we can do trial division by i from 2 to 1e6. That's acceptable? 1e6 iterations per test case, and only one test case, so it's fine in C++ (fast). In Python, 1e6 may be okay if optimized, but we can also use a faster method: iterate i up to int(sqrt(X)) and break when i*i > X. Since X decreases as we divide, it might be faster. But 1e6 is okay in Python? Possibly borderline but likely fine with PyPy; 1e6 operations is trivial. However, we must be careful: In Python, a loop up to 1e6 doing division and modulo is fine (<0.1 sec). But we also need to consider that we might divide by i up to sqrt(1e12) = 1,000,000, that's 1 million iterations, which is okay. So we can do:
def factorize(x):
factors = []
i = 2
while i*i <= x:
if x % i == 0:
cnt = 0
while x % i == 0:
x //= i
cnt += 1
factors.append(i**cnt) # store prime power
i += 1 if i == 2 else 2? We can just increment by 1, it's okay for 1e6.
if x > 1:
factors.append(x) # x is prime, exponent 1
return factors
But careful: For x up to 1e12, after dividing out small factors, the remaining x could be a prime > 1e6, but its square would exceed 1e12? Actually if x > 1 and after loop, it could be a prime > 1e6. Since we stopped when ii > original x? Actually we need to loop while ii <= current x. But as we divide, x shrinks. At the end, if x > 1, it is a prime (since any composite would have a factor <= sqrt(x), but sqrt(x) might be less than the current i? Actually after loop condition ii <= original x? Typically we loop while ii <= n, but as we divide n, we can update limit: while ii <= n. At each step, after dividing, n decreases, so the condition ii <= n becomes tighter. But if we exit loop when ii > n, and n > 1, then n is prime. So we can just do while ii <= x: and after loop if x>1, it's prime. That works.
Now after we have the list of prime powers, we generate subset products. Since number of items d ≤ 11, we can use bitmask from 0 to (1<<d)-1. For each mask, compute product = 1, multiply by v[i] for set bits. But need to be careful with product overflow beyond 64-bit? X up to 1e12, product of all v[i] = X, so any subset product ≤ X, fits in 64-bit.
We'll compute max_val = max(product, X // product). Keep track of min_max and best pair (a,b). Since we need any, we can store the product a for which max is minimal.
But note: For each mask, we get a and b = X//a. The pair (a,b) is valid. Also the complement mask (i.e., all bits flipped) gives the swapped pair. So we can just iterate all masks; both will be considered. The minimal max may be achieved by multiple masks.
Complexities: O(d * 2^d) which is trivial.
But we must ensure that we treat the case when product = 0? No.
Now, is there any nuance about the condition "both a and b should be positive integers"? Yes, they are.
Thus solution seems straightforward.
But let's double-check with examples.
Example 1: X=2 -> factors: [2]. d=1, subsets: mask 0: a=1, b=2, max=2; mask1: a=2, b=1, max=2. min_max=2, output either (1,2) or (2,1). Example gives (1,2). Good.
Example 2: X=6=2*3. factors: [2,3]. subsets: a=1 -> b=6 max=6; a=2 -> b=3 max=3; a=3 -> b=2 max=3; a=6 -> b=1 max=6. min_max=3, output (2,3) or (3,2). Example gives (2,3). Good.
Example 3: X=4=2^2. factors: [4]. subsets: a=1,b=4 max=4; a=4,b=1 max=4. min_max=4, output (1,4) or (4,1). Example gives (1,4). Good.
Example 4: X=1 -> factors: [] subsets: only mask 0: a=1,b=1 max=1 -> output (1,1). Good.
Example: X=12=2^2*3. factors: [4,3]. sqrt=~3.464. subsets: a=1->12 max=12; a=4->3 max=4; a=3->4 max=4; a=12->1 max=12. min_max=4, outputs (4,3) or (3,4). Which is correct? Actually earlier we thought (3,4) gave max=4. So output (3 4) or (4 3). Good.
Example: X=36=4*9? Actually 2^2 * 3^2 -> factors: [4,9]. subsets: a=1->36 max=36; a=4->9 max=9; a=9->4 max=9; a=36->1 max=36. min_max=9 -> outputs (4,9) or (9,4). Works.
Example: X=72=89? Actually 2^33^2 -> factors: [8,9]. min_max=9 -> (8,9). Good.
Now, consider X with many small prime powers: e.g., X = 23571113 = 30030. sqrt ~173.2. Partition prime powers: we need to find subset product closest to sqrt. That can be done via subset enumeration. Possibly output (165, 182)? Actually 165=3511, 182=2713 = 182? 27*13=182, product=30030, max=182. But maybe there is better? We'll rely on enumeration.
Thus algorithm stands.
But wait, is there any scenario where the optimal solution does not correspond to a partition of prime powers? According to the earlier reduction, we argued we can always transform any solution to one with disjoint prime powers without increasing max. But we need to be careful: In the transformation, we set for each prime: if both numbers have exponent e (i.e., both full), we arbitrarily assign one of them to have exponent e and the other 0. That reduces that number? For the number that loses the exponent e, we are removing p^e from it, which reduces it significantly. But could that cause the other number to become larger? No, because we are only reducing numbers, so max decreases or stays same. However, we need to ensure that after transformation, the lcm still equals X. In the case where both had exponent e, we assign full exponent to only one; then for that prime, the other number has exponent 0, but max(f',g')=e still because one has e. So lcm remains X. So it's valid. So indeed there exists an optimal solution with disjoint prime powers. So our approach is correct.
Therefore solution: factor X into prime powers; generate all subset products; find the product a that minimizes max(a, X/a); output a and X/a.
Implementation details:
- Use long long (or Python int).
- Factorization: we can loop i from 2 to sqrt(n) (int(sqrt(n))). For each i dividing n, count exponent, compute power = i**exp, add to list. After loop, if n>1, then it's prime with exponent 1, add n.
- Generate subset products: use recursion or bitmask. Since d ≤ 11, bitmask from 0 to (1<<d)-1 works. For each mask, compute product. However, careful: 2^11 = 2048, but we might compute product by iterating bits each time O(d) per mask, total O(d * 2^d) = ~ 22k, trivial.
- Keep track of best_a such that max(a, X//a) is minimal. If tie, any.
Edge Cases:
- X=1: factor list empty; loop over masks from 0 to (1<<0)-1 = 0? Only mask 0. product=1, max=1.
- If d=0? Actually for X=1, we treat specially? We can handle with list empty and subset generation: if empty, then product=1.
One nuance: When computing product, be cautious about overflow when multiplying? In Python it's fine.
But we must also consider that the prime powers can be large, e.g., 2^40? But X ≤ 1e12, so maximum exponent for 2 is up to log2(1e12) ≈ 40. So p^e fits in 64-bit.
So final solution in Python:
def solve():
import math
X = int(input())
# factorize
n = X
factors = []
i = 2
while i * i <= n:
if n % i == 0:
cnt = 0
pw = 1
while n % i == 0:
n //= i
cnt += 1
pw *= i
factors.append(pw)
i += 1 if i == 2 else 2 # we can speed up by skipping evens after 2, but not necessary
if n > 1:
factors.append(n) # n is prime with exponent 1
# Now generate subsets
d = len(factors)
best_a = 1
best_max = X # since max(1, X) = X
# iterate all subsets
for mask in range(1 << d):
a = 1
for i in range(d):
if mask & (1 << i):
a *= factors[i]
# Since a <= X, no overflow
b = X // a
cur_max = a if a > b else b
if cur_max < best_max:
best_max = cur_max
best_a = a
# Output best_a and X//best_a (order doesn't matter)
print(best_a, X // best_a)
But careful: In Python, if d=0 (X=1), then range(1<<0) = range(1) -> mask=0, a=1, b=1, cur_max=1, best_max initial X=1, so fine.
But note: best_max initially set to X, which for X=1 is 1. Works.
We need to ensure that we don't miss the subset where a = X (mask all bits). That will be considered.
Potential issue: When d is up to 11, the product of factors may exceed X? No, because product of all factors = X exactly. So for mask all bits, a = X, b = 1. So all fine.
Thus this solution is correct and efficient.
But wait: Could there be a scenario where the optimal a is not of the form product of entire prime powers? Our transformation argument suggests not, but let's test with an example to be absolutely sure. Consider X = 8*9 = 72 as earlier. Our algorithm yields a=8 or 9 gives max=9. Could there be a pair with max=8? Not possible as reasoned. Good.
Consider X = 2^2 * 3^1 = 12. Our algorithm yields a=4 or 3 gives max=4. Could there be a pair with max=3? Try a=2, b=6? lcm(2,6)=6, no. a=2, b=12? lcm=12, max=12. a=3, b=4 => max=4. a=6, b=4? lcm(6,4)=12? Actually lcm(6,4)=12, max=6. So no. So max=4 is minimal.
Consider X = 2^3 * 3^1 = 24. Prime powers: 8 and 3. Subset products: 1->24 max=24; 8->3 max=8; 3->8 max=8; 24->1 max=24. So best max=8 -> a=8,b=3 or a=3,b=8. Could there be a pair with max=6? Check possibilities: a=4, b=6? lcm(4,6)=12, not 24. a=12,b=8? lcm=24, max=12. a=6,b=8? lcm(6,8)=24? Actually lcm(6,8)=24, max=8, same as (8,3). But 6 and 8: 6=2*3, 8=2^3. Here for prime 2: a exponent 1, b exponent 3 -> max=3 (good). For prime 3: a exponent 1, b exponent 0 -> max=1 (needs 1? Actually need exponent max=1, a has 1, so ok). So (6,8) is a valid pair, with max=8. So max is still 8. Could there be a pair with max=6? a=2, b=12? lcm=12, no. a=2,b=24? lcm=24, max=24. a=4,b=12? lcm=12, no. a=4,b=6? lcm=12. So no. So max=8 is optimal.
Now consider X = 2^2 * 3^2 * 5^2 = 4925=900. Subset products: try to get near sqrt(900)=30. Partition: 425=100, 9=9 -> max=100? Actually a=100,b=9 => max=100; a=36,b=25 => max=36; a=20? Not possible because 20 requires splitting exponents? Let's compute all subset products: possible a: 1,4,9,25,49=36,425=100,925=225,4925=900. So best is a=36, b=25 gives max=36, or a=25,b=36 max=36. But could there be a non-coprime pair with max less than 36? Try a=30, b=30? lcm(30,30)=30, not 900. a=20,b=45? 20=2^25, 45=3^25? Actually 20=2^25, 45=3^25, lcm=2^23^25=180, not 900. Need factor 5^2=25 in lcm. So at least one number must have 5^2=25. So one number >=25. The other must have 2^23^2=36? Actually to get lcm=900, we need max exponents: 2^2 from somewhere, 3^2 from somewhere, 5^2 from somewhere. So we could have one number = 25, the other = 36? That gives max=36. Could we have one number = 25, other = 18? 18=23^2, lcm(25,18)=450? No. So need exponent 2 for 2 somewhere. If we put 2^2 in the number with 25, then that number becomes 100, other must have 3^2=9, giving max=100. If we put 2^2 in the other number, then that number gets 36, other 25. So max=36. Could we split the exponent of 5^2? For example, a=5, b=20? But then lcm would have 5^1? Actually need exponent 2 for 5. If we set a=5, b=20 (2^2*5), then max exponents: 2^2 from b, 5^1 from a, 5^1 from b, max exponent for 5 = 1, not 2. So not enough. To get exponent 2, at least one must have 5^2. So the number with 5^2 is at least 25. So the other number must provide 2^2 and 3^2, which multiply to 36. So the other number is at least 36 if it includes both. But could we share 2^2 and 3^2 such that one number includes some of them and the other includes the rest? But then the number that doesn't have 5^2 must still provide the missing exponents? Actually if we put 2^2 in the number that already has 5^2, then that number becomes at least 100. The other number then must have 3^2, so at least 9, so max=100. If we put 3^2 with 5^2, that number becomes at least 225, other must have 2^2=4, max=225. So the minimal max is 36, achieved by grouping 2^2 and 3^2 together (36) and 5^2 alone (25). Actually 36 > 25, so max=36. So it matches the subset product approach.
Thus the algorithm seems solid.
Now, could there be a case where the optimal solution does not have a and b coprime? Suppose we have X= p^e q^f, where p and q are primes. If we set a = p^{e} q^{f-1}, b = q^{1}. Then lcm = p^e q^f = X. Compute max. Example: p=2,e=2, q=3,f=2: X=36. a = 2^23^1 = 12, b = 3^1 = 3? Actually b = q^{1} = 3, then lcm(12,3)=12? Not 36. Need b to have q^f? Actually to get lcm=X, we need max exponent for q to be f. If b = q^1, then max exponent for q is max(f-1? Actually a has exponent f-1, b has exponent 1, so max = max(f-1,1) which is f-1 if f-1 >=1? For f=2, max = max(1,1)=1, insufficient. So b must have exponent f if a doesn't. So to have one number with exponent f, either a or b must have full exponent f. If we try to split exponents, we must ensure at least one has full. So a could have p^e and q^{f-1}, b = q^1 (so b has full q? Actually b has exponent 1, which for f=2 is not full. So lcm would have exponent max(e, f-1?) Actually exponent for q would be max(f-1, 1) = max(1,1)=1, not f. So fails. To have full, one of the two must have exponent f. So if we give a exponent f-1, b exponent f, then a has p^e q^{f-1}, b has q^f. Then lcm = p^e q^f = X. So a = p^e q^{f-1}, b = q^f. Then max = max(p^e q^{f-1}, q^f). Compare to partition solution: a' = p^e, b' = q^f or a'' = q^f, b''=p^e. Which is better? Partition gives max = max(p^e, q^f). The alternative gives max = max(p^e q^{f-1}, q^f). Since p^e q^{f-1} >= p^e (if q>1) and >= q^{f-1}. Typically it may be larger than both p^e and q^f? Compare p^e q^{f-1} vs q^f: which is larger? If p^e > q, then p^e q^{f-1} > q^f? Actually p^e q^{f-1} vs q^f = q * q^{f-1}? Wait q^f = q * q^{f-1}. So p^e q^{f-1} compared to q * q^{f-1} = q^{f-1} * q. So p^e q^{f-1} > q^f if p^e > q. This could be bigger than the partition's max which is max(p^e, q^f). Suppose p^e is 100, q^f is 3, partition max=100. Alternative a = 1003=300? Actually p^e=100, q^f=3. If we set a = 100 * 3^{f-1}? But f=1? If f=1 then q^f=3, then splitting exponent not possible. For f>=2, suppose q=3, f=2, q^f=9. p^e=100. Partition max=100. Alternative: a=1003=300, b=9, max=300 >100. So worse. If p^e is small and q^f is large, say p^e=2, q^f=100, then partition max=100. Alternative: a=210? Actually f=2, q=10? Not prime, but prime q small? Let's take p=2, e=1 (p^e=2), q=5, f=2 (q^f=25). Partition max=25. Alternative: a = 25=10, b=25, max=25. Actually max=25 same? Because a=10, b=25 -> max=25. Partition yields a=2,b=25 -> max=25. So same? Actually a=25=10, b=25 -> max=25. So equal. But could it be smaller? Compare a=10, b=25, max=25 vs a=2,b=25 also max=25. Not better. If p^e is close to q^f, maybe a = p^e q^{f-1} could be less than max(p^e, q^f)? That's impossible because p^e q^{f-1} is at least p^e and at least q^{f-1}, but q^{f-1} may be less than q^f. But max(p^e, q^f) could be either p^e or q^f. Since p^e q^{f-1} >= p^e, if p^e is the max, then a is >= max. If q^f is the max, then b = q^f is exactly the max. So the max of (a,b) is at least max(p^e, q^f). So partition already achieves the lower bound: the max cannot be less than the larger of the two prime powers because one number must contain the largest prime power (since that prime power must appear in at least one number). Actually more precisely, for each prime, the prime power p_i^{e_i} must appear entirely in at least one of a or b. So the maximum of a and b must be at least the maximum of these prime powers. The partition solution yields max = max(product of a subset, product of complement) which may be larger than the largest prime power, but it's balanced. However, could a non-coprime solution give a max that is smaller than the best partition? Suppose we have three prime powers: 2,3,5. Partition: best might be 23*5=30. Partition best: a=6,b=5 max=6 or a=10,b=3 max=10 or a=15,b=2 max=15. So best is 6. Can we get max=5? That would require both numbers ≤5, but then one number must contain 5 (since 5 must appear fully). If that number is 5, the other number must contain 2 and 3 (to get lcm=30) so must be at least 6. So max at least 6. So no.
Thus partition is optimal.
Hence algorithm is correct.
But note: We must consider that the prime factorization may have exponents >1, but we treat the whole p^e as a single item. That yields numbers a and b that are coprime. But is it possible that an optimal solution with non-coprime numbers could achieve a max smaller than the best coprime partition? Let's attempt a scenario with exponents >1 where splitting an exponent between a and b could reduce the max. Example: X = 2^4 * 3^4 = 16 * 81 = 1296. sqrt ≈ 36. Partition of prime powers: groups: 16 and 81 => max=81; 1681=1296 and 1 => max=1296; 1681? That's all. So best max=81? But wait, we could also partition 2^4 and 3^4 separately, but we can also put both in same group: 1681=1296 vs 1 => max=1296. So best is 81. Now consider a non-coprime assignment: Let a = 2^3 * 3^2 = 89=72, b = 2^1 * 3^2 = 29=18? Check lcm: For prime 2, max(3,1)=3, but need 4; missing exponent 1. So not valid. Need exponent 4 for 2 somewhere: Suppose a = 2^4 * 3^2 = 169=144, b = 2^2 * 3^4 = 481=324? Actually compute: a exponent 2: 4 and 2; b exponent: 2 and 4; lcm exponents: max(4,2)=4 for 2, max(2,4)=4 for 3, so lcm = 2^4 * 3^4 = 1296. Then a=144, b=324, max=324. Worse. What about a = 2^4 * 3^3 = 1627=432, b = 3^1 = 3? Need exponent 3? Actually b=3^1 gives exponent 1, but lcm exponent for 3 would be max(3,1)=3, need 4, so not valid. So need b to have exponent 4? Then b large. So seems max can't be <81. Actually lower bound: the larger of the two numbers must be at least the larger prime power (81) because 3^4=81 must be fully in one number, so that number is at least 81. The other number must contain 2^4=16? Not necessarily; the number containing 81 could also contain some of 2^4. But if it contains 81, it's at least 81. If it also contains 16, it becomes 1296, which is >81. So to keep max small, we want the other number to contain the other prime power (16) so that the max is max(81,16)=81. That's the partition solution. If we try to put part of 2's exponent into the 81-containing number, it increases beyond 81, making max larger. If we put part of 2's exponent into the other number but not all, then the other number still must have the full 2^4 somewhere? Actually if we split 2's exponent, say a has 2^4, b has 2^3, then both have less than full? But then max exponent for 2 would be 4 (since a has 4). That's okay. But then a gets 2^4, b gets something. Now consider 3: we need at least one has 3^4. Suppose we assign 3^4 to b, then b = 3^4 * 2^3 = 818=648, a = 2^4=16, max=648. If we assign 3^4 to a, then a = 2^43^4=1296, b=2^3=8, max=1296. So worse. So indeed the best is to keep each prime power whole and assign to separate numbers to balance.
Thus algorithm stands.
Now implement in Python.
But we can also do a more efficient method: Since d is small, subset enumeration is fine.
We must watch out for integer sqrt performance. However, factorization by trial division up to sqrt(X) might be too slow if X is a prime close to 1e12? In worst case, X is a large prime near 1e12, the loop goes up to sqrt(X) ≈ 1e6. That's 1 million iterations, which is fine in Python. But we can optimize by checking only odd numbers after 2. We'll implement:
def factorize(X):
factors = []
n = X
i = 2
while i*i <= n:
if n % i == 0:
cnt = 0
pw = 1
while n % i == 0:
n //= i
cnt += 1
pw *= i
factors.append(pw)
i += 1 if i == 2 else 2
if n > 1:
factors.append(n)
return factors
But careful: after processing all i, if n > 1, then n is a prime factor with exponent 1, so pw = n. Good.
Now subset enumeration: Since d up to 11, 2^11 = 2048, we can even generate recursively to avoid bit operations, but bitmask is fine.
Edge case: X might be 0? No, X >=1.
Now verify with a few random cases.
But there is also a nuance: The factorization might produce large exponents and the prime powers might be computed by repeated multiplication, which is fine.
Complexity: O(sqrt(X)) for factorization (worst 1e6) + O(2^d * d) which is negligible.
But we could also use a faster factorization algorithm like Pollard Rho for safety, but not needed.
Thus Python solution should pass.
Now, we might consider whether there is an even simpler solution: Because we want to find a divisor a of X that is closest to sqrt(X) among divisors that are coprime to X/a? Actually we need a and X/a to be coprime. But any divisor a of X, with b = X/a, they are not necessarily coprime. However, we argued that the optimal a must be of the form that uses entire prime powers. So it's not any divisor; it's a divisor that is composed of whole prime powers. So we need to consider only those.
We can generate all such divisors by taking product of subsets of prime powers.
Implementation:
factors = [p**e for p, e in prime_factorization] (list of prime powers).
Then:
best = (1, X) initially.
For mask in range(1<<len(factors)):
a = 1
for i, val in enumerate(factors):
if mask & (1 << i):
a *= val
b = X // a
if max(a, b) < max(best[0], best[1]):
best = (a, b)
Print best[0], best[1]
But note: When we compute b = X // a, ensure it's integer; it will be because a divides X.
One caution: For large d, 2^d up to 2048, fine.
Now, consider the possibility that there may be multiple prime factors with high exponents but we treat each prime power as a whole. That's correct.
Let's test with X=9999999967? Not needed.
Now produce final answer.
But wait: The problem states X up to 10^12, but factorization by trial division up to sqrt(10^12)=10^6, that's 1e6 iterations. In Python, 1e6 loops is fine (maybe 0.05-0.1 sec). However, we must be careful about the while loop inside factorization that might also iterate many times for powers. That's okay.
However, I recall that Python's loops can be a bit slower, but 1e6 is still acceptable. We can further optimize by only checking up to sqrt(n) and breaking when i*i > n. Also we can skip even numbers after 2. That reduces iterations to about 500k. That's fine.
Alternatively, we could use a precomputed list of primes up to 10^6 via sieve, but that would be overkill.
Thus solution is simple.
Now, think about potential pitfalls:
-
If X is a perfect square? For example X=36, sqrt=6, but the divisors of the form product of whole prime powers are 1,4,9,36. Closest to 6 is 4 or 9, both give max 9. That's fine.
-
If X has many prime factors, 2^d might be up to 2048, fine.
-
For X=1, factors empty, subset iteration works.
Now, is there any possibility that the minimal max is achieved by a and b not coprime but with a different a that is not a product of whole prime powers but yields a smaller max than any coprime pair? Based on earlier reduction, no, because we can transform any solution to coprime without increasing max. But let's double-check the transformation logic more formally:
Given (a,b) with lcm=X. For each prime p, let e = exponent in X. Let f = exponent in a, g = exponent in b, with max(f,g)=e. Define new exponents: if f=e, set f'=e, g'=0; else (i.e., g=e) set f'=0, g'=e. In case both f=g=e, set arbitrarily f'=e, g'=0. Then define a' = ∏ p^{f'}, b' = ∏ p^{g'}. Then a' ≤ a and b' ≤ b because we only removed factors (since we set exponents to 0 where they were positive but less than e or equal? Actually in case f=e and g<e, a' = a? For primes where f=e, we keep f'=e so a' includes p^e. In original a, exponent is e. So a' is same as a for those primes. For primes where f<e (so g=e), originally a had exponent f (<e). In a' we set f'=0, so we removed that p^f factor. So a' ≤ a. Similarly b' ≤ b. Thus max(a',b') ≤ max(a,b). Also lcm(a',b') = X because for each prime, one of them has exponent e. So (a',b') is a valid pair with the required property. And a' and b' are coprime (since for each prime, only one has positive exponent). Also a' and b' are of the form product of whole prime powers.
Therefore, for any optimal pair, there exists a coprime pair with max not larger. So optimum must be achieved by a coprime pair. So our enumeration covers all coprime pairs (since any coprime pair must assign each prime power wholly to one side, because if they were coprime, they share no prime, so each prime appears wholly in one number, i.e., exponent either e in one and 0 in the other. So indeed the coprime pairs correspond exactly to subset assignments. Hence enumeration of subsets yields the global optimum.
Thus algorithm correct.
Now we produce final answer.
Potential improvements: We could also use recursion to compute products and keep track of best, but bitmask is simpler.
Now, we should consider memory and time: fine.
Now, I'll write the final solution.
But before final, test on some random values:
-
X=1000000000000 = 10^12 = 2^12 * 5^12? Actually 10^12 = (2*5)^12 = 2^12 * 5^12. So prime powers: 2^12=4096, 5^12=244140625? Actually 5^12 = 244140625? Let's compute: 5^6=15625, 5^12=244140625? Wait 5^12 = (5^6)^2 = 15625^2 = 244140625. Yes. So factors: 4096 and 244140625. sqrt(X)=10^6=1000000. Subset products: a=4096, b=244140625 => max=244140625; a=244140625, b=4096 same; a=1, b=10^12 max=1e12; a=10^12,b=1 max=1e12. So minimal max = 244140625. So answer would be (4096,244140625) or (244140625,4096). This is plausible.
-
X=999999999989 (prime close to 1e12). Then only one prime power: X itself. So factors: [X]. Subset products: a=1,b=X; a=X,b=1. So output (1,X). So max = X.
Now, implement.
One more check: In factorization, if X is a perfect square of a prime? e.g., X=49=7^2. factors: [49]. Works.
Now, think about large exponents: X=2^40 ≈ 1e12, factor = [2^40]. output (1,X).
Now, we need to ensure we compute prime powers correctly, especially when exponent is large, we can compute power by repeated multiplication in the while loop, but better to compute as pw = 1; while n%i==0: n//=i; pw*=i. That's fine.
Now, consider the case where X is 0? Not allowed.
So code is ready.
But wait, the problem