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.42.0) (judge verdict: Accepted)

use std::collections::BTreeMap;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let d = sc.read();

    let mut ans = 0;
    for i in 1..19 {
        ans += solve(d, i);
    }
    println!("{}", ans);
}

fn solve(d: i64, digits: usize) -> i64 {
    let mut head = 1;
    for _ in 1..digits {
        head *= 10;
    }
    let mut tail = 1;

    let mut dp: BTreeMap<i64, i64> = BTreeMap::new();
    dp.insert(d, 1);
    for i in 0..(digits / 2) {
        let mut next_dp = BTreeMap::new();
        for (cur, count) in dp.into_iter() {
            for next in -9i64..=9 {
                let comb = 10 - next.abs() - if i == 0 && next >= 0 { 1 } else { 0 };
                let next = next * (head - tail);
                if (cur - next) % (tail * 10) != 0 {
                    continue;
                }
                let next = cur - next;
                *next_dp.entry(next).or_insert(0) += count * comb;
            }
        }
        dp = next_dp;
        head /= 10;
        tail *= 10;
    }

    dp.get(&0).unwrap_or(&0) * if digits % 2 == 1 { 10 } else { 1 }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write_all(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}