Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 
Chef likes problems related to numbers a lot. He is generally quite good at solving these kinds of problems, but today he got stuck at one number theory problem, and so he asks your help to solve it.

Given a positive integer N, find out how many positive integers X satisfy the equation X + S(X) + S(S(X)) = N, where S(X) denoting sum of digits of X in decimal (base 10) representation.

------ Input ------ 

The only line of the input contains one integer number - N.

------ Output ------ 

Output single integer in a line denoting the count of number of positive integers X satisfying the above given equation.

------ Constraints ------ 

$1 ≤ N ≤ 10^{9}$

----- Sample Input 1 ------ 
6
----- Sample Output 1 ------ 
1
----- explanation 1 ------ 
Example 1. Only one positive integer X = 2 satisfies the equation X + S(X) + S(S(X)) = 6, as X + S(X) + S(S(X)) = 2 + 2 + 2 = 6.

----- Sample Input 2 ------ 
9939
----- Sample Output 2 ------ 
4
----- explanation 2 ------ 
Example 2.X can be 9898, 9907, 9910 and 9913. 
9898 + S(9898) + S(S(9898)) = 9898 + 34 + 7 = 9939
9907 + S(9907) + S(S(9907)) = 9907 + 25 + 7 = 9939
9910 + S(9910) + S(S(9910)) = 9910 + 19 + 10 = 9939
9913 + S(9913) + S(S(9913)) = 9913 + 22 + 4 = 9939
You can verify that there is not other positive value of X satisfying the given equation.

Source: codechef | Difficulty: MEDIUM
Tags: Number theory

Mathematics


Solutions:
def count(num):
	si = 0
	while num:
		si += num % 10
		num = num // 10
	return si
n = int(input())
cnt = 0
if n < 100:
	ra = 0
else:
	ra = n - 100
s = 0
for i in range(ra, n):
	fi = count(i)
	se = count(fi)
	s = i + fi + se
	if s == n:
		cnt += 1
	elif s < n:
		s = 0
print(cnt)


def sumDigit(n):
	return sum(list(map(int, str(n))))
n = int(input())
ans = 0
for i in range(1, 82):
	f = i
	s = sumDigit(i)
	rest = n - f - s
	if rest > 0:
		if sumDigit(rest) == i:
			ans += 1
print(ans)


def sum_of_digit(n):
	strr = str(n)
	list_of_number = list(map(int, strr.strip()))
	return sum(list_of_number)
n = int(input())
count = 0
for i in range(max(0, n - 100), n + 1):
	k1 = sum_of_digit(i)
	k2 = sum_of_digit(k1)
	k = i + k1 + k2
	if k == n:
		count += 1
print(count)


def fsum(a):
	ans = 0
	for r in a:
		ans += int(r)
	return ans
n = int(input())
fans = 0
for r in range(n, max(0, n - 160), -1):
	p = fsum(str(r))
	q = fsum(str(p))
	if r + p + q == n:
		fans += 1
print(fans)


def f(h):
	ans = 0
	while h:
		ans += h % 10
		h = h // 10
	return ans
n = int(input())
ans = 0
for i in range(max(0, n - 100), n + 1):
	if f(i) + i + f(f(i)) == n:
		ans += 1
print(ans)


def s(n):
	res = 0
	while n > 0:
		res += n % 10
		n //= 10
	return res
n = int(input())
val = max(0, n - 97)
res = 0
for i in range(val, n + 1):
	if i + s(i) + s(s(i)) == n:
		res += 1
print(res)


def cal_sum(num):
	total = 0
	while num > 0:
		total += num % 10
		num //= 10
	return total
num = int(input())
maximum = num - 97
temp = num
ans = 0
while temp > 0 and temp >= maximum:
	sx = cal_sum(temp)
	ssx = cal_sum(sx)
	if temp + sx + ssx == num:
		ans += 1
	temp -= 1
print(ans)


def cal_sum(num, n=1):
	if n == 3:
		return 0
	total = 0
	while num > 0:
		total += num % 10
		num //= 10
	return total + cal_sum(total, n + 1)
num = int(input())
maximum = num - 97
temp = num
ans = 0
while temp > 0 and temp >= maximum:
	if temp + cal_sum(temp) == num:
		ans += 1
	temp -= 1
print(ans)


def solve(x):
	s = 0
	while x > 0:
		s += x % 10
		x //= 10
	return s
n = int(input())
res = 0
for i in range(max(0, n - 97), n + 1):
	sx = solve(i)
	ssx = solve(sx)
	if i + sx + ssx == n:
		res += 1
print(res)


def solve(x):
	sume = 0
	while x > 0:
		sume += x % 10
		x //= 10
	return sume
n = int(input())
ans = 0
for i in range(max(0, n - 97), n + 1):
	sx = solve(i)
	ssx = solve(sx)
	if i + sx + ssx == n:
		ans += 1
print(ans)


n = int(input())
count = 0
k = max(n // 3, n - 90)
for i in range(k, n):
	a = i
	b = sum([int(j) for j in str(a)])
	c = sum([int(k) for k in str(b)])
	if a + b + c == n:
		count += 1
print(count)


def sumdig(x):
	s = 0
	while x > 0:
		s += x % 10
		x = x // 10
	return s
n = int(input())
count = 0
for i in range(max(0, n - 97), n + 1):
	a = sumdig(i)
	b = sumdig(a)
	res = i + a + b
	if res == n:
		count += 1
print(count)


def get_sum(n):
	sum = 0
	while n > 0:
		sum += n % 10
		n //= 10
	return sum
n = int(input())
ans = 0
for X in range(max(0, n - 97), n + 1):
	S_X = get_sum(X)
	S_S_X = get_sum(S_X)
	if X + S_X + S_S_X == n:
		ans += 1
print(ans)


n = int(input())
count = 0
for i in range(100):
	a = n - i
	ans = 0
	if a < 0:
		break
	ans += a
	s = 0
	while a > 0:
		s += a % 10
		a = a // 10
	ans += s
	m = 0
	while s > 0:
		m += s % 10
		s = s // 10
	ans += m
	if ans == n:
		count += 1
print(count)


n = int(input())
z = 0
for i in range(100):
	res = 0
	a = n - i
	if a <= 0:
		break
	res += a
	s = 0
	while a > 0:
		s += a % 10
		a //= 10
	res += s
	while s > 0:
		res += s % 10
		s //= 10
	if res == n:
		z += 1
print(z)


n = int(input())

def snek(d):
	s = 0
	for i in str(d):
		s += int(i)
	return s
c = 0
if n - 100 < 0:
	x = 1
else:
	x = n - 100
for i in range(x, n):
	if i + snek(i) + snek(snek(i)) == n:
		c += 1
print(c)


n = int(input())

def g(num):
	s = 0
	for i in str(num):
		s += int(i)
	return s
d = 0
x = 1 if n - 100 < 0 else n - 100
for i in range(x, n):
	if i + g(i) + g(g(i)) == n:
		d += 1
print(d)


def digitsum(n):
	sum = 0
	while n > 0:
		sum += n % 10
		n = n // 10
	return sum
n = int(input())
a = 0
x = 1 if n - 100 < 0 else n - 100
for i in range(x, n):
	if i + digitsum(i) + digitsum(digitsum(i)) == n:
		a += 1
print(a)


n = int(input())
st = str(n)
l = len(st)
z = l * 18 - 1
count = 0
z = n - z
if z <= 0:
	z = 1
for j in range(z, n + 1):
	s = str(j)
	sums = j
	d = 0
	for i in range(0, len(s)):
		d = d + int(s[i])
	sums = sums + d
	s = str(d)
	d = 0
	for i in range(0, len(s)):
		d = d + int(s[i])
	sums = sums + d
	if sums == n:
		count = count + 1
print(count)


def sum_digits(n):
	sum_ = 0
	while n:
		sum_ += n % 10
		n //= 10
	return sum_
n = int(input())
(cnt, temp) = (0, n - 100)
start = 1 if temp <= 0 else temp
for i in range(start, n + 1):
	if i + sum_digits(i) + sum_digits(sum_digits(i)) == n:
		cnt += 1
print(cnt)


def tosum(s):
	tot = 0
	l = list(s)
	for i in l:
		tot = tot + int(i)
	return tot
n = int(input())
tot = 0
for i in range(n - 97, n + 1):
	if i >= 0:
		i1 = tosum(str(i))
		i2 = tosum(str(i1))
		if i + i1 + i2 == n:
			tot = tot + 1
print(tot)


def s(num):
	c = 0
	while num > 0:
		c = c + num % 10
		num = int(num / 10)
	return c
N = int(input())
c = 0
t = max(1, N - 100)
for i in range(t, N + 1):
	s1 = s(i)
	s2 = s(s1)
	if N == i + s1 + s2:
		c = c + 1
print(c)


def getSum(n):
	k = list(map(int, str(n)))
	sum = 0
	for i in k:
		sum = sum + i
	return sum
n = int(input())
if n - 97 < 0:
	c = 0
	for i in range(n + 1):
		temp = getSum(i)
		if i + temp + getSum(temp) == n:
			c += 1
else:
	c = 0
	for i in range(n - 97, n + 1):
		temp = getSum(i)
		if i + temp + getSum(temp) == n:
			c += 1
print(c)


n = int(input())
ans = 0

def S(x):
	t = str(x)
	return sum([int(i) for i in t])
for j in range(n - 100, n + 1):
	if j < 0:
		continue
	elif j + S(j) + S(S(j)) == n:
		ans += 1
print(ans)


def s(n):
	return sum([int(i) for i in str(n)])
n = int(input())
print(len([i for i in range(n, max(n - 200, -1), -1) if i + s(i) + s(s(i)) == n]))


def s(n):
	return sum([int(i) for i in str(n)])
for _ in range(1):
	n = int(input())
	a = 0
	for i in range(n, n - 200, -1):
		if i < 1:
			break
		if i + s(i) + s(s(i)) == n:
			a += 1
	print(a)


def dig(a):
	ans = 0
	while int(a) > 0:
		ans = ans + 1
		a /= 10
	return ans

def sum(a):
	ans = 0
	while int(a) > 0:
		ans = ans + a % 10
		a = int(a / 10)
	return ans
n = int(input())
i = n - (dig(n) * 9 + sum(dig(n)) * 9)
res = c = 0
while i < n:
	res = i + sum(i) + sum(sum(i))
	if res == n:
		c += 1
	i += 1
print(c)


n = int(input())
k = 9 * len(str(n))
ans = 0
for i in range(k, -1, -1):
	(temp1, s1, s2) = (i, 0, 0)
	while temp1 > 0:
		s1 += temp1 % 10
		temp1 //= 10
	temp2 = n - s1 - i
	while temp2 > 0:
		s2 += temp2 % 10
		temp2 //= 10
	if s2 == i:
		ans += 1
print(ans)


def tot(n, count):
	if count >= 2:
		return 0
	temp = 0
	while n > 0:
		temp += n % 10
		n //= 10
	return temp + tot(temp, count + 1)
n = int(input())
count = 0
if n < 130:
	for i in range(1, n):
		if i + tot(i, 0) == n:
			count += 1
else:
	for i in range(n - 120, n):
		if i + tot(i, 0) == n:
			count += 1
print(count)


def sumx(n):
	the_sum = 0
	for ch in str(n):
		the_sum += int(ch)
	return the_sum
N = int(input())
tests = []
for i in range(N - 100, N):
	if i > 0:
		tests.append(i)
count = 0
for num in tests:
	if num + sumx(num) + sumx(sumx(num)) == N:
		count += 1
print(count)


def s(n):
	sum1 = 0
	if n % 10 == n:
		return n
	while n > 0:
		sum1 += n % 10
		n //= 10
	return sum1
n = int(input())
count = 0
if n < 11:
	for i in range(1, n):
		a = s(i)
		if i + a + s(i) == n:
			count += 1
	print(count)
else:
	for i in range(1, 100):
		a = n - i
		if n - i < 0:
			break
		b = s(n - i)
		if a + b + s(b) == n:
			count += 1
	print(count)


def dig(n):
	sum = 0
	while n > 0:
		sum += n % 10
		n = n // 10
	return sum
n = int(input())
l = n - 97
if l <= 0:
	l = 1
c = 0
for i in range(l, n + 1):
	v = i + dig(i) + dig(dig(i))
	if v == n:
		c += 1
print(c)


def sumX(x):
	sum1 = 0
	if x % 10 == x:
		return x
	while x > 0:
		sum1 += x % 10
		x //= 10
	return sum1
n = int(input())
count = 0
if n < 10:
	for i in range(1, n):
		s = sumX(i)
		sum1 = i + s + sumX(s)
		if sum1 == n:
			count += 1
else:
	for i in range(1, 100):
		t = n - i
		s1 = sumX(t)
		s2 = sumX(s1)
		if t + s1 + s2 == n:
			count += 1
print(count)


def s(t):
	ans = 0
	while t > 9:
		ans += t % 10
		t = t // 10
	ans += t
	return ans
n = int(input())
if n < 10:
	print(1)
else:
	c = 0
	for i in range(100):
		t = n - i
		s1 = s(t)
		s2 = s(s1)
		if t + s1 + s2 == n:
			c += 1
	print(c)


def sumDigit(n):
	tot = 0
	while n > 0:
		dig = n % 10
		tot = tot + dig
		n = n // 10
	return tot
N = int(input())
if N < 10:
	print(1)
else:
	count = 0
	for i in range(N - 9 * len(str(N)) - 9 * len(str(9 * len(str(N)))), N + 9 * len(str(N)) + 9 * len(str(9 * len(str(N)))) + 1):
		z = sumDigit(i)
		k = sumDigit(z)
		if i + z + k == N:
			count += 1
	print(count)


def sum_of_dig(i):
	l = list(map(int, list(str(i))))
	return sum(l)
n = int(input())
c = 0
for i in range(n - 97, n):
	if i > 0:
		if i + sum_of_dig(i) + sum_of_dig(sum_of_dig(i)) == n:
			c += 1
print(c)


def sum_dig(x):
	x = str(x)
	return sum([int(x) for x in x])
n = int(input())
cnt = 0
for i in range(n - 97, n):
	if i > 0:
		if i + sum_dig(i) + sum_dig(sum_dig(i)) == n:
			cnt += 1
print(cnt)


n = int(input())
count = 0

def sumse(x):
	sums = 0
	while x:
		sums += x % 10
		x = x // 10
	return sums
for i in range(max(0, n - 1000), n + 1):
	res = i + sumse(i) + sumse(sumse(i))
	if res == n:
		count += 1
print(count)


def sum_dig(n):
	ans = 0
	while n:
		ans += n % 10
		n = n // 10
	return ans
n = int(input())
count = 0
for i in range(100):
	temp = n - i
	if temp < 0:
		break
	temp1 = sum_dig(temp)
	temp2 = sum_dig(temp1)
	if temp + temp1 + temp2 == n:
		count += 1
print(count)


def conv(n):
	tot = 0
	while n > 0:
		dig = n % 10
		tot = tot + dig
		n = n // 10
	return tot
n = int(input())
length = len(str(n))
first = int(str(n)[0])
begin = n - 2 * (first + (length - 1) * 9)
count = 0
for num in range(begin, n):
	a = num
	b = conv(num)
	c = conv(b)
	if a + b + c == n:
		count += 1
print(count)


def sum(N):
	sum1 = 0
	while N != 0:
		sum1 += N % 10
		N = N // 10
	return sum1
N = int(input())
c = 0
for i in range(max(0, N - 1000), N + 1):
	sum1 = i + sum(i) + sum(sum(i))
	if sum1 == N:
		c += 1
print(c)


n = int(input())

def calc(r1, n):
	res = 0
	if r1 < 0:
		r1 = 1
	for i in range(r1, n):
		s = 0
		d = 0
		for k in str(i):
			s = s + int(k)
		for j in str(s):
			d = d + int(j)
		if i + s + d == n:
			res += 1
	return res

def rng(n):
	b = 9
	while n > 10:
		b += 9
		n = int(n / 10)
	return b

def func(n):
	l1 = rng(n)
	l2 = rng(l1)
	res = calc(n - l1 - l2, n)
	return res
print(func(n))


n = int(input())

def sum(a):
	c = 0
	i = 0
	while i < a:
		c += a % 10
		a = int(a / 10)
	return c

def limit(n):
	b = 9
	while n > 10:
		b += 9
		n = int(n / 10)
	return b

def temp(n):
	c1 = 0
	b1 = limit(n)
	b2 = limit(b1)
	up = n - b2 - b1
	if up < 0:
		up = 0
	for i in range(up, n):
		a1 = sum(i)
		a2 = sum(a1)
		if i + a1 + a2 == n:
			c1 += 1
	return c1
print(temp(n))


def sum(n):
	res = 0
	while n != 0:
		res += n % 10
		n = n // 10
	return res
n = int(input())
c = 0
for i in range(max(0, n - 1000), n + 1):
	res = i + sum(i) + sum(sum(i))
	if res == n:
		c += 1
print(c)


def SoD(n):
	return sum((int(x) for x in str(n)))
a = int(input())
ans = 0
for i in range(max(1, a - 1000), a):
	b = SoD(i)
	c = SoD(b)
	if i + b + c == a:
		ans += 1
print(ans)


def aman(p):
	sum = 0
	while p != 0:
		sum = sum + int(p % 10)
		p = int(p / 10)
	return sum
n = int(input())
k = (1 + len(str(n))) * 9
e = 0
for i in range(max(0, n - 100), n + 1):
	z = i + aman(i) + aman(aman(i))
	if z == n:
		e += 1
print(e)


k = 0
a = int(input())
for i in range(max(0, a - 100), a):
	n = str(i)
	b = list(n)
	b[:] = map(lambda d: int(d), b)
	p = sum(b)
	f = str(p)
	f = list(f)
	f[:] = map(lambda s: int(s), f)
	z = sum(f)
	if i + z + p == a:
		k += 1
print(k)


try:
	n = int(input())
	l = []
	q = 0
	if n > 100:
		v = n - 100
	else:
		v = 0
	for i in range(v, n):
		u = i
		w = i
		e = i
		for j in range(2):
			s = 0
			f = abs(w)
			while f != 0:
				s = s + f % 10
				f = f // 10
			e = e + s
			w = s
		l = l + [u]
		if e == n and l.count(u) == 1:
			q = q + 1
	print(q)
except:
	pass


def Sum_of_digits(string):
	ans = 0
	for i in range(len(string)):
		ans += int(string[i])
	return ans
N = int(input())
count = 0
for i in range(max(0, N - 150), N):
	if i + Sum_of_digits(str(i)) + Sum_of_digits(str(Sum_of_digits(str(i)))) == N:
		count += 1
print(count)


a = int(input())
w = a
count = 0
le = (1 + len(str(w))) * 9

def get_sum(w):
	if w == 0:
		return 0
	elif w < 10:
		return w
	elif w % 10 == 0:
		return int(get_sum(w / 10))
	else:
		return get_sum(w % 10) + get_sum(w - w % 10)
for i in range(w - le, w - 1):
	if i + get_sum(i) + get_sum(get_sum(i)) == w:
		count += 1
print(count)


def cheffed(n):

	def sum_of_digits(n):
		s = 0
		while n:
			s += n % 10
			n //= 10
		return s
	count = 0
	for i in range(1, 81):
		ssx = sum_of_digits(i)
		sx = i
		if sx + ssx >= n:
			continue
		x = n - sx - ssx
		if sum_of_digits(x) == sx:
			count += 1
	return count
n = int(input())
print(cheffed(n))


n = int(input())
le = (1 + len(str(n))) * 9

def getsums(q):
	su = 0
	while q != 0:
		su = su + int(q % 10)
		q = int(q / 10)
	return su
c = 0
for i in range(n - le, n - 1):
	if i + getsums(i) + getsums(getsums(i)) == n:
		c += 1
print(c)


n = int(input())

def getsums(q):
	su = 0
	f = 0
	while q != 0:
		su = su + int(q % 10)
		q = int(q / 10)
	f += su
	while su != 0:
		f = f + int(su % 10)
		su = int(su / 10)
	return f
c = 0
for i in range(n - 97, n - 1):
	if i + getsums(i) == n:
		c += 1
print(c)


n = int(input())
start = n - 97
end = n
if start < 0:
	start = 0
total = 0
for i in range(start, end + 1):
	count = i
	s = str(i)
	lis = []
	for j in s:
		lis.append(int(j))
	ans = sum(lis)
	count += ans
	ans = str(ans)
	lis = []
	for j in ans:
		lis.append(int(j))
	ans1 = sum(lis)
	count += ans1
	if count == n:
		total += 1
print(total)


a = int(input())
t = 0
if a >= 90:
	t = a - 90
q = 0

def s(i):
	x = 0
	for j in str(i):
		x = x + int(j)
	return x
for i in range(t, a):
	if i + s(i) + s(s(i)) == a:
		q = q + 1
print(q)


n = int(input())
c = 0
tem = n - 90 if n - 90 >= 0 else 0

def s(x):
	l = [int(j) for j in str(x)]
	return sum(l)
for i in range(tem, n - 1):
	if i + s(i) + s(s(i)) == n:
		c += 1
print(c)