1 second
256 megabytes
standard input
standard output
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.
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, 就这样
也模仿他人, 把那些能省事的宏定义写上,慢慢来吧
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
#define LL long long
#define usLL unsigned LL
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array)) LL bit_sum(LL x)
{
LL sum = ;
while(x)
{
sum += x % ;
x /= ;
}
return sum;
} LL powd(int t,int n){
LL sum=;
for(int i=;i<=n;i++)
sum*=t;
return sum;
} int main()
{
int a, b, c;
cin >> a >> b >> c;
int ans[];
int cnt = ;
for(int i = ; i <= ; i++)
{
LL t = powd(i, a);
LL x = t*b + c;
if(x > && x < && bit_sum(x)==i)
{
ans[cnt++] = x;
}
} printf("%d\n", cnt);
for(int i = ; i < cnt; i++)
{
printf("%d ", ans[i]);
}
puts("");
return ;
}