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



Sample input:
63


Sample output:
2

Submission status: Accepted

Solution code:
D=int(input())
count=0
while D%10==0:
    count+=1
    D//=10
if not count:
    ans=0
    for i in range(1,1000):
        val=str(i)
        val=val[::-1]
        rev=int(val)
        if rev-i==D:
            ans+=1

    for d in range(4,18):
        dic1={}
        dic2={}
        if d%2==0:
            k=d//2
            l=k//2
            def dfs(n,a):
                if n==l:
                    val=0
                    res=1
                    for i in range(len(a)):
                        val+=a[i]*(10**(d-1-i)-10**i)
                        if i!=0:
                            res*=10-abs(a[i])
                        else:
                            res*=9-abs(a[i])
                    if val not in dic1:
                        dic1[val]=0
                    dic1[val]+=res
                    return 0

                for i in range(-9,10):
                    dfs(n+1,a+[i])

            dfs(0,[])

            def dfs2(n,a):
                if n==k:
                    val=0
                    res=1
                    for i in range(len(a)):
                        val+=a[i]*(10**(d-1-l-i)-10**(i+l))
                        if i!=0:
                            res*=10-abs(a[i])
                        else:
                            res*=10-abs(a[i])
                    if val not in dic2:
                        dic2[val]=0
                    dic2[val]+=res
                    return 0

                for i in range(-9,10):
                    dfs2(n+1,a+[i])

            dfs2(l,[])
            for i in dic1:
                if D-i in dic2:
                    ans+=dic1[i]*dic2[D-i]

        else:
            k=d//2
            l=k//2
            def dfs(n,a):
                if n==l:
                    val=0
                    res=1
                    for i in range(len(a)):
                        val+=a[i]*(10**(d-1-i)-10**i)
                        if i!=0:
                            res*=10-abs(a[i])
                        else:
                            res*=9-abs(a[i])
                    if val not in dic1:
                        dic1[val]=0
                    dic1[val]+=res
                    return 0

                for i in range(-9,10):
                    dfs(n+1,a+[i])

            dfs(0,[])

            def dfs2(n,a):
                if n==k:
                    val=0
                    res=1
                    for i in range(len(a)):
                        val+=a[i]*(10**(d-1-l-i)-10**(i+l))
                        if i!=0:
                            res*=10-abs(a[i])
                        else:
                            res*=10-abs(a[i])
                    if val not in dic2:
                        dic2[val]=0
                    dic2[val]+=res
                    return 0

                for i in range(-9,10):
                    dfs2(n+1,a+[i])

            dfs2(l,[])

            for i in dic1:
                if D-i in dic2:
                    ans+=dic1[i]*dic2[D-i]*10

    print(ans)
else:
    ans=0
    for d in range(1,18):
        dic1={}
        dic2={}
        if d%2==0:
            k=d//2
            l=k//2
            def dfs(n,a):
                if n==l:
                    val=0
                    res=1
                    for i in range(len(a)):
                        val+=a[i]*(10**(d-1-i)-10**i)
                        if i!=0:
                            res*=10-abs(a[i])
                        else:
                            res*=10-abs(a[i])
                    if val not in dic1:
                        dic1[val]=0
                    dic1[val]+=res
                    return 0

                for i in range(-9,10):
                    dfs(n+1,a+[i])

            dfs(0,[])

            def dfs2(n,a):
                if n==k:
                    val=0
                    res=1
                    for i in range(len(a)):
                        val+=a[i]*(10**(d-1-l-i)-10**(i+l))
                        if i!=0:
                            res*=10-abs(a[i])
                        else:
                            res*=10-abs(a[i])
                    if val not in dic2:
                        dic2[val]=0
                    dic2[val]+=res
                    return 0

                for i in range(-9,10):
                    dfs2(n+1,a+[i])

            dfs2(l,[])
            for i in dic1:
                if D-i in dic2:
                    ans+=dic1[i]*dic2[D-i]

        else:
            k=d//2
            l=k//2
            def dfs(n,a):
                if n==l:
                    val=0
                    res=1
                    for i in range(len(a)):
                        val+=a[i]*(10**(d-1-i)-10**i)
                        if i!=0:
                            res*=10-abs(a[i])
                        else:
                            res*=10-abs(a[i])
                    if val not in dic1:
                        dic1[val]=0
                    dic1[val]+=res
                    return 0

                for i in range(-9,10):
                    dfs(n+1,a+[i])

            dfs(0,[])

            def dfs2(n,a):
                if n==k:
                    val=0
                    res=1
                    for i in range(len(a)):
                        val+=a[i]*(10**(d-1-l-i)-10**(i+l))
                        if i!=0:
                            res*=10-abs(a[i])
                        else:
                            res*=10-abs(a[i])
                    if val not in dic2:
                        dic2[val]=0
                    dic2[val]+=res
                    return 0

                for i in range(-9,10):
                    dfs2(n+1,a+[i])

            dfs2(l,[])

            for i in dic1:
                if D-i in dic2:
                    ans+=dic1[i]*dic2[D-i]*10

    print(ans*9*10**(count-1))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

D = int(input())

memo_F = [0] * 30
memo_T = [0] * 30

for x in range(10):
    for y in range(10):
        memo_T[y-x] += 1
        if x != 0:
            memo_F[y-x] += 1

def F(K,D,allow_leading_zero):
    if K == 0:
        return 1 if D == 0 else 0
    if K == 1:
        if D != 0:
            return 0
        if allow_leading_zero:
            return 10
        else:
            return 9
    x = 10**(K-1)-1
    d = (-D)%10
    # xをd個またはd-10個
    ret = 0
    for use_x in [d,d-10]:
        if allow_leading_zero:
            coef = memo_T[use_x]
        else:
            coef = memo_F[use_x]
        if coef == 0:
            continue
        ret += coef * F(K-2,(D-x*use_x)//10,True)
    return ret

answer = sum(F(K,D,False) for K in range(1,20))
print(answer)

Submission status: Accepted

Solution code:
def f(d,x,y):n=int("9"*x+"0"*y);return d==0 if x<1 else sum(f(d-i*n,x-2,y+1)*(10-abs(i)-(y<1))for i in [d/n,d/n+1]if -10<i<10)
D=input();print sum(f(D,i,0)*(10-i%2*9)for i in range(1,21))

Submission status: Accepted

Solution code:
def f(d,x,y):n=int("9"*x+"0"*y);return d==0 if x<1 else sum(f(d-i*n,x-2,y+1)*(10-abs(i)-(y<1))for i in range(-9,10)if abs(d-i*n)<n)
D=input();print sum(f(D,i,0)*(10-i%2*9)for i in range(1,21))

Submission status: Accepted

Solution code:
def f(d,x,y):n=int("9"*x+"0"*y);return d==0 if x<1 else sum(f(d+i*n,x-2,y+1)*(10-abs(i)-(y<1))for i in range(-9,10)if abs(d+i*n)<n)
D=input();print sum(f(D,i,0)*(10-i%2*9)for i in range(1,21))

Submission status: Accepted

Solution code:
def func(d, x, y):
    n = int("9" * x + "0" * y)
    return d == 0 if x < 1 else sum(func(d + i * n, x - 2, y + 1) * (10 - abs(i) - (y < 1)) for i in range(-9, 10) if abs(d + i * n) < n)
D = input();print sum(func(D, i, 0) * (10 - i % 2 * 9) for i in range(1, 21))

Submission status: Accepted

Solution code:
def func(d, cnt9, cnt0):
    if cnt9 < 1 : return d == 0
    n = int("9" * cnt9 + "0" * cnt0)
    return sum(func(d + i * n, cnt9 - 2, cnt0 + 1) * (9 - abs(i) + 1 - (cnt0 < 1)) for i in range(-9, 10) if abs(d + i * n) < n)

D = input()
print sum(func(D, i, 0) * (10 - i % 2 * 9) for i in range(1, 21))

Submission status: Accepted

Solution code:
def func(diff, cnt9, cnt0):
    if cnt9 <= 0 : return diff == 0
    num = int("9" * cnt9 + "0" * cnt0)
    return sum(func(diff + i * num, cnt9 - 2, cnt0 + 1) * (9 - abs(i) + 1 - (cnt0 < 1)) for i in range(-9, 10) if abs(diff + i * num) < num)

D = input()
print sum(func(D, d, 0) * (10 - d % 2 * 9) for d in range(1, 21))

Submission status: Accepted

Solution code:
import itertools as ite
import math
import random

INF = 10 ** 18
MOD = 10 ** 9 + 7

def func(diff, cnt9, cnt0):
    if cnt9 <= 0:
        return (diff == 0)
    num = int("9" * cnt9 + "0" * cnt0)
    return sum(func(diff + i * num, cnt9 - 2, cnt0 + 1) * (9 - abs(i) + 1 - (cnt0 < 1)) for i in range(-9, 10) if abs(diff + i * num) < num)

D = input()

ans = 0
for d in range(1, 21):
    ans += func(D, d, 0) * (10 - d % 2 * 9)
print ans

Submission status: Accepted

Solution code:
import itertools as ite
import math
import random

INF = 10 ** 18
MOD = 10 ** 9 + 7

def func(diff, cnt9, cnt0):
    if cnt9 <= 0:
        return (diff == 0)
    num = int("9" * cnt9 + "0" * cnt0)
    return sum(func(diff + i * num, cnt9 - 2, cnt0 + 1) * (9 - abs(i) + 1 - (cnt0 < 1)) for i in range(-9, 10) if abs(diff + i * num) < num)

D = input()

ans = 0
for d in range(1, 21):
    MUL = 1 if d % 2 == 1 else 10
    ans += func(D, d, 0) * MUL
print ans

Submission status: Accepted

Solution code:
import itertools as ite
import math
import random

INF = 10 ** 18
MOD = 10 ** 9 + 7

def func(diff, cnt9, cnt0):
    if cnt9 <= 0:
        return (diff == 0)
    num = int("9" * cnt9 + "0" * cnt0)
    ret = 0
    for i in range(-9, 10):
        if abs(diff + i * num) < num:
            MUL = 9 - abs(i) + 1
            MUL -= (cnt0 == 0)
            ret += func(diff + i * num, cnt9 - 2, cnt0 + 1) * MUL
    return ret

D = input()

ans = 0
for d in range(1, 21):
    MUL = 1 if d % 2 == 1 else 10
    ans += func(D, d, 0) * MUL
print ans

Submission status: Accepted

Solution code:
from collections import deque
D=int(input())
def f(d,k):
  if 0<=k<=1:
    if d!=0:
      return "Unko"
    if k==1:
      return deque([0])
    else:
      return deque([])
  if d>=0:
    s=10-(d%10)
  else:
    s=-(d%10)
  if s==10:
    A=f(d//10,k-2)
    s=0
  #print(d,s,d-s*(10**(k-1))+s)
  else:
    A=f((d-s*(10**(k-1))+s)//10,k-2)
    
  if A=="Unko":
    return "Unko"
  A.appendleft(s)
  A.append(-s)

  return A
ans=0
for k in range(1,50):
  A=f(D,k)
  if A=="Unko":
    continue
 # print(A)
  res=1
  for j in range((k+1)//2):
    if j==0:
      res*=9-abs(A[j])
    else:
      if A[j]==0:
        res*=10
      else:
        res*=10-abs(A[j])
  ans+=res
print(ans)

Submission status: Accepted

Solution code:
D = int(input())

def table(i, k):
    if i == k:
        return list(range(9, -1, -1)) + [0]*9
    else:
        return list(range(10, 0, -1)) + list(range(1, 10))

def nine(i):
    return 10**i - 1

def rec(d, i, k):
    res = 0
    num  = table(i, k)
    if i == 1:
        for j in range(-9, 10):
            if 9*j == d:
                return num[j]
        return 0
    if i == 2:
        for j in range(-9, 10):
            if d == 99*j:
                return 10*num[j]
    if not -10*nine(i) <= d <= 10*nine(i):
        return 0
    for j in range(-9, 10):
        if d%10 == j*nine(i)%10:
            res += num[j] * rec((d-j*nine(i))//10, i-2, k)
    return res

l = 0
while D % 10 == 0:
    D//= 10
    l += 1

if l == 0:
    a = 1
else:
    a = 9*10**(l-1)

ans = 0
for i in range(1, 20):
    if not l:
        ans += rec(D, i, i)
    else:
        ans += rec(D, i, 100)
print(a * ans)

Submission status: Accepted

Solution code:
import math

diff = int(raw_input())

res = 0

for digits in range(1, 20):
	A = [0 for i in range(digits)]

	r = digits - 1
	l = 0

	ite = 0

	while l < r:
		A[ite] = 10**r - 10**l
		ite += 1
		l += 1
		r -= 1

	bonus = 1
	if l == r:
		bonus = 10

	ways_in_pos_0 = [0 for i in range(20)]
	ways_in_other = [0 for i in range(20)]

	for i in range(0, 10):
		for j in range(0, 10):
			ways_in_other[i - j] += 1
	for i in range(1, 10):
		for j in range(1, 10):
			ways_in_pos_0[i - j] += 1

	def recurse(pos, made):
		if pos == ite:
			if made == diff:
				return 1
			else:
				return 0
		elif pos == 0:
			curr = 0

			for i in range(-9, 9 + 1):
				if abs(made + i * A[pos] - diff) <= A[pos]:
					curr += recurse(pos + 1, made + i * A[pos]) * ways_in_pos_0[i]

			return curr
		else:
			curr = 0

			for i in range(-9, 9 + 1):
				if abs(made + i * A[pos] - diff) <= A[pos]:
					curr += recurse(pos + 1, made + i * A[pos]) * ways_in_other[i]

			return curr

	res += bonus * recurse(0, 0)

print res

Submission status: Accepted

Solution code:
def solve(d, K):
  r = 1
  for k in range(K,1,-2):
    if d >= 10**k:
      return 0
    t = (-d)%10
    d = abs((d-t*(10**(k-1)-1))//10)
    r *= 10-t-(K==k)
  return r*(10**(K%2)) if d == 0 else 0

D = int(input())
result = 0
l = len(str(D))
for k in range(l,2*l+1):
  result += solve(D, k)

print(result)

Submission status: Accepted

Solution code:
# (10-x) ways: (0,x)-(9-x,9) -> (x, -x)           <- This should never happens when D > 0
# (x) ways:    (10-x,0)-(9,x-1) -> (x-10, 10 - x)

def solve(d, k, outer):
  if k <= 1:
    return 10**k if d == 0 else 0
  # if d >= 10**k:
  #   return 0

  t = (-d)%10 # 10-d%10 doesn't work when d = 10
  d -= t*10**(k-1) - t
  return (10-t-outer)*solve(abs(d//10), k-2, 0)

D = int(input())
result = 0
for k in range(1,20):
  result += solve(D, k, 1)

print(result)

Submission status: Accepted

Solution code:
def result(letters, number, cant_zero):
    if letters <= 1:
        if number == 0:
            return 10 ** letters
        return 0
    diff = (10 - number%10) % 10
    variants = number % 10 - cant_zero
    if number % 10 == 0:
        variants = 10 - cant_zero
    number -= diff * (10**(letters-1) - 1)
    number = abs(number // 10)
    return variants * result(letters-2, number, 0)

d = int(input())
ans = 0
for i in range(20):
    ans += result(i, d, 1)
print(ans)

Submission status: Accepted

Solution code:
def solve(k, d, fl):
    if k <= 1:
        if d == 0:
            return 10 ** k
        return 0
    x = (-d) % 10
    c = 9 - x + 1 - fl
    d -= 10 ** (k - 1) * x - x
    return c * solve(k - 2, abs(d) // 10, 0)

d = int(input())

ans = 0
for i in range(1, 100):
    ans += solve(i, d, 1)
print(ans)

Submission status: Runtime Error

Solution code:
def f(d,x,y):n=int("9"*x+"0"*y);return d==0if x<1else sum(f(d+i*n,x-2,y+1)*(10-abs(i)-(y<1))for i in range(-9,10)if abs(d+i*n)<n)
D=input();print sum(f(D,i,0)*(10-i%2*9)for i in range(1,21))

Submission status: Runtime Error

Solution code:


# (10-x) ways: (0,x)-(9-x,9) -> (-x, x)
# (x) ways:    (10-x,0)-(9,x-1) -> (10-x, -10 + x)

def solve(D):

  leading_zeros = 0
  while D[-1] == 0:
    D.pop()
    leading_zeros += 1

  r = 1
  if len(D) >= 2:
    h,t = D.popleft(), D.pop()
    c = h + t

    if not D:
      r = t - (leading_zeros == 0) if c == 9 else 0
    elif 9 <= c <= 10:
      r = t - (leading_zeros == 0)
      c -= 10
      D[0] += c*10
      D[-1] += 1
      i = len(D)-1
      while i > 0 and D[i] == 10:
        D[i] = 0
        D[i-1] += 1
        i -= 1

  while len(D) > 2:
    h,t = D.popleft(), D.pop()
    c = h + t

    if -1 <= c <= 0:
      r *= 10-t
      D[0] += c*10
    elif 9 <= c <= 10:
      r *= t
      c -= 10
      D[0] += c*10
      D[-1] += 1
      i = len(D)-1
      while i > 0 and D[i] == 10:
        D[i] = 0
        D[i-1] += 1
        i -= 1
    else:
      r = 0
      D.clear()

  if len(D) == 2:
    h,t = D.popleft(), D.pop()
    c = h + t
    if c == 0:
      r *= 10 - t
    elif c == 9:
      r *= t
    else:
      r = 0

  if len(D) == 1:
    r *= 10 if D[0] == 0 else 0

  if r != 0 and leading_zeros > 0:
    r *= 9
    r *= 10**(leading_zeros-1)

  return r


from collections import deque
from random import randint


D = deque(map(int,input()))
r = solve(D.copy())

Q = D.copy()
for d in D:
  Q.appendleft(0)
  temp = solve(Q.copy())
  r += temp

if r == 0:
  Q = D.copy()
  for d in D:
    Q.append(0)
    r = solve(Q.copy())
    if r != 0:
      break

print(r)

Submission status: Runtime Error

Solution code:


# (10-x) ways: (0,x)-(9-x,9) -> (-x, x)
# (x) ways:    (10-x,0)-(9,x-1) -> (10-x, -10 + x)

def solve(D):
  leading_zeros = 0
  while D[-1] == 0:
    D.pop()
    leading_zeros += 1

  if len(D) >= 2:
    h,t = D.popleft(), D.pop()
    c = h + t

    if not D:
      r = t - (leading_zeros == 0) if c == 9 else 0
    elif 9 <= c <= 10:
      r = t - (leading_zeros == 0)
      c -= 10
      D[0] += c*10
      D[-1] += 1
      i = len(D)-1
      while i > 0 and D[i] == 10:
        D[i] = 0
        D[i-1] += 1
        i -= 1

  while len(D) > 2:
    h,t = D.popleft(), D.pop()
    c = h + t

    if -1 <= c <= 0:
      r *= 10-t
      D[0] += c*10
    elif 9 <= c <= 10:
      r *= t
      c -= 10
      D[0] += c*10
      D[-1] += 1
      i = len(D)-1
      while i > 0 and D[i] == 10:
        D[i] = 0
        D[i-1] += 1
        i -= 1
    else:
      r = 0
      D.clear()

  if len(D) == 2:
    h,t = D.popleft(), D.pop()
    c = h + t
    if c == 0:
      r *= 10 - t
    elif c == 9:
      r *= t
    else:
      r = 0

  if len(D) == 1:
    r *= 10 if D[0] == 0 else 0

  if r != 0 and leading_zeros > 0:
    r *= 9
    r = str(r)+'0'*(leading_zeros-1)

  return r


from collections import deque


D = deque(map(int,input()))

print(solve(D))

Submission status: Runtime Error

Solution code:


# (10-x) ways: (0,x)-(9-x,9) -> (-x, x)
# (x) ways:    (10-x,0)-(9,x-1) -> (10-x, -10 + x)

def solve(D):
  leading_zeros = 0
  while D[-1] == 0:
    D.pop()
    leading_zeros += 1

  if len(D) >= 2:
    h,t = D.popleft(), D.pop()
    c = h + t

    if not D:
      r = t - (leading_zeros == 0) if c == 9 else 0
    elif 9 <= c <= 10:
      r = t - (leading_zeros == 0)
      c -= 10
      D[0] += c*10
      D[-1] += 1
      i = len(D)-1
      while i > 0 and D[i] == 10:
        D[i] = 0
        D[i-1] += 1
        i -= 1

  while len(D) > 1:
    h,t = D.popleft(), D.pop()
    c = h + t

    if -1 <= c <= 0:
      r *= 10-t
      D[0] += c*10
    elif 9 <= c <= 10:
      r *= t
      c -= 10
      D[0] += c*10
      D[-1] += 1
      i = len(D)-1
      while i > 0 and D[i] == 10:
        D[i] = 0
        D[i-1] += 1
        i -= 1
    else:
      r = 0
      D.clear()

  if len(D) == 1:
    r *= 10 if D[0] == 0 else 0

  if r != 0 and leading_zeros > 0:
    r *= 9
    r = str(r)+'0'*(leading_zeros-1)

  return r


from collections import deque

D = deque(map(int,input()))
print(solve(D))

Submission status: Runtime Error

Solution code:
from collections import deque

D = deque(map(int,input()))

leading_zeros = 0
while D[-1] == 0:
  D.pop()
  leading_zeros += 1

# (10-x) ways: (0,x)-(9-x,9) -> (-x, x)
# (x) ways:    (10-x,0)-(9,x-1) -> (10-x, -10 + x)

def solve(D):
  r = 1
  while len(D) > 1:
    h,t = D.popleft(), D.pop()
    c = h + t

    if -1 <= c <= 0:
      r *= 10-t
      D[0] += c*10
    elif 9 <= c <= 10:
      r *= t
      c -= 10
      D[0] += c*10
      D[-1] += 1
      i = len(D)-1
      while i > 0 and D[i] == 10:
        D[i] = 0
        D[i-1] += 1
        i -= 1
    else:
      r = 0
      D.clear()

  if len(D) == 1:
    r *= 10 if D[0] == 0 else 0
  return r

r = 1

if len(D) >= 2:
  h,t = D.popleft(), D.pop()
  c = h + t

  if not D:
    r = t - (leading_zeros == 0) if c == 9 else 0
  elif 9 <= c <= 10:
    r = t - (leading_zeros == 0)
    c -= 10
    D[0] += c*10
    D[-1] += 1
    i = len(D)-1
    while i > 0 and D[i] == 10:
      D[i] = 0
      D[i-1] += 1
      i -= 1

r *= solve(D)

if leading_zeros > 0:
  r *= 9
  r = str(r)+'0'*(leading_zeros-1)

print(r)

Submission status: Runtime Error

Solution code:
from collections import deque

D = deque(map(int,input()))

leading_zeros = 0
while D[-1] == 0:
  D.pop()
  leading_zeros += 1

# (10-x) ways: (0,x)-(9-x,9) -> (-x, x)
# (x) ways:    (10-x,0)-(9,x-1) -> (10-x, -10 + x)

def solve(D):
  r = 1
  while len(D) > 1:
    h,t = D.popleft(), D.pop()
    c = h + t

    if -1 <= c <= 0:
      r *= 10-t
      D[0] += c*10
    elif 9 <= c <= 10:
      r *= t
      c -= 10
      D[0] += c*10
      D[-1] += 1
      i = -1
      while D[i] == 10:
        D[i] = 0
        D[i-1] += 1
        i -= 1
    else:
      r = 0
      D.clear()

  if len(D) == 1:
    r *= 10 if D[0] == 0 else 0
  return r

r = 1

if len(D) >= 2:
  h,t = D.popleft(), D.pop()
  c = h + t

  if not D:
    r = t - (leading_zeros == 0) if c == 9 else 0
  elif 9 <= c <= 10:
    r = t - (leading_zeros == 0)
    c -= 10
    D[0] += c*10
    D[-1] += 1
    i = -1
    while D[i] == 10:
      D[i] = 0
      D[i-1] += 1
      i -= 1

r *= solve(D)

if leading_zeros > 0:
  r *= 9
  r = str(r)+'0'*(leading_zeros-1)

print(r)

Submission status: Runtime Error

Solution code:

def rec(d, outer = False):
  head, tail = d[-1], d[0]
  if len(d) == 0:
    return 1
  if len(d) == 1:
    return 10-outer if tail == 0 else 0

  # (10-tail) ways: (0,tail)-(9-tail,9) -> (-tail, tail)
  # (tail) ways:    (10-tail,0)-(9,tail-1) -> (10-tail, -10 + tail)

  t = head + tail
  if len(d) == 2:
    if t == 0:
      return 10-tail
    if t == 9:
      return tail-outer
    else:
      return 0
    
  else:
    sub = d[1:-1]
    if -1 <= t <= 0:
      sub[-1] += t*10
      return (10-tail)*rec(sub)
    t -= 10
    if -1 <= t <= 0:
      sub[-1] += t*10
      sub[0] += 1
      return (tail-outer)*rec(sub)


D = list(map(int,input()))
D = D[::-1]

leading_zeros = 0
while D[leading_zeros] == 0:
  leading_zeros += 1
D = D[leading_zeros:]

r = rec(D, outer=True)
if r != 0 and leading_zeros > 0:
  r *= 9
  r = str(r)
  r += "0"*(leading_zeros-1)
print(r)

Submission status: Runtime Error

Solution code:

def rec(d, outer = False):
  head, tail = d[-1], d[0]
  if len(d) == 0:
    return 1
  if len(d) == 1:
    return 10-outer if tail == 0 else 0

  # (10-tail) ways: (0,tail)-(9-tail,9) -> (-tail, tail)
  # (tail) ways:    (10-tail,0)-(9,tail-1) -> (10-tail, -10 + tail)

  t = head + tail
  if len(d) == 2:
    if t == 0:
      return 10-tail
    if t == 9:
      return tail-outer
    else:
      return 0
    
  else:
    sub = d[1:-1]
    if -1 <= t <= 0:
      sub[-1] += t*10
      return (10-tail)*rec(sub)
    t -= 10
    if -1 <= t <= 0:
      sub[-1] += t*10
      sub[0] += 1
      return (tail-outer)*rec(sub)


D = list(map(int,input()))
D = D[::-1]

leading_zeros = 0
while D[0] == 0:
  leading_zeros += 1
D = D[leading_zeros:]

r = rec(D, outer=True)
if r != 0 and leading_zeros > 0:
  r *= 9 * (10**(leading_zeros-1))
print(r)

Submission status: Runtime Error

Solution code:
def recursion(length, ll, string, cc):
    if ll * 2 < length:
        for i in range(10):
            string1 = string + str(i)
            recursion(length, ll+1, string1, cc)
    else:
        number = str(int(string) + int(cc[-ll:]))
        ds = ""
        for i in range(length // 2):
            ds += number[-i-1]
        string = ds + string
        if str(int(string) + int(cc)) == string[::-1] and string[0] != "0":
            global answer
            answer += 1
            

d = input()
plus = 0
while True:
    if d[-1] == "0":
        d = d[:-1]
        plus += 1
    else:
        break
answer = 0
recursion(len(d), 0, "", d)
recursion(len(d)+1, 0, "", d)
while plus > 0:
    if plus == 1:
        plus -= 1
        answer = answer * 9
    else:
        plus -= 1
        answer *= 10
print(answer)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL sum = 0;
LL Pow[20];
int main(){
	LL d;
	Pow[1] = 1;
	for(int i = 2; i <= 19; i++) Pow[i] = Pow[i - 1] * 10;
	scanf("%lld", &d);
	LL s = d;
	for(int i = 2; i <= 19; i++){
		s = d;
		LL tmp = 1;
		int k = (i + 1) / 2;
		for(int j = i; j > k; j--){
			LL t = Pow[j] - Pow[i - j + 1];
			//cout << j << endl;
			if(s < t) tmp *= 9 + (i != j);
			else if(s >= t){
				LL kk = s / t;
				int less = 10 - kk - (i == j);
				if(less <= 0) break;
				tmp *= less;
				s -= kk * t;
			}
		}
		if(i % 2) tmp *= 10;
		if(s) tmp = 0;
		//cout << i << " " << s << " " << tmp << endl;
		sum += tmp;
	}
	cout << sum << endl;
}