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
最后别忘了答案只在 109 以下要判断ans是否合法。
代码(本人在计算位数和得到时候非常暴力……轻喷……)
#include<iostream>
#include<cmath>
using namespace std;
#define ll long long
int a=0,b=0,c=0,t=0;
ll ans[83];
bool check(int k,int sum) {
ll a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
a=k/100000000;
if (a!=0) k=k-a*100000000;
b=k/10000000;
if (b!=0) k=k-b*10000000;
c=k/1000000;
if (c!=0) k=k-c*1000000;
d=k/100000;
if (d!=0) k=k-d*100000;
e=k/10000;
if (e!=0) k=k-e*10000;
f=k/1000;
if (f!=0) k=k-f*1000;
g=k/100;
if (g!=0) k=k-g*100;
h=k/10;
if (h!=0) k=k-h*10;
i=k;
if (sum==a+b+c+d+e+f+g+h+i) return true;
else return false;
}
int main() {
cin >> a >> b >>c;
ll x=0,l=1;
for (int sum=1;sum<=81;sum++){
l=1;
for (int j=1;j<=a;++j) l=l*sum;
x=b*l+c;
if (check(x,sum) && x<1000000000) {
t++;
ans[t]=x;
}
}
cout << t << endl;
for (int i=1;i<=t;i++) cout << ans[i] << " ";
return 0 ;
}