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:
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.
The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000; - 10000 ≤ c ≤ 10000).
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.
3 2 8
3
10 2008 13726
1 2 -18
0
2 2 -1
4
1 31 337 967
这道题目比较好,想法,一开始不是我想的,枚举x很不现实, 1 ~ 1亿, 这样枚举肯定超时
而 s(x) 的范围就很明确, 1~81, 最多 999999999, 也就是9 个 9.所以就会用这个枚举。
枚举 s(x), 然后验证 是否是 x, 就这样
也模仿他人, 把那些能省事的宏定义写上,慢慢来吧
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 6 using namespace std; 7 #define LL long long 8 #define usLL unsigned LL 9 #define mz(array) memset(array, 0, sizeof(array)) 10 #define minf(array) memset(array, 0x3f, sizeof(array)) 11 12 LL bit_sum(LL x) 13 { 14 LL sum = 0; 15 while(x) 16 { 17 sum += x % 10; 18 x /= 10; 19 } 20 return sum; 21 } 22 23 LL powd(int t,int n){ 24 LL sum=1; 25 for(int i=1;i<=n;i++) 26 sum*=t; 27 return sum; 28 } 29 30 int main() 31 { 32 int a, b, c; 33 cin >> a >> b >> c; 34 int ans[100000]; 35 int cnt = 0; 36 for(int i = 1; i <= 81; i++) 37 { 38 LL t = powd(i, a); 39 LL x = t*b + c; 40 if(x > 0 && x < 1000000000 && bit_sum(x)==i) 41 { 42 ans[cnt++] = x; 43 } 44 } 45 46 printf("%d ", cnt); 47 for(int i = 0; i < cnt; i++) 48 { 49 printf("%d ", ans[i]); 50 } 51 puts(""); 52 return 0; 53 }