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 C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
using Int = long long;

Int d;
Int ans=0;

auto getK=[&](Int x,Int k){
  while(--k) x/=10;
  return x%10;
};

void dfs(Int k,Int p,Int q,Int x,Int y){
  if(p>q){
    ans+=(x+d==y); 
    return;
  }
  Int nk=k+1,np=p*10,nq=q/10;
  for(Int i=0;i<10;i++){
    Int nx=x+p*i,ny=y+q*i;
    if(p!=q){
      Int z=getK(nx+d,k);
      nx+=q*z;ny+=p*z;
      if(z==0&&p==1) continue;
    }
    if(nx+d+q<ny) continue;
    if(nx+d>ny+q) continue;
    dfs(nk,np,nq,nx,ny);
  }
};

//INSERT ABOVE HERE
signed main(){
  cin>>d;
  vector<Int> po(20,1);
  for(Int i=0;i<18;i++) po[i+1]=po[i]*10;
  for(Int i=1;i<17;i++) dfs(1,1,po[i],0,0);
  cout<<ans<<endl;
  return 0;
}