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: Wrong Answer)

#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
#define w1 first
#define ls (x<<1)
#define w2 second
#define ins insert
#define rs (x<<1|1) 
#define mp make_pair
#define pb push_back
#define mid ((l+r)>>1)
#define sqr(x) ((x)*(x))
#define cle(x) ((x).clear())
#define lowbit(x) ((x)&(-x))
#define SZ(x) (int((x).size()))
#define ms(x,y) memset(x,y,sizeof (x))
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define rep2(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define per(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define Rep(p,x) for(int (p)=head[(x)];(p);(p)=nxt[(p)]) 
#define Rep2(p,x) for(int (p)=cur[(x)];(p);(p)=nxt[(p)])
#define debegin printf("debug\nbegin\n")
#define deend printf("debug\nend\n")
#define isfind printf("find\n")
template<class T>inline void rread(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
inline int getgcd(int x,int y){if(!x)return y;return getgcd(y%x,x);}
inline int power(int x,int k,int p){int res=1;for(;k;k>>=1,x=(ll)x*x%p)if(k&1)res=(ll)res*x%p;return res;}
const double pi=acos(-1);
inline void judge(){
	freopen("input.txt","r",stdin);
} 
//********************************head*************************************	
int D;
ll ans;
int mi[10];
map<int,int>f[10],g[10];
typedef map<int,int>::iterator It;
int main(){
	mi[0]=1;rep(i,1,9)mi[i]=mi[i-1]*10;
	rread(D);
	rep(now,2,9){
		int ub=(now+1)/2;
		f[0].clear();
		g[0].clear();
		f[0][0]=1;
		g[0][0]=1;
		rep(w,0,ub-1){
			f[w+1].clear();
			for(It it=f[w].begin();it!=f[w].end();it++){
				int nowd=it->w1,res=it->w2;
				int l=0,r=9;
				if(!w)l=1;
				rep(i,l,r)rep(j,l,r){
					if((now&1)&&(w==ub-1)){
						if(i!=j)continue;
					}else if(w==0){
						if(i==j)continue;
					}
					int nxtd=nowd+(i-j)*(mi[now-1-w]-mi[w]);
					f[w+1][nxtd]+=res;
				}
			}
			g[w+1].clear();
			for(It it=g[w].begin();it!=g[w].end();it++){
				int nowd=it->w1,res=it->w2;
				int l=0,r=9;
				rep(i,l,r)rep(j,l,r){
					if((now&1)&&(w==ub-1)){
						if(i!=j)continue;
					}else if(w==0){
						if(i==j)continue;
					}
					int nxtd=nowd+(i-j)*(mi[now-1-w]-mi[w]);
					g[w+1][nxtd]+=res;
				}
			}
		}
		for(It it=f[ub].begin();it!=f[ub].end();it++)if(it->w1==D)ans+=it->w2;
		for(It it=g[ub].begin();it!=g[ub].end();it++){
			int nowd=it->w1,res=it->w2;
			if(D%nowd)continue;
			rep(i,1,9)if(mi[i]==D/nowd)ans+=res*9ll*mi[i-1];
		}
	}
	printf("%lld\n",ans);
	return 0;
}