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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn get<T: FromStr>() -> T
        where <T as FromStr>::Err: Debug
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
        where <T as FromStr>::Err: Debug
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}

fn main() {
    let d = util::get();
    let digits = format!("{}", d).len();

    let tmp = (max(2, digits)..digits * 2 + 1)
        .map(|l| {
            search(l, Vec::new(), 0, d)
                .into_iter()
                .map(|v| {
                         v.iter()
                             .enumerate()
                             .map(|(i, &x)| if i == 0 { 9 - x.abs() } else { 10 - x.abs() })
                             .fold(1, |acc, x| acc * x) *
                         if l % 2 == 1 { 10 } else { 1 }
                     })
                .sum::<isize>()
        })
        .sum::<isize>();
    println!("{:?}", tmp);
}

fn search(l: usize, cur: Vec<isize>, memo: isize, d: isize) -> Vec<Vec<isize>> {
    let base = 10usize.pow(l as u32 - 1 - cur.len() as u32) as isize -
               10usize.pow(cur.len() as u32) as isize;

    if cur.len() == l / 2 - 1 {
        (-9..10)
            .filter(|x| memo + base * x == d)
            .map(|k| {
                     let mut v = cur.clone();
                     v.push(k);
                     v
                 })
            .collect()
    } else {
        (-9..10)
            .map(|x| (x, memo + base * x))
            .filter(|&(_, x)| (d - x).abs() < base)
            .map(|(k, x)| {
                     let mut v = cur.clone();
                     v.push(k);
                     search(l, v, x, d)
                 })
            .fold(Vec::new(), |mut acc, mut v| {
                acc.append(&mut v);
                acc
            })
    }
}