Codeforces Round #262 (Div. 二) B. Little Dima and Equation
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 functions(x) determines the sum of all digits in the decimal representation of numberx.
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 printn integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than109.
3 2 8
3 10 2008 13726
1 2 -18
0
2 2 -1
4 1 31 337 967
大意:求X的各位和等于S(X)。
对公式分析。由于X的值<1e9所以,S(x)和最大为81即999999999.注意计算时不要超出数据给的范围。
#include<iostream> #include<cstring> #include<cstdio> #include<string> #include<cmath> #include<algorithm> #define LL __int64 #define inf 0x3f3f3f3f using namespace std; LL p[1000000]; int main() { LL n,m,i,j,k,x,y,pp; LL a,b,c; while(~scanf("%I64d %I64d %I64d",&a,&b,&c)) { y=0; for(i=1;i<=81;i++) { pp=1; for(j=1;j<=a;j++)//有些注成败的细节要注意,pow尽量少和整形数据运算容易数据丢失 pp*=i; x=b*pp+c; k=x; LL ans=0; while(x) { ans+=x%10; x/=10; } if(ans==i&&k>=1&&k<1e9)//注意运算结果在给定的范围内 { p[y++]=k; } } printf("%I64d\n",y);<span id="transmark"></span>//%I64d<span id="transmark"></span> for(i=0;i<y;i++) i==y-1?printf("%I64d\n",p[i]):printf("%I64d ",p[i]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。