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;

#define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++)
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;

int N;
ll D;
ll powTable [15];
ll dp [20] [2];

int digit_count(ll x)
{
	int res = 0;
	do{
		res++;
	}while(x /= 10);
	return res;
}

ll dfs(int n,vector<int>& v)
{
	ll res = 0;
	if(n == N / 2){
		ll sum = 0;
		FOR(i,0,n){
			sum += v [i] * (powTable [N - 1 - i] - powTable [i]);
		}
		if(sum == D){
			memset(dp,0,sizeof(dp));
			int M = N / 2;
			dp [0] [1] = 1;
			FOR(i,0,M) FOR(j,0,2){
				FOR(k,0,10){
					if(k + v [i] >= 0 && k + v [i] <= 9){
						if(j == 1 && k == 0) continue;
						dp [i + 1] [0] += dp [i] [j];
					}
				}
			}
			if(N % 2){
				dp [M] [0] *= 10;
			}
			res = dp [M] [0];
			return res;
		}
		return 0;
	}
	FOR(i,-9,10){
		v.push_back(i);
		res += dfs(n + 1,v);
		v.pop_back();
	}
	return res;
}

int main()
{
	scanf("%lld",&D);

	N = digit_count(D);
	powTable [0] = 1;
	for(int i = 1;i < 15;i++){
		powTable [i] = powTable [i - 1] * 10;
	}
	vector<int> v;
	ll ans = dfs(0,v);

	printf("%lld\n",ans);

	return 0;
}