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: Time Limit Exceeded)

use std::collections::HashMap;

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

    let mut ans = 0;
    for digit in 1..=7 {
        ans += solve(d, digit, false);
        ans += solve(d, digit, true) * 10;
    }
    println!("{}", ans);
}

fn solve(d: i64, digit: i64, center: bool) -> i64 {
    let mut offset = 1;
    for _ in 0..digit {
        offset *= 10;
    }

    let mut count = HashMap::new();
    for head in (offset / 10)..offset {
        let tail = mirror(head, digit);
        assert_ne!(tail % 10, 0);
        if center {
            let e = head * offset * 10 - tail;
            *count.entry(e).or_insert(0) += 1;
        } else {
            let e = head * offset - tail;
            *count.entry(e).or_insert(0) += 1;
        }
    }

    let mut ans = 0;
    for (&v, &c) in count.iter() {
        if v < d {
            continue;
        }
        let t = v - d;
        let c = c * *count.get(&t).unwrap_or(&0);
        ans += c;
    }
    ans
}

fn mirror(mut x: i64, digit: i64) -> i64 {
    let mut result = 0;
    for _ in 0..digit {
        result *= 10;
        let y = x % 10;
        x /= 10;
        result += y;
    }
    result
}

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(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()
    }
}