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;

using ll = long long;
using vi = vector<int>;
using db = double;
#define pb push_back

ll d;
ll ans = 0;
ll p[18];
#define dig(x, i) ((x) / p[i] % 10)

const int W = 11;

int main() {
	cin >> d;
	p[0] = 1;
	for (int i = 1; i < 18; ++i)
		p[i] = p[i - 1] * 10;
	
	for (int i = 2; i <= W * 2; ++i)
		if (i % 2 == 0) {
			unordered_map<ll,ll> s;
			int al = i / 2;
			int ar = min(W, i - 1);
			int bl = i - 1 - ar;
			int br = i / 2 - 1;
			ll fr = p[i - 1 - ar];
			if (i - 1 - ar) fr = fr / 10 * 9;
			int len = ar + 1 - al;
			
			for (ll x = 0; x < p[len]; ++x) if (ar < i - 1 || dig(x, len - 1)) {
				ll t = 0;
				for (int j = 0; j < len; ++j)
					t += dig(x, j) * -(p[al + j] - p[br - j]);
				++s[t];
			}
			
			for (ll x = 0; x < p[len]; ++x) {
				ll t = 0;
				for (int j = 0; j < len; ++j)
					t += dig(x, j) * +(p[al + j] - p[br - j]);
				ans += s[d - t] * fr;
			}
			
		} else {
			unordered_map<ll,ll> s;
			int al = i / 2 + 1;
			int ar = min(W, i - 1);
			int bl = i - 1 - ar;
			int br = i / 2 - 1;
			ll fr = p[i - 1 - ar + 1];
			if (i - 1 - ar) fr = fr / 10 * 9;
			int len = ar + 1 - al;
			
			for (ll x = 0; x < p[len]; ++x) if (ar < i - 1 || dig(x, len - 1)) {
				ll t = 0;
				for (int j = 0; j < len; ++j)
					t += dig(x, j) * -(p[al + j] - p[br - j]);
				++s[t];
			}
			
			for (ll x = 0; x < p[len]; ++x) {
				ll t = 0;
				for (int j = 0; j < len; ++j)
					t += dig(x, j) * +(p[al + j] - p[br - j]);
				ans += s[d - t] * fr;
			}
		}
	
	cout << ans << endl;
	
	return 0;
}