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: Runtime Error)

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

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

	void run() {
		Scanner sc = new Scanner(System.in);
		long d = sc.nextLong();
		long ans = 0;
		for (int i = 1; i <= 18; ++i) {
			ans += solve(d, i);
		}
		System.out.println(ans);
	}

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

		while (len > 0) {
			int x = -999999;
			boolean update = false;
			if (len != 1)
				for (int i = -9; i <= 9; ++i) {
					if (d % 10 == i * (long) (Math.pow(10, len - 1) - 1) % 10 && Math
							.abs(d - i * (long) (Math.pow(10, len - 1) - 1)) <= (long) (Math.pow(10, len - 1) - 1)) {
						x = i;
						update = true;
						break;
					}
				}
			else {
				x = (int) d;
				if (d > 9 || d < 0)
					return 0;
				update = true;
			}
			// ba=ab+D
			// (ba-ab)=D
			// 9*(b-a)=D
			if (!update) {
				return 0;
			}
			d -= x * (long) (Math.pow(10, len - 1) - 1);
			int 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();
			d /= 10;
		}
		if (d != 0) {
			return 0;
		} else {
			return comb;
		}
	}

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