We need to find all pairs (n, m) (presumably positive integers, n rows, m columns) such that the total number of squares in an n x m grid (rectangular table) equals exactly x. The total number of squares in an n x m grid, with n <= m? Actually the formula for total number of squares in a rectangular grid of size n rows, m columns is:
Let N = min(n, m), M = max(n, m). The total number of squares is sum_{k=1}^{N} (n - k + 1)*(m - k + 1). However, the formula is symmetric: total = sum_{k=1}^{min(n,m)} (n - k + 1)(m - k + 1). So it's symmetric in n and m. So (n,m) and (m,n) give the same total, but they are considered distinct? In the outputs, they list both orders, e.g., for x=26 they list: 1 26, 2 9, 3 5, 5 3, 9 2, 26 1. So they consider (n,m) with n and m positive integers, order matters (n rows, m columns) but they list all pairs with given total, sorted by increasing n, then m. So it includes both (n,m) and (m,n) if n != m. For n=m, it's just one.
So we need to find all (n,m) positive integers such that sum_{k=1}^{min(n,m)} (n - k + 1)(m - k + 1) = x.
Given x up to 1e18, we need efficient algorithm.
Let's denote n, m positive integers. The sum can be expressed in closed form.
Let a = n, b = m. Without loss of generality, let a <= b? Actually we need all ordered pairs, but we can find unordered pairs and then double except when equal.
We can derive formula: total squares S(a,b) = sum_{k=1}^{a} (a - k + 1)(b - k + 1) where a = min(n,m)? But careful: if we treat ordered, we can't assume a <= b; the sum is over k=1 to min(n,m). So we need to consider both cases. But the sum is symmetric, so we can compute for all unordered pairs (u, v) with u <= v. Then ordered pairs are (u,v) and (v,u) if u != v; and (u,u) only one.
Thus we need to find all unordered positive integer pairs (u, v) with u <= v such that S(u,v) = x.
Now, S(u,v) = sum_{i=1}^{u} i * (something)? Let's set i = u - k + 1? Actually let i = u - k + 1, then as k runs from 1 to u, i runs from u down to 1. Then (u - k + 1) = i, and (v - k + 1) = v - (u - i + 1) + 1? Actually k = u - i + 1, so v - k + 1 = v - (u - i + 1) + 1 = v - u + i. So S = sum_{i=1}^{u} i * (v - u + i) = sum_{i=1}^{u} (i*(v-u) + i^2) = (v-u)* sum_{i=1}^{u} i + sum_{i=1}^{u} i^2.
Thus S(u,v) = (v - u) * (u(u+1)/2) + (u(u+1)(2u+1)/6).
Let d = v - u >= 0.
Then S(u, v) = u(u+1)(2u+1)/6 + d * u(u+1)/2.
Simplify: S = u(u+1)/6 * (2u+1 + 3d) = u(u+1)/6 * (2u+1 + 3d).
But d = v-u, so v = u + d.
Thus S(u, v) = u(u+1)(2u+1)/6 + d * u(u+1)/2.
That's a nice linear expression in d.
Given u (size of smaller side) and total x, we can solve for d:
x = A(u) + d * B(u), where:
A(u) = u(u+1)(2u+1)/6.
B(u) = u(u+1)/2.
Then d = (x - A(u)) / B(u), provided that x >= A(u) and (x - A(u)) is divisible by B(u). Also d >= 0 integer, and v = u + d must be >= u obviously, and v is any integer >= u. However note that this formula assumed u = min(n,m) and v = max. But if we treat ordered, we could also have n > m? Actually the sum formula works for any order: S(n,m) = sum_{k=1}^{min} (n-k+1)(m-k+1). That is exactly what we derived with u = min(n,m). So if we let u = min(n,m) and v = max(n,m), then S = A(u) + (v-u) B(u). So for any unordered pair with min = u and max = v, S is given.
Thus, to find all unordered pairs, we can iterate over possible u (the smaller side) such that A(u) <= x, and compute d accordingly. Since x up to 1e18, u cannot be too large: A(u) grows roughly like u^3/3 (since u(u+1)(2u+1)/6 ~ (2u^3)/6 = u^3/3). So u up to about cube root of 3x ~ 1e6? Actually x=1e18, cube root is 1e6, since (1e6)^3=1e18. So u up to about 1.5e6 maybe. But we can bound more precisely: u^3/3 <= 1e18 => u^3 <= 3e18 => u <= (3e18)^(1/3) ≈ 1.44e6. So u up to ~1.44e6. That's manageable for iteration.
But we also need to consider that d can be large. However for each u we compute d = (x - A(u))/B(u). Then check if d is integer and non-negative. If yes, then v = u + d gives a valid unordered pair (u, v). But note: In this unordered representation we have u <= v. That's fine.
Now, we need to also consider the possibility that we could have n and m both less than u? Actually the formula with u = min(n,m) covers all ordered pairs. So by iterating u from 1 to some maximum, we find all unordered pairs. However, there might be a case where n and m are both > u but min is not u? That doesn't make sense; we iterate u over all possible values of the smaller side. So we will find all unordered pairs.
But caution: The formula S = A(u) + d B(u) derived under assumption that u is the min. But what if n = m? Then u = n = m, d = 0, S = A(u). That's captured.
Thus algorithm:
- Precompute A(u) and B(u) for u up to some maximum (maybe 2e6 to be safe) using 128-bit integers or careful overflow (since x up to 1e18, but A(u) can exceed 1e18 for large u; we need to stop when A(u) > x). We can compute iteratively with 64-bit unsigned, but careful of overflow when computing u(u+1)(2u+1) may exceed 2^63? Actually u up to 1.5e6, product u*(u+1)*(2u+1) ~ (1.5e6)^3 * 2 = 6.75e18, which is above 2^63-1 (~9.22e18) but close. Actually 1.5e6^3 = 3.375e18, times 2 gives 6.75e18, which is less than 9.22e18, so fits in signed 64-bit? But careful: 6.75e18 < 9.22e18, so it fits in signed 64-bit. However, we will also have multiplication by 1? Actually A = product /6. product may be up to about 6.75e18, which fits in signed 64-bit. So we can use 128-bit via __int128 or careful using unsigned long long which can go up to 1.8e19, product fits. Actually unsigned long long max is about 1.84e19, so it's safe. So we can use 64-bit unsigned.
But we need to compute A(u) = u(u+1)(2u+1)/6. For large u, product may overflow 64-bit? Let's compute worst-case u such that A(u) <= 1e18. Solve u^3/3 ≈ 1e18 => u ≈ (3e18)^(1/3) ≈ 1442249. Let's compute max product: u=1442249, u+1=1442250, 2u+1=2884499. Their product: approx 1442249*1442250 ≈ 2.079e12? Actually that's wrong: (1.44e6)^2 = 2.07e12, times another 1.44e6 gives 2.99e18. Let's compute more accurately: 1.442e6^3 = 3e18? Actually (1.442e6)^3 = 2.997e18. Then times 2 (since 2u+1 ≈ 2u) gives ~5.994e18. So product ~6e18, which is less than 2^63-1 (9.22e18) and less than 2^64-1 (1.84e19). So indeed product fits in 64-bit unsigned. So we can compute A(u) using integer arithmetic with 64-bit.
But to be safe, we can use __int128 for intermediate or do division carefully: compute numerator = u*(u+1)/2 maybe, then times (2u+1)/3? But careful with divisibility: u(u+1)(2u+1) is divisible by 6 always. So we can compute as (u*(u+1)/2)(2u+1)/3? Actually (u(u+1)/2) is integer, and then (that)(2u+1) may still be large, but then divide by 3. Since (u(u+1)/2) may not be divisible by 3, but product divisible by 3 overall. Better to compute using 128-bit to avoid overflow? But likely 64-bit suffices. However, product u*(u+1) may overflow 64-bit? u up to 1.44e6, product ~2e12, safe. Multiplying by (2u+1) ~2.88e6 gives ~5.76e18, still safe in 64-bit unsigned. So unsigned long long (range 0 to 2^64-1 ~1.84e19) can hold it. So we can compute numerator = u; numerator *= (u+1); numerator = (2u+1); then A = numerator/6. For unsigned 64-bit, multiplication can overflow if product > 2^64-1, but we estimated max product ~5.76e18, well below 1.84e19, so safe. So we can use unsigned long long.
Similarly B(u) = u(u+1)/2. That's at most ~1e12, safe.
Thus we can iterate u from 1 while A(u) <= x. For each u compute remainder = x - A(u). If remainder % B(u) == 0, then d = remainder / B(u). Since d >= 0 automatically if remainder >= 0, but we need v = u + d. Since we didn't impose any upper bound on v (except perhaps x is finite, but v can be arbitrarily large? Let's check: For fixed u, S = A(u) + d B(u) grows linearly with d. For any d, S increases. Since x is fixed, we only get one d. So v = u + d. That v may be very large; there is no bound except that d must be integer. So for each u, we may get a solution if the division works. But wait: In our derivation, we assumed u = min(n,m). So v = max(n,m) should be >= u. That's satisfied. But could there be any further restriction? The formula S(u,v) works for any positive integers u <= v. So v can be arbitrarily large; it's just that for given u, S = A(u) + (v-u)B(u). For a fixed x, if (x-A(u)) is divisible by B(u) and nonnegative, then v = u + d is determined. There's no upper bound. So for each u, we may get at most one solution.
But wait: Are we sure that the expression holds for all u and v? Yes, because we derived S = sum_{i=1}^u i*(v-u + i) = A(u) + d * B(u). So it's valid for any u, v with u = min. So that's correct.
Thus the number of unordered pairs (u,v) with u <= v such that S = x is at most the number of u with A(u) <= x and (x - A(u)) divisible by B(u). That could be up to about cube root of x, i.e., ~1.5e6. That's feasible.
But we also need to include ordered pairs. For each unordered solution (u,v) with u <= v, we output two ordered pairs: (u, v) and (v, u) except when u = v. So we need to sort them by n (first component) and then m. The output format: "First print a single integer k — the number of tables with exactly x distinct squares inside. Then print k pairs of integers describing the tables. Print the pairs in the order of increasing n, and in case of equality — in the order of increasing m."
We need to output all ordered pairs (n,m). That includes both (u,v) and (v,u) for u<v. So we need to generate the list of ordered pairs, sort by n then m.
But if we find all unordered pairs, we can generate the ordered pairs accordingly. Then sort.
But note: Could there be solutions where n < m but our method with u = min = n, v = max = m; that's captured. And also solutions where n > m correspond to the same unordered pair but swapped. So generating both yields all ordered.
Thus algorithm:
- Read x.
- Initialize an empty list of ordered pairs.
- Loop u from 1 upwards while A(u) <= x:
- Compute A = u(u+1)(2u+1)/6.
- Compute B = u(u+1)/2.
- Compute remainder = x - A.
- If remainder >= 0 and remainder % B == 0:
d = remainder / B.
v = u + d.
Add pair (u, v) to list (ordered? Actually we add both (u, v) and (v, u) if u != v, else just (u,u)).
- After loop, sort the list of pairs (n,m) by n then m.
- Output k = len(list) and then each pair.
But careful: Does this loop over all possible u? u is the smaller side. Could there be a solution where the smaller side u is greater than the bound where A(u) <= x? If u is larger, then A(u) > x. But since d >= 0, S = A(u) + d B(u) >= A(u) > x, so no solution. So indeed only u with A(u) <= x need to be considered.
But wait: What about the possibility that the smaller side u is actually not the min? That's definition. So that's covered.
Thus the algorithm yields all unordered solutions.
But double-check with examples.
Example 1: x = 26.
Compute u=1: A=123/6=1; B=1*2/2=1; remainder=25, divisible? 25%1==0, d=25, v=26. So (1,26) solution. Add (1,26) and (26,1).
u=2: A=235/6=30/6=5? Actually 235=30, /6=5. B=2*3/2=3. remainder=21, 21%3==0, d=7, v=9. So (2,9) and (9,2).
u=3: A=347/6=84/6=14? Actually 347=84, /6=14. B=3*4/2=6. remainder=12, 12%6=0, d=2, v=5. So (3,5) and (5,3).
u=4: A=459/6=180/6=30 >26, stop. So only these three unordered solutions. That gives 6 ordered pairs: (1,26), (2,9), (3,5), (5,3), (9,2), (26,1). Sorted by n increasing: (1,26), (2,9), (3,5), (5,3), (9,2), (26,1). That matches output.
Example 2: x=2.
u=1: A=1, B=1, remainder=1, d=1, v=2 => (1,2) and (2,1).
u=2: A=5? 235/6=30/6=5 >2, stop. So only (1,2) and (2,1) -> 2 pairs. Output matches.
Example 3: x=8.
u=1: A=1, B=1, remainder=7, d=7, v=8 => (1,8),(8,1).
u=2: A=5, B=3, remainder=3, d=1, v=3 => (2,3),(3,2).
u=3: A=14 >8 stop. So ordered pairs: (1,8),(2,3),(3,2),(8,1) sorted as (1,8),(2,3),(3,2),(8,1). Output matches.
Thus algorithm seems correct.
Now we need to consider edge cases and potential pitfalls:
- x up to 1e18, u up to about 1.44e6, which is fine.
- However, we need to compute A(u) for u up to maybe a bit larger than the threshold where A(u) > x, but we stop when A(u) > x. But we must ensure we use a loop that increments u until A(u) > x. Since A(u) grows, we can compute while true.
But we must be careful: Could there be a case where for a given u, remainder is negative but A(u) <= x? Actually if A(u) <= x, remainder >=0. So ok.
But what about overflow in computing A(u) for u near the limit? At u where A(u) just exceeds x, we still compute A(u) maybe > 1e18 but still within 64-bit. However, if we keep looping beyond that, A(u) may eventually overflow 64-bit? But we stop when A(u) > x, and x max 1e18, so u at stop is around 1.44e6, product ~5.8e18, safe. But if we erroneously loop further beyond, eventually product may exceed 2^64-1, causing overflow. So we need to stop at the bound. That's fine.
But we also need to ensure that we don't miss any solution where u > bound. As argued, A(u) > x, and since S >= A(u), impossible. So correct.
Now we need to consider if there is any solution where n and m are such that min(n,m) is not the one we think? That's by definition.
Thus problem reduces to solving for u and d.
But wait: Is the formula correct when n and m are swapped? Yes, S(n,m)=S(m,n). So we are good.
Now complexity: iterate up to ~1.44e6 steps, each with a few multiplications and divisions, fine.
But we must be careful about division: Need to check divisibility of remainder by B. remainder = x - A. Since A and x are both up to 1e18, remainder up to 1e18. Use 64-bit.
Now, we need to output the number of pairs and then the pairs.
But is there any case where the pair (u,v) obtained has u > v? By construction, v = u + d, so v >= u. So it's fine.
Now, we should also consider the possibility that the derived v from d could be less than u? Only if d negative, but remainder is non-negative, so d >=0.
Thus algorithm seems straightforward.
But before coding, we must also consider if there is any other type of solution? Could there be a scenario where n < m but we treat u = n, but our formula assumed min = n. However, what if n > m? In that case, min = m, but if we iterate u as the smaller, we would capture that when u = m, v = n. So both ordered possibilities are captured via swapping. So no missing.
Thus it's good.
Now we need to generate ordered pairs and sort. The number of ordered pairs could be up to about 2 * (number of unordered solutions). The number of unordered solutions is at most the number of u values that satisfy divisibility, which is at most about 1.44e6. But is it possible to have that many? Let's think: For each u, we get at most one solution. So unordered solutions <= 1.44e6. So ordered pairs <= ~2.88e6. That's acceptable for output? The output would be huge, but typical problems expect answer size manageable. However, is it possible that for a given x, there are many u with divisibility? Possibly many, but likely not all. However, worst-case might be when x is something like 0? But x>=1. Could there be an x that yields many solutions? We need to consider that the answer's size k might be up to ~1.5e6, which would be huge output (millions of lines). Is that allowed? The problem statement doesn't specify constraints on k, but typical Codeforces problem with x up to 1e18 might expect k not too large. But we must consider worst-case. Could there be an x with many divisors? Since condition is (x - A(u)) divisible by B(u). This is like solving for d. It's not obvious that many u satisfy. But theoretically, if x is huge, there might be many u. Let's attempt to bound the number of solutions. Each u gives unique d, and v is determined. So at most number of u that we iterate, i.e., about cube root of x. So maximum possible k (ordered) is about 2 * cube root of x. For x=1e18, cube root ~1e6, so k could be up to 2 million. Is it feasible to output 2 million lines? Usually in programming contests, output of a few million lines is borderline but possible if efficient I/O (fast output). Typically Codeforces limits output size? But they have no explicit limit; however, problems usually expect answer not too huge. But 2 million lines of two integers each is about 20-30 MB, which might be okay if time limit is generous. But we also need to generate and sort them. Sorting 2 million pairs (n,m) might take some time but is doable in C++ with O(k log k). 2 million log2 2 million ~ 2e6 * 21 ~ 42e6 comparisons, fine.
But is it likely that for some x, nearly all u from 1 to 1.44e6 yield a solution? That would require that for each u, (x - A(u)) is divisible by B(u). That is a strong condition. Could it happen? Let's analyze the equation:
x = A(u) + d B(u) => x mod B(u) = A(u) mod B(u). But A(u) = u(u+1)(2u+1)/6, B(u)=u(u+1)/2.
We can simplify: A(u) = B(u) * (2u+1)/3? Actually B(u) = u(u+1)/2. So A(u) = u(u+1)(2u+1)/6 = (u(u+1)/2) * (2u+1)/3 = B(u) * (2u+1)/3. But note that (2u+1)/3 may not be integer. However, A(u) is integer, so B(u) * (2u+1) is divisible by 3. Actually A(u) = (B(u)(2u+1))/3. So x = (B(u)(2u+1))/3 + d B(u) = B(u) * (d + (2u+1)/3). So x must be divisible by B(u) up to the fraction? Let's express differently.
Let’s set B = u(u+1)/2. Then x = B * ( (2u+1)/3 + d ). Since (2u+1)/3 may be not integer, but the whole product is integer. So the condition for integer d is that x - A is divisible by B. That's equivalent to x ≡ A (mod B). For each u, this is a congruence condition.
Could it be that for many u, x satisfies that? Possibly if x is something like 0? But x>0. Could there be x such that many small u satisfy? Possibly if x is a multiple of many B(u) with appropriate offset. But it's unlikely that more than a small fraction of u up to 1e6 satisfy. However, we cannot rely on that; we must assume worst-case could be all u. But maybe the problem setter expects this O(cuberoot(x)) solution and accepts up to ~2e6 output. I've seen similar problems like "Find all rectangles with n x m that have exactly x squares" on Codeforces? Actually there is a problem "Spongebob and Squares" or something. Searching memory: There's a Codeforces problem 599D? Actually "Spongebob and Squares" is problem 599D? Let's recall: Codeforces Round #332 (Div. 2) D. "Spongebob and Squares"? Wait I remember a problem about counting squares in a table, and the solution involved iterating over n up to cube root of x. Yes, I think it's Codeforces Round #599? I'm not sure. But I recall a problem "Spongebob and Squares" or something like that. The solution involved iterating over n until n*(n+1)(2n+1)/6 > x, checking divisibility. And outputting pairs. So indeed that is the intended solution. The constraints x up to 10^18, and they likely test that k can be up to about 2e6? Actually I think I've seen editorial, and they say that the number of solutions is O(sqrt(x))? But here it's O(cuberoot(x)). Possibly they mention that the number of answers does not exceed 210^6. So it's fine.
Thus we proceed.
Now, careful with sorting: We need to output ordered pairs in increasing n then m. Since we generate both (u,v) and (v,u) for u<v, these may not be sorted. We can generate all ordered pairs into a vector, then sort. Alternatively, we could generate unordered solutions and then for each, insert both but maintain order manually? Since u is increasing, (u,v) is increasing in u, but v may be larger than u, and (v,u) might have first component v which could be larger than the next u's. So we need global sorting.
Simplest: store all ordered pairs in vector<pair<long long, long long>>, then sort.
But memory: Up to 2e6 pairs, each two 64-bit integers -> 32 bytes per pair approx (if using pair of long long). 2e6 * 32 = 64 MB, which is okay for typical memory limits (256 MB). So fine.
But we can optimize: We can store unordered pairs and then output in order by generating two sequences: For all unordered solutions (u,v) with u <= v, we have two ordered pairs. The ordered pairs with first component <= second? Not necessarily. To output sorted by first component, we could collect all ordered pairs and sort.
But we could also generate two lists: one list of pairs where first <= second (i.e., the (u,v) with u<=v) and another list where first > second (i.e., (v,u) with u<v). But then merging them sorted by first component could be done by noticing that the first components of the (v,u) are the v's, which are often larger than the u's? Actually v could be huge for small u, but also small for u close to v? Let's examine: For u from 1 to U_max, we get v = u + d, where d = (x - A(u))/B(u). d could be large or small. For small u, d tends to be large (since B(u) small). So v could be large, possibly larger than U_max. For larger u, v may be close to u. So the set of first components n among ordered pairs includes all u values (from solutions) and all v values (from swapping). Some v may be less than some later u? Possibly? Let's check example: For x=26, u=1 gives v=26; u=2 gives v=9; u=3 gives v=5. So v values are 26,9,5. Ordered pairs: (1,26), (2,9), (3,5), (5,3), (9,2), (26,1). Sorted by first: (1,26) u=1, (2,9) u=2, (3,5) u=3, (5,3) v=5, (9,2) v=9, (26,1) v=26. So the v values appear after the u values because v >= u, and as u increases, v decreases? In this case, v decreases as u increases. So v values might be larger than some later u? Actually u=2 gives v=9, which is > u=3? 9 > 3, so v from u=2 is 9, while u=3 is 3. So the ordered pair (9,2) has first component 9, which is > 3. In the sorted order, (3,5) appears before (5,3) because 3<5. So indeed, after u=3, we have (5,3) with first=5, which is >3. So the ordering is mixed: after we output all (u_i, v_i) where u_i in increasing order (which we generate as we iterate u increasing), we then need to output (v_i, u_i) but they may interleave with later (u_j, v_j) if v_i is less than u_j? In the example, u=3 (v=5) and then (5,3) has first=5, but u_j after u=3 none, because u max is 3. So if there were more u's, say u=4 yields v=4? Actually if u=4 gave v= something less than 5? Could that happen? Since v = u + d, and d = (x - A(u))/B(u). As u increases, A(u) increases, B(u) increases, so d tends to decrease. But v may not be monotonic with u. Possibly v could first be large for small u, then decrease, then maybe increase again? Let's examine: For fixed x, as u increases, A(u) and B(u) increase, so (x - A(u)) decreases, B(u) increases, so d roughly declines like (x - ~u^3/3) / (u^2/2) ~ (2x/u^2) - (2u/3). So v = u + d ~ u + (2x/u^2) - (2u/3) = (u/3) + (2x/u^2). For u around cube root of x, the term (2x/u^2) becomes about 2x/(x^{2/3}) = 2 x^{1/3}. So v ~ u/3 + 2x^{1/3}. So near the maximum u, v may be roughly of order x^{1/3} as well. So v as function of u may have a minimum. Actually the derivative etc. But important: v can be less than some later u? For u1 < u2, could v1 (from u1) be less than u2? Possibly if v1 is small. In example, u1=2 gives v1=9, u2=3 gives u2=3; 9 > 3. So v1 > u2. For v1 to be less than u2, we need v1 < u2. Since v1 = u1 + d1 and u2 > u1, if d1 is small, maybe v1 < u2? For example, suppose x is such that for u=1, v=2? That would be d=1, v=2; then u=2 might also produce v? But if u=1 gives v=2, then u=2 might have A(2)=5> x if x is small. But if x is larger, can we have u=10 give v=12, and u=11 maybe also give v=11? Possibly v from u=10 is 12, which is greater than u=11? Actually 12 > 11, so v > next u. Could we have v from u=10 = 11? Then that would be u=v, actually u=10, v=11? Actually v=11 > u=10, but u=11 is not considered yet; but if we later consider u=11, that would be separate. So v=11 is less than u=11? Actually u=11 is 11, so v=11 equals u=11. That would be a solution with u=11 v=11? That's fine. But v from u=10 = 11, which is equal to u=11? That's not less than u=11; it's equal. So v from earlier u may be equal to a later u (if later u yields the same v? But u is min, so if v = later u, then the later u solution would have min = u, but if v = later u, that's possible. But then (later u, later v) would be a different pair. In that case, the ordered pair (v, u) from earlier will have first component = v which equals later u. Then we have two ordered pairs with same first component: (later u, later v) and (v, u). Sorting by first then second will intermix. So we can't just output all (u,v) then all (v,u) because they may not be in correct order.
Thus sorting the whole list is simplest.
Alternatively, we could generate the ordered pairs and then sort. That's fine.
But we need to be careful about memory and time.
Now, we need to ensure we don't miss any pair where n = m? Those are included as (u,u) and we add only one ordered pair.
Now, we must consider if n or m can be 0? No, table dimensions positive, so n,m >=1.
Now, we have to think about potential duplicates: Could the same ordered pair be generated from two different u? That would mean there exist (n,m) such that there are two representations with different min values? Suppose (n,m) with n <= m. Then min = n. So u = n. That's unique. So the unordered representation is unique: min is uniquely determined. So each unordered pair corresponds to exactly one u. So there will be no duplicate ordered pairs from different u. So we can safely add both.
Now, we need to ensure that when we compute d, v = u + d, we get integer v. But v might overflow 64-bit? Since d = (x - A(u))/B(u). For small u, B(u) small, d could be huge. Let's compute maximum possible v. x up to 1e18, u=1 gives A=1, B=1, d = x-1, v = x. So v can be as large as 1e18. That's huge but can be stored in 64-bit (since 1e18 < 2^63-1). So v may be up to 1e18. That's fine. For u=1, v could be 1e18. So n and m can be up to 1e18. So we need to use 64-bit for n,m as well. Indeed, output includes values like 26 in example, but could be larger.
Thus we store pairs as long long.
Now, the iteration: u up to about 1.44e6. For each u we compute A(u) and B(u). However, we can compute these incrementally using recurrence to avoid multiplication of large numbers? But multiplication is cheap. But we must be careful about integer overflow in intermediate steps? For u up to 1.44e6, product u*(u+1)*(2u+1) is safe as argued. But for u=1.44e6, product ~ 1.44e6 * 1.44e6 * 2.88e6 = 5.97e18, fits in 64-bit unsigned. But using signed 64-bit (long long), which typically goes up to 9.22e18, also safe. So we can use signed long long (int64_t) for convenience. However, for u=1.44e6, we compute A = product/6 which is about 1e18, fits. So we can store A as long long.
But careful: For u near the upper bound, product may be slightly above 2^63-1? Let's compute precisely the maximum u such that A(u) <= 1e18. We can compute the bound precisely: find max u where u(u+1)(2u+1)/6 <= 1e18. But we need to be sure that product itself doesn't exceed 2^63-1 for that u. Actually if product is near 6e18, that's less than 9.22e18, so safe. However, if we compute product using signed long long, it may overflow if product exceeds 2^63-1. But it does not for u up to about 2.1e6? Let's check: 2.1e6^3 approx 9.26e18, times 2 gives 1.85e19 > 9.22e18, so product might overflow for u > 1.5e6 maybe. But we only go up to u such that A(u) <= 1e18. So we won't go beyond u where product > 9e18. So safe.
But we can also use unsigned long long to be extra safe.
Now, we need to ensure that while loop condition: while A(u) <= x. Since A(u) can be computed and may overflow if u goes too high, but we stop before that because A(u) > x. But what if x is very large, e.g., 1e18, and we compute A(u) for u up to say 1.44e6, product ~5.97e18, which fits. So no overflow. If x is larger than 1e18? Constraint says x ≤ 1e18. So max x is 1e18. So we are safe.
Now, we also need to consider if the computed d might not be integer due to division rounding error. We'll use integer arithmetic.
Now, after generating ordered pairs, we need to sort them. Since the number of pairs can be up to 2e6, sorting is okay. But we need to output them after sorting.
But careful: For u=1, if we get a solution, we add (1, v) and (v, 1). For u=2, etc. The total may be up to 2e6. Sorting 2e6 pairs can be done with std::sort.
But we could avoid sorting by noticing that ordered pairs naturally appear in a certain order if we collect both and then sort? Actually we could output them in two passes: first, all pairs where n <= m (the (u,v) with u<=v) are generated in increasing u. Since u increases, these pairs have first component increasing (n = u). So they are already sorted by n. Then we need to output the swapped pairs (v,u) where u<v. These have first component = v, which is decreasing? Not necessarily monotonic. But we could sort this second list. However, we might also interleave with the first list if any v is less than some later u. Is that possible? Let's examine if v from a smaller u could be less than a larger u. Suppose u1 < u2, we have v1 = u1 + d1, v2 = u2 + d2. Could v1 < u2? That would require u1 + d1 < u2. Since u1 < u2, it's possible if d1 is very small, i.e., d1 = 0? But d1 = 0 would mean v1 = u1, but u1 < u2, then v1 = u1 < u2. But d1=0 implies x = A(u1). For u1 small, A(u1) is relatively small, and x would be that small. Then for u2 > u1, A(u2) > x, so u2 wouldn't be considered because A(u2) > x. So in the iteration, u2 is not processed if it exceeds the bound. So the only u processed are those with A(u) <= x. If x = A(u1) exactly, then for u1, d=0, v1=u1. For any larger u, A(u) > A(u1) = x, so they are not processed. So there cannot be a case where we have two different u both processed and v1 < u2 because u2 would be > u1 but if v1 < u2, then v1 <= u2 - 1. But then for u2, we have A(u2) <= x? Possibly if x is large enough to include both. Let's test: Take x such that both u1 and u2 are valid. Could v1 be less than u2? Let's attempt to find an example numerically. We need u1 < u2, both A(u1) <= x and A(u2) <= x. Since A(u) grows, okay. Now v1 = u1 + (x - A(u1))/B(u1). v2 = u2 + (x - A(u2))/B(u2). Could v1 be less than u2? That would mean u1 + (x - A(u1))/B(u1) < u2. Rearranged: (x - A(u1))/B(u1) < u2 - u1. Since u2 - u1 >=1, we need d1 < u2-u1. So d1 is small relative to gap. Is that possible? Let's try to see if there exists x and u1 < u2 such that d1 is 0 or 1. If d1=0, then x = A(u1). Then A(u2) > A(u1) = x, so u2 not valid. So d1 must be at least 1. So we need d1 < u2 - u1. Could be possible if u2 is much larger than u1 and d1 is small. For small u1, d1 typically large because B(u1) small, so d1 is huge. But maybe for moderate u1, d1 could be small if x is just a little above A(u1). For example, take u1=100, A(100) =? Let's compute approximate: A(100) ≈ 100101201/6 = (10100201)/6 = 202,? Actually 100101=10100, 201=2,030,100, /6 ≈ 338,350. B(100)=100101/2=5050. So if x = A(100)+5050 = 338350+5050=343400, then d1=1, v1=101. So u1=100, v1=101. Now u2 could be, say, 101? But u2 must be <= v? Actually u2 must be <= something? We need to check if u2=101 is valid: A(101)=101102203/6 =? 101102=10302, 203=2,091,? 10302203=10302200 +103023=2,060,400+30,906=2,091,306, /6=348,551. That's > x (343,400). So u2 not valid. u2=99? But u2 must be > u1? Actually we consider u2 > u1. For u2=101, A(101)>x, so not considered. For u2=102, even larger. So to have u2 valid, we need A(u2) <= x. Since A(u2) grows, if u2 > u1, it's possible only if x is significantly larger than A(u1). For d1 small, x is only slightly above A(u1), so A(u2) will likely exceed x. Let's check if there exists u2 such that A(u2) <= A(u1) + B(u1) (u2-u1-1)? Because if d1 < u2-u1, then x = A(u1) + d1 B(u1) < A(u1) + (u2-u1) B(u1). We need A(u2) <= x. But note that A(u2) = A(u1) + sum_{i=u1+1}^{u2}? Actually A(u) can be expressed incrementally: A(u+1) = A(u) + (u+1)^2? Wait the sum of squares? Actually A(u) = sum_{i=1}^u i^2. So A(u2) = A(u1) + sum_{i=u1+1}^{u2} i^2. That sum is at least (u1+1)^2 + ... which is > (u2-u1)*(u1+1)^2. Meanwhile, B(u1) = sum_{i=1}^{u1} i = u1(u1+1)/2. For u2 not too large relative to u1, it's plausible that A(u2) could be > A(u1) + (u2-u1)B(u1). Let's test inequality: Is A(u2) - A(u1) >= (u2-u1) * B(u1)? LHS = sum_{i=u1+1}^{u2} i^2. RHS = (u2-u1) * sum_{i=1}^{u1} i. For i > u1, i^2 > i? Actually i^2 is much larger than typical numbers in B(u1). Specifically, the smallest term in LHS is (u1+1)^2, which for u1>=1 is at least 4. RHS average per term is B(u1) which is about u1^2/2. Actually B(u1) ~ u1^2/2. So for u2 - u1 small, (u2-u1)*B(u1) might be ~ (u2-u1)(u1+1)^2 ~ (u2-u1)1=1. 4>1. u1=2, u2=3: A(3)-A(2)=14-5=9, (1)*B(2)=3, 9>3. u1=3, u2=4: A(4)=30, difference=16, (1)*B(3)=6, 16>6. u1=1, u2=3: difference=13, (2)*1=2, 13>2. So indeed A(u2) - A(u1) > (u2-u1) B(u1). Therefore, if x = A(u1) + d1 B(u1) with d1 < u2-u1, then x < A(u1) + (u2-u1) B(u1) < A(u2). Thus A(u2) > x, so u2 would not be valid. This proves that for any two valid u1 < u2, we must have d1 >= u2 - u1. Consequently, v1 = u1 + d1 >= u2. So v1 >= u2. This means that for the sequence of u in increasing order, the corresponding v values are non-increasing? Let's check: If u1<u2, we have v1 >= u2. Also note v2 = u2 + d2. Could v1 be less than v2? Possibly, but we have v1 >= u2. And v2 >= u2. So v1 could be > or < v2. But we have v1 >= u2. Since u2 is the next u, v1 is at least u2. So in particular, v1 (the max side from earlier) is at least the next min side. That does not guarantee that v1 > u2 (strictly > maybe except when d1 = u2-u1 exactly? Actually if d1 = u2 - u1, then v1 = u2. That would be equal to u2. Then we have two unordered pairs: (u1, u2) and (u2, v2). But is it possible that both u1 and u2 are valid and d1 = u2 - u1? Let's examine condition: x = A(u1) + (u2-u1)B(u1). Then A(u2) = A(u1) + sum_{i=u1+1}^{u2} i^2. Since sum_{i=u1+1}^{u2} i^2 > (u2-u1) B(u1) as we argued, we have A(u2) > x. So u2 would not be valid. So d1 cannot be exactly u2-u1, because that would make x < A(u2). So we must have d1 > u2-u1. Thus v1 > u2. So v1 > u2 strictly. This is a key property: For any two valid u1 < u2, we have v1 > u2. That is, the max side from the smaller u is strictly greater than the next u.
Now, what about v2? v2 could be less than v1? Possibly. But we have v1 > u2 and v2 = u2 + d2, with d2 >=? Since x = A(u2) + d2 B(u2). Since A(u2) > A(u1) and B(u2) > B(u1), d2 is typically smaller.
Now, consider ordered pairs: The ones with n = u (the min) are (u, v). Since u increases, these are sorted by n (first component) as we generate them. The ones with n = v (the swapped) have first component v, which are > u for the current u? Actually for a given unordered solution (u, v) with u < v, we have v > u. And from the property, for any later u' (larger than u), we have v > u'? Not necessarily: For u, v, and the next valid u' (which is > u), we have v > u' (strict). Because v = v1 > u2, where u2 is the next valid u. But careful: This property holds for consecutive valid u values? If we have multiple valid u's, they are in increasing order. For each u_i, we have v_i. The property derived: for any two valid u_i < u_j, we have v_i > u_j. Because suppose u_i < u_j, then d_i must be > u_j - u_i (since otherwise x < A(u_j)). So v_i = u_i + d_i > u_j. So indeed v_i > u_j. So for all i < j, v_i > u_j. That means all v from earlier u are larger than all later u. So the set of u values (min sides) is increasing, and the corresponding v values are decreasing? Not necessarily monotonic but we can deduce ordering: Since v_i > u_j for all j > i, but we don't know relation between v_i and v_j for i<j. However, we can infer something about ordering of the first components in the swapped pairs: The swapped pairs are (v_i, u_i). Since v_i > u_j for all j > i, we have that v_i > u_i+1, etc. So the first components of swapped pairs (v_i) are all greater than any u_j for j>i, and also v_i > u_i obviously. But how do different v_i compare? Since v_i > u_{i+1} and v_{i+1} > u_{i+2}, etc., we cannot directly compare v_i and v_{i+1}. However, we can show that v_i > v_{i+1}? Let's test with example: u1=1, v1=26; u2=2, v2=9; u3=3, v3=5. Here v1 > v2 > v3. Is that always true? Possibly because as u increases, v decreases. Let's attempt to prove: For i < j, we have v_i > u_j. Also, we can consider v_j = u_j + d_j. Since d_j = (x - A(u_j))/B(u_j). Compare v_i and v_j. Since both are positive, maybe v_i > v_j always? Let's see if it's possible to have v_i <= v_j. Suppose v_i <= v_j. Since v_i > u_j (as above) and u_j < v_i <= v_j. Could that happen? Possibly if d_j is large enough. But recall that x = A(u_i) + (v_i - u_i)B(u_i). Also x = A(u_j) + (v_j - u_j)B(u_j). For i < j, u_i < u_j. Could v_j be larger than v_i? Let's try to construct a counterexample. Use approximate formula: v ≈ u + (x - A(u))/B(u). The function f(u) = u + (x - A(u))/B(u). Take derivative w.r.t. u? Might be decreasing for x fixed? Let's analyze discrete: For u increasing, A(u) increases faster than B(u). Typically (x - A(u)) declines, and dividing by B(u) which grows, d decreases. So v = u + d might have a certain shape. For large x, maybe v first large then small? Let's test with some numbers: Suppose x=1000000. Compute u=1: v ≈ 1 + (1e6-1)/1 = 1e6. So v=1,000,000. u=2: A(2)=5, B=3, d=(1e6-5)/3≈333331.666? Actually integer if divisible? Let's take x such that divisible; but approximate: d ≈ 333331, v ≈ 333333. That's smaller than 1e6. u=3: A(3)=14, B=6, d ≈ (1e6-14)/6≈166,664.33, v≈166,667. So decreasing. u=4: A(4)=30, B=10, d= (1e6-30)/10=99,997, v=100,001. So yes decreasing. So seems v decreases as u increases. Could it increase at some point? Let's check derivative of continuous version: Let S(u) = A(u) + (v-u) B(u) = x, treat v as function of u. Compute derivative implicitly? But we have discrete u. However, given the condition that for valid u, x is fixed, we can think of v as (x - A(u))/B(u) + u. The function f(u) = u + (x - A(u))/B(u). Compute f(u+1) - f(u). Might be negative? Let's test with a sample where divisibility holds, but maybe there is an edge case? Probably f(u) is strictly decreasing for u where x >= A(u). Because as u increases, the term (x - A(u))/B(u) decreases faster than the increase in u. To be rigorous, we can show that for any u < u', if both satisfy A(u) <= x and A(u') <= x, then v(u) > v(u'). Actually using the earlier property v(u) > u' and u' <= v(u')? Not enough. But we can try to prove: Suppose u < u'. Then we have x = A(u) + (v(u)-u)B(u) = A(u') + (v(u')-u')B(u'). Consider the difference. Not trivial.
Nevertheless, from the example and intuition, v values are decreasing as u increases. And the property v_i > u_j for i<j already ensures that the swapped pairs (v_i, u_i) have first components v_i that are greater than all later u_j. However, since v_i might be larger than v_{i+1} or not, but we can examine ordering: The sorted list of all first components (n) will consist of: all u_i in increasing order, then maybe some v_i? Since v_i > u_j for all j>i, the smallest v_i among all swapped pairs is likely the one with largest i (the last valid u). That v_i might be less than some u_j? No, because for i being the largest index (max u), there is no j>i. But for the largest u, say u_max, there could be v_max. Compare v_max with some earlier u? Since earlier u < u_max, and we have v_early > u_max (from property), so v_early > u_max. And v_max could be > u_max (since v_max >= u_max, usually > if d>0). v_max may be equal to u_max if d=0 (square case). So the set of first components: all u_i (increasing), and all v_i (which are all greater than any u_j for j>i). In particular, the smallest v_i is v_max (largest u). And v_max might be less than some u_i? Not possible because u_i < u_max, and v_max >= u_max > u_i? Actually u_max is the largest u, so v_max >= u_max > any other u_i. So v_max > all u_i except possibly equal? If u_max is the largest, then v_max >= u_max, and if v_max = u_max (square), then v_max equals the largest u, which is also an u_i (the max). So in that case, v_max = u_max is already present as a u_i. But as an ordered pair (v_max, u_max) would be (u_max, u_max) which is already accounted as (u_max, v_max) with u_max = v_max? Actually if u_max = v_max, then the unordered pair is (u_max, u_max). In that case we only add one ordered pair (u_max, u_max). So no separate swapped pair. So for non-square, v_max > u_max, so v_max is > all u_i. Similarly, other v_i are > u_max? Not necessarily: For early i, v_i > u_max (since u_max is one of the later u_j). So all v_i for i < max are > u_max. So the smallest v_i is v_max if v_max > u_max, otherwise none. And v_max might be close to u_max. So the set of first components from swapped pairs are all greater than or equal to u_max, with the smallest being v_max (if non-square). Therefore, all u_i (except possibly the largest) are less than all v_i (including the largest). So the sorted order of first components will be: all u_i in increasing order, then all v_i in increasing order? But v_i might not be monotonic increasing; we need to check if v_i are increasing or decreasing. Since v_i > v_{i+1}? In example, v values: 26,9,5. That's decreasing. In our approximate, they seem decreasing. If they are decreasing, then the swapped pairs (v_i, u_i) would have first components decreasing as u increases. So when sorted by first component, the v_i will appear in increasing order if we list them from smallest to largest. Since they are decreasing with i, the smallest v is the last one (v_max), then next is v_{max-1} > v_max, etc. So sorted v_i would be increasing if we list from v_max up to v_1. So after all u_i, the next smallest first component is v_max, then v_{max-1}, ..., v_1. But we need to also sort by second component when first component equal. However, first components are all distinct? Could two different v_i be equal? Could v_i = v_j for i != j? That would imply two different u give same v? Possibly if different solutions yield same max side? Let's examine equation: If (u1, v) and (u2, v) both satisfy with u1<u2<v, then both are unordered pairs with same max v but different min. Is that possible? Let's test: Suppose x fixed, u1 and u2 both valid with v1 = v and v2 = v? That would require both satisfy x = A(u1)+(v-u1)B(u1) and = A(u2)+(v-u2)B(u2). Subtract: A(u1)+(v-u1)B(u1) = A(u2)+(v-u2)B(u2). Rearr: (v)(B(u1)-B(u2)) = A(u2)-A(u1) + u1B(u1)-u2B(u2). Could happen? But given the strict inequalities we derived, likely not. Let's test with actual numbers? Possibly not. We'll assume distinct, but not necessary for algorithm.
Thus we could output in order by: first, all (u_i, v_i) for i=1..t (where u_i sorted increasing). Then, all (v_i, u_i) for i=t..1 (if v_i > u_i) in order of increasing v (i.e., from smallest v to largest v). But to ensure correct order when there may be equalities or interleaving, we should simply sort.
Given simplicity, we will just collect and sort.
Now, the main challenge is to iterate u up to bound where A(u) <= x. But we need to compute A(u) for each u. We can compute incrementally: A(1)=1, B(1)=1. For u+1: A(u+1) = A(u) + (u+1)^2; B(u+1) = B(u) + (u+1). This avoids multiplication each step and potential overflow? But (u+1)^2 up to (1.44e6)^2 ≈ 2e12, fits in 64-bit. So we can compute incrementally with addition, which is faster and simpler. Let's do that: initialize a=1, b=1 for u=1. Then for u from 1 while a <= x: process, then update: u++, then a += u*u; b += u; (but careful: when we move to next u, we need a for that u). So we can loop with variable u starting at 1, and maintain a and b. At the start of loop, a = A(u), b = B(u). After processing, we increment u and update a and b.
But we need to compute a = A(u) = sum_{i=1}^u i^2. For u=1, a=1. b = sum_{i=1}^u i =1.
Now, we also need to compute d = (x - a) / b. But we must ensure division exact. We can compute quotient and remainder.
So algorithm steps:
- read x.
- vector<pair<long long, long long>> pairs.
- long long u = 1;
- long long a = 1; // A(1)
- long long b = 1; // B(1)
- while (a <= x) {
long long diff = x - a;
if (diff % b == 0) {
long long d = diff / b;
long long v = u + d;
// add (u, v) and if u != v, also (v, u)
pairs.push_back({u, v});
if (u != v) pairs.push_back({v, u});
}
// update to next u
u++;
// a += u*u; b += u;
// careful: now u is increased, so a becomes A(u) for new u.
// compute before or after? Let's do:
// At the end of loop, update and continue.
// But we need to ensure we don't overflow when updating a and b. a may become > x, but we will check condition at next iteration.
a += (long long)u * u; // because a was A(u-1), now u increased, we add u^2.
b += u;
}
- After loop, sort(pairs.begin(), pairs.end()).
- Output pairs.size() and then each pair.
Check: For u=1, a=1, b=1. Process, then u becomes 2, a=1 + 4 =5 (A(2)=5), b=1+2=3. Good.
But we need to be careful about overflow when adding uu: u up to maybe 1.44e6, uu up to ~2e12, a can become up to about 1e18, sum of squares up to u_max. That's fine. b up to about u_max*(u_max+1)/2 ~ 1e12, safe.
But note: The loop condition while (a <= x) will exit when a > x. However, after updating a for the next u, if a overflows? It won't because a <= x before update? Actually at start of loop, a is for current u, which is <= x. Then we update to next u: a_new = a + (u+1)^2. a_new may exceed x, that's fine; we will check at next iteration and exit. So no overflow beyond 1e18+ (1.5e6)^2 = 1e18+2.25e12 ~ 1.000002e18, still less than 2^63-1. So safe.
But wait: At the final iteration when a <= x, we update to next u, and a might become > x. Then loop condition fails next time. That's fine.
But what about the case when a can exceed 2^63-1? x max 1e18, a_max for u where a <= 1e18 is at most 1e18, so a_new for u+1 could be >1e18 but still < 2^63-1? Since 2^63-1 ≈ 9.22e18, and we might potentially update a beyond that if we kept iterating, but we stop when a > x. Since x <= 1e18, a at the iteration we stop is > 1e18 but maybe up to about 1e18 + (next u^2). Could a ever exceed 9e18? The maximum a for u ~ 2e6? Actually if we mistakenly kept iterating beyond bound, a could become large. But we stop when a > x (1e18). So a will be at most 1e18 + (u+1)^2, which is about 1e18 + 4e12 = 1.000004e18, far below 9e18. So safe.
Thus iterative method is safe and efficient.
Now, we need to consider the scenario where x is huge, like 10^18, and we have to iterate u up to 1.44e6. That's fine.
Now, we should also consider if there might be solutions where the smaller side u is larger than the bound computed by while (a <= x) but still A(u) <= x? That's the bound. Our loop condition is a <= x, so we include all u with A(u) <= x. That's correct.
Now, what about u=0? Not allowed.
Thus the algorithm is correct.
But we must double-check the derivation and edge cases: When x is very small, the loop runs few times.
Edge case: x=1. Then what pairs? We need (n,m) such that total squares = 1. For a 1x1 table, there is exactly 1 square (1x1). Also 1x? bigger? Let's compute: 1 x m: total squares = number of 1x1 squares = 1*m = m. So to have total=1, m must be 1. So only (1,1). Also m x 1 gives same. So ordered pairs: (1,1) only. Let's test algorithm: u=1, a=1, diff=0, diff%b=0, d=0, v=1. Add (1,1) (once because u==v). u=2, a=5 >1, stop. So pairs = [(1,1)]. Output k=1, pair (1,1). That seems correct.
Another edge: x=3? Let's compute manually: 1x3: total squares = 3. So (1,3) and (3,1). Also 2x2: total squares = 1^2? Actually 2x2: squares: 1x1: 4, 2x2: 1 => total 5. Not 3. 1x3 is 3, so (1,3). Also (3,1). Are there any others? 2x? 2x? No. So expected (1,3) and (3,1). Let's test algorithm: u=1, a=1, diff=2, b=1, d=2, v=3 => add (1,3),(3,1). u=2, a=5>3 stop. So output (1,3),(3,1). Works.
Now, we should also consider if there are solutions where u is not the min but due to symmetry we capture. So fine.
Now, one more nuance: In the formula S = A(u) + d * B(u) we assumed u = min. But what if n and m are such that min is not u? Actually we defined u as min. So if we find (u,v) with u <= v, that's the unordered representation.
But what about the case where n < m but we iterate u from 1 to ... and for each u we compute d and v. That yields (u, v) with u <= v. So we get all unordered pairs. Good.
Now, we need to ensure that when we add (v, u) for u<v, we are not adding a duplicate if there is also an unordered solution (v, u) from some other u? But since min of (v,u) is u, that unordered solution would be (u,v) again, so it's the same unordered. So no duplicate.
Thus algorithm yields each ordered pair exactly once.
Now, about sorting: We'll use std::sort on vector<pair<ll,ll>>. Since pairs could be up to 2e6, we need to be careful with memory allocation. We'll reserve capacity maybe, but not necessary.
Now, what about time? 1.44e6 iterations, each O(1). Sorting O(k log k). With k up to 2.88e6, sorting is about maybe 0.1 seconds? Actually sorting 2 million pairs might be a bit heavier but still within typical time limits (2 seconds) in C++ with optimizations. However, we can optimize by not sorting if we can output in order using the property we discussed. But sorting is simpler and less error-prone.
But we should be aware that k could be as high as maybe 2e6, which is okay. I'll implement sorting.
Now, we should also check potential integer overflow in computing diff and multiplication when computing d? diff = x - a, x and a up to 1e18, diff up to 1e18. d = diff / b, b up to ~1e12, so d up to ~1e6? Actually when u=1, b=1, d can be up to 1e18, so d can be huge (up to 1e18). That's fine because v = u + d, v can be up to ~1e18. So v can be up to 1e18, which fits in long long. But note: when we add (v, u), we also need to store v up to 1e18. That's fine.
Now, one more nuance: In the loop, we compute diff = x - a. Since a <= x, diff >= 0. But we should use unsigned? Use signed long long.
Now, about the condition (diff % b == 0). Since b could be up to ~1e12, diff up to 1e18, modulus operation is fine.
Now, we must also consider that there may be many solutions and storing all pairs could be memory heavy if k ~ 3e6? Actually max unordered solutions is about number of u with A(u) <= x, which is about 1.44e6. So ordered pairs <= 2unordered - (number of squares, i.e., when u=v). In worst-case, if all u from 1 to U produce a solution, and all are non-square (i.e., d>0), then ordered pairs = 2U. That could be ~2.88e6. That's fine.
But wait: Could there be more than one solution for a given u? No, because d is uniquely determined from x and u. So at most one per u.
Thus k <= 2*U_max where U_max = max u with A(u) <= x. For x=1e18, U_max ≈ floor( cuberoot(3x) )? Actually we can compute U_max exactly: Find largest u such that u(u+1)(2u+1)/6 <= 1e18. Let's approximate u=1442249? We'll compute later. So U_max about 1.44e6. So 2.88e6 pairs max.
Now, sorting 2.88e6 pairs. Each pair 16 bytes (two 8-byte ints) plus overhead in vector (maybe 16 bytes per element? Actually std::pair likely 16 bytes, vector stores them contiguously. So 2.88e6 * 16 = 46 MB. Plus some overhead, still within 256 MB.
Thus fine.
Now, we must also consider that the problem might require to print the pairs in increasing n, and for equal n increasing m. Sorting does that.
Now, let's test algorithm with some random values and verify.
But before coding, we should consider potential pitfalls: Off-by-one in loop: We start u=1, a=1, b=1. Then process. Then update u to 2, a to 5, b to 3. Continue until a > x. But careful: For u where a <= x, we process. After processing, we update to next u even if that next u's a may exceed x; loop condition checks at beginning. That's fine.
But what about the case where a may overflow? Not as argued.
Now, we should also consider the possibility of x being such that for some u, diff % b == 0 but v = u + d may be less than u? That can't happen because d >=0.
Now, test with x=2: u=1, a=1, diff=1, 1%1=0, d=1, v=2. Add (1,2) and (2,1). u becomes 2, a=5 >2, stop. Output pairs sorted: (1,2) and (2,1). Good.
Test with x=8: u=1: a=1,diff=7,d=7,v=8 -> add (1,8),(8,1). u=2: a=5,diff=3,d=1,v=3 -> add (2,3),(3,2). u=3: a=14>8 stop. Output sorted: (1,8),(2,3),(3,2),(8,1). Good.
Test with x=26: as before.
Now test with x=1e18 perhaps? We'll test with code later.
Now, we need to be careful about data types: Use long long (int64_t) for x, a, b, diff, d, v. Use unsigned long long maybe for safety, but since we do subtraction x - a, and x and a are positive, diff is non-negative. Using signed is okay if we ensure no overflow: x max 1e18 < 2^63-1. So signed long long fine.
But for multiplication in a += uu: u up to 1.44e6, uu up to ~2e12, fits in signed long long. So fine.
Now, we need to handle large number of output efficiently. Use printf or cout with ios_base::sync_with_stdio(false) and cin.tie(0). We'll use printf for speed, or write with putchar. But printing up to 2.88e6 lines each with two integers and newline. That's about 2.88e6 * (maybe 20 chars) = 57 MB. Might be okay but borderline for I/O. Use fast output: we can use printf for each line but that's many calls; perhaps use std::cout with '\n' and tie untied, or use write buffer. I'll implement with printf for simplicity? Actually 2.88e6 printf calls could be a bit slow. Better to use fast I/O: use write own buffer or use cout with sync off. I'll do:
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
Then use cout << ... << '\n'; But still many lines. Could use vector and then output all at once? We'll just output as we go after sorting.
Alternatively, we could store output in a string buffer and then output, but not necessary.
Now, we must also consider that the answer expects the integer k first, then pairs. So we can first compute k = pairs.size(), then print k, then iterate.
Now, edge cases: x=0? Not allowed (1 <= x). So fine.
Now, are we missing any solution where n and m are not positive? No.
Thus code seems straightforward.
But let's double-check the derivation: sum_{k=1}^{min(n,m)} (n-k+1)(m-k+1). With u = min, v = max. Let i = u-k+1, then i runs 1..u. Then n and m? We assumed n = u, m = v. But careful: In our derivation we set u = min(n,m) and v = max(n,m). But we didn't assign which is rows and which columns. That's fine for unordered.
But what if we have n=3, m=5, then u=3, v=5. Our formula yields S = A(3) + (5-3)B(3) = 14 + 26 = 26. Correct.
If we have n=5, m=3, then u=3, v=5 still, same S.
Thus we capture both ordered via swapping.
Now, think about the condition for existence: d must be integer and >=0. That's all.
Thus solution is to iterate u from 1 while A(u) <= x, test divisibility, and collect.
Now, is there any possibility that u could be larger than the cube root but still A(u) <= x because of the shape? Actually A(u) grows like u^3/3, so the condition A(u) <= x implies u is O(x^{1/3}). So maximum u is about (3x)^{1/3}. For x=1e18, that's ~1442249. So okay.
Now, we need to ensure that we compute the bound correctly and not miss u where A(u) exactly equals x and then updates to next u where a becomes > x and we stop, which is fine.
Now, implement.
But wait: There's a potential nuance: The formula S(u,v) = A(u) + (v-u)B(u) derived under assumption that u = min. But what about the case where n < m but we treat u = n, v = m. That's correct. But suppose we consider n <= m, but our loop iterates over u as min. But we don't know which is min for each ordered pair. For each u, we produce unordered (u,v). But could there be a scenario where the min side is some value u, but the max side v is less than u? That would be contradiction. So not.
Thus correct.
Now, I'd like to test with random x to see if we get symmetric output and that S(n,m) matches.
Now, implement.
But I'm thinking about potential integer division issues: diff % b == 0. Since diff and b are positive (except diff could be 0), we need to handle diff=0. That's fine.
Now, let's write code.
Pseudo:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll x;
cin >> x;
vector<pair<ll, ll>> ans;
ll u = 1;
ll a = 1; // A(u)
ll b = 1; // B(u)
while (a <= x) {
ll diff = x - a;
if (diff % b == 0) {
ll d = diff / b;
ll v = u + d;
ans.emplace_back(u, v);
if (u != v) {
ans.emplace_back(v, u);
}
}
u++;
// update a and b for next u
a += u * u; // now a = A(u) for new u
b += u; // now b = B(u)
}
sort(ans.begin(), ans.end());
cout << ans.size() << "\n";
for (auto& p : ans) {
cout << p.first << " " << p.second << "\n";
}
return 0;
}
Now, verify for x=26 yields 6 pairs. For x=1e18, we need to ensure that we don't have performance issues.
But I'm concerned about the loop: while (a <= x). When u becomes large, a may become > x and we break. That's fine.
Now, what about a and b update: At u=1, a=1. After processing u=1, we set u=2, then a += 4 => a=5, which is A(2). But note: For u=2, we should also have b=1+2=3 which is B(2). Good.
But there is a subtle bug: The initial values a and b correspond to u=1. Then after processing u=1, we increment u to 2 and then update a and b. But what about the order: If we do u++ first, then a += u*u, b += u, then next iteration we process with the new u. That's fine.
But we need to be careful: In the loop condition we check a <= x. At the start, a is for current u. After processing, we update u and a. Then loop checks condition again. So it's correct.
Now, test manually with x=1:
Start u=1,a=1,b=1: a<=1 true. diff=0, divisible, d=0, v=1, add (1,1) only once. Then u=2, a=1+4=5, b=1+2=3. Next loop check a<=x? 5<=1 false, break. ans size=1, sorted yields (1,1). Good.
Now, test x=2:
u=1,a=1: diff=1, d=1, v=2 -> add (1,2),(2,1). u=2,a=5,b=3; break. ans size=2, sorted: (1,2),(2,1). Good.
Now, test x=8: as before.
Now, test x=50 manually? Let's compute to see if any off-by-one. Not necessary.
Now, we should also consider potential integer overflow in a += uu: for u up to 1.44e6, uu is about 2e12, a is at most about 1e18, so safe. But what if x is smaller, then loop stops early, so a never gets large.
Thus safe.
Now, we also need to consider the case where x is such that A(u) may overflow when we compute? No.
Now, we should also consider if the pair (v,u) could have v exceed 1e18? For u=1, d = x-1, v = x. Since x max 1e18, v max 1e18. So v fits in long long.
Now, is there any scenario where v > 1e18? Not because x <= 1e18, so v <= x? Actually for u=1, v = x, since u=1, A=1, B=1, d=x-1, v=1 + (x-1) = x. So v = x, which is <= 1e18. For u>1, v = u + d, with d = (x - A(u))/B(u). Since A(u) >= 1, d <= (x-1)/B(u) <= x-1 <= 1e18 -1. Adding u (<=1e6) still <= 1e18+1e6, which could exceed 1e18? Actually if d = (x-1)/B(u) and B(u) could be as low as 1? Only u=1 has B=1. For u>=2, B(u) >=3, so d <= (1e18)/3 ~ 3.33e17. Adding u <=1.44e6 gives still < 1e18. So v <= 1e18 roughly. But what about u=2? B=3, d = (x-5)/3. x max 1e18, d max ~ 3.33e17, plus 2 = still < 1e18. So v <= 1e18. Actually could v exceed 1e18? Suppose x=1e18, u=2, A(2)=5, B=3, d = (1e18-5)/3 ≈ 3.333333e17, v ≈ 3.333333e17 +2 = 3.33333333e17, far below 1e18. So v is less than x for u>1. So v max is x (when u=1). So v <= 1e18. So no overflow.
Thus using signed 64-bit is fine.
Now, memory: ans vector may be large. We'll reserve maybe to avoid reallocation: we can estimate max size: 2 * floor(cuberoot(3e18)). But we can just let it grow.
Alternatively, we could output directly without storing all? Since sorted order might be achievable by generating the ordered pairs in order. Let's try to see if we can output without sorting.
From earlier analysis: For all unordered solutions (u_i, v_i) with u_i increasing, we have v_i > u_j for all j>i. Also v_i likely decreasing. So the set of first components: all u_i (increasing) then all v_i (which are all > u_max). Since u_i are increasing and all less than any v_j? Actually we need to check: Is it possible that some v_i is less than some u_j with j>i? We proved v_i > u_j for i<j. So v_i > u_j for all j > i. So for the largest i, v_i is the smallest among v's. Since u_j for j>i don't exist, we only need to compare v_i with u_k for k < i? Actually u_k < u_i for k < i. Since v_i > u_i > u_k? Because v_i > u_i obviously. So v_i > all u_k? For i being the largest index, v_i > u_i > all previous u_k. So indeed all v_i are greater than all u_j? Let's verify: Take any v_i (from some u_i) and any u_j. If j > i, then v_i > u_j by property. If j < i, then u_j < u_i < v_i (since v_i > u_i). So v_i > u_j as well. So indeed every v_i is greater than every u_j. Therefore, all first components of the (u,v) pairs (where first = u) are less than all first components of the swapped pairs (where first = v). So in sorted order, all (u,v) come before all (v,u). Good.
Now, among the (u,v) pairs, they are already generated in increasing u (since we iterate u increasing). And since u is the first component, they are automatically sorted by first component (n). For ties (same n) we need increasing m. Since for a given u, there is exactly one v (if solution exists). So no ties. So (u,v) are already sorted correctly.
Now, among the swapped pairs (v,u), we need to sort them by first component (v). We need to output them in increasing v. Do we have a guarantee that v_i are decreasing as i increases? If that's the case, then if we generate swapped pairs in order of decreasing u (i.e., from largest u to smallest), then v will be increasing? Because if v_i are decreasing with i, then the largest i (largest u) gives smallest v, and smallest i (smallest u) gives largest v. So if we iterate u from max down to 1 and for each solution generate (v,u), then we will output in increasing v. Let's verify if v_i is strictly decreasing with increasing u. Is it always? From earlier analysis, it seems plausible but we need to be sure.
We derived that for i<j, we have v_i > u_j and also v_i > v_j? Let's try to prove v_i > v_j for i<j. Using the equation:
x = A(u_i) + (v_i - u_i) B(u_i) = A(u_j) + (v_j - u_j) B(u_j).
Suppose i<j => u_i < u_j.
We want to show v_i > v_j.
Consider the function f(u) = u + (x - A(u))/B(u). This is v(u). We can analyze derivative in continuous sense. For discrete, we can show that if u increases, v decreases. Let's attempt to prove by contradiction: Assume v_i <= v_j. Since u_i < u_j, we have v_i <= v_j. Then we have:
A(u_i) + (v_i - u_i) B(u_i) = A(u_j) + (v_j - u_j) B(u_j).
Rearrange: (v_i B(u_i) - v_j B(u_j)) = A(u_j) - A(u_i) + u_i B(u_i) - u_j B(u_j).
Not simple.
Alternatively, use the property of total squares: The total squares S is convex in both dimensions? Maybe not needed.
We can test with some numbers to see if it's possible to have v_i < v_j for i<j. Let's try to search small x manually? Let's consider x=50. Compute u values where A(u) <=50: u=1 A=1, u=2 A=5, u=3 A=14, u=4 A=30, u=5 A=55>50 so u up to 4. For each, compute d if divisible.
u=1: a=1,b=1,diff=49,d=49,v=50.
u=2: a=5,b=3,diff=45,d=15,v=17.
u=3: a=14,b=6,diff=36,d=6,v=9.
u=4: a=30,b=10,diff=20,d=2,v=6.
Here v: 50,17,9,6 decreasing.
Now try x=100: u=1 v=100; u=2 a=5 b=3 diff=95 not divisible? 95%3=2, so no; u=3 a=14 b=6 diff=86%6=2; u=4 a=30 b=10 diff=70%10=0 d=7 v=11; u=5 a=55 b=15 diff=45%15=0 d=3 v=8; u=6 a=91 b=21 diff=9%21? Actually 100-91=9, 9%21!=0; u=7 a=140>100 stop. So solutions: u=1 v=100, u=4 v=11, u=5 v=8. v:100,11,8 decreasing. So seems decreasing.
Now try to see if there could be a case where v_i increases. Consider u=2 gives v= some value, u=3 gives v larger? That would require d2 to be large enough to overcome. Since A(3) > A(2) and B(3) > B(2), the d for u=3 is (x - A(3))/B(3). For v3 > v2, we need u3 + d3 > u2 + d2. Since u3 > u2, d3 would need to be not too small. But recall that x = A(u2)+(v2-u2)B(u2). If v3 > v2, then we have A(u3)+(v3-u3)B(u3) = x. Since A(u3) > A(u2), to keep same x, the term (v3-u3)B(u3) must be smaller than (v2-u2)B(u2). That's possible if B(u3) is much larger, but v3-u3 could be much smaller. But can v3 be larger than v2? Let's try to find numeric counterexample.
We need u2 < u3, and v3 > v2. That is:
v2 = u2 + d2, v3 = u3 + d3, with v3 > v2.
From x = A(u2) + d2 B(u2) = A(u3) + d3 B(u3).
We can attempt to solve for x such that both are integers and v3 > v2.
Let’s denote u2=2, u3=3. Then A(2)=5, B(2)=3; A(3)=14, B(3)=6.
We need d2, d3 integers >=0 such that 5+3d2 = 14+6d3 => 3d2 - 6d3 = 9 => d2 - 2d3 = 3 => d2 = 2d3+3.
Then v2 = 2 + d2 = 2 + 2d3+3 = 2d3+5.
v3 = 3 + d3.
We want v3 > v2 => 3+d3 > 2d3+5 => 3+d3 > 2d3+5 => bring: 3-5 > 2d3 - d3 => -2 > d3 => d3 < -2, impossible since d3>=0. So v3 < v2 always. So for u2=2, u3=3, v3 < v2.
Now try u2=3, u3=4. A(3)=14,B=6; A(4)=30,B=10. Equation: 14+6d2 = 30+10d3 => 6d2 -10d3 = 16 => divide 2: 3d2 -5d3 =8 => d2 = (5d3+8)/3. For integer, need 5d3+8 ≡0 mod3 => 5d3 ≡1 mod3 => 2d3 ≡1 mod3 => d3 ≡2 mod3? Let's solve: d3=2 gives 52+8=18, d2=6; then v2=3+6=9, v3=4+2=6 => v3<v2. d3=5 gives 55+8=33, d2=11; v2=14, v3=9 => still v3<v2. So seems v3 < v2.
In general, we can attempt to prove v(u) is decreasing. Consider u and u+1. Let’s derive relation:
We have x = A(u) + d(u) B(u) = A(u+1) + d(u+1) B(u+1).
We know A(u+1) = A(u) + (u+1)^2, B(u+1) = B(u) + (u+1).
Then:
A(u) + d B(u) = A(u) + (u+1)^2 + d' (B(u) + (u+1))
=> d B(u) = (u+1)^2 + d' B(u) + d' (u+1)
=> (d - d') B(u) = (u+1)^2 + d' (u+1)
=> (d - d') B(u) = (u+1)(u+1 + d').
Now, note that B(u) = u(u+1)/2.
Thus (d - d') * u(u+1)/2 = (u+1)(u+1+d').
Cancel (u+1) (positive):
(d - d') * u / 2 = u+1 + d'.
=> (d - d') * u = 2(u+1+d').
=> d u - d' u = 2u + 2 + 2d'
=> d u = d' u + 2u + 2 + 2d'
=> d u = d' (u + 2) + 2u + 2.
=> d = d' (1 + 2/u) + 2 + 2/u.
This is not directly showing relation between v = u+d and v' = u+1+d'. But we can compute v = u+d, v' = u+1+d'.
Compute v - v' = (u+d) - (u+1+d') = d - d' - 1.
From earlier: (d - d') = [2(u+1+d')] / u.
So v - v' = [2(u+1+d')]/u - 1 = (2(u+1+d') - u)/u = (2u+2+2d' - u)/u = (u+2+2d')/u = 1 + (2+2d')/u? Actually (u+2+2d')/u = 1 + (2+2d')/u. That's > 0. So v - v' > 0, i.e., v > v'. So indeed v(u) > v(u+1). So strictly decreasing. Great! So v_i is strictly decreasing as u increases. So the sequence v_i is decreasing. Therefore, for i < j, u_i < u_j, v_i > v_j.
Thus we have:
- All (u_i, v_i) have first component u_i increasing.
- All swapped pairs (v_i, u_i) have first component v_i decreasing with i.
- All v_i are greater than all u_j (as shown). So all (v_i, u_i) have first components larger than any u_j.
Therefore, to output in increasing n (first component), we can first output all (u_i, v_i) for i from 1 to t in increasing order (which we already have as we iterate u increasing), and then output (v_i, u_i) for i from t down to 1 (since v_i decreasing, i=t gives smallest v, i=1 gives largest v). That would produce the swapped pairs in increasing order of first component.
But careful: There may be a square case where u_i = v_i. Then we only added one pair (u_i, u_i). In the list of (u_i, v_i) we have that. For swapped pairs, we do not add when u=v. So when iterating i from t down to 1, we should only output (v_i, u_i) if u_i != v_i.
Thus we can avoid sorting by generating the ordered pairs in the correct order directly. This would be more efficient and avoid storing many pairs? We still need to store at least the unordered pairs (u_i, v_i) to later output swapped pairs in reverse order. But we could store them in a vector as we generate, then after the loop, we have the list of unordered solutions (u, v). Since we iterated u increasing, the vector of unordered solutions is in increasing u order. Then we can output:
-
First, output total count k = sum_{sols} (1 if u==v else 2). We can compute k as we go, maybe store.
-
Then, iterate over the unordered solutions (each stored) and for each, output (u, v) (this will be in increasing n because u increasing).
-
Then, iterate over the unordered solutions in reverse order, and for each with u != v, output (v, u). Since v are decreasing as we go reverse, (v, u) will be in increasing n because the smallest v is from the last solution, etc.
But we must ensure that when we output (v,u) in reverse order, the first components are strictly increasing. Since v_i are decreasing as i increases, reversing gives increasing order. However, is it guaranteed that v_i are distinct? Possibly could be equal? Let's check if two different u could yield same v. Suppose (u1, v) and (u2, v) both satisfy with u1<u2<v. Then we have:
x = A(u1)+(v-u1)B(u1) = A(u2)+(v-u2)B(u2)
=> A(u1)-A(u2) + v(B(u1)-B(u2)) - u1B(u1)+ u2B(u2) = 0
=> (A(u1)-A(u2)) + v(B(u1)-B(u2)) = u1B(u1)-u2B(u2)
Given that B(u1) < B(u2) and A(u1) < A(u2), it might be possible? Let's test numerically if we can find such case. Could v be same? Using the relation derived earlier between consecutive u: we had d u = d' (u+2) + 2u+2, and v = u+d, v' = u+1+d'. We proved v > v' strictly. So for consecutive u, v > v'. For non-consecutive, since v is decreasing, v_i > v_j for i<j, thus all v_i distinct. So no two different u give the same v. So v_i are distinct and strictly decreasing as u increases. Good.
Thus the reverse iteration yields strictly increasing v.
Also note: Could there be a case where a v from one unordered pair equals a u from another unordered pair? That would require v_i = u_j for some i<j. But we proved v_i > u_j for all i<j, and for i>=j? Actually if i >= j, u_j <= u_i? Since i>=j, u_i >= u_j. v_i > u_i >= u_j, so v_i > u_j for i>=j? Actually if i=j, v_i > u_i? Yes if d>0, v_i > u_i; if d=0, v_i = u_i. So v_i >= u_i. For i>j, u_i > u_j, and v_i > u_i > u_j, so v_i > u_j. For i<j, we proved v_i > u_j as well. So v_i > u_j for all i,j unless possibly i=j and d=0 then v_i = u_i, but that equals u_j only when i=j. So v_i could equal some u_j only if i=j and d=0. In that case, it's the square case. So that's fine.
Thus, after outputting all (u_i, v_i) (which include squares as (u,u)), the next output will start with the smallest v (from the last unordered solution, which has the largest u). That v will be > all u_i? Actually the smallest v is from the largest u (if d>0). Since that v > u_max (strictly if d>0), it is greater than all u_i (including u_max? Actually u_max is the largest u, and v_max > u_max, so v_max > all u_i). So after the last (u_max, v_max) (if v_max>u_max) we have already output (u_max, v_max) in the first pass. Then in second pass we output (v_max, u_max) which has first component v_max. Since v_max > u_max, and all previous first components in first pass were ≤ u_max, this new first component is larger than any previous. And since v are decreasing as u increases, the last solution has smallest v, so in reverse order we output increasing v. So the sequence of first components in second pass is strictly increasing.
Thus this method yields correctly sorted output without needing to sort all pairs. It also reduces memory because we only need to store unordered pairs, not both ordered. However, we still need to store unordered pairs to output them in reverse order later. That's about half the size of ordered pairs. But we could also store them and then output as described.
Alternatively, we could compute and store the ordered pairs directly, but sorting is okay. However, given that k may be up to ~3e6, sorting may be a bit slower but still acceptable. But perhaps implementing the order manually is simple and avoids sorting, making it more efficient. I'll go with that.
But careful: The output format expects first k then the pairs. We need to know k before printing pairs. With this two-pass approach, we can first compute all unordered solutions, storing them in a vector, and also compute k = total ordered count. Then print k, then first pass (u,v) for each stored unordered, then second pass in reverse for those with u != v.
We must ensure that for squares (u=v), we only output once, and we should not output swapped. In first pass we output (u,u). In reverse pass, we skip if u==v.
Now, we also need to verify that the order of pairs with same n (first) but different m is increasing m. In first pass, for each (u,v) with u fixed, m=v. Since v is decreasing as u increases? Actually v decreasing, but for first pass, we output in increasing u, and v for each u could be anything. But the requirement: order by increasing n, then increasing m. For pairs with same n, there could be multiple? Could there be multiple ordered pairs with same n? That would require either two different unordered solutions having the same u (impossible, as each u gives at most one solution) or a swapped pair from some other solution having n = that same u. As argued, swapped pairs have n = v, and v > all u. So no duplicate n across first and second pass. However, could there be duplicate n among swapped pairs? Since v_i are distinct, each swapped pair has distinct n. So overall, each n appears at most once. So the ordering by n is simply all first components distinct, so m order doesn't matter for ties. But it's still good to ensure that within same n (if any) we output increasing m. Since there are no ties, it's fine.
But consider the possibility of a square case: (u,u) appears in first pass. Could there be a swapped pair from another solution that also has first component = u? That would require some v_j = u. Since v_j > all u_i as argued, v_j > u_max >= u (since u <= u_max). For v_j = u, that would imply v_j <= u_max, but v_j > u_max, contradiction. So no.
Thus the two-pass method yields correct order.
Now, we must also ensure that for the square case, when u=v, we only output once, and not in reverse pass.
Now, implement.
We'll use a struct or pair to store unordered solutions. We'll store u and v.
During iteration, when we find a solution, we store {u, v} in a vector, say sols. Also compute total count: if u==v, ans_count += 1; else ans_count += 2.
After loop, we print ans_count.
Then, for each sol in sols (in order), print sol.u and sol.v. This is first pass.
Then, iterate over sols in reverse order (from last to first), and for each sol, if sol.u != sol.v, print sol.v and sol.u.
That should produce the correct sorted order.
Let's test with example x=26:
sols = [(1,26), (2,9), (3,5)] in that order. ans_count = 6. First pass: (1,26),(2,9),(3,5). Second pass reverse: start from (3,5): u!=v so print (5,3); next (2,9): print (9,2); next (1,26): print (26,1). Output sequence: (1,26),(2,9),(3,5),(5,3),(9,2),(26,1). That matches sorted order.
x=8: sols = [(1,8),(2,3)]. first pass: (1,8),(2,3). second pass reverse: (2,3) -> print (3,2); (1,8) -> print (8,1). Output: (1,8),(2,3),(3,2),(8,1). Sorted.
x=2: sols = [(1,2)]. first pass: (1,2). second pass reverse: (1,2) u!=v so print (2,1). Output: (1,2),(2,1). Sorted.
x=1: sols = [(1,1)]. first pass: (1,1). second pass reverse: skip (since u==v). Output: (1,1). Sorted.
Works.
Now, we need to ensure that for x large, sols may contain many elements. Storing sols of size up to ~1.44e6, each a pair of 64-bit ints -> about 16 bytes per element -> 23 MB. Acceptable.
Thus final algorithm:
- Read x.
- Initialize vector< pair<ll,ll> > sols; (or two vectors)
- ll u=1, a=1, b=1;
- while (a <= x):
diff = x - a;
if (diff % b == 0):
d = diff / b;
v = u + d;
sols.push_back({u, v});
// update for next u
u++;
a += u*u;
b += u;
- After loop, compute k = 0; for each sol: if u==v then k+=1 else k+=2.
- Print k.
- For each sol in order: print sol.first, sol.second.
- For each sol in reverse order: if sol.first != sol.second, print sol.second, sol.first.
Make sure to use fast output.
Now, one more nuance: In the loop, we use u as current value. We start with u=1, a=1, b=1. Then after processing, we increment u and update a,b. But careful: The update uses the new u. So after loop, we have processed all u up to where a <= x. Good.
But we need to ensure that we don't miss u where a exactly equals x? That's included because condition a <= x includes equality.
Now, consider the case where x is huge, the loop will run many iterations. The updates a += u*u may cause a to exceed 2^63-1? As argued, not before breaking because a > x <= 1e18. But if x=1e18, a will be around 1e18 at the last iteration. Then we compute a += (u+1)^2, but we break before next iteration because a > x, we don't use that a except for next condition. But the addition is done after processing. That's fine.
Now, let's compute the maximum u precisely. For safety, we could use while (true) and break when a > x after update? But the loop condition checks at start. So at start, we have a for current u. After processing, we update to next u. So the loop will eventually have a for u where a > x, and then while condition fails, so we exit. That's fine.
But we must be careful: At the last valid u, after processing, we update to u+1, compute a and b. Then the loop condition checks for the next iteration and finds a > x, so exits. So we don't process invalid u.
Now, edge case: What if x is 0? Not allowed.
Now, we should also consider if x is large enough that a may become negative due to overflow? Not.
Now, we can finalize code.
But we need to be careful about the type of u: u can go up to about 1.44e6, which fits in 32-bit int. But we use long long for simplicity.
Now, I'll implement using int64_t from for clarity.
Implementation details:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll x;
cin >> x;
vector<pair<ll, ll>> sols;
ll u = 1;
ll a = 1; // sum of squares up to u
ll b = 1; // sum of integers up to u
while (a <= x) {
ll diff = x - a;
if (diff % b == 0) {
ll d = diff / b;
ll v = u + d;
sols.push_back({u, v});
}
u++;
a += u * u;
b += u;
}
// compute total count
ll k = 0;
for (auto& p : sols) {
if (p.first == p.second) k += 1;
else k += 2;
}
cout << k << "\n";
// first pass
for (auto& p : sols) {
cout << p.first << " " << p.second << "\n";
}
// second pass in reverse
for (auto it = sols.rbegin(); it != sols.rend(); ++it) {
if (it->first != it->second) {
cout << it->second << " " << it->first << "\n";
}
}
return 0;
}
But wait: We output first pass and then reverse. However, the order of pairs overall should be increasing n. As argued, first pass gives all pairs with n = u (in increasing order). Then reverse pass gives pairs with n = v, where v are increasing because we output from smallest v to largest v. So total order is correct.
But we must ensure that no pair is output twice: In first pass, we output (u,v) including squares. In reverse pass, we output (v,u) only if u!=v. So squares not duplicated.
Now, test with example x=26: sols order: (1,26), (2,9), (3,5). first pass outputs (1,26),(2,9),(3,5). reverse pass: from rbegin to rend: (3,5) -> output (5,3); (2,9) -> (9,2); (1,26) -> (26,1). Final order: (1,26),(2,9),(3,5),(5,3),(9,2),(26,1). That's sorted by n: (1,26) n=1, (2,9) n=2, (3,5) n=3, (5,3) n=5, (9,2) n=9, (26,1) n=26. Yes increasing.
Now test with x=1: sols = [(1,1)]. first pass: (1,1). reverse pass: skip. Output: (1,1). Good.
Now test with x=4? Let's compute manually: which (n,m) give total 4? 1x4: total=4 => (1,4) and (4,1). Also 2x3? 2x3 total=8, not. 2x2 total=5, not. So only (1,4),(4,1). Our algorithm: u=1: a=1,b=1,diff=3,d=3,v=4 -> sols add (1,4). u=2: a=5>4 stop. sols = [(1,4)]. k=2. first pass: (1,4). reverse: (4,1). Output (1,4),(4,1). Good.
Now test with x=5: total squares=5? 2x2 gives 5. Also 1x5 gives 5? Actually 1x5 total=5 (only 1x1 squares). So (1,5) and (5,1) and (2,2). So three ordered pairs: (1,5),(2,2),(5,1). Sorted: (1,5),(2,2),(5,1). Our algorithm: u=1: a=1,b=1,diff=4,d=4,v=5 -> sols add (1,5). u=2: a=5,b=3,diff=0,d=0,v=2 -> sols add (2,2). u=3: a=14>5 stop. sols = [(1,5),(2,2)]. k = (1!=5 => +2) + (2==2 => +1) = 3. first pass: (1,5),(2,2). reverse pass: iterate from rbegin: (2,2) skip (u=v), (1,5) output (5,1). Final output: (1,5),(2,2),(5,1). Sorted? (1,5) n=1, (2,2) n=2, (5,1) n=5. Good.
Now test with x=50 (we computed earlier): u=1 (1,50), u=2? a=5,b=3 diff=45? 45%3=0,d=15,v=17 => (2,17). u=3 a=14,b=6 diff=36%6=0,d=6,v=9 => (3,9). u=4 a=30,b=10 diff=20%10=0,d=2,v=6 => (4,6). u=5 a=55>50 stop. sols = [(1,50),(2,17),(3,9),(4,6)]. first pass outputs those. reverse pass outputs (6,4),(9,3),(17,2),(50,1). So total ordered pairs = 8. Sorted by n: (1,50),(2,17),(3,9),(4,6),(6,4),(9,3),(17,2),(50,1). Check if any missing? Could there be (5,?) no. So seems correct.
Now test with x=0? not allowed.
Now, we must consider integer overflow in a += uu. Since u up to maybe 1.44e6, uu up to 2e12, a up to 1e18, safe. But what if x is very close to 1e18, and we have u_max near 1.44e6, a after update to next u (u_max+1) could become slightly > 1e18. But a is computed using a += (u_max+1)^2, where u_max+1 might be up to ~1.44e6+1, square ~2e12, so a becomes about 1e18+2e12, still within 9e18, safe. But what if x is exactly 1e18 and a for u_max is <=1e18, then we update to u_max+1, a may become >1e18 but still safe.
But is it possible that during the loop, a becomes negative due to overflow? Not as argued.
Now, we need to also consider that for u=1, b=1, diff%1 always 0, so we always get a solution for any x? Let's check: For u=1, A=1, B=1, diff = x-1, d = x-1, v= x. So (1, x) always a solution. Indeed, a 1 by x table has exactly x squares (all 1x1). So (1,x) and (x,1) are always solutions. So there is at least one solution for any x. So k >= 2 for x>1, and k=1 for x=1. That matches examples.
Thus algorithm always finds at least u=1.
Now, we need to consider if there is any solution where u is not integer? No.
Now, test with x=10^18, we will iterate u up to ~1.44e6. The loop is fine.
Now, potential issue: In the loop, we compute diff % b. b can be large, up to about 1e12. Mod operation on 64-bit integers is fine.
Now, we need to ensure we use correct data types: long long is signed. diff = x - a, both positive, diff could be as large as 1e18. That's fine. d = diff / b could be up to 1e18 (for u=1, b=1). That's fine. v = u + d, up to 1e18+1e6, still within 2^63-1? 1e18 + 1e6 ~ 1e18, which is less than 9.22e18, so okay.
But careful: When u=1, d = x-1, v = 1 + x-1 = x. So v = x, which is up to 1e18, so v stored as long long okay.
Now, what about if x = 10^18 and u=1, v = 10^18, which is within range.
Now, check for x = 10^18 exactly, our loop will run many iterations, but it's fine.
Now, we also need to think about the maximum possible u. Using while (a <= x). For large x, a may become very large but we break. But to be safe, we should ensure that when we update a, we do not overflow before checking the loop condition for the next iteration. The loop condition is checked at the beginning, not after update. So we compute a for next u, then at next iteration we check if a <= x. So if a overflowed to negative, it would likely be <= x? But if overflow occurs, it would be negative, which is < x, so loop would continue erroneously, causing infinite loop or errors. So we must guarantee that a never overflows before we break. As argued, maximum a we compute is for u where we may process up to u_max such that A(u_max) <= x. After processing u_max, we update to u_max+1 and compute a = A(u_max+1). This a may be > x, but still less than 2^63-1? Actually A(u_max+1) could be > x, but we need to check if A(u_max+1) could exceed 2^63-1. Since x <= 1e18, and u_max is such that A(u_max) <= 1e18, then u_max+1 is maybe 1.44e6+1. A(1.44e6+1) approx (u+1)^3/3. With u_max+1 ~ 1.44e6+1, the cube is ~3.0e18? Actually (1.44e6)^3 = 2.985e18. /3 = 0.995e18. Wait earlier we estimated A(u) ~ u^3/3. For u=1.44e6, u^3/3 = (2.985e18)/3 = 9.95e17? That seems off. Let's recalc: 1.44e6 cubed = (1.44)^3 * 1e18 = 2.985... * 1e18? Actually 1.44^3 = 2.985984, so 1.44e6^3 = 2.985984e18. Divide by 3 gives 9.95328e17, which is about 1e18. So A(1.44e6) ≈ 1e18. So indeed A(1.44e6) ≈ 1e18. So u_max ≈ 1.44e6. Then A(u_max+1) = A(u_max) + (u_max+1)^2. (u_max+1)^2 ≈ (1.44e6)^2 = 2.07e12. So A(u_max+1) ≈ 1e18 + 2e12 = 1.000002e18, still < 2^63-1 (9.22e18). So no overflow.
Thus safe.
Now, we also need to consider the case where x is small, e.g., x=1. Then u_max =1. After processing u=1, we update to u=2, a=5, which is >1 but still safe.
Thus fine.
Now, one more nuance: The sum of squares formula A(u) = u(u+1)(2u+1)/6 is integer, but our incremental approach a = a + u*u (starting from 0) yields the same. We start with a=1 for u=1, then for u=2, a=1+4=5, etc. That's correct.
Now, confirm that for u=1, b = sum i from 1 to 1 = 1. For u=2, b=1+2=3, correct.
Now, we should test a larger random x to ensure no off-by-one in the loop condition causing missing the last u where a <= x. Our loop starts with u=1, a=1. At the end of loop for u=U, we update to u=U+1 and a becomes A(U+1). If A(U+1) <= x, then the loop will continue because at next iteration condition checks a <= x. So we will process U+1. So we will process all u such that A(u) <= x. Good.
Now, what about u where A(u) exactly equals x? That's included and d=0, v=u. Good.
Now, we must consider if x can be such that A(u) <= x for u up to some large number but the loop might become infinite if we never exceed x due to overflow? Not.
Now, implement.
One more optimization: We can compute total count k while generating sols, not needing second loop.
Now, about output: Use cout with '\n', and after the loop flush. With ios::sync_with_stdio(false), cout is fast.
But printing up to ~3 million lines using cout may be okay, but to be safe we can use printf for faster output? Actually cout with sync off and using '\n' is comparable to printf. I'll use printf for simplicity? But mixing printf and cout after syncing off can cause issues if not careful. Better to use cout consistently.
I'll use cout with '\n' and ensure we don't use endl (which flushes). Also we can reserve output by building a string but not needed.
Now, one more thought: The problem might require that n and m be positive integers up to 10^18, so we output them as such.
Now, final code.
But before finalizing, let's test with some random x using brute force for small x to verify.
We can write a small brute for x up to, say, 1000 and compare with our algorithm.
We'll do mental check for some values:
x=1: our output (1,1) ok.
x=2: (1,2),(2,1) ok.
x=3: (1,3),(3,1) ok.
x=4: (1,4),(4,1) ok.
x=5: (1,5),(2,2),(5,1) ok.
x=6: Which pairs give 6? 1x6 =6 -> (1,6),(6,1). 2x3? 2x3=8, not 6. 2x4? 2x4: squares: 1x1:8, 2x2:3? Actually 2x4: 1x1:8, 2x2:3? Let's compute: For 2x4: total = sum_{k=1}^{2} (2-k+1)(4-k+1) = k=1:24=8; k=2:13=3; total=11. Not 6. 3x? no. So only (1,6). Also (2,? ) no. (3,?) no. So only (1,6) and (6,1). Our algorithm: u=1 -> (1,6); u=2 a=5,b=3 diff=1 not divisible; u=3 a=14>6 stop. sols = [(1,6)]; k=2; output (1,6),(6,1). Good.
x=7: 1x7=7 -> (1,7),(7,1). any other? u=2 a=5,b=3 diff=2 not divisible; u=3 a=14>7. So only (1,7),(7,1). Our algorithm: same.
x=8: we already did, includes (1,8),(2,3),(3,2),(8,1). Good.
x=9: Let's compute manually: 1x9=9 -> (1,9),(9,1). 2x? 2x5? 2x5 total: 1x1:10, 2x2:4? Actually compute 2x5: squares: 1x1:10, 2x2:4 => total=14. 2x4:11, 2x6:? 2x6: 1x1:12, 2x2:5 =>17. 3x? 3x3:14? Actually 3x3 total: 1x1:9,2x2:4,3x3:1 =>14. 3x4: 1x1:12,2x2:6,3x3:2 =>20. So maybe only (1,9). But also (3,?) maybe? What about (2,2) gives 5, (2,3) gives 8, (2,4) 11, (3,2) 8, (3,3) 14. So only (1,9). Our algorithm: u=1: (1,9). u=2: a=5,b=3 diff=4 not divisible by 3. u=3: a=14>9. So sols [(1,9)] => output (1,9),(9,1). That's correct? But wait, is (9,1) included? Yes 9x1 table has 9 squares. So total squares 9. So only these two. So k=2. Good.
x=10: 1x10=10 -> (1,10),(10,1). Also maybe (3,?) no. (2,?) 2x? compute 2x? total =? For 2x? we have formula S(2,m)=5 + (m-2)*3 = 5+3(m-2)=3m-1? Let's derive: For u=2, S=5+3d, with d=m-2, so S=5+3(m-2)=3m-1. Set 3m-1=10 => 3m=11 => m=11/3 not integer. So no. So only (1,10). Our algorithm: u=1 (1,10); u=2 diff=5 not divisible by3; u=3 a=14>10 => sols [(1,10)]. k=2. Good.
x=11: 1x11=11. Also 2x4 gives? 2x4 total 11? Let's compute 2x4: u=2, v=4: d=2, S=5+3*2=11. Yes! So (2,4) and (4,2) are solutions. So total ordered: (1,11),(2,4),(4,2),(11,1). That's 4 pairs. Our algorithm: u=1: (1,11). u=2: a=5,b=3,diff=6, d=2, v=4 -> (2,4). u=3: a=14>11 stop. sols = [(1,11),(2,4)]. k = 2+2=4. first pass: (1,11),(2,4). reverse: (2,4) -> (4,2); (1,11) -> (11,1). Output order: (1,11),(2,4),(4,2),(11,1). Sorted by n: (1,11),(2,4),(4,2),(11,1). That's correct: n=1,2,4,11 increasing. Note that (4,2) has n=4 which is >2, so order correct.
Thus algorithm passes these tests.
Now, we should consider potential integer division issue: diff % b == 0. Since diff and b are positive, that's fine. For large numbers, modulus is fine.
Now, we also need to consider that b might become zero? b starts at 1, always positive.
Now, we need to ensure that we handle large x and many solutions without timeouts. The loop runs up to ~1.44e6 iterations, each doing a few operations. That's very fast (<0.01 sec). The output printing may be the bottleneck if k is large. But 3 million lines printing with cout may take some time but should be okay within 2 seconds if optimized? I'm not entirely sure; printing 3 million lines might be around 30-40 MB, which can be done within a second with fast I/O. I'll use printf for output to be safe? But mixing cout and printf after syncing off can cause disorder. I'll stick with cout but ensure we use '\n' not endl.
Alternatively, we could use write function but not needed.
I can also compute the total string size and output with puts? Not.
I'll use cout.
Now, final code.
But before finalizing, we need to consider one more nuance: The loop uses a and b as cumulative sums. However, we need to ensure that for u where a is exactly x, diff=0, d=0, v=u. That's correct.
Now, check for x very large: x=10^18, the loop will run about 1.44e6 iterations. In each iteration we do a diff% b, which is a modulo operation on 64-bit ints. That's fine.
Now, we need to make sure that we do not use too much memory for sols. sols will have size equal to number of valid u. That could be up to 1.44e6. So vector of pairs of long long is about 1.44e6 * 16 = 23 MB. Acceptable.
Now, we can also compute k as we push: k += (u == v ? 1 : 2). That avoids extra loop.
Now, implement.
Edge case: When x=10^18, for u=1, we get v=x, which is 10^18. That's fine.
Now, let's compute maximum u for x=10^18 exactly. We can compute while a <= x. At u=1442249? We'll trust.
Now, one more test: x=1000000000000000000 (1e18). We can't test manually but code should run.
Now, consider potential integer overflow in a += uu for u near 1.44e6: uu ≈ 2e12, a at that point is about 1e18, sum ~ 1.000002e18, still < 2^63-1. So safe.
Now, think about using int for u? Could use int up to 2e9, but we can use long long to avoid casts. But u increment up to 1.44e6 fits in int. However, when computing a += uu, if u is int, uu might overflow 32-bit (since u up to 1.44e6, square ~2e12 > 2^31). So we need to cast to long long. So better to use long long for u to avoid overflow in multiplication. Or we can do a += 1LL * u * u, but if u is int, 1LLuu promotes to long long. But simpler: declare u as long long.
Thus:
ll u = 1;
ll a = 1;
ll b = 1;
Now, a += u*u, where u is ll, safe.
Now, final code.
But before final, let's test with x=10^18 mentally: nothing.
Now, we must consider if there is any case where the computed v for a solution exceeds 2^63-1? For u=1, v=x, x <= 1e18, safe. For u>1, v is less than x, safe. So v always <= x <= 1e18. So safe.
Now, final answer.
But wait: The problem statement says "Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m columns." It says "n rows and m columns". Usually rows and columns could be up to 10^18? Not specified, but from our solution, they can be large. So output large numbers.
Now, test with example: input 26, output matches.
Now, finalize.
I'll produce the final solution with explanation.
But the user asked for "We need to produce final answer". Actually the conversation is about solving the problem, and we need to output the answer as code? The user is an assistant, we need to provide solution description? The instruction: "Spongebob is already tired ..." we need to write code? Probably they want the solution in the form of a code. However, the user message: "Spongebob is already tired ... Input ... Output ..." It looks like a problem statement, and the assistant should provide a solution (likely code). In typical ChatGPT interaction for programming problems, the assistant responds with explanation and final code. So I'll provide explanation and then the code.
Given the instruction: "You are an AI...", I need to respond with helpful answer. I'll explain the approach and then provide the C++ code.
Now, final response: explanation of deriving formula, iterating over smaller side, checking divisibility, and output ordering. Then present code.
I'll also mention the reasoning about the number of solutions and ordering.
Thus final answer. We need to find all ordered pairs of positive integers (n, m) such that the total number of squares inside an n × m rectangular grid equals a given x (1 ≤ x ≤ 10^18).
The number of squares is symmetric:
S(n, m) = Σ_{k=1}^{min(n,m)} (n - k + 1)(m - k + 1).
If we set u = min(n, m) and v = max(n, m), the sum simplifies to
S = A(u) + (v - u) * B(u),
where A(u) = u(u+1)(2u+1)/6 and B(u) = u(u+1)/2.
For a fixed x, we iterate over all possible u (the smaller side) while A(u) ≤ x.
For each u we compute d = (x - A(u)) / B(u).
If d is integer (i.e. (x - A(u)) % B(u) == 0), then v = u + d gives an unordered solution (u, v).
Because the expression is symmetric, every unordered solution (u, v) with u ≤ v corresponds to two ordered pairs (u, v) and (v, u), except when u = v (only one).
We can generate all unordered solutions in increasing order of u by iterating u from 1 upward.
The values A(u) and B(u) can be updated incrementally to avoid large multiplications.
After collecting the unordered solutions, we output:
- first all pairs
(u, v) in the order they were found (increasing n),
- then all swapped pairs
(v, u) in reverse order of the list, because the corresponding v's are strictly decreasing when u increases, so reversed they become increasing.
This yields all ordered pairs sorted by the first component (and automatically by the second when needed).
The loop runs for u up to about ∛(3x) ≈ 1.44·10^6 for the maximal x, which is fast.
Memory usage is about O(∛x) for storing the unordered solutions.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll x;
cin >> x;
vector<pair<ll, ll>> sols; // store (u, v) with u <= v
ll k = 0; // total number of ordered pairs
ll u = 1; // current smaller side
ll a = 1; // A(u) = sum of squares up to u
ll b = 1; // B(u) = sum of integers up to u
while (a <= x) {
ll diff = x - a;
if (diff % b == 0) {
ll d = diff / b;
ll v = u + d;
sols.emplace_back(u, v);
k += (u == v ? 1 : 2);
}
// prepare next u
u++;
a += u * u; // add u^2 to sum of squares
b += u; // add u to sum of integers
}
cout << k << '\n';
// first pass: (u, v) in increasing u
for (auto& p : sols)
cout << p.first << ' ' << p.second << '\n';
// second pass: (v, u) for u != v, in reverse order to get increasing v
for (auto it = sols.rbegin(); it != sols.rend(); ++it)
if (it->first != it->second)
cout << it->second << ' ' << it->first << '\n';
return 0;
}