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 pair<int, int> pii;
typedef long long ll;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()
#define dump(x) cerr << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; }
template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

int D;

ll dfs(int len, int cur, ll dif) {
	if (cur == len - 1 - cur) {
		if (dif != D) {
			return 0;
		} else {
			int c = 0;
			rep(i, 10) {
				if (!(i == 0 && cur == 0)) {
					++c;
				}
			}
			return c;
		}
	}

	if (cur * 2 + 1 > len) {
		return (dif == D);
	}

	ll res = 0;
	int la = -10;
	for (int x = -9; x <= 9; ++x) {
		assert(len - 1 - cur >= cur);
		ll ad = x * (TEN(len - 1 - cur) - TEN(cur));
		if (dif + ad <= D) {
			la = x;
		}
	}

	{
		int c = 0;
		rep(i, 10) {
			int j = i + la;
			if (j >= 0 && j < 10) {
				if (!(i == 0 && cur == 0)) {
					++c;
				}
			}
		}
		ll ad = la * (TEN(len - 1 - cur) - TEN(cur));
		res += dfs(len, cur + 1, dif + ad) * c;
	}
	{
		int c = 0;
		rep(i, 10) {
			int j = i + la -+1;
			if (j >= 0 && j < 10) {
				if (!(i == 0 && cur == 0)) {
					++c;
				}
			}
		}
		ll ad = (la + 1) * (TEN(len - 1 - cur) - TEN(cur));
		res += dfs(len, cur + 1, dif + ad) * c;		
	}
	return res;
}

int main() {
	cin >> D;
	ll ans = 0;
	for (int i = 1; i <= 18; ++i) {
		ans += dfs(i, 0, 0ll);
	}
	cout << ans << endl;
	return 0;
}