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;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int D;
string str;
string d;

int ans=0;
int toint(string &s){
    int d=0;
    for(int i=s.size()-1;i>=0;i--)d=d*10+s[i]-'0';
    return d;
}
string tostr(int d){
    string s;
    while(d){
        s+='0'+d%10;
        d/=10;
    }
    return s;
}

int rev(int d){
    string s=tostr(d);
    reverse(all(s));
    return toint(s);
}

void dfs(int n,int u,int w,int l){
    if(n==(l+1)/2){

        int x=toint(str);
        reverse(all(str));
        int y=toint(str);
        reverse(all(str));
        if(x+D==y)ans++;
        return;
    }

    for(int i=0;i<10;i++){

        str[n]=i+'0';
        int tmp=i+u+d[n]-'0';
        int uku=(tmp%10+d[l-1-n]-'0');
        str[l-1-n]=tmp%10+'0';

        if(n!=l/2){
            if(!(uku%10==i||(uku+1)%10==i))continue;
            int latte=uku;
            if((uku+1)%10==i)latte++;
            if(w!=latte/10)continue;
        }
        if(n==0&&str[l-1]=='0')continue;

        if(uku%10==i)dfs(n+1,tmp/10,0,l);
        else dfs(n+1,tmp/10,1,l);
    }
}

signed main(){
    cin>>D;
    d=tostr(D);
    d+=string(100,'0');
    for(int i=1;i<=20;i++){
        str=string(i,'0');
        dfs(0,0,0,i);
    }
    cout<<ans<<endl;
    return 0;
}