We are given n, 1 <= n <= 1e9. We need to find all positive integers x such that x + sum_of_digits(x) = n.
We need to output the count and the values in ascending order.
The straightforward approach: Since n <= 1e9, x is at most n (since sum_of_digits(x) >= 1 for positive x, so x <= n-1). But checking all x from 1 to n-1 is impossible for n up to 1e9. However, we can use the property: sum_of_digits(x) <= 9 * number_of_digits. For x up to 1e9, x has at most 10 digits? Actually 1e9 is 1,000,000,000, which is 10 digits (if we consider up to 1e9 inclusive, max 10 digits? 1e9 = 1000000000 has 10 digits). So max digits is 10. So sum_of_digits(x) <= 910 = 90. Therefore, x is close to n: x = n - s, where s = sum_of_digits(x). Since s is between 1 and 90 (or maybe up to 910=90, but for n up to 1e9, x is at most n, so s <= 90 indeed). But s depends on x, but if we suppose x = n - s, then s = sum_of_digits(n - s). So we can iterate over possible s from 1 to 90 (maybe up to 910=90, but also consider that for larger n, digits could be 10, so max 90; but also n might be up to 1e9, but if n=1e9, then x could be near 1e9, sum digits up to 9+9+... Actually 1,000,000,000 sum digits =1, so sum digits could be small, but maximum sum digits for numbers up to 1e9 is for 999,999,999: sum=81? Wait 999,999,999 has 9 digits? Actually 999,999,999 is 9 digits, sum=81. But 1e9 is 10 digits? 1,000,000,000 is 10 digits, but its sum is 1. The maximum sum for numbers <= 1e9: Since 1e9 = 1000000000 (10 digits) but numbers less than 1e9 have at most 9 digits? Actually 1e9 = 1,000,000,000, which is the smallest 10-digit number. The constraint n <= 1e9, so n can be exactly 1e9. x must be positive integer such that x + sum(x) = n. x can be up to n-1 (since sum >= 1). So x <= 999,999,999 maybe? Because if n=1e9, x max = 999,999,999? Actually if n=1,000,000,000, then x can be 999,999,999? Check: 999,999,999 + 81 = 1,000,000,080 > n. So x must be less. The maximum sum digits for x is at most 9 * number_of_digits(x). The number of digits of x could be up to 9 or 10? Since x <= n <= 1e9, and x can be n itself? No, x positive, sum >=1, so x <= n-1. So x <= 999,999,999 if n=1e9. So max 9 digits. So max sum digits = 99 = 81. However, if n is smaller, number of digits less. But also if n is, say, 100, x could be 2-digit, sum digits up to 18. So we can bound s by, say, 910 = 90 to be safe (since maybe x can have 10 digits if n > 1e9? But n <= 1e9, so x <= 1e9, but x could be exactly 1e9? 1e9 + 1 = 1,000,000,001 > n because n <= 1e9, so x cannot be 1e9 because sum digits =1 => total = 1e9+1 > n. So x max = 999,999,999, which is 9 digits. So max sum digits = 81. So we can iterate s from 1 to 81 maybe, but to be safe, iterate s from 1 to 910 = 90. Also note that s could be 0? For x positive, sum digits positive? x>0 => at least one digit >=1, so sum >=1. So s from 1 to 90 is fine. Also s could be up to maybe 9*9=81. But to be safe for other limits? n up to 1e9, x can be as high as 999,999,999, sum digits 81. So s <= 81. So we can iterate s = 1 to 81? But what about n=1? Then x must be positive? n=1: x + sum(x) = 1 => x must be 0? But x positive? The problem says "certain positive integer x". So x must be positive. For n=1, there is no solution because min x=1 => 1+1=2 >1. So ok. So we iterate s from 1 to maybe 90.
We can also consider s=0? Not needed. So algorithm: For each s from 1 to maybe 81 (or 90), compute candidate x = n - s. If x <= 0, continue. Then compute sum of digits of x. If sum(x) == s, then x is a solution. But careful: s is sum_of_digits(x) for candidate x. So we check. Since s is at most maybe 90, we only check up to 90 candidates. That's efficient.
But is it sufficient? Could there be x with sum of digits > 90? As argued, no. But let's double-check: n could be up to 1e9, x <= n-1. The maximum possible x is 999,999,999 (9 digits) sum=81. If n=1e9, x max is 999,999,999 as argued? Actually x could be 999,999,999? Check: 999,999,999 +81 = 1,000,000,080 > 1e9, so not valid. So x will be less than that, but still up to maybe 999,999,999 -? But anyway sum digits remains <=81. So s <=81. So checking up to 81 is sufficient. But to be safe, we can check up to 90 (or maybe 9*10=90) because sometimes x could be 1e9? Actually if n is 1e9, x cannot be 1e9 because sum digits=1 yields 1e9+1 > n. So not. So indeed s <= 81.
However, there is nuance: Could there be x with more than 9 digits? If n=1e9, x max = 1e9 - 1 = 999,999,999, which is 9 digits. So no. So max digits = 9. So sum max = 81. For n=999,999,999? Actually n <= 1e9, but n could be 999,999,999 as well. x could be up to 999,999,999 - 1 = 999,999,998, still 9 digits. So same.
Thus algorithm: iterate s from 1 to 81 (or 90). Compute x = n - s; if x <= 0 break? Actually if n-s <=0, we can break early if s > n? But n can be small, so we can stop when s >= n? Because x must be positive. So for s >= n, x <= 0; skip. So we can set upper bound for s as min(81, n-1). But it's okay.
But careful: Is there any possibility that x is larger than n? No, because sum of digits positive => x < n. So x = n - s with s>0.
Now, one more nuance: Could there be x such that sum(x) is larger than 81? Suppose n = 1,000,000,000 and x = 999,999,990? sum digits = 98 + 9? Actually 999,999,990 sum = 97 + 9? Let's compute: 9+9+9+9+9+9+9+9+0 = 72? Actually eight 9's and a 0: 8*9 =72. So still <=81. So yes.
Thus we can just iterate s from 1 to 90 maybe, but we need to be sure that we don't miss any solutions where sum digits might be 0? But x positive so sum>0.
So solution is simple: generate candidates x = n - s for s from 1 to 90 (or up to maybe 81). Check if x > 0 and sum_of_digits(x) == s. Collect.
But wait: There is a subtlety: The relationship is x + sum(x) = n. So if we iterate over s (possible sum), we consider x = n - s. But s is the sum of digits of that x. Since s is bounded, we only check a few candidates. However, what about the case where multiple x could have the same s? That's fine; each candidate is uniquely determined by s. Actually for each possible s, we get at most one x that could satisfy the equation: if there is an x with sum s and x = n - s, then that x is uniquely determined. So we have at most ~90 candidates.
Thus the solution is O(log n) per candidate, overall trivial.
But is it always true that s is bounded by 90? Let's double-check: n could be up to 10^9, but what about x with more digits? Could x be 1,000,000,000? That's 10 digits. But x cannot be >= 1e9 if n = 1e9? Actually if n=1e9, x must be < n because sum positive, so x <= 999,999,999. So indeed max 9 digits. However, what if n is something like 2,000,000,000? But n is <= 1e9, so not.
But wait: The input constraint is n ≤ 10^9. So maximum n is 1,000,000,000. Therefore, as argued, x max is 999,999,999 (since x < n and n <= 1e9). So indeed x has at most 9 digits. Sum digits <= 81.
But consider n = 10^9 exactly: Then x can be 999,999,999? Let's test: 999,999,999 sum=81, total=1,000,000,080 > n. So not valid. But there could be x = 999,999,998 sum=80, total=1,000,000,078 > n? Actually 999,999,998 + 80 = 1,000,000,078 > 1e9? 1e9 = 1,000,000,000, so 1,000,000,078 > 1,000,000,000, so too high. So maybe x must be less, but still x could be up to 999,999,999? It's not a valid solution, but it's a candidate when we iterate s. For s=81, x = n-81 = 999,999,919? Actually 1,000,000,000 - 81 = 999,999,919. Its sum? Let's compute: 9+9+9+9+9+9+9+1+9? Actually 999,999,919 sum = 9+9+9+9+9+9+9+1+9 = 73? Wait 9+9+9+9+9+9+9 = 63, plus 1+9 =10 => total 73. So not 81. So no solution.
Thus upper bound of s = 81 is safe.
Edge cases: n small, e.g., n=1: s from 1 to maybe 1? x=0 invalid, so no.
n=2: s=1 => x=1, sum(1)=1 => 1+1=2 => solution. So x=1.
n=3: s=1 -> x=2 sum=2 not 1; s=2 -> x=1 sum=1 not2; no solution? Check manually: 1+1=2;2+2=4;3+3=6. So none.
n=4: s=2 -> x=2 sum=2 => 2+2=4 yes. So x=2.
But we must be careful: The condition is x + sum(x) = n. So for n=4, x=2 works. Also x= ? 1+1=2, no. So ok.
Our algorithm would find: s from 1 to 81, for n=4, s=2 gives x=2, sum(2)=2 => yes. So works.
Now verify with sample: n=21: s from 1..81. For s=6? Actually 15 sum=6 => x = n - 6 = 15, sum=6 => yes. Also s=15? No. So will find.
But also possible s=... Might find more? Let's check: s=18 => x=3, sum=3 not 18. So only one.
Second sample n=20: Check s from 1..81: s=... For s=5 => x=15 sum=6 not 5; s=6 => x=14 sum=5 not6; s=4 => x=16 sum=7 not4; etc. So none.
Thus algorithm works.
But wait: There's also possibility that x could be something like 0? The problem says positive integer x. So ignore 0.
Now, consider possible multiple solutions? For example, n=10? Let's test: 5+5=10? 5+5=10 => sum(5)=5 => 5+5=10 works. Also 8+8=16? no. 9+9=18. So only x=5? Also 4+4=8, no. 1+1=2. So unique? There is also x=10? 10+1=11. Not. So maybe more than one. Actually consider n=101? Might be multiple? Let's try n=2: only x=1. n=3: none. n=4: x=2. n=5: check: 1+1=2;2+2=4;3+3=6;4+4=8;5+5=10; so none? Actually x=5? 5+5=10 no. So none? n=5: s=1->x=4 sum=4 no; s=2->x=3 sum=3 no; s=3->x=2 sum=2 no; s=4->x=1 sum=1 no. So none. n=6: 5+5=10 no; 3+3=6? 3+3=6 yes, x=3. n=7? 1+1=2;2+2=4;3+3=6;4+4=8;5+5=10;6+6=12;7+7=14; none? Actually maybe x=5? 5+5=10 no. So none. n=8: 7+7=14;6+6=12;5+5=10;4+4=8 => x=4. n=9: 8+8=16;7+7=14;6+6=12;5+5=10;4+4=8;3+3=6;2+2=4;1+1=2; none? Actually 9+9=18; so none. n=10: x=5 works, x=8? 8+8=16 no; x=10? no. So just one.
There's a known property: For any n, there are at most about 100 solutions, and the above method works.
Now we must be careful about upper bound for s. Could s be more than 81 if x has more than 9 digits? But x cannot be larger than n, and n ≤ 1e9, so x ≤ 1e9, but x could be exactly 1e9? That's 10 digits (1,000,000,000) but sum =1. Actually x can be 1e9 if n = 1e9+1? But n ≤ 1e9, so n cannot be 1e9+1. So x cannot be 1e9 because x + sum(x) > n (since sum(x) at least 1). So x is at most n-1 ≤ 999,999,999. So max 9 digits. So s ≤ 81.
But to be extra safe, since n could be 1e9, x max 999,999,999, sum digits max 81. So iterate s from 1 to 81. However, some solutions could have sum digits less than 1? No. So that's fine.
But wait: What if n is something like 1000000000? The max possible sum of digits for numbers less than 1e9 is 81. So s max = 81. But we might also consider s=0? No.
Thus algorithm:
Read n.
Initialize result list.
For s from 1 to 81 (or maybe up to 90 to be safe) do:
x = n - s
if x <= 0: continue (or break if s >= n? Actually if s > n, x negative, but we could break because as s increases further, x becomes more negative. But for n small, s up to 81 may exceed n; we can skip. But maybe we can limit s up to n-1? Actually for n small, say n=5, s can be at most 4? Because x positive => n - s >= 1 => s <= n-1. So we can bound s <= min(81, n-1). This would be efficient.
if x > 0:
compute sum_digits(x)
if sum_digits(x) == s: add x to list.
After loop, sort the list (they should already be in increasing order if we iterate s increasing because x = n - s decreases as s increases, so the x we find will be in descending order? Let's see: As s increases from 1 upwards, x = n - s decreases. So we might first get large x for small s, then smaller x. To output in ascending order, we can collect and then sort, or we can collect and later reverse if we iterate s from 1 to max, because x decreases, so the order is descending. So after collection, we can sort ascending. Since number of solutions is small (<= 81) sorting is trivial.
Alternatively, iterate s from max down to 1 to get ascending x? For s decreasing, x = n - s increases, so ascending x. But we need to output in ascending order. However, we need to output all solutions, but number is small so sorting is fine.
We must also be careful about multiple solutions: Are there cases where the same x could be obtained from two different s? No, because s is determined by x. So each valid x will be found exactly once.
Now, also check for x = n? Not possible because sum positive.
Now, test with some edge cases: n=1 -> s from 1 to min(81,0) actually n-1 =0, so no loop. Output 0.
n=2 -> s from 1 to min(81,1) =1. s=1, x=1, sum=1 => yes. So output 1, then 1. Good.
n=10^9 -> s from 1 to min(81, 999999999) =81. Loop 81 times. Check each.
Now, can there be a case where s > 81 but still possible? For n=10^9, x could be 999,999,999? sum=81, but total > n, so not valid. For a valid x, x = n - s. Since s > 81, then x < n - 81 <= 999,999,919? Actually n=1e9 => x <= 999,999,919 for s=81? Actually if s > 81, x < 999,999,919, but sum of digits of such x? Could it be large? The maximum sum of digits for numbers less than 999,999,919 is still at most 81? Actually maximum sum of digits for any number less than 1e9 is 81 (for 999,999,999). But numbers less than that also have sum <= 81. So any x < 1e9 has sum digits <= 81. So if s > 81, then sum(x) <=81 < s, so cannot equal s. So no solution for s > 81. So safe.
Thus algorithm correct.
But we must also consider n up to 1e9, but could be, say, n=1000000000 (10^9). The loop is 81 iterations, fine.
Now, what about n=0? Not allowed because n >=1.
Implementation details:
- read n as integer.
- max_s = min(81, n-1) (since s must be <= n-1 to have x>=1).
- For s in range(1, max_s+1):
x = n - s
if x <= 0: continue (but we have max_s ensures x>=1)
compute sum_digits = sum(int(d) for d in str(x)) (but careful, conversion to string is fine for up to 1e9).
Alternatively compute by loop.
if sum_digits == s: add x to list.
- After loop, sort list (if needed). Since we iterate s increasing, x decreasing, we can either collect and then sort ascending, or we can collect and then reverse. But note that if we iterate s from 1 to max_s, the x's we find will be from largest to smallest. For example, n=21, s=6 gives x=15, s=... others might be smaller? If there were multiple, like n= ? Actually can there be multiple? Possibly yes: For example, n= ? Let's check n=100? Try to find solutions: We'll brute small? But multiple solutions can exist? Let's test: Suppose n=199999? Not sure. Typically, for each n, there are not many solutions. But there could be multiple. For example, n= 100000? Could there be two? Possibly, but we can handle.
But note: Since x = n - s, and s increases, x decreases strictly. So if we find solutions for s1 < s2, then x1 = n - s1 > n - s2 = x2. So if we find solutions in order of increasing s, we get decreasing x. So to output in ascending order, we need to reverse the list of found x's.
Thus we can either collect and then sort ascending, which is O(k log k), trivial; or simply reverse at end.
We must also output the count k and then values each on separate line.
Time complexity: O(max_s * log n) = constant.
Now, verify with some additional test cases:
- n=1 -> no solution. Output 0.
- n=2 -> output 1, then 1.
- n=3 -> none. Output 0.
- n=4 -> output 1, 2.
- n=5 -> none.
- n=6 -> 3.
- n=7 -> none.
- n=8 -> 4.
- n=9 -> none.
- n=10 -> 5? 5+5=10 yes. Also maybe ? Let's test x=8: 8+8=16 no. So only 5.
- n=11? s=... Check x=10? 10+1=11 => x=10 works? 10 + sum(10)=10+1=11 => Yes! So n=11 has x=10. Also any other? x= ? 9+9=18 no. So one.
- n=12? x= ? 11+2=13 no. 10+1=11, 12+3=15, 9+9=18, 6+6=12? 6+6=12 yes! So x=6 works. Also maybe? x= ? 5+5=10, 7+7=14, 8+8=16, 11+2=13. So only 6? Also x= ? 0? no. So 6. So multiple? n=12: 6 and maybe? 10+1=11, 9+9=18, 8+8=16, 7+7=14, 5+5=10, 4+4=8, 3+3=6, 2+2=4, 1+1=2. Only 6. So one solution.
But n=11 had x=10, which sum=1, s=1, x=10; s=1 gave x=10? For n=11, s=1 -> x=10, sum=1 => works. Also maybe s=10 -> x=1, sum=1 not 10; so only one.
Now, is there any n with two solutions? Let's try to find. We need x1 + sum(x1) = n and x2 + sum(x2) = n with x1 != x2. That means n - x1 = sum(x1), n - x2 = sum(x2). Since sum(x) is relatively small, x1 and x2 are both close to n. For two different x's, they must have different sums. Suppose x1 = n - s1, x2 = n - s2 with s1 != s2. For both to be valid, we need sum(x1)=s1 and sum(x2)=s2. Since x1 and x2 are different, s1 and s2 are different. Typically not many. But can there be? For example, n=101? Let's brute mental: For s=... maybe x=100? sum=1 => total=101 => works. Also x=? 91? sum=10 => total=101? 91+10=101, yes! So x=91 works (9+1=10). So n=101 has solutions x=100 and x=91. Also maybe x= ? 99? 99+18=117 no. So two solutions. Let's verify: 100 + 1 = 101; 91 + 9+1 = 91+10=101. Yes. So k=2. So ascending order: 91, 100.
Our algorithm: max_s = min(81, n-1=100) -> 81. Iterate s from 1 to 81. For s=1: x=100, sum=1 -> yes. For s=10: x=91, sum=9+1=10 -> yes. Others? s= ? maybe 2: x=99 sum=18 no; etc. So it will find both. The order of found: s=1 gives x=100; s=10 gives x=91; so collected in list as [100,91] if we append in order of s. After sorting ascending: [91,100] correct.
Thus works.
Now, consider n=1000000000? Let's see if there are any solutions. Could be many? We'll compute later.
Also check n= 0? not allowed.
Now, ensure we include s up to 81 inclusive.
But also consider n=81? Then x could be? s from 1..80? Actually max_s = min(81, 80) = 80. But s=81 cannot because n-81=0 not positive. So fine.
Now, one nuance: The sum of digits of x for x up to 1e9 can be computed with while loop or string.
Edge: x could be large, but we treat as integer.
Now, implementation in Python:
def sum_digits(num):
s = 0
while num:
s += num % 10
num //= 10
return s
Then:
def solve():
import sys
n = int(sys.stdin.readline().strip())
results = []
max_s = min(81, n-1)
for s in range(1, max_s+1):
x = n - s
if x <= 0: continue
if sum_digits(x) == s:
results.append(x)
results.sort()
print(len(results))
for x in results:
print(x)
We need to be careful about the case when n is 1: n-1=0, max_s=0, loop range(1,1) empty, results empty, print 0.
Works.
Now, can max_s be less than 81? Yes, for small n.
Now, verify that for n=101, we got two solutions, which matches expectation.
Now, test n=1000? Possibly multiple solutions.
Now, is there any scenario where s > 81 could be valid? Suppose n is, say, 999999999 (max 9-digit). x could be 999999999? That's n? No, x must be less than n because sum positive, unless n is such that x = n and sum(x)=0? But x positive so sum>0. So x < n. So x <= 999,999,998. That's still 9 digits, sum <= 81 (since max sum for 9-digit number is 99=81, but 999,999,998 sum=98+8? Actually 9+9+9+9+9+9+9+9+8 = 80? Wait compute: eight 9's and one 8: 8*9=72, +8=80). So sum <=81. So s <=81. For n less than 1e9, even smaller. So fine.
But what about n being something like 10^9 - 1 = 999,999,999? Then x max = n-1 = 999,999,998, sum max=80. So s <=80. So we are safe.
Thus bound 81 is enough.
But let's be thorough: The maximum possible sum of digits for any number less than 10^9 is 81 (since 999,999,999 is less than 10^9? Actually 999,999,999 is less than 1,000,000,000 = 10^9, so it is allowed as x if n > 999,999,999? But n max 10^9, and x must be less than n, so if n=10^9, x can be 999,999,999, but we saw that is not a solution because sum=81 => total 1,000,000,080 > 10^9. But that doesn't matter; the sum 81 is a possible s for some x, but x would be n - s = 10^9 - 81 = 999,999,919, which has sum less than 81 typically. So s=81 could be a candidate, but the candidate x = n - 81 is less than 999,999,999. That x's sum digits could be up to maybe 81? Let's see: The maximum sum of digits for any number less than 10^9 is 81, but the number n - s with s=81 is 10^9 - 81 = 999,999,919, sum digits = 9+9+9+9+9+9+9+1+9 = 73? Actually compute: 999,999,919 digits: 9,9,9,9,9,9,9,1,9? Wait 999,999,919 has digits: hundred millions? Let's write: 999,999,919 = 910^8? Actually it's 999,999,919. It has 9 digits: hundreds millions? The digits: 9 (100 million), 9 (10 million), 9 (1 million), 9 (100 thousand), 9 (10 thousand), 9 (1 thousand), 9 (hundreds), 1 (tens), 9 (ones). So sum = 9+9+9+9+9+9+9+1+9 = 70? Let's sum: 97=63, plus 1+9=10, total 73. Actually we have eight 9's? Let's count: 999,999,919 has 9 digits: positions: hundred millions:9, ten millions:9, millions:9, hundred thousands:9, ten thousands:9, thousands:9, hundreds:9, tens:1, units:9. That's seven 9's? Wait: hundred millions:9, ten millions:9, millions:9, hundred thousands:9, ten thousands:9, thousands:9, hundreds:9 => that's seven 9's? Actually from hundred millions to hundreds inclusive: 7 positions? Let's list: 9|9|9|,|9|9|9|,9|1|9? I'm confusing. Better: Write number: 999,999,919 -> groups: 999 (millions?), Actually 999,999,919 in digits: 9 9 9 9 9 9 9 1 9? That's 8 nines? Let's compute using python mental: 999,999,919 = 999,999,000 + 919. Actually easier: It's 1e9 - 81 = 1,000,000,000 - 81 = 999,999,919. So digits: 999,999,919. Count the 9's: The first seven digits are 9? Actually 999,999,919: break as 999, 999, 919. So first three: 9,9,9; next three:9,9,9; last three:9,1,9. That's 3+3+2 = 8 nines and a one. So eight 9's and one 1. Sum = 89 + 1 = 72 + 1 = 73? Wait 89=72, plus 1 =73. But we have nine digits, eight 9's and one 1? Actually last three: 9,1,9 gives two 9's, so total nines: 3+3+2 = 8, yes. So sum = 8*9 + 1 = 73. So not 81.
But could there be any x such that sum(x)=81? That would require x to have all 9 digits (i.e., 999,999,999). But that number is 1e9 - 1. For s=81, x = n - 81. For n large enough, x could be 999,999,999 only if n - 81 = 999,999,999 => n = 1,000,000,080, which exceeds 1e9. So no candidate x can have sum 81 because if x is 999,999,999, sum=81, then n = x + 81 = 1,000,000,080 > 1e9, not allowed. So for n <= 1e9, s=81 may never be a valid solution because x = n - 81 would be at most 1e9 - 81 = 999,999,919, which cannot have sum 81. But that's okay; we still check s=81, it will just yield false. So bound 81 is safe.
But wait, could there be x with sum 80? For x to have sum 80, possible numbers like 999,999,998 sum=80, or others. Then n = x + 80. For x = 999,999,998, n = 1,000,000,078 > 1e9, so not allowed. So the maximum possible s for a valid solution might be less than 81 for n close to 1e9. But we don't need to optimize bound; using 81 is fine.
Thus solution is simple.
But is there any chance that x has more than 9 digits if n is exactly 1e9? As argued, x < n, so x <= 999,999,999, which is 9 digits. So sum <= 81.
Thus the solution is correct.
Now, also we need to consider x positive integer. Our loop ensures x > 0.
Now, check the output format: first line k, then k lines each with x. If k=0, only output 0.
Implementation details: In Python, reading input and writing output.
Now, let's test with some random values to ensure no off-by-one:
Case n=10^9: Let's compute possible solutions.
We could try to see if any exist.
We can quickly reason: x = n - s, where s is sum of digits of x. For large n, s is small relative to n, so x is roughly n. So sum of digits of n - s is roughly sum of digits of n, maybe. Let's try compute with a small script? But manual? Possibly there are solutions like x = 999,999,990? sum=9*8+0=72? 999,999,990+72=1,000,000,062 > n. So not. Might be none. But we trust algorithm.
Now, confirm: Our loop from 1 to min(81, n-1). For n=10^9, n-1 = 999,999,999 >81, so loop 81 times. So we check 81 numbers near n. That is fine.
Now, test for n=1000: Let's manually find solutions. We'll compute quickly: s from 1 to min(81, 999) = 81. Check few: s=1 -> x=999, sum(999)=27 not 1; s=2 -> x=998 sum=26; s=... likely none? But could be: x= 994? n=1000, s=6 => x=994, sum=9+9+4=22; no. Actually maybe x= 974? 974+20=994? No. Let's find systematically: x = 1000 - s, sum digits of x must equal s. Since x is about 1000, its sum digits is at least 1 (for 1000 itself? x cannot be 1000 because sum digits=1, then total=1001). So x ~ 1000. For x= 991, sum=19, total=1010 >1000. So maybe no solution? Let's check known property: For any n, there are usually few solutions. But our algorithm will determine.
Now, test n=19: s=1 -> x=18 sum=9 no; s=2 -> x=17 sum=8 no; s=3 -> x=16 sum=7 no; s=4 -> x=15 sum=6 no; s=5 -> x=14 sum=5? 1+4=5, yes! 14+5=19. So x=14. Also s= ? s=6 -> x=13 sum=4 no; s=7 -> x=12 sum=3 no; s=8 -> x=11 sum=2 no; s=9 -> x=10 sum=1 no; So only one solution. So n=19 gives 14.
Now, test n=20: as sample none.
n=21: as sample 15.
Now, test n=101: we found 91 and 100.
Now, test n=2: x=1.
Now, think about edge where n=81? Let's compute: s from 1 to 80. Try s=9 -> x=72 sum=9? 7+2=9, yes! 72+9=81. So x=72. Also s=15? x=66 sum=12 no. s=18? x=63 sum=9 no. So at least one. Also s= ? x=80? s=1 -> x=80 sum=8 no. So one.
Now, n=73: s= ? x= ? 68+14=82? Not. Could be multiple? Let's not worry.
Now, consider whether we need to handle large n and sum digits computation correctly. Use while loop or convert to string.
Now, finalize solution.
But before finalizing, we need to ensure that the bound 81 is indeed sufficient for all n <= 10^9. However, note that n could be as high as 10^9, but also consider that x could be, for n slightly less than 10^9, like n=999,999,999. Then x could be up to 999,999,998. That's still 9-digit number. Sum digits max is 81 (but actually 999,999,998 sum=80). So still <=81. So bound 81 works. For n=100,000,000 (8-digit), x up to 99,999,999 (8 digits) sum max = 9*8 = 72. So still <=81. So 81 is an upper bound for any n <= 10^9.
But is there any scenario where sum_of_digits(x) could be larger than 81? If x had 10 digits, it would be at least 1,000,000,000, but that is equal to 10^9. Since n <= 10^9, x could be exactly 1,000,000,000 only if n = 1,000,000,001 (since sum=1), but that's >10^9, so not allowed. So no 10-digit x. So max sum is 81.
Thus okay.
Now, a more careful bound: The maximum sum of digits for numbers less than 10^9 is for 999,999,999 (9 digits all 9) which is 81. So yes.
Thus algorithm is correct.
Now, we must output in ascending order. So after collecting, we sort.
Now, let's also consider potential pitfalls: If n is small (like n=1), max_s = min(81, 0) = 0, loop runs from 1 to 0? That range will be empty. So no issue.
Now, implement.
But wait, there's a nuance: The problem statement says: "a certain positive integer x was given. The task was to add x to the sum of the digits of the number x written in decimal numeral system." So the equation is n = x + sum_of_digits(x). We are to find all positive integer x satisfying that. So we must output all such x. The above method yields exactly that.
Now, check if there could be solutions where sum_of_digits(x) is greater than 81 due to leading zeros? No, decimal representation without leading zeros.
Thus solution done.
But wait, there is also possibility of multiple-digit numbers with leading zeros? Typically not; sum of digits doesn't count leading zeros.
Thus fine.
Now, we need to output k and then each x.
Now, confirm with sample:
Input: 21
Our loop: max_s = min(81,20)=20. s from 1 to 20:
s=6 -> x=15 sum=1+5=6 => yes. s=15? x=6 sum=6 no. So only one. Output 1 and 15. Good.
Second sample: 20, max_s=min(81,19)=19. Check s=1..19: none. Output 0. Good.
Now, test with n=101: max_s=min(81,100)=81. s=1 -> x=100 sum=1 => yes. s=10 -> x=91 sum=10 => yes. s=... others not. Output 2, then 91, 100 after sort. Good.
Now, test with n=1: output 0.
Now, test with n=2: output 1 and 1.
Now, test with n=1000000000: Let's see if any solution? We'll trust algorithm.
Now, consider that we are to "search for arbitrary values of the number n for all suitable values of x". So correct.
Now, we might want to also consider that the number of solutions can be zero, then output 0.
Now, we need to ensure we don't miss x where sum of digits is zero? x=0 is not positive, so no.
Thus solution is ready.
But is there any scenario where sum_of_digits(x) could be > 81? Suppose n=10^9, x=999,999,990, sum=9*8+0=72. So no.
Thus the solution is correct and efficient.
Now, we can produce Python code.
But before finalizing, let's think about potential pitfalls: The bound of s up to 81 might be insufficient if n > 10^9? But constraint says n ≤ 10^9. So fine.
However, we should note that for n up to 10^9, the maximum possible sum of digits for x could be 9*9 = 81. But what if n = 10^9 exactly? Then x can be at most 10^9 - 1 = 999,999,999, sum max 81. So s <= 81.
But wait, could x be 0? No.
Thus done.
Now, alternative approach: Could also generate x by iterating over possible digit sums? Already done.
One more nuance: Our loop uses s from 1 to min(81, n-1). But for n less than 81, max_s = n-1, which is fine. For n=2, max_s=1, we check s=1 only.
Now, verify with n=81: n-1=80, max_s=80. s=9 gave x=72. Also s= ? maybe s=18? x=63 sum=9 no. So only one? But also s= ? x=80? s=1 -> x=80 sum=8 no. So 72 only.
Now, also check n=90: s from 1..89? But max_s = min(81, 89) =81. Actually n-1=89, so we check s=1..81. s=9 -> x=81 sum=9 no; s=18? x=72 sum=9 no; s= ? Let's compute x=81? sum=9; 81+9=90, so x=81 works: 81 + 9 = 90. So s=9? Actually x=81 => sum=9 => n = 81+9=90, so s should be 9. Check: s=9 => x=90-9=81, sum(81)=9, yes. So we would catch that. So n=90 yields x=81. Also maybe x= ? 85+13=98 no. So one.
Now, test n=100: s=1-> x=99 sum=18 no; s=10-> x=90 sum=9 no; s= ? maybe x=91? 91+10=101; no. x=89? 89+17=106. x=86? 86+14=100? 86+14=100? 8+6=14, so 86+14=100, yes! So x=86 works. Check: s=14? x=100-14=86, sum=8+6=14 => yes. So s=14 found. Also x= ? 80+8=88, no. x= ? 92+11=103. So one. So output 1, 86.
Now, test n=101: we already have two.
Now, test n=2: output 1, 1.
Now, test n=11: output 1,10.
Now, test n=12: output 1,6? Actually we found 6. Check: 6+6=12, yes. So x=6.
Now, test n=13: possible? 7+7=14, 8+8=16, 9+9=18, 4+4=8, 5+5=10, 11+2=13? 11+2=13, yes! So x=11 works. Also maybe 10+1=11, no. So x=11. Also maybe 12+3=15, no. So one. So n=13: s=2? Actually 11 sum=2 => x=11, s=2, n=13 => x= n - s = 11. So s=2. So output 1,11.
Now, test n=14: x= ? 13+4=17, 12+3=15, 11+2=13, 10+1=11, 7+7=14? Actually 7+7=14, yes x=7 works. Also maybe 9+9=18, no. So x=7. So n=14: s=7? Wait 7+7=14, so sum=7, s=7, x=7. So s=7: x=14-7=7, sum=7 -> yes. So output 1,7.
Now, test n=15: x= ? 9+9=18, 8+8=16, 7+7=14, 6+6=12, 5+5=10, 4+4=8, 3+3=6, 2+2=4, 1+1=2. Maybe 12+3=15? 12+3=15, yes x=12 works (1+2=3). Also maybe 9+9=18 no. So x=12. Also maybe 14+5=19 no. So one. So n=15: s=3? x=12, sum=3. So works.
Now, test n=16: 8+8=16? 8+8=16 yes, x=8. Also 15+6=21 no. So one.
Now, test n=17: maybe 16+7=23 no. 15+6=21 no. 14+5=19 no. 13+4=17? 13+4=17, yes x=13. So one.
Now, test n=18: 17+8=25, 16+7=23, 15+6=21, 14+5=19, 13+4=17, 12+3=15, 11+2=13, 10+1=11, 9+9=18? 9+9=18, yes x=9. Also maybe 18+9=27, no. So one.
Now, test n=19: we had 14+5=19, so x=14.
Now, test n=20: none.
Now, we see pattern.
Now, ensure that we output all.
Thus solution is correct.
Now, implement.
But wait: Is there any case where sum_of_digits(x) might exceed 81? As argued, no.
However, what about n= 10^9? We might want to set max_s to 81, but also we should consider that for small n, s should not exceed n-1. So we set max_s = min(81, n-1). But what about n less than 1? Not possible.
Now, consider n=1000000000: n-1 = 999999999, min(81, 999999999)=81. So loop 81 times.
Now, compute sum_digits efficiently: use while x>0: s+=x%10; x//=10. Or use str conversion: sum(map(int,str(x))). Both fine.
Now, we need to print k then each x.
Edge case: There might be many solutions? Possibly up to 81? Actually could there be more than 81? Since we only check up to 81 values, maximum solutions found is at most 81, because each solution corresponds to a distinct s (since s = sum(x) uniquely determined by x). But could two different s yield same x? Impossible because x = n - s, so if s differs, x differs. So at most one per s, so at most 81. So output size small.
Now, the problem is trivial.
But wait: Could there be any solution with s > 81? As argued no. But just to be extra sure, let's test if n= 10^9 (1,000,000,000) and s= 90? Then x= 999,999,910. Its sum digits: 9+9+9+9+9+9+9+1+0 = 64? Actually compute: 999,999,910 = digits: 9,9,9,9,9,9,9,1,0? That's eight 9's? Let's see: 1,000,000,000 - 90 = 999,999,910. Digits: 999,999,910 -> groups: 999,999,910: first three: 9,9,9; second three: 9,9,9; last three: 9,1,0? Actually 999,999,910 is 9 digits? It's 999,999,910 which is 9 digits? Wait 1,000,000,000 is 10 digits? Actually 1,000,000,000 has 10 digits. 999,999,910 has 9 digits? Let's count: 999,999,910: it's 9 digits? The number 999,999,910 is 9 digits? 999,999,910 is actually 9 digits: 9 hundred millions? The maximum 9-digit number is 999,999,999, which is 9 digits. 999,999,910 is also 9 digits. So its digit sum is 9+9+9+9+9+9+9+1+0 = Let's sum the digits: The number written as 999,999,910: hundred millions:9, ten millions:9, millions:9, hundred thousands:9, ten thousands:9, thousands:9, hundreds:9, tens:1, units:0. That's seven 9's? Actually from hundred millions to hundreds: that's 7 positions? Let's count: positions: hundred millions (10^8), ten millions (10^7), millions (10^6), hundred thousands (10^5), ten thousands (10^4), thousands (10^3), hundreds (10^2) => that's 7 digits. Then tens (10^1):1, units (10^0):0. So we have 7 nines? Wait, 999,999,910: The digits: 9 (100 million), 9 (10 million), 9 (million), 9 (100 thousand), 9 (10 thousand), 9 (thousand), 9 (hundred), 1 (ten), 0 (one). That is 7 nines? Actually it's 7? Let's count: The number 999,999,910 is 9 digits: the first 6? I'm mixing. Better to write: 999,999,910 = 999,999,900 + 10? Actually I'll compute digits by dividing: 999,999,910 // 100,000,000 = 9 (hundred millions). Remainder 99,999,910? This is messy. But we can just note that 999,999,910 has 9 digits, and its sum of digits is less than 81. Actually maximum sum for 9-digit numbers is 81, but this number is less than 999,999,999, so sum < 81. So s=90 would not match.
Thus s cannot exceed 81 for a valid solution because x would have at most 9 digits, sum <= 81.
But is there any x with 10 digits? As argued, x must be < n ≤ 1e9. The smallest 10-digit number is 1,000,000,000 (10^9). Since n ≤ 10^9, if n = 10^9, x can be at most 999,999,999, which is 9 digits. So x never has 10 digits. So sum max 81.
Thus fine.
Thus solution is correct.
Now, we can produce final answer.
But I recall that some solutions on Codeforces use brute force over possible sums, exactly this method.
One subtle nuance: What about n=10^9? Could there be a solution where x has less than 9 digits but sum of digits is larger than 81? No, sum of digits is at most 9 * number_of_digits. For numbers with fewer digits, max sum is even smaller.
So bound is safe.
Thus final code.
But before finalizing, check if we need to output "0" only with no extra lines. Yes.
Now, I will produce code with reading from stdin and writing to stdout.
Now, but wait: The problem statement says "In the first line print one integer k — number of different values of x satisfying the condition. In next k lines print these values in ascending order." So if k=0, we just print 0.
Now, confirm with some random tests using brute for small n to ensure algorithm catches all solutions. For n up to, say, 1000, we could brute all x and compare. But I trust.
Now, maybe there is another nuance: The equation is n = x + sum_of_digits(x). If we solve for x, x = n - sum_of_digits(x). Since sum_of_digits(x) is small, we can also search for x in the range [n-81, n]. That is, check x from max(1, n-81) to n-1. That might be simpler: just iterate x from max(1, n-81) to n-1, compute sum_of_digits(x) and check if x + sum(x) == n. This is also O(81 * log n). That avoids the min bound on s. Actually both are equivalent: checking candidate x directly in the range near n. Because if x is a solution, then sum(x) = n - x <= 81, so x >= n - 81. So x must be within [n-81, n-1] (since x < n). So we can just iterate x in that range, check condition. That is even simpler. Yes! Because from x + sum(x) = n, sum(x) >= 1, so x <= n-1, and sum(x) <= 81, so x >= n-81. So x is in [max(1, n-81), n-1]. That's at most 81 numbers. So we can simply iterate x in that range and test. That is more straightforward and avoids any worry about bound of s. Also we don't need to compute s from sum and compare; we just compute and check if x + sum(x) == n. So we can implement:
lower = max(1, n - 81) # or n-81? Actually if n-81 < 1, lower =1.
upper = n - 1
for x in range(lower, upper+1):
if x + sum_digits(x) == n:
add to results.
But note that if n is small, say n=2, lower = max(1, 2-81)=1, upper=1, so check x=1 -> 1+1=2 => works.
If n=1, lower = max(1, 1-81)=1, upper = 0? Actually n-1 =0, so range(1,0+1?) In Python, range(lower, upper+1) but if upper < lower, we need to handle. So better: for x in range(max(1, n-81), n): because x < n, so up to n-1 inclusive. In Python, range(start, stop) goes up to stop-1, so range(start, n) iterates x from start to n-1 inclusive. So if n-1 < start, then range is empty (since start >= n). So we need to ensure start < n. For n=1, start = max(1, 1-81)=1, n=1, range(1,1) empty. So works. So simpler: iterate x from max(1, n-81) to n-1 inclusive.
But is 81 the correct bound? We reasoned that sum(x) <= 81 because x <= 999,999,999. But could there be x with sum(x) > 81 if x is less than n but n is larger than 10^9? Not. But for n up to 10^9, x is at most 999,999,999, so max sum = 81. However, if n is small, the bound n-81 could be negative, but we take max with 1. So that works.
But wait: Is it possible that for some n, the solution x is less than n-81? Because sum(x) could be larger than 81? But we argued sum(x) cannot exceed 81. However, what if n is, say, 10^9? x could be as low as 1, but if sum(x) is at most 81, then x = n - sum(x) >= n-81. So indeed any solution x must satisfy x >= n-81. So yes, all solutions lie in [n-81, n-1]. So iterating that range is sufficient. This is a common approach for this problem (Codeforces 355A? Actually it's Codeforces 1060B? Not sure). It's a known problem "Little Elephant and Function"? Actually it's from Codeforces #514? The problem "Cifera"? Hmm. Anyway, it's classic.
Thus simpler: just iterate x from max(1, n-81) to n-1, check if x + digit_sum(x) == n, collect. That yields at most 81 candidates.
But note that for n up to 10^9, n-81 could be negative, but we handle max(1, n-81). Good.
Also, we need to ensure that we include x that might be exactly n? No, because x + sum(x) > n if sum(x)>=1, so x cannot be n.
Now, what about the possibility that sum(x) could be greater than 81 for x having more digits? As argued, not possible given n max. But just to be thorough: If n is 10^9, x could be 999,999,999? That's sum 81. So bound 81. However, what if n is, say, 999,999,999? Then x could be 999,999,918? sum digits maybe 9+9+9+9+9+9+9+1+8 = 72? not exceed 81. So bound works.
But what if n is, say, 100,000,000? Then x could be 99,999,999, sum = 98 = 72? Actually 99,999,999 is 8 digits? Wait 99,999,999 is 8 digits? No, it's 8-digit? 99,999,999 is actually 8 digits? The largest 8-digit is 99,999,999? That's 8 digits? 99,999,999 is 8 digits? Let's count: 99,999,999 has 8 digits? Actually 100,000,000 is 9 digits. 99,999,999 is 8 digits? Wait: 100,000,000 is 9 digits (1 followed by 8 zeros). 99,999,999 is 8 digits? The largest 8-digit number is 99,999,999? That is 8 digits? Let's check: 99,999,999 written out: it has 8 digits? Actually 10 million is 8 digits? 10,000,000 is 8 digits? Let's think: 1-digit numbers: 1-9. 2-digit: 10-99. 3-digit: 100-999. 4-digit: 1000-9999. 5-digit: 10000-99999. 6-digit: 100000-999999. 7-digit: 1,000,000-9,999,999. 8-digit: 10,000,000-99,999,999. 9-digit: 100,000,000-999,999,999. So 99,999,999 is 8 digits. So sum digits for 8-digit max is 89=72. So bound 81 still holds.
Thus iterating from n-81 to n-1 is safe.
But is 81 a strict bound? What about if n is, say, 100, and x = 91, sum=10, x=91, which is n-9? Actually 100-9=91, yes. So x is within n-9. So for n=100, lower = max(1, 100-81)=19, upper = 99. That includes 91, so good.
Now, what if n=1000? lower = max(1, 1000-81)=919, upper=999. So we check from 919 to 999. That's 81 numbers. In that range, possible solutions like x= 994? sum=22, 994+22=1016, not 1000. Maybe none? But our earlier check didn't find any. But if there was a solution, it would be there.
Now, what about n=101? lower = 20, upper=100. That includes both 91 and 100. Good.
Now, confirm with n=21: lower = max(1,21-81)=1, upper=20. So range 1 to 20. Includes x=15. Good.
n=20: range 1 to 19. No solution.
n=2: range 1 to 1. includes 1.
n=1: range(1,1) empty.
Thus the simpler method works.
Now, which is better? Both are similar. The first method uses s loop, second uses x loop. Since both are O(81), both fine. I'll use the x loop for simplicity: iterate x from max(1, n-81) to n-1, check condition.
But careful: Could there be a solution where x is less than n-81? As argued, no, because if x is a solution, then n - x = sum(x) ≤ 81, so x ≥ n-81. So yes.
Thus algorithm:
- read n.
- results = []
- for x in range(max(1, n-81), n):
if x + digit_sum(x) == n: results.append(x)
- print(len(results))
- for x in results: print(x)
But note: For n up to 10^9, n-81 could be negative, so max(1, n-81) ensures start at least 1.
Also note: if n is small, say n=5, range is from max(1, 5-81= -76) =1 to 4, so check x=1,2,3,4. That's fine.
Now, check if we need to consider x=0? No, because range starts at 1 if n-81 <=0, so we don't include 0.
Thus done.
But wait: Are there any solutions where x=0? The problem says positive integer x, so ignore.
Now, test with n=1000: start = max(1, 919) =919, end = 999, so check 81 numbers. Good.
Now, we need to ensure that we include x that may be n-81 exactly? Yes, inclusive of start. Since x = n - 81 is possible if sum(x)=81. But is it possible that sum(x)=81? As argued, x would need to be 999,999,999, but then n would be 1,000,000,080 > 10^9, so for n ≤ 10^9, x = n - 81 can't have sum 81. But we still check it; it will just not be a solution. So inclusive is fine.
Now, we also need to consider that if n is large, say n=10^9, start = n-81 = 999,999,919, end = 999,999,999. That's 81 numbers. But note: x can be up to n-1 = 999,999,999, so range includes that. Good.
Now, confirm that x = n-81 may be less than 1 for small n, but we handle.
Thus code is straightforward.
One more nuance: The range includes n-1, but if n-1 < start, then range is empty. For n=1, start = 1, range(1,1) empty, good.
Now, is the bound 81 always enough? Let's compute the maximum possible sum of digits for numbers less than n. But what if n is less than 10^9 but x could have more than 9 digits? No, because x < n ≤ 10^9. So x ≤ 10^9 - 1 = 999,999,999, which is 9 digits. So maximum sum is 9*9 = 81. So correct.
But wait: Could n be exactly 10^9? Then x can be 999,999,999 (n-1), which has sum 81. So s=81 possible if x=999,999,999? But then x+sum = 999,999,999+81 = 1,000,000,080 > n, so not a solution. However, it's still a candidate in range. So bound 81 includes that candidate.
Now, is there any possibility that sum(x) could be 81 for a solution? Only if x = 999,999,999 and n = 1,000,000,080, but n <= 10^9, so no. So s=81 will not yield solution, but we still check it. So inclusive bound is fine.
Thus the x loop method is correct.
Now, we must be careful: The range should include n-81 if n-81 >= 1. But if n-81 is less than 1, we start at 1. So the range may be less than 81 numbers for small n, but that's fine.
Now, implement.
But wait: Could there be solutions with x > n? No.
Now, we must output values in ascending order. The loop from start to n-1 goes from smaller x to larger x (since start <= n-1). So the results will be collected in ascending order automatically, because we iterate increasing x. So no need to sort. That's a benefit. So we can simply append as we find.
But careful: start = max(1, n-81). For n > 81, start = n-81, which is less than n-1, so iterating from start to n-1 yields increasing x. For n <= 81, start = 1, iterate from 1 to n-1, also increasing. So results will be in ascending order as we find them (since we check x in increasing order). Good. So we can avoid sorting.
But note: Could there be multiple solutions? They will be found in increasing order. So just output as is.
Thus final algorithm:
- n = int(input().strip())
- ans = []
- for x in range(max(1, n-81), n):
if x + digit_sum(x) == n:
ans.append(x)
- print(len(ans))
- for x in ans:
print(x)
Now, we need to implement digit_sum(x). Define function: sum(map(int, str(x))) or while loop. While loop might be faster and avoid string conversion, but both fine.
Edge cases: For large n, the loop runs at most 81 iterations.
Now, confirm with sample:
n=21: range(max(1,21-81)=1, to 20: check x=1..20. At x=15, 15+6=21 => add. Output 1,15.
n=20: range 1..19: none. Output 0.
n=101: range max(1,101-81=20) to 100: x from 20 to 100. Check x=91: 91+10=101 => add; x=100: 100+1=101 => add. So order: 91,100? Since we go from 20 to 100, we encounter 91 before 100? Actually 20..100 includes 91 then later 100. So we get [91,100] automatically. Ascending order. Good.
But wait: For n=101, start=20, so we check x=20,...,100. 91 < 100, so 91 will be added first, then 100 later, so output will be 91 then 100, which is ascending. So correct.
Now, for n=100, start=19, end=99. Solutions? Let's check x=86: 86+14=100 => add. So output 86.
For n=90, start=9, end=89. x=81 occurs at 81, which is >9 and <90. So we will find 81.
For n=19, start=1, end=18, we will find x=14? 14+5=19 => yes, at x=14. So output 14.
So all good.
Now, test n=1: range(1,1) empty, ans empty, print 0.
Now, test n=2: range(1,2) => x=1 only: 1+1=2 => ans=[1], print 1 and 1.
Now, test n=3: range(1,3) => x=1,2. Check: 1+1=2 !=3; 2+2=4 !=3; none. print 0.
Now, test n=1000000000: We'll compute quickly maybe there are solutions? Let's try to see if any: We'll check x from 999,999,919 to 999,999,999. We need x + digit_sum(x) = 1,000,000,000. So digit_sum(x) = 1,000,000,000 - x. Let d = 1e9 - x. Then d is between 1 and 81. So we need digit_sum(x) = d and x = 1e9 - d. So essentially we need sum_digits(1e9 - d) = d for d from 1 to 81. That's like checking s loop. We can quickly check a few: d=1 => x=999,999,999, sum=81 !=1; d=2 => x=999,999,998 sum=80 !=2; ... likely none. But we can let program find.
Thus algorithm works.
Now, potential pitfalls: The upper bound 81 is correct for n up to 10^9, but what about n up to 10^9, but x could be up to 10^9 -1, which has at most 9 digits, so sum digits max 81. However, consider n=10^9 - 1 = 999,999,999. Then x can be up to 999,999,998. The sum digits of 999,999,998 is 80. So still <=81. So bound okay.
Now, we should also consider that the maximum sum of digits for numbers less than n but n can be less than 10^9, so x could have fewer digits, but sum digits max is still <=81. So bound is safe.
Now, we must consider if n is very small, say n=81, then range is from max(1,81-81=0)=1 to 80. So we check x=1..80. That includes x