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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.Arrays;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	long[] p10 = new long[19];

	void run() {
		p10[0] = 1;
		for (int i = 1; i < p10.length; ++i) {
			p10[i] = p10[i - 1] * 10;
		}
		Scanner sc = new Scanner(System.in);
		long d = sc.nextLong();
		// while (true) {
		// tr(d);
		long ans = 0;
		for (int i = 1; i <= 19; ++i) {
			ans += solve(d, i);
		}
		System.out.println(ans);
		// d = (long) (Math.random() * 1_000_000_000);
		// }
	}

	// Nがlen桁でrev(N)=N+Dを満たすNの数を数えたい。
	// dcba=abcd+D
	// D=999*(d-a)+90*(c-d)
	long solve(long d, int len) {
		long comb = 1;
		boolean f = true;

		while (len > 0) {
			long x = -999999;
			boolean update = false;
			if (len != 1)
				for (long i = -9; i <= 9; ++i) {
					if (d % 10 == i * (p10[len - 1] - 1) % 10
							&& Math.abs(d - i * (p10[len - 1] - 1)) <= p10[len - 1] - 1) {
						x = i;
						if (update)
							throw new AssertionError();
						update = true;
					}
				}
			else {
				x = d;
				if (d > 9 || d < 0)
					return 0;
				update = true;
			}
			// ba=ab+D
			// (ba-ab)=D
			// 9*(b-a)=D
			if (!update) {
				return 0;
			}
			if (len == 1) {
				d -= x;
			} else {
				d -= x * (p10[len - 1] - 1);
			}
			long cnt = 0;
			for (int i = 0; i <= 9; ++i) {
				if (f && i == 0)
					continue;
				if (i + x >= 0 && i + x <= 9) {
					++cnt;
				}
			}
			comb *= cnt;
			f = false;
			len -= 2;
			if (d % 10 != 0) {
				throw new AssertionError();
			}
			if (len > 1)
				d /= 10;
		}
		if (d != 0) {
			return 0;
		} else {
			return comb;
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}