Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment.
Find all integer solutions x (0 < x < 109) of the equation:
x = b·s(x)a + c,
where a, b, c are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x.
The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: a, b, c. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem.
Input
The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000; - 10000 ≤ c ≤ 10000).
Output
Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 109.
Examples
Input
3 2 8
Output
3
10 2008 13726
Input
1 2 -18
Output
0
Input
2 2 -1
Output
4
1 31 337 967
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to find all integer solutions x with 0 < x < 10^9, for given a, b, c, satisfying:
x = b * (s(x))^a + c
where s(x) is sum of digits in decimal representation.
We are given constraints: a in [1,5], b in [1,10000], c in [-10000,10000]. Also x > 0 and x < 1e9 (1,000,000,000). So x up to 9-digit numbers.
We need to output the number of solutions and the solutions in increasing order.
This is a typical problem: iterate over possible digit sums? Since s(x) is sum of digits, maximum possible s(x) for x<1e9: maximum x is 999,999,999 => s=81. Actually 99=81? Wait 10^9 is 1,000,000,000 (exclusive) meaning max x = 999,999,999, sum digits = 99=81. So s(x) ∈ [1,81] because x>0 so s(x) >=1 (unless x=0 but x>0). Actually if x is say 10^9-1 = 999,999,999 sum=81. If x less, sum can be less. Minimum s for positive x is 1 (x=1,10,100, etc). So s(x) ∈ [1,81].
Thus we can iterate over all possible s values from 1 to 81. For each s, compute candidate x = b * s^a + c. Then check if this x satisfies: 0 < x < 10^9, and s(x) == s. Also note that x must be integer obviously. Since b, s, a, c are integers, candidate x is integer. So we can compute candidate and verify digit sum.
But careful: the equation is x = b * s(x)^a + c. So if we let t = s(x), then x = b*t^a + c. However, this might produce a candidate for each t, but we need to verify that t is indeed the digit sum of that candidate. Since t is small (max 81), this is feasible.
However, note that x can have s(x) different from t we guessed; but if the equation holds, then t must equal s(x). So enumerating t from 1 to 81 is correct: the solution x must correspond to some t = s(x), and we compute candidate x = b*t^a + c. So if candidate's digit sum equals t, and x in range, it's a solution. This yields at most 81 candidates, easy.
But is it always that t = s(x) <= 81? Since x < 1e9, maximum digits 9, max sum 81. Yes.
But wait: what about numbers with leading zeros? Not relevant.
Thus the solution is straightforward: iterate t=1..81, compute X = b * (t^a) + c. Check if X > 0 and X < 1e9, then compute digit sum s_X, if s_X == t, then it's a solution. Collect and sort (should be already sorted if we iterate t increasing but X might not be monotonic with t? Actually t increasing, t^a increases, so X = b * t^a + c is increasing if b>0. Since b >= 1, yes b positive. So as t increases, X increases. But c might be negative, but still monotonic because b * t^a is increasing. So the candidates will be in increasing order of t, and X also increasing. However, if some candidate is invalid, we still might need to output in increasing order. Since we collect valid X's in order of increasing t, they'll be increasing as well. But is it guaranteed that b*t^a + c is strictly increasing in t? Since b>0 and t^a increasing, yes. So sorted by t yields sorted X. Good.
But wait: t could be up to 81, but t^a may be large: For a=5, 81^5 = 81^2=6561, 81^4= (6561)^2 ≈ 43 million, times 81 gives about 3.5 billion. 81^5 = 81^5 = 3486784401? Let's calculate: 3^5=243, but 81=3^4, so 81^5 = (3^4)^5=3^20 = 3486784401. That's > 1e9? 3^20 = 3,486,784,401 > 1e9. So b81^5 + c could exceed 1e9 for b up to 10000, but we check bounds. However, t^a can exceed 1e9, but bt^a could overflow typical 32-bit integer if not careful. Use 64-bit (long long). For t up to 81 and a up to 5, t^a max ~ 3.5e9 fits in 64-bit (signed 64-bit). Multiply by b up to 10000 gives up to ~3.5e13, which fits in 64-bit (9e18). So we can use 64-bit for candidate X, but then we need to check if X < 1e9, so we can break early when bt^a + c > 1e9? Actually since b positive and t^a increasing, once bt^a + c > 1e9 (and c might be negative, but eventually it will exceed), we can stop early? But careful: c could be negative large, making X smaller; but as t increases, t^a increases, so b*t^a eventually dominates and becomes > 1e9 - c. For a=5, t^5 at t=81 is 3.5e9, times b=1 gives 3.5e9, plus c maybe negative but could still be >1e9? Actually 3.5e9 is >1e9, so X >1e9 likely, but we need to check for all t. However we can loop t from 1 to 81, it's only 81 iterations, so fine.
We should compute t^a properly using pow(t, a) but careful about floating point. Use integer power.
Check candidate x>0 and x<1000000000. Also need to consider that c can be negative, so b*t^a + c might be <=0. Then not solution.
Compute digit sum: function that sums digits of x (positive integer). Use modulo operations.
One more nuance: The problem statement says "Find all integer solutions x (0 < x < 10^9) of the equation: x = b·s(x)a + c" So it's x = b * (s(x))^a + c. Yes.
Edge cases: a=1, b up to 10000, c negative. Works.
But is it possible that s(x) could be >81? For x<1e9, maximum is 9*9=81, correct. But what about x=10^9-1=999,999,999 => 81. So t range is 1..81. However, consider x=0? Not allowed >0. So fine.
But wait: x is strictly less than 10^9, so the maximum 9-digit number is 999,999,999, sum=81. So correct.
Thus algorithm:
Read a, b, c.
Initialize empty list solutions.
For t = 1 to 81:
compute p = t^a (as integer)
X = b * p + c
if X <= 0 or X >= 1000000000: continue
compute s = sum_of_digits(X)
if s == t: add X to solutions
Output len(solutions)
If >0, output them in increasing order (already sorted by t because X increasing, but we should sort anyway to be safe, but they will be sorted; however if multiple t give same X? That could happen if b*t^a + c same for different t? Since function strictly increasing in t, impossible. So sorted automatically)
Also we missed t that gives 2008. Let's find t for 2008: x=2008, sum=10. So t should be 10. Check t=10: p=1000, X=2*1000+8=2008, sum=10 -> matches. So solutions: t=1 ->10, t=10 ->2008, t=19->13726. Exactly three. So output matches.
Second example: a=1, b=2, c=-18. Then x = 2*s(x) - 18. For t from 1..81: x = 2t -18. Need x>0 -> 2t-18>0 => t>9 => t>=10. And x<1e9. Check t=10 => x=2, sum=2 !=10. t=11 => x=4 sum=4 !=11. ... We might not find any. Also if x is two-digit maybe not. So output 0.
c can be negative, so X may be negative or zero. We skip.
We must use 64-bit to avoid overflow when computing b * t^a. t^a fits in 32-bit for t up to 81 and a<=5? t^5=81^5=3,486,784,401 which is >2^31-1 (2,147,483,647), so it exceeds 32-bit signed int. So use long long (int64). Multiply by b up to 10000 => up to 3.486e13, fits in signed 64-bit (9.22e18). So long long is fine.
The output requires printing n and then the integers in increasing order. Since we loop t increasing and X is increasing, we can just store in order. However, if c is negative enough that some X might be decreasing? No, b*t^a is monotonic increasing, so overall increasing. So order is fine.
Edge case: if there are duplicate solutions? Not possible due to strict monotonicity, but we could check anyway. Not needed.
Need to ensure we only include x such that 0 < x < 1e9. The bound is strict less than 10^9, not inclusive. So x must be < 1,000,000,000. So check x < 1000000000.
Since x must be integer, we use integer arithmetic.
Implementation steps:
Read a,b,c.
Precompute powers: maybe compute t^a in loop.
Initialize vector sols.
For t = 1 to 81 inclusive:
p = 1; for i=1..a: p *= t; (or pow(t,a) using integer pow)
x = b * p + c;
if x <= 0 or x >= 1000000000: continue;
sum_digits = digit_sum(x);
if sum_digits == t: sols.push_back(x);
Output sols.size().
If sols.size()>0: for each x in sols: cout << x << (space or newline? Usually space-separated). In examples, they print newline after count then numbers separated by spaces, maybe also trailing space? But we can output numbers with spaces, and final newline.
But careful: The output format: "Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation." So after n, we print n integers separated by spaces. Usually newline after n, then numbers separated by spaces. In example they have newline after n then numbers with spaces. We'll do:
cout << sols.size() << endl;
if (sols.size()) {
for (int i=0; i<sols.size(); ++i) {
if (i>0) cout << " ";
cout << sols[i];
}
cout << endl;
}
But note: The example output shows:
3
10 2008 13726
So after last number there might be space? Not necessary. But we can output with spaces.
Complexities: trivial.
But is there any hidden nuance? Possibly t could be 0? Since x>0, s(x) cannot be 0 because sum of digits of positive integer is at least 1. So t starts from 1.
What about numbers like 10^k? sum=1, but x may be large. Could there be a solution where s(x) > 81? For x < 10^9, the maximum sum is 81, as we said. But what about x exactly 10^9? Not allowed because strictly less than 10^9. So okay.
But wait: 10^9 is 1,000,000,000, sum=1. That is also 9 digits? Actually 1,000,000,000 has 10 digits? 10^9 = 1,000,000,000, that's 10 digits (1 followed by nine zeros). But x < 10^9, so maximum is 999,999,999, which is 9 digits, sum=81. So no.
One more thing: Could x be something like 0? Not allowed >0.
Thus solution stands.
But double-check: For a=5, b=10000, c=-some, x could be large. But we skip if x >= 1e9.
Edge case: c negative, so X = bt^a + c may become <=0 for small t even if bt^a positive. For t small, bt^a may be less than -c? If c is very negative, then X could be negative even for larger t? Actually if c is -10000, bt^a can be small for t=1: b*1 + (-10000) could be negative if b<10000? But b≥1, if b=1, c=-10000, then X=1-10000=-9999 negative. So we skip. But as t increases, X becomes positive eventually. So we must continue looping until maybe X exceeds 1e9 or until t=81. Since t max 81, we can just loop all.
Now, are there any solutions where s(x) >81? Possibly x could be exactly 10^9? Not allowed. So not.
Thus final answer.
Wait, is there any possibility that s(x) might be 0? No, positive x implies at least one digit non-zero, so sum ≥1.
So we'll implement.
But I recall some problems where digit sum for numbers up to 10^9 might be up to 9*9=81, correct.
Let's test some random values to see if any solution might have s(x) >81 due to maybe x being exactly 10^9 - some? No, all numbers less than 10^9 have at most 9 digits, each digit max 9, sum ≤81.
Thus done.
We'll implement digit sum function:
int sum_digits(long long x) {
int s = 0;
while (x>0) {
s += x % 10;
x /= 10;
}
return s;
}
But careful: x may be exactly 0? But we only call for x>0. So okay.
Potential overflow: x up to 1e9-1 fits in 32-bit, but we use long long.
One more thought: The input constraints say a up to 5, b up to 10000, c between -10000 and 10000. x must be integer, but also x must be integer solution. Since we are computing x = b * s^a + c, it's integer always. So fine.
But wait: Could there be a solution where s(x) is >81? If x is exactly 10^9 - 1? That's 999,999,999 sum=81. So no.
What about numbers with leading zeros? No.
Thus solution correct.
Now let's reflect: The problem states "Find all integer solutions x (0 < x < 10^9)". That's it.
But the teacher changes a, b, c. So we need to produce list.
Thus answer is simple: iterate over possible digit sums.
I recall known Codeforces problem: "Little Dima and Equation" or something like that. Yes, Codeforces Round #... Problem 460B? Actually there is a problem "Little Dima and Equation" from Codeforces Round #262 (Div 2) Problem B? Or maybe it's "460B - Little Dima and Equation". Let me recall: That problem was: find all x = b * s(x)^a + c, with x positive and less than 1e9. Indeed a, b, c given, and answer is this. So the approach is exactly as described: iterate s from 1 to 81. So we confirm.
Therefore the solution is straightforward.
Now about implementation details:
We'll use int for a,b,c. For power compute: long long p = 1; for (int i=0;i<a;i++) p *= t;
Then long long x = b * p + c;
if (x>0 && x<1000000000) then check digit sum.
But note: b * p might overflow long long if we used int? But we use long long for p and product. b is int, but we can cast to long long. So fine.
When checking x<1000000000, note that if x might be bigger than 2^63? No, b * p is at most 10000 * 81^5 = 10000 * 3.486e9 = 3.486e13, which fits in signed 64-bit. So safe.
digit_sum: for long long x, but x<1e9, so int fine.
Output: if n=0, just print 0 and maybe newline? In example they printed 0 and presumably no numbers. They printed just "0". So we output "0\n".
Okay.
Now let's consider if there is any tricky aspect: The equation is x = b·s(x)a + c. Possibly some interpreted as x = b * (s(x))^a + c. Yes.
Now I should test edge cases.
Case: a=5, b=1, c= -10000. Then t^5 max 81^5=3,486,784,401, plus c = 3,486,774,401, which is >1e9. So some t might give x>1e9? But we check. Since t^5 grows quickly, we might stop early if b*t^a + c > 1e9, but we don't need.
Case: a=5, b=10000, c=0: t=1 => x=10000, sum=1? 1+0+0+0+0=1, matches if sum=1? Actually s(x)=1 matches t=1, so solution. t=2 => x=10000*32=320,000? Actually 2^5=32, x=320,000, sum=3+2+0+0+0+0=5, not 2. t=3 => 3^5=243, x=2,430,000, sum=2+4+3=9, not 3. t=4 => 4^5=1024, x=10,240,000 sum=1+0+2+4=7, not 4. etc. Some might match.
But all fine.
One potential: b * t^a + c could exceed 1e9 but also become negative due to overflow if we used 32-bit. But we use 64-bit.
Another nuance: For a=5, t^5 could exceed 2^63? 81^5= ~3.5e9, times b up to 10,000 gives 3.5e13, safe. So fine.
Thus the solution is done.
But wait: The range of x is (0, 10^9) exclusive, so x cannot be exactly 10^9. We check x < 1000000000.
Now the problem statement: "0 < x < 10^9" meaning strictly less than. So correct.
Now final step: Write code.
I'll write in C++.
Pseudo:
#include
#include
#include
using namespace std;
int digit_sum(long long x) {
int s=0;
while (x) { s += x%10; x/=10; }
return s;
}
int main() {
int a,b,c;
cin >> a >> b >> c;
vector ans;
for (int t=1; t<=81; t++) {
long long p=1;
for (int i=0;i<a;i++) p *= t; // integer power
long long x = b * p + c;
if (x > 0 && x < 1000000000) {
if (digit_sum(x) == t) {
ans.push_back(x);
}
}
}
cout << ans.size() << endl;
if (ans.size()) {
for (size_t i=0; i<ans.size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
cout << endl;
}
return 0;
}
But we might want to break early if x becomes too large? But 81 iterations is trivial.
Test with given examples.
Example1: a=3,b=2,c=8 -> should output 3 lines: 3\n10 2008 13726.
Example2: 1 2 -18 -> 0.
Example3: 2 2 -1 -> 4\n1 31 337 967.
Works.
Now think: Could there be a solution where s(x) >81? For x less than 10^9, the maximum number of digits is 9, but what about 10^9-1 = 999,999,999 -> sum=81. So s(x) ≤81. So okay.
Thus final answer.
But note: There is also a possibility that x = b * s(x)^a + c, and s(x) might be computed as sum of digits of x, which is obviously integer. So our method covers all solutions.
But wait: What about numbers like 0? Not allowed.
Thus we are done.
Now I'm ready to produce final answer. ```cpp
#include
#include
using namespace std;
// sum of decimal digits of x (x > 0)
int digit_sum(long long x) {
int s = 0;
while (x > 0) {
s += x % 10;
x /= 10;
}
return s;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
code
vector<long long> solutions;
// s(x) can be at most 81 for x < 10^9
for (int s = 1; s <= 81; ++s) {
// compute s^a
long long power = 1;
for (int i = 0; i < a; ++i)
power *= s;
long long x = b * power + c; // candidate x
// check the required range
if (x > 0 && x < 1000000000) {
if (digit_sum(x) == s) {
solutions.push_back(x);
}
}
}
// output
cout << solutions.size() << endl;
if (!solutions.empty()) {
for (size_t i = 0; i < solutions.size(); ++i) {
if (i > 0) cout << ' ';
cout << solutions[i];
}
cout << endl;
}
return 0;
512000+8=1,024,000+8=1,024,008. sum=1+0+2+4+0+0+8=15 !=80, also X >=1e9? 1,024,008 <1e9? Actually 1,024,008 is less than 1,000,000,000? No, it's 1 million, <1e9. So okay.
t=81: p=531441, X=2