Codeforces Little Dima and Equation mathematical question
B. Little Dima and Equationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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 solutionsX(0? X?
X? =?
B·
S(
X)
A? +?
C,?
WhereA,B,CAre 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.
Input
The first line contains three space-separated integers:A,?B,?C(1? ≤?A? ≤? 5; 1? ≤?B? ≤? 10000 ;? -? 10000? ≤?C? ≤? 10000 ).
Output
Print integerN-The number of the solutions that you 've found. Next printNIntegers 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
310 2008 13726
Http://codeforces.com/contest/460/problem/ B
It is not a difficult question, that is, brute-force enumeration. However, enumeration is not so easy. Rather, it requires a good logical thinking ability to come up with answers to the question within such a period of time.
Thoughts:
1. How to Find the rule?
2. No rule found. Brute Force Search?
3. How to perform brute force search? Traverse? Which value is used as the traversal?
4. Use x as the traversal? The value range is too large. It must have timed out.
5 uses s (x) as the traversal. s (x) indicates the addition of the numeric values of x. The sum range of the numeric values must be very small. Therefore, we can select this value for traversal.
Step 6 is a turning point in thinking and a bit reverse thinking. Brute force enumeration can also be tricky. Not so easy to master.
#include
#include
#include
#include #include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int MAX_N = 1000;const int MAX_VAL = 1000000000;int digit[MAX_N];int sumDigits(int num){int sum = 0;while (num){sum += num %10;num /= 10;}return sum;}int main(){int a, b, c, N;long long num;while (scanf("%d %d %d", &a, &b, &c) != EOF){N = 0;for (int i = 1; i < MAX_N; i++)//enumerate i, which is the sum of digits{num = (long long) pow(double(i), double(a));num = num * b + c;if (num >= MAX_VAL) break;if (sumDigits((int)num) == i) digit[N++] = (int)num;}sort(digit, digit+N);printf("%d\n", N);for (int i = 0; i < N; i++){printf("%d ", digit[i]);}if (N) putchar('\n');}return 0;}