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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<cstdio>
#include<algorithm>
long long pow10[15];
long long mapL[1000005]; int sizeL = -1;
long long mapR[1000005]; int sizeR = -1;
//org: a[n],a[n-1]...a[2],a[1]
//rev: a[1],a[2]...a[n-1],a[n]
//dfs on rev, a[n]!=0
//end of left is (n+1)/2

void dfsL(int p,int n,long long sum){
    int mid = (n+1)/2;
    if(p==mid+1) mapL[++sizeL] = sum;
    else{
        for(long long i = 0; i <= 9; i++){
            long long add = i*pow10[n-p], minus = i*pow10[p-1];
            dfsL(p+1,n,sum+add-minus);
        }
    }
}

void dfsR(int p,int n,long long sum){
    if(p==n+1) mapR[++sizeR] = sum;
    else{
        int start = 0;
        if(p==n) start = 1;
        for(long long i = start; i <= 9; i++){
            long long add = i*pow10[n-p], minus = i*pow10[p-1];
            dfsR(p+1,n,sum+add-minus);
        }
    }
}

int getFirst(long long u,long long map[],int size){
    int L = 1, R = size;
    int res = -1;
    while(L<=R){
        int M = (L+R)/2;
        if(map[M]>=u){
            res = M;
            R = M-1;
        }
        else L = M+1;
    }
    return res;
}

int getLast(long long u,long long map[],int size){
    int L = 1, R = size;
    int res = -1;
    while(L<=R){
        int M = (L+R)/2;
        if(map[M]<=u){
            res = M;
            L = M+1;
        }
        else R = M-1;
    }
    return res;
}

int main(){
    pow10[0] = 1;
    for(int i = 1; i <= 13; i++) pow10[i] = pow10[i-1]*10ll;
    long long d;
    scanf("%lld",&d);
    long long ans = 0;
    for(int i = 1; i <= 10; i++){
        sizeL = sizeR = 0;
        int mid = (i+1)/2;
        dfsL(1,i,0);
        dfsR(mid+1,i,0);
        std::sort(mapL+1,mapL+1+sizeL);
        std::sort(mapR+1,mapR+1+sizeR);
        for(int j = 1; j <= sizeL; j++){
            long long u = mapL[j];
            long long need = d-u;
            int lower = getFirst(need,mapR,sizeR), upper = getLast(need,mapR,sizeR);
            if(lower!=-1 && upper!=-1 && mapR[lower]==need && mapR[upper]==need) ans += (long long)(upper-lower+1);
        }
    }
    printf("%lld\n",ans);
    return 0;
}