Problem:
Score : 800 points

Problem Statement

For a positive integer n, we denote the integer obtained by reversing the decimal notation of n (without leading zeroes) by rev(n). For example, rev(123) = 321 and rev(4000) = 4.

You are given a positive integer D. How many positive integers N satisfy rev(N) = N + D?

Constraints

D is an integer.

1 ≤ D < 10^9

Input

Input is given from Standard Input in the following format:

D

Output

Print the number of the positive integers N such that rev(N) = N + D.

Sample Input 1

63

Sample Output 1

2

There are two positive integers N such that rev(N) = N + 63: N = 18 and 29.

Sample Input 2

75

Sample Output 2

0

There are no positive integers N such that rev(N) = N + 75.

Sample Input 3

864197532

Sample Output 3

1920

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

long long pw[21];
long long solve(int sum,int len,bool first) {
    if (len == 0) return (sum == 0);
    if (len == 1) return (sum == 0) * 10;
    long long coef = pw[len - 1] - 1;
    int cur = sum / coef;
    long long ret = 0;
    if (first) {
        int new_sum = sum - cur * coef;
        if (new_sum % 10 == 0) ret += max(0, 9 - cur) * solve(new_sum / 10, len - 2, 0);
        new_sum = (cur + 1) * coef - sum;
        if (new_sum % 10 == 0) ret += max(0, 8 - cur) * solve(new_sum / 10, len - 2, 0);
    }
    else {
        int new_sum = sum - cur * coef;
        if (new_sum % 10 == 0) ret += max(0, 10 - cur) * solve(new_sum / 10, len - 2, 0);
        new_sum = (cur + 1) * coef - sum;
        if (new_sum % 10 == 0) ret += max(0, 9 - cur) * solve(new_sum / 10, len - 2, 0);
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(false);
    int d; cin >> d;
    pw[0] = 1; for (int i = 1; i <= 19; ++i) pw[i] = pw[i - 1] * 10;
    long long ans = 0;
    for (int i = 1; i <= 19; ++i) ans += solve(d, i, 1);
    cout << ans << '\n';
}