Problem:
 Score : 400 points    Problem Statement  Given is a positive integer N . Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:   When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B , and the first digit of A is equal to the last digit of B .       Constraints   1 \leq N \leq 2 \times 10^5  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N       Output  Print the answer.        Sample Input 1 25      Sample Output 1 17  The following 17 pairs satisfy the condition: (1,1) , (1,11) , (2,2) , (2,22) , (3,3) , (4,4) , (5,5) , (6,6) , (7,7) , (8,8) , (9,9) , (11,1) , (11,11) , (12,21) , (21,12) , (22,2) , and (22,22) .       Sample Input 2 1      Sample Output 2 1       Sample Input 3 100      Sample Output 3 108       Sample Input 4 2020      Sample Output 4 40812       Sample Input 5 200000      Sample Output 5 400000008

Solution (Python):
n = int(input())

a_list = [ [ 0 for j in range(9) ] for i in range(9) ]
b_list = [ [ 0 for j in range(9) ] for i in range(9) ]
for i in range(1,n+1):
    a = i // ( 10 ** (len(str(i)) - 1 ) )
    b = i % 10

    if b != 0:
        if (a != b):
            a_list[a-1][b-1] += 1
        else:
            a_list[a-1][b-1] += 1

for i in range(9):
    for j in range(9):
        if i == j:
            b_list[i][j] = a_list[i][j]**2
        else:
            b_list[i][j] = (a_list[i][j] * a_list[j][i]) 
        
ans = 0

for i in b_list:
    ans += sum(i)
print(ans)

Solution (Python):





def f(N, a, b):
    s = 0
    d = 1

    if a == b and a <= N:
        s += 1

    N0 = N

    N //= 10
    while(N > a):
        s += d
        d *= 10
        N //= 10
    
    if N == a:
        s += (N0 - d*a*10 - b)//10 + 1

    return s


N = int(input())

p = [[0]*10 for _ in range(10)]

for a in range(1, 10):
    for b in range(1, 10):
        p[a][b] = f(N, a, b)

s = 0
for a in range(1, 10):
    for b in range(1, 10):
        s += p[a][b]*p[b][a]

print(s)

Solution (Python):
import numpy as np
def count(a,b):
    if ln_n > 1:
        ret = int('1' * (ln_n-1))
    else:
        ret = 0
    
    if a == b and n >= a:
        ret += 1
    
    if ln_n >= 1:
        if a < n_top:
            ret += 10**(ln_n-1)
        elif a == n_top:
            ret += max(n_inner, 0)
            if b <= n_bottom:
                ret += 1
    return ret

n, = [list(map(int, s.split())) for s in open(0)][0]
ln_n = int(np.log10(n))
n_top = n // (10**ln_n)
n_bottom = n % 10
n_inner = 0
if ln_n > 1:
    n_inner = int(str(n)[1:-1])

ans = 0
for i in range(1,10):
    for j in range(1,10):
        tmp = count(i,j) * count(j,i)
        ans += tmp
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    str_N=str(N)
    ans=0
    ans2=0
    for i in range(1,N+1):
        s=str(i)
        if s[-1]=='0':
            continue
        if s[0]==s[-1]:
            ans+=1
        if int(s[-1]+s[0])<=N:
            ans+=1
        f=True
        n=1
        while f:
            z=int(s[-1]+str(10**n-1)+s[0])
            if z<=N:
                ans+=10**n
            else:
                if int(s[-1]+str('0'*n)+s[0])>N:
                    pass
                else:
                    m=int(str_N[1:-1])
                    ans+=m
                    if int(str_N[-1])>=int(s[0]):
                        ans+=1
                f=False
                break
            n+=1
    print(ans-ans2)
                

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
if n <= 9:
    print(n)
else:
    y = 9
    z = 9
    for i in range(11,n+1):
        p = str(i)
        if p[0] == p[len(p)-1]:
            y += 2
            z += 1
            if len(p) >= 4:
                for j in range(3, len(p)):
                    y += 10 ** (j-2)
            if len(p) >= 3:
                y += int(p[1:len(p)-1])+1
        elif int(p[0]) > int(p[len(p)-1]) > 0:
            for j in range(2, len(p)+1):
                y += 10 ** (j-2)
        elif len(p) >= 3 and not i%10 == 0: 
            for j in range(2, len(p)):
                y += 10 ** (j-2)
    print(2*y-z)

Solution (Python):
def count(x, y, N):
    l = len(str(N)) 
    a = int(str(N)[0]) 
    b = int(str(N)[-1]) 

    if l == 1:
        if x == y and a >= x:
            return 1
        else:
            return 0
    elif l == 2:
        res = 0
        if x == y:
            res += 1
        if a >= x and b >= y:
            res+=1
        return res
    elif l >=3:
        res = 0
        if x == y:
            res += 1 
        res += 1 
        for i in range(1, l - 2): 
            res += 10 ** i
        if a > x:
            res += 10 ** (l - 2) 
        elif a == x and b >= y:
            res += int(str(N)[1:-1]) + 1
        elif a == x and b < y:
            res += int(str(N)[1:-1])
        return res

N = int(input())
ans = 0
for x in range(1, 10):
    for y in range(1, 10):
        
        ans += count(x, y, N) * count (y, x, N)

print(ans)

Solution (Python):
def count(x, y, N):
    l = len(str(N)) 
    a = int(str(N)[0]) 
    b = int(str(N)[-1]) 

    if l == 1:
        if x == y and a >= x:
            return 1
        else:
            return 0
    elif l == 2:
        res = 0
        if x == y:
            res += 1
        if a >= x and b >= y:
            res+=1
        return res
    elif l >=3:
        res = 0
        if x == y:
            res += 1 
        res += 1 
        for i in range(1, l - 2): 
            res += 10 ** i
        if a > x:
            res += 10 ** (l - 2) 
        elif a == x and b >= y:
            res += int(str(N)[1:-1]) + 1
        elif a == x and b < y:
            res += int(str(N)[1:-1])
        return res

N = int(input())
ans = 0
for x in range(1, 10):
    for y in range(1, 10):
        
        ans += count(x, y, N) * count (y, x, N)

print(ans)

Solution (Python):
from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 7)


@lru_cache(maxsize=None)
def check(l, r, n, k=1):
    t = l + '0' * k + r
    e = l + '9' * k + r

    if int(e) <= n:
        return pow(10, k) + check(l, r, n, k + 1)

    if int(t) <= n < int(e):
        x = int(str(n)[1:-1])
        if int(str(n)[-1]) < int(r):
            x -= 1
        return x + 1

    if n < int(t):
        return 0


def main():
    n = int(input())

    ret = 0
    for x in range(1, n + 1):
        if x % 10 == 0:
            continue
        s = str(x)
        t = s[-1] + s[0]
        if s[0] == s[-1]:
            ret += 1
        if int(t) <= n:
            ret += 1
        ret += check(s[-1], s[0], n, k=1)

    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
ls=len(str(n))
for ai in range(1,10):
    for bi in range(1,10):
        
        p=q=0
        if ai==bi:
            if n>=ai: q+=1
            for i in range(ls-2):
                q+=10**i
            if int(str(n)[0])==ai:
                if (n-ai*(10**(ls-1))-ai)>=0:
                    q+=(n-ai*(10**(ls-1))-ai)//10+1
            elif int(str(n)[0])>ai:
                q+=10**(ls-2)
            count+=q**2
            continue
        for i in range(ls-2):
            p+=10**i; q=p
        if int(str(n)[0])==ai:
            if (n-ai*(10**(ls-1))-bi)>=0:
                p+=(n-ai*(10**(ls-1))-bi)//10+1
        elif int(str(n)[0])>ai:
            p+=10**(ls-2)
        if int(str(n)[0])==bi:
            if (n-bi*(10**(ls-1))-ai)>=0:
                q+=(n-bi*(10**(ls-1))-ai)//10+1
        elif int(str(n)[0])>bi:
            q+=10**(ls-2)
        count+=p*q
print(count)

Solution (Python):
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

N=II()
cnt = 0
for i in range(1,10):
    for i_keta in range(6):
        for j in range(1,10):
            for j_keta in range(6):
                A = i if i_keta == 0 else int(str(i) + ("0"*(i_keta-1)) + str(j))
                B = j if j_keta == 0 else int(str(j) + ("0"*(j_keta-1)) + str(i))
                if A <= N and B <=N:
                    if i_keta == 0 or j_keta == 0:

                        patA = 0
                        if i_keta:
                            if (i+1)*(10**i_keta) <= N:
                                patA = 10**(i_keta-1)
                            else:
                                for k in range(10000):
                                    if A+k*10 <= N:
                                        patA += 1
                                    else:
                                        break
                        else:
                            patA=1

                        patB = 0
                        if j_keta:
                            if (j+1)*(10**j_keta) <= N:
                                patB = 10**(j_keta-1)
                            else:
                                for k in range(10000):
                                    if B+k*10 <= N:
                                        patB += 1
                                    else:
                                        break
                        else:
                            patB = 1


                        if i == j:
                            
                            cnt += patA*patB
                    else:
                        patA = 0
                        if (i+1)*(10**i_keta) <= N:
                            patA = 10**(i_keta-1)
                        else:
                            for k in range(10000):
                                if A+k*10 <= N:
                                    patA += 1
                                else:
                                    break

                        patB = 0
                        if (j+1)*(10**j_keta) <= N:
                            patB = 10**(j_keta-1)
                        else:
                            for k in range(10000):
                                if B+k*10 <= N:
                                    patB += 1
                                else:
                                    break
                        cnt += patA*patB
                        

print(cnt)

Solution (Python):
N = int(input())
S = str(N)
D = len(S)
M = N%10
SE = N//(10**(D-1))
ans = 0
for i in range(1,N+1):
    x = i
    t = str(x)
    d = len(t)
    m = x%10
    s = x//(10**(d-1))
    if m == 0:
        continue
    for j in range(1,D+1):
        if j == 1:
            if s == m:
                ans += 1
        elif 2 <= j <= D-1:
            ans += 10**(j-2)
        else:
            if SE > m:
                ans += 10**(j-2)
            elif SE == m:
                p = N%(10**(D-1))
                p = p//10
                ans += p+1
                if M < s:
                    ans -= 1
print(ans)

Solution (Python):
def run():
  N = int(input())
  ans = 0
  di = {}
  for a in range(1, 10):
    for b in range(1, 10):
      key = str(a)+str(b)
      di[key] = 0
  for n in range(1, N+1):
    if n%10 == 0:
      continue
    n = str(n)
    start, end = n[0], n[-1]
    di[start+end] += 1
    if start == end:
      ans += max(0, di[end+start]*2-1)
    else:
      ans += di[end+start]*2
  print(ans)
    

if __name__ == '__main__':
  run()

Solution (Python):
N = int(input())
len_N = len(str(N))

memo = {}

if str(N)[1:len_N-1]:
    center_val = int(str(N)[1:len_N-1])
else:
    center_val = 0
for a in range(1, 10):
    for b in range(1, 10):
        count = 0
        if a == b and a <= N:
            count += 1
        if len_N >= 2 and 10*a + b <= N:
            count += 1
        if len_N >= 3:
            for i in range(3, len_N):
                count += 10**(i-2)
            if a < int(str(N)[0]):
                count += 10**(len_N-2)
            elif a == int(str(N)[0]):
                if b <= int(str(N)[-1]):
                    count += center_val + 1
                else:
                    count += center_val
        memo[(a, b)] = count


ans = 0
for A in range(1, N+1):
    A1 = str(A)[0]
    A2 = str(A)[-1]
    if A1 == "0" or A2 == "0":
        continue
    ans += memo[(int(A2), int(A1))]
    
print(ans)

Solution (Python):
N = int(input())

import numpy as np

X = np.arange(N+1)
A = X.copy()

for i in [1,10,100,1000,10000,100000]:
    A[i:i*10] = X[i:i*10] // i
B = X % 10

C = np.zeros((10,10), dtype=int)

for i in range(1,N+1):
    C[A[i], B[i]] += 1
count = 0

for i in range(10):
    for j in range(10):
        count += C[i][j] * C[j][i]

print(count)

Solution (Python):
N = int(input())
d = len(str(N))
if d == 1:
    print(N)
    exit()
x = int(str(N)[0])
y = int(str(N)[-1])
if d == 2:
    M = 0
else:
    M = int(str(N)[1:-1])
count = 0
for A in range(1, N + 1):
    count0 = 0
    a = int(str(A)[0])
    b = int(str(A)[-1])
    if b == 0:
        continue
    if a == b:
        count0 += 1
    count0 += (10 ** (d - 2) - 1) // 9
    if b < x:
        count0 += (10 ** (d - 2))
    if b == x:
        count0 += M
        if a <= y:
            count0 += 1
    count += count0
print(count)

Solution (Python):
N = int(input())

ans = 0
ten_lst = [(10 ** i - 1) // 9 for i in range(1, 7)]
for i in range(1, N + 1):
    str_i = str(i)
    l = len(str_i)
    if str_i[-1] == '0':
        continue
    elif str_i[0] == str_i[-1]:
        
        if l == 1:
            ans += 1
        elif l == 2:
            ans += 3
        else:
            ans += 3 + 2 * (ten_lst[l - 3] + int(str_i[1:-1]))
    elif int(str_i[0]) > int(str_i[-1]):
        
        ans += 2 * ten_lst[l - 2]
    elif l >= 3:
        
        ans += 2 * ten_lst[l - 3]
print(ans)

Solution (Python):
N=int(input())
nf=int(str(N)[0])
nl=len(str(N))
if nl==1:
  print(N)
  exit()

ans=0
for i in range(1,N+1):
  a=i
  f=int(str(a)[0])
  l=int(str(a)[-1])
  if l!=0:
    if l<nf: 
      for j in range(2,nl+1):
        ans+=10**(j-2)
      if f==l:
        ans+=1
    elif l>nf:
      if nl-3>=0:
        for j in range(3,nl+1):
          ans+=10**(j-3)
        if f==l:
          ans+=1
      else:
        if f==l:
          ans+=1
    else:
      if int(str(N)[-1])>=f:
        if nl>=3:
          ans+=int("".join(str(N)[1:-1]))+1
          for j in range(3,nl+1):
            ans+=10**(j-3)
          if f==l:
            ans+=1
        else:
          ans+=1
          if f==l:
            ans+=1
      else:
        if nl>=3:
          ans+=int("".join(str(N)[1:-1]))
          for j in range(3,nl+1):
            ans+=10**(j-3)
          if f==l:
            ans+=1
print(ans)

Solution (Python):
N = int(input())
l = [[0 for i in range(10)] for i in range(10)]
for i in range(1, N+1):
    l[int(str(i)[0])][int(str(i)[-1])] += 1
ans = 0
for i in range(1, 10):
    for j in range(i, 10):
        if i == j:
            ans += l[i][j] * l[j][i]
        else:
            ans += l[i][j] * l[j][i] * 2
print(ans)

Solution (Python):
n = int(input())
n_digit = len(str(n))
count = 0
for i in range(1, 10):
    for j in range(1, 10):
        
        
        a_count = 0
        b_count = 0
        for k in range(j, n+1, 10):
            k_str = str(k)
            if(i == int(k_str[0])):
                a_count += 1
        for k in range(i, n+1, 10):
            k_str = str(k)
            if(j == int(k_str[0])):
                b_count += 1
        count += a_count * b_count
print(count)

Solution (Python):
n=int(input())
L=[[0]*9 for i in range(9)]

for i in range(1,n+1):
    a=int(str(i)[0])
    b=int(str(i)[-1])
    if a!=0 and b!=0:
        L[a-1][b-1]+=1

r=0
for j in range(9):
    for k in range(9):
        if j==k:
            r+=L[j][j]*L[j][j]
        else:
            r+=L[j][k]*L[k][j]
print(r)

Solution (Python):
N = int(input())


if N <= 10:
    print(N)
    exit()

dic = {}
for i in range(11,100):
    if i % 10 == 0:
        continue
    if i % 11 == 0:
        dic[i] = 1
    else:
        dic[i] = 0

for i in range(11, N+1):
    if i % 10 == 0:
        continue
    tmp = str(i)[0] + str(i)[-1]
    dic[int(tmp)] += 1

res = 0
for current, num in dic.items():
    if current % 11 == 0:
        res += num * num
    else:
        tmp = int(str(current)[::-1])
        res += num * dic[tmp]

print(res)

Solution (Python):
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines








def main():
	n=int(input())
	
	ans1=0
	ans2=0
	
	for i in range(1,n+1):
		a=int(str(i)[0])
		b=i%10
		
		if b==0:
			continue
		k=len(str(i))
		if k==1:
			ans2+=1
		elif a>b:
			ans1+=(10**(k-1)-1)//9
		elif a<b:
			ans1+=(10**(k-2)-1)//9
		else:
			ans1+=(10**(k-2)-1)//9+1+(int(str(i)[1:])-a)//10
			ans2+=1
	print(ans1*2+ans2)
	
	
	
	
	


if __name__ == "__main__":
	main()

Solution (Python):
n = int(input())
ans = 0
c = len(str(n))
if n <= 10:
    print(min(n, 9))
else:
    for i in range(1, 10):
        for j in range(1, 10):
            pa = 0
            pa2 = 0
            if i == j:
                pa += 1
                pa2 += 1
            temp = n - (i * 10**(c-1) + j)
            temp2 = n - (j * 10**(c-1) + i)
            if c > 2:
                if temp >= 0:
                    pa += min(10**(c-2), temp//10 + 1)
                if temp2 >= 0:
                    pa2 += min(10**(c-2), temp2//10 + 1)
            else:
                if temp >= 0:
                    pa += 1
                if temp2 >= 0:
                    pa2 += 1
            for k in range(0, c-2):
                pa += 10**k
                pa2 += 10**k
            ans += pa*pa2
            
    print(ans)

Solution (Python):
n=int(input())
ans=0
v=[[0 for i in range(9)] for j in range(9)]
for i in range(1,n+1):
  s=str(i)
  a,b=int(s[0]),i%10
  if a*b!=0:
    v[a-1][b-1]+=1

for i in range(9):
  for j in range(9):
    x=v[i][j]
    y=v[j][i]
    ans+=x*y



print(ans)

Solution (Python):
n=int(input())
cnt=0
data=[[0 for j in range(10)] for i in range(10)]
for ii in range(1,n+1):
    i=str(ii)
    if i[-1]!=0:
        data[int(i[-1])][int(i[0])]+=1

for i in range(9):
    for j in range(9):
        cnt+=data[i+1][j+1]*data[j+1][i+1]
print(cnt)

Solution (Python):
n=int(input())
ans=0
n_len=len(str(n))-1



for i in range(1,n+1):
    
    if i%10==0:
        continue
    tmp=str(i)
    tmp=tmp[-1]+tmp[0]
    check=0
    if int(tmp)<=n:
        check+=1
    if tmp[0]==tmp[-1]:
        check+=1
    
    for j in range(1,n_len):
        
        cal_min=int(tmp[0]+"0"*j+tmp[-1])
        cal_max=int(tmp[0]+"9"*j+tmp[-1])
        
        if cal_min>n:
            continue
        if cal_max<=n:
            check+=10**j
            continue
        cal_n=str(n)[1:-1]
        check+=int(cal_n)+1
        if int(str(n)[-1])<int(tmp[-1]):
            check-=1
    
    ans+=check
print(ans)

Solution (Python):
N = int(input())

matrix = [[0] * 10 for i in range(10)]

for n in range(1, N + 1):
    
    n_head = int(str(n)[0])
    
    n_tail = int(str(n)[-1])
    matrix[n_head][n_tail] += 1

answer = 0
for n in range(1, N + 1):
    
    n_head = int(str(n)[0])
    
    n_tail = int(str(n)[-1])
    answer += matrix[n_tail][n_head]

print(answer)

Solution (Python):
n = int(input())
d = [str(i) for i in range(1, n+1)]
data = [0] * 100
answer = 0
for i in range(1, n+1):
    a = int(list(str(i))[0])
    b = int(list(str(i))[-1])
    data[a*10 + b] += 1
for i in range(1, 10):
    for j in range(1, 10):
        answer += data[i*10 + j] * data[j*10 + i]
print(answer)

Solution (Python):
N_str = input().strip()
N = int(N_str)
Ns = int(N_str[0])
Ne = int(N_str[-1])
w = len(str(N)) - 2
if w > 0:
	Nm = int(N_str[1:-1])
else:
	Nm = 0

count = 0
for A in range(1, N + 1):
	A_str = str(A)
	As = int(A_str[0])
	Ae = int(A_str[-1])
	if Ae == 0:
		continue

	if w > 0:
		for i in range(w - 1):
			count += 10 ** (i + 1)
		
		if Ae < Ns:
			count += 10 ** w
		elif Ae == Ns:
			if As > Ne:
				count += Nm
			else:
				count += Nm + 1

	if Ae == As:
		count += 1
	
	if Ae * 10 + As <= N:
		count += 1
	
print(count)

Solution (Python):
N = int(input())
d = len(str(N))
s = [0 for k in range(100)]

for k in range(1,N+1):
    l = len(str(k))
    s[10*(k//(10**(l-1)))+k%10] += 1

ans = 0
for k in range(1,10):
    ans += s[k]**2

for x in range(1,10):
    for y in range(1,10):
        ans += s[x*10+y]*s[y*10+x]

print(ans)

Solution (Python):
n = int(input())
d = {
    i: {j: 0 for j in range(10)} for i in range(1, 10)
}
res = 0
for i in range(n+1):
    str_i = str(i)
    t_i = int(str_i[0])
    b_i = int(str_i[-1])
    if b_i == 0:
        continue
    res += d[b_i][t_i] * 2
    d[t_i][b_i] += 1
    res += 1 if t_i == b_i else 0
print(res)

Solution (Python):
N=int(input())
P=list(range(100))
for y in range(100):
 P[y]=0
for i in range(N):
 if i+1<=9:
  c=(i+1)*11
  P[c]+=1
 else:
  z=str(i+1)
  d=len(z)
  e=int((i+1)/(10**(d-1)))
  f=(i+1)%10
  g=10*e+f
  P[g]+=1
k=0
for i in range(9):
 for j in range(9):
  s=(i+1)*10+(j+1)
  t=(j+1)*10+(i+1)
  z=P[s]*P[t]
  k+=z
print(k)

Solution (Python):
N = int(input())
digit = len(str(N))

ans = 0
for a in range(1, 10):
    for b in range(1, 10):
        x, y = 0, 0
        if a == b:
            if a <= N:
                x += 1
                y += 1

        if a*10+b <= N:
            x += 1

        if b*10+a <= N:
            y += 1

        if digit <= 2:
            ans += x*y
            continue

        d = 3
        while d < digit:
            x += 10**(d-2)
            y += 10**(d-2)
            d += 1

        for i in range(10**(digit-2)):
            if int(str(a)+str(i).zfill(digit-2)+str(b)) <= N:
                x += 1
            if int(str(b)+str(i).zfill(digit-2)+str(a)) <= N:
                y += 1
        ans += x*y

print(ans)

Solution (Python):

import sys
import math
INF = float("inf")


def solve(N: int):

    debug = False
    SN = str(N)
    keta = int(math.log10(N)+1)
    count = 0
    for i in range(1, N+1):
        first = str(i)[0]
        last = str(i)[-1]
        if last == "0":         
            continue
        
        if first == last:
            count += 1
        
        if int(last+first) <= N:
            count += 1
        if keta < 3:
            continue
        
        for j in range(3, keta):
            count += 10**(j-2)
        
        
        if last < SN[0]:
            count += 10**(keta-2)
        elif last == SN[0]:
            
            DP = [[0]*2 for _ in range(keta-1)]
            DP[0][False] = 1
            for j in range(1, keta-1):
                DP[j][False] = DP[j-1][False]*1
                DP[j][True] = DP[j-1][True]*10 + DP[j-1][False]*int(SN[j])
            count += DP[-1][True]
            if SN[-1] >= first:
                count += 1
    print(count)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    solve(N)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
ans = 0
d = {}
for i in range(1,n+1):
    t = str(i)
    key = t[0]+t[-1]
    if "0" not in key:
        d[key] = d.get(key,0) + 1

for k in d:
    k2 = k[1]+k[0]
    ans += int(d[k])*int(d.get(k2,0))

print(ans)

Solution (Python):

import functools as ft

n = int(input())


mp_ = [0]*100 

for i in range(1,n+1):

    t = i%10

    if t == 0:
        continue
    else:

        h = int(str(i)[0])
        
        mp_[h*10 + t] += 1


ans = 0
for i, v in enumerate(mp_):

    if v == 0:
        continue

    

    j = (i%10)*10 + i//10
    t = mp_[j] * v
    ans += t

print(ans)

Solution (Python):
from itertools import permutations

N = int(input())

if N < 10:
    print(N)
    exit()


def cnt(s, e):
    ans = 0
    for i in range(0,10):
        num = s * 10**(i+1) + e

        if num > N:
            return ans

        if i == 0:
            ans += 1
            continue

        x = 10**i - 1
        if num + x * 10 <= N:
            ans += x + 1
            continue

        for j in range(10**i-1, -1, -1):
            if num + 10 * j <= N:
                ans += j + 1
                break 
        
        return ans

    return ans

ans = 0
for s, e in permutations([i for i in range(1,10)], 2):
   ans += cnt(s, e) * cnt(e, s)


for i in range(1, 10):
    ans += (cnt(i, i) + 1) ** 2

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def keta(n):
    return len(str(n))

def make(n, l, r):
    ans = 0
    sn = str(n)
    for i in range(5):
        if keta(n) > 2+i:
            ans += 10 ** i
        elif keta(n) == 2 + i:
            if sn[0] > str(l):
                ans += 10 ** i
            elif sn[0] == str(l):
                if sn[-1] < r:
                    ans += int(sn[1:-1])
                else:
                    if sn[1:-1] == '':
                        ans += 1
                    else:
                        ans += int(sn[1:-1]) + 1
            else:
                pass
        else:
            pass
    if l == r:
        ans += 1
    return ans

def main():
    n = int(input().strip())
    ans = 0
    for i in range(1, n+1):
        p = make(n, str(i)[-1], str(i)[0])
        if i % 10 == 0:
            continue
        
        
        
        ans += p
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
keta = len(str(N))
if keta == 1:
    print(N)
    sys.exit()
ans = {}
for i in [1,2,3,4,5,6,7,8,9]:
    for j in [1,2,3,4,5,6,7,8,9]:
        ans[(i, j)] = 0
for i in range(1, N+1):
    maxdigit = int(str(i)[0])
    mindigit = int(str(i)[-1])
    if mindigit != 0:
        ans[(maxdigit, mindigit)] += 1
cnt = 0
for i in range(1, 10):
    for j in range(1, 10):
        if i == j:
            cnt += ans[(i, i)] ** 2
        else:
            cnt += ans[(i, j)] * ans[(j, i)] * 2
            ans[(i, j)] = 0
            ans[(j, i)] = 0
print(cnt)

Solution (Python):
n = int(input())
A = [[0]*10 for _ in range(10)]
for i in range(1, n+1):
  t = str(i)
  a, b = int(t[0]), int(t[-1])
  A[a][b] += 1
ans = 0
for i in range(1, 10):
  for j in range(1, 10):
    ans += A[i][j] * A[j][i]
print(ans)

Solution (Python):
n=int(input())
a=[[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]]]
for i in range(n):
    p=str(i+1)
    b,c=int(p[0]),int(p[-1])
    if c>0:
        a[b-1][c-1].append(p)
q=0
for i in range(9):
    for j in range(9):
        q+=len(a[i][j])*len(a[j][i])
print(q)

Solution (Python):
N = int(input())

li = [[0 for j in range(9)] for i in range(9)]


for i in range(1,N+1):
    start = str(i)[0]
    end = str(i)[-1]
    if start != '0' and end != '0':
        li[int(start)-1][int(end)-1] += 1
ans = 0
for i in range(1,N+1):
    start = str(i)[0]
    end = str(i)[-1]
    if start != '0' and end != '0':
        ans += li[int(end)-1][int(start)-1]
print(ans)

Solution (Python):
N = int(input())

g = [[0] * 10 for i in range(10)]

for i in range(1, N+1):
    s = str(i)
    a = int(s[0])
    b = int(s[-1])
    g[a][b] += 1

ans = 0

for i in range(1, 10):
    for j in range(1, 10):
        ans += g[i][j] * g[j][i]
print(ans)

Solution (Python):
n = int(input())

ll = []
for i in range(n):
  a = list(str(i+1))
  ll.append(a[0]+a[-1])
ls = sorted(list(set(ll)))
lc = []
for s in ls:
  lc.append(ll.count(s))

r = len(ls)
ans = 0
for i in range(r):
  if ls[i][0] == ls[i][1]:
    ans += lc[i] ** 2
  for j in range(i):
    if ls[i][0] == ls[j][1] and ls[i][1] == ls[j][0]:
      ans += 2 * lc[i] * lc[j]

print(ans)

Solution (Python):
import sys
N = int(input())

if N < 10:
	print(N)
	sys.exit()

if N < 100:
	cnt = 0
	for i in range(1,N+1):
		if i < 10:
			cnt += 1
			if (i*10)+i <= N:
				cnt += 1
		else:
			s = str(i)
			sa,sb = s[0],s[-1]
			if sb == '0':
				continue
			ss = sb+sa
			if int(ss) <= N:
				cnt += 1
			if sa == sb:
				cnt += 1
	print(cnt)
	sys.exit()



k = len(str(N))
auto = 0
for i in range(k-1):
	auto += 10**i

auto2 = (auto-10**(k-2)) + (int(str(N)[1:]) // 10)
auto3 = auto - 10**(k-2)
m = str(N)[0]
mm = str(N)[-1]
def solve(a,b):
	ans = 0
	if a == b:
		ans += 1
	if a < m:
		return ans + auto
	elif a == m:
		if b <= mm:
			return ans + auto2 + 1
		else:
			return ans + auto2
	else:
		return ans + auto3

cnt = 0
for i in range(1,N+1):
	s = str(i)
	if s[-1] == '0':
		continue
	if i < 10:
		cnt += solve(s,s)
	else:
		sa,sb = s[0],s[-1]
		cnt += solve(sb,sa)

print(cnt)

Solution (Python):
n = int(input())
ans = 0
matrix = [[0 for _ in range(9)] for _ in range(9)]
for i in range(n):
    a = str(i+1)
    b = n
    x = int(a[0])-1
    y = int(a[-1])-1
    if y != -1:
        matrix[x][y] += 1
for i in range(9):
    for j in range(9):
        ans += matrix[i][j]*matrix[j][i]
print(ans)

Solution (Python):
from collections import defaultdict
x=int(input())
s=set()
d=defaultdict(int)
for i in range(1,x+1):
    p=str(i)
    k=p[0]+p[len(p)-1]
    d[k]+=1
    s.add(k)
a=0    
for i in s:
    c=i[::-1]
    a+=d[i]*d[c]
    
print(a)

Solution (Python):
N = int(input())
ans = 0
i = 1
while i <= N:
    if i < 10:
        ans += 1
    elif i < 100:
        if i % 10 == 0:
            pass
        elif i // 10 == i % 10:
            ans += 3
        elif i // 10 > i % 10:
            ans += 2
        else:
            pass
    elif i < 1000:
        if i % 10 == 0:
            pass
        elif i // 100 == i % 10:
            ans = ans + ((i % 100)//10)*2 + 5
        elif i // 100 > i % 10:
            ans += 22
        else:
            ans += 2
    elif i < 10000:
        if i % 10 == 0:
            pass
        elif i // 1000 == i % 10:
            ans = ans + ((i % 1000)//10)*2 + 25
        elif i // 1000 > i % 10:
            ans += 222
        else:
            ans += 22
    elif i < 100000:
        if i % 10 == 0:
            pass
        elif i // 10000 == i % 10:
            ans = ans + ((i % 10000)//10)*2 + 225
        elif i // 10000 > i % 10:
            ans += 2222
        else:
            ans += 222
    else:
        if i % 10 == 0:
            pass
        elif i // 100000 == i % 10:
            ans = ans + ((i % 100000)//10)*2 + 2225
        elif i // 100000 > i % 10:
            ans += 22222
        else:
            ans += 2222
    i +=1

print(ans)

Solution (Python):
n=int(input())


from collections import defaultdict
nn = defaultdict(lambda: 0) 

for i in range(1,n+1):
    if i%10!=0:
        nn[str(i)[0]+str(i)[-1]]+=1

result=0

for i in range(1,10):
    for j in range(1,10):
        number=str(i)+str(j)
        numRev=str(j)+str(i)
        if i==j:
            result+=nn[number]*nn[number]
            
        else:
            result+=nn[number]*nn[numRev]
            
            

print(result)