Codeforces Round #262 (Div. 2) B
B. Little Dima and Equation
time limit per test
1 second memory limit per test
256 megabytes input
standard input output
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. 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. Sample test(s)
Input
3 2 8 Output
3 Input
1 2 -18 Output
0 Input
2 2 -1 Output
4 |
题意:给出一个公式x = b·s(x)a + c,其中s(x)为x的各个位的数字之和。给出a,b,c,求x在1~10^9内的所有解。
题解:枚举s(x),算出来x后看看x和s(x)符不符合,x在不在范围内。因为x<=10^9,所以s(x)为0~81,一下就枚举完了。
注意a<=5,81^5已经超int了,要用long long……血的教训
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
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))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
vector<int>v;
int a,b,c;
int main(){
int i,j;
ll k;
scanf("%d%d%d",&a,&b,&c);
v.clear();
for(i=;i<=;i++){
k=;
REP(j,a)k*=i;
ll x=k*b+c;
if(x<= || x>=)continue;
int y=x,sum=;
while(y){
sum+=y%;
y/=;
}
if(sum==i)v.pb(x);
}
int maxi=v.size();
printf("%d\n",maxi);
if(maxi>)printf("%d",v[]);
for(i=;i<maxi;i++)
printf(" %d",v[i]);
return ;
}