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;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
    ios::sync_with_stdio(false);
    ll d, z = 0, p[19] = {1};
    for (int i = 1; i <= 18; i++) p[i] = p[i - 1] * 10;
    cin >> d;
    for (int i = 2; i <= 19; i++) {
        ll c = d, y = 1;
        if (i % 2) y = 10;
        set<pair<ll, ll>> a = {{c, y}};
        for (int j = 0; j + j + 1 < i; j++) {
            set<pair<ll, ll>> b;
            for (auto l : a) {
                ll e = -17;
                for (int k = -9; k <= 9; k++) {
                    if (l.first - k * (p[i - j - 1] - p[j]) < 0) {e = k - 1; break;}
                }
                for (ll f = e - 1; f <= e; f++) {
                    if (f > -10 && f < 10) {
                        b.insert({l.first - f * (p[i - j - 1] - p[j]), l.second * (9 + (j != 0) - abs(f))});
                    }
                }
            }
            a = b;
        }
        set<ll> s;
        for (auto j : a) {
            if (!j.first) z += j.second;
        }
    }
    cout << z;
}