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 nintegers 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
题意: 题意就是说,给你一个数学公式 让你去求满足的的值有多少个,并输出出来,其中s(x)表示的是x的各位数字之和;
思路: 由于我们的x是到1e9的 比较大,而我们的s(x)只有81,所以我们直接枚举s(x),之后看所得的数x是否等于s(x)就好了,worng了两发,第一发是关于0这个条件,x的数据范围是0到1e9,没有0,之后是 乘方超界worng 了一发,总结一下就是 看清题意啊!!!
最后边看cf数据 边改bug的感觉 好羞耻啊 。。。
上代码吧:#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; long long cc(int n,int m) { int sum=1; for(int i=0;i<m;i++) { sum*=n; } return sum; } int judge(int x) { int sum=0; while(x) { sum+=x%10; x=x/10; } return sum; } int main() { int a,b,c; long long x,k=0; int sum=0,ans=0; int bb[1000]; while(scanf("%d%d%d",&a,&b,&c)!=EOF) { k=0; memset(bb,0,sizeof(bb)); for(int i=1;i<81;i++) { x=b*cc(i,a)+c; if(x>1e9) { continue; } sum=judge(x); // printf("x==%d sum==%d i==%d \n",x,sum,i); if(sum==i) { bb[k]=x; k++; ans++; } } printf("%d\n",ans); for(int i=0;i<k;i++) { printf("%d ",bb[i]); } printf("\n"); } }