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 Fortran (gfortran v4.8.4) (judge verdict: Accepted)

program mirrored
  implicit none
  integer :: d, n, x(0:10), i, j, k
  integer(8) :: b(0:18) = 1_8, ans = 0_8, v(0:10)
  read(*,*) d
  if (mod(d,9) /= 0) then
    write(*,'(i0)') 0
    stop
  end if
  do i = 1, 18
    b(i) = 10_8*b(i-1)
  end do
  d = d/9
  do i = 1, 18
    n = (i+1)/2
    x = 0
    v = 0_8
    do j = 0, n-1
      do k = j, i-j-1
        v(j) = v(j)+b(k)
      end do
    end do
    ans = ans+num(int(d,8),i,0,n,x(0:n-1),v(0:n-1))
  end do
  write(*,'(i0)') ans
  stop
contains
  recursive function num(d,l,t,n,x,v) result(ret)
    implicit none
    integer, intent(in) :: l, t, n
    integer, intent(inout) :: x(0:n-1)
    integer(8), intent(in) :: d
    integer(8), intent(inout) :: v(0:n-1)
    integer :: i
    integer(8) :: ret
    ret = 0_8
    if (t == n) then
      if (d /= 0) return
      ret = 1_8
      ret = ret*int(9-abs(x(0)),8)
      do i = 1, n-1
        ret = ret*int(10-abs(x(i)),8)
      end do
      if (mod(l,2) == 0) ret = ret*10_8
      return
    end if
    do i = -9, 9
      if (-v(t) < d+v(t)*int(i,8) .and. d+v(t)*int(i,8) < v(t)) then
        x(t) = i
        ret = ret+num(d+v(t)*int(i,8),l,t+1,n,x,v)
      end if
    end do
    return
  end function num
end program mirrored