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

class Num:
    def __init__(self, h, t, d):
        self.head = h
        self.tail = t
        self.digit = d
        self.min = self.get_min()
        self.max = self.get_max()

    def get_min(self):
        if self.digit == 1:
            if self.head == self.tail:
                return self.head
            else:
                return 0
        else:
            return self.head * (10 ** (self.digit-1)) + self.tail

    def get_max(self):
        if self.digit == 1:
            if self.head == self.tail:
                return self.head
            else:
                return 0
        if self.digit == 2:
            return self.get_min()
        else:
            return self.get_min() + (10 ** (self.digit-1) - 10)


def solve(a, b, n):
    if a.min * a.max * b.min * b.max == 0:
        return 0

    if a.min > n:
        return 0

    if b.min > n:
        return 0

    if a.max > n:
        if a.digit >= 3:
            if str(a.max)[-1] > str(n)[-1]:
                a.max = int(str(a.max)[0] + str(n-10)[1:-1] + str(a.max)[-1])
            else:
                a.max = int(str(a.max)[0] + str(n)[1:-1] + str(a.max)[-1])

    if b.max > n:
        if b.digit >= 3:
            if str(b.max)[-1] > str(n)[-1]:
                b.max = int(str(b.max)[0] + str(n-10)[1:-1] + str(b.max)[-1])
            else:
                b.max = int(str(b.max)[0] + str(n)[1:-1] + str(b.max)[-1])

    total_a = (a.max - a.min) // 10 + 1
    total_b = (b.max - b.min) // 10 + 1
    count = total_a * total_b

    
    
    
    

    return count


def main():
    n = int(input())
    digit_n = len(str(n))

    total = 0
    for a_head in range(1, 10):
        for a_tail in range(1, 10):

            b_head = a_tail
            b_tail = a_head

            for digit_a in range(1, digit_n+1):
                for digit_b in range(1, digit_n+1):
                    a = Num(a_head, a_tail, digit_a)
                    b = Num(b_head, b_tail, digit_b)
                    count = solve(a, b, n)
                    total += count

    print(total)


if __name__ == '__main__':
    main()

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

l = [[0 for i in range(10)] for j in range(10)]
for i in range(1,n+1):
    a = str(i)
    l[int(a[:1])][int(a[-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 += 2 * l[i][j] * l[j][i]
print(ans)

Solution (Python):
N = input()
L = len(N)
if L == 1:
    print(int(N))
    exit()

def count(c,d):
    if c == int(N[0]):
        if L == 2:
            if d > int(N[-1]):
                n = 0
            else:
                n = 1
        else:
            n = int(N[1:-1]) + 1
            n += sum(10**i for i in range(L-2))
            if d > int(N[-1]):
                n -= 1
    elif c < int(N[0]):
        n = sum(10**i for i in range(L-1))
    else: 
        if L == 2:
            n = 0
        else:
            n = sum(10**i for i in range(L-2))
    if c == d:
        n += 1
    return n

ans = 0
for c in range(1,10):
    for d in range(1,10):
        nA = count(c,d)
        nB = count(d,c)
        
        
        ans += nA * nB
print(ans)

Solution (Python):
import math
n=int(input())
nn=str(n)


a=list(range(1,n+1))



b=[[[0] for _ in range(10)] for __ in range(10)]










        
        
        
for h in a:
    x=int(str(h)[0])

    y=int(str(h)[-1])
    b[x][y][0]+=1





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

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

cnt = 0
for i in range(1, N+1):
    first = str(i)[0]
    last = str(i)[-1]
    if first == last:
        cnt += 1
    if last != '0':
        if len(str(N)) == 2:
            if int(last+first) <= N:
                cnt += 1
        if len(str(N)) >= 3:
            for j in range(len(str(N))-2):
                cnt += 10**j
            middle = str(N)[1:-1]
            if last < str(N)[0]:
                cnt += 10**(len(str(N))-2)
            elif last == str(N)[0]:
                if first > str(N)[-1]:
                    cnt += int(middle)
                else:
                    cnt += int(middle) + 1

print(cnt)

Solution (Python):
N = input()
i_N = int(N)
s_N = str(N)

lis = [[] for i in range(9)]
for i in range(9):
  lis[i] = [[] for i in range(9)]

for i in range(1,i_N+1):
  row = int(str(i)[0])-1
  column = int(str(i)[-1])-1
  if not row == -1 and not column == -1: 
    lis[row][column].append(1)

times = 0
for i in range(9):
  for j in range(9):
    if i != j:
      times += len(lis[i][j]) * len(lis[j][i])
    else:
      times += len(lis[i][i]) **2
print(times)

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

group = [[[] for _ in range(10)] for _ in range(10)]

for n_i in range(1, n+1):
  str_n = str(n_i)
  for i in range(1, 10):
    if int(str_n[0]) == i:
      for j in range(1, 10):
        if int(str_n[-1]) == j:
          group[i][j].append(n_i)

ans = 0
for n_i in range(1, n+1):
  str_n = str(n_i)
  f_n = int(str_n[0])
  l_n = int(str_n[-1])
  ans += len(group[l_n][f_n])
      
print(ans)

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

ans=0
_dict={}

for i in range(1,10):
    for g in range(1,10):
        _dict[str(i)+str(g)]=0

for i in range(1,min(10,n+1)):
    _dict[str(i)*2]+=1

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

for i,m in _dict.items():
    ans+=m*_dict[i[::-1]]
print(ans)

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

X = int(str(N)[0])
Y = int(str(N)[-1])
L = len(str(N))
if L==1:
    print(N)
else:
    M = N//10 - X*(10**(L-2)) + 1
    D = [[0]*10 for i in range(10)]
    for i in range(1,10):
        for j in range(1,10):
            if i==j:
                D[i][j]+=1
            for k in range(2,L):
                D[i][j]+=10**(k-2)
            if i<X:
                D[i][j]+=10**(L-2)
            elif i==X:
                if j<=Y:
                    D[i][j]+=M
                else:
                    D[i][j]+=M-1
    ans=0
    for i in range(1,10):
        for j in range(1,10):    
            ans+=D[i][j]*D[j][i]
    
    print(ans)

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

A=[0]*(100)
B = [0]*(100)
M=10

A=[[0 for j in range(M)]for i in range(M)]
B = [[0 for j in range(M)]for i in range(M)]

for n in range(1,N+1):
    if n>N:
        continue
    sn=list(map(int,list(str(n))))
    
    if sn[0]==0 or sn[-1]==0:
        continue
    
    A[sn[0]][sn[-1]]+=1    
    


"""
     tmp =10*sn[0]+sn[-1]
    A[tmp]+=1
    tmp = 10*sn[-1]+sn[0]
    B[tmp] += 1 
"""

res=0
for n in range(1,9+1):
    if n > N:
        continue

    


res=0

for top in range(1,10):
    for tail in range(1,10):


        tmp = (A[top][tail]*A[tail][top])
        if tmp>0:
            
            res+=(A[top][tail]*A[tail][top])
            


print(res)

Solution (Python):


n = int(input())
dict_x = {}
for i in range(1, n + 1):
    a = i % 10
    if a == 0:
        continue
    b = (i // 10**(len(str(i))-1) ) % 10
    if str(a) + "_" + str(b) in dict_x:
        dict_x[str(a) + "_" + str(b)] += 1
    else:
        dict_x[str(a) + "_" + str(b)] = 1
count = 0
for key in dict_x:
    a, b = key.split("_")
    if str(b) + "_" + str(a) in dict_x:
        t1 = dict_x[key]
        t2 = dict_x[str(b) + "_" + str(a)]
        count += t1 * t2
print(count)

Solution (Python):
a = [[0 for i in range(9)] for j in range(9)]
for i in range(1,int(input())+1):
  i = str(i)
  if i[-1] != "0":
   	a[int(i[0])-1][int(i[-1])-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    if i != j:
      ans += a[i][j]*a[j][i]
    else:
      ans += a[i][j] ** 2
print(ans)

Solution (Python):
import math
import numpy as np


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

    M = [N // 10 ** i for i in range(5 + 1)]

    mat = [[0 for _ in range(10)] for _ in range(10)]

    for i in range(1, 10):
        for j in range(1, 10):

            for k in range(6 + 1):
                if k == 0:  
                    if i == j and N >= i:
                        mat[i][j] += 1

                else:  
                    max_ = (i + 1) * 10 ** k - 10 + j
                    min_ = i * 10 ** k + j

                    if N >= max_:
                        mat[i][j] += 10 ** (k - 1)
                        continue
                    elif N < min_:
                        continue
                    else:
                        mat[i][j] += (N - min_) // 10 + 1
                        continue

    

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
ans = 0
for i in range(1,n+1):
    if str(i)[-1] == "0":
        pass
    elif i < 10:
        ans += 1
    elif i <= 100:
        if str(i)[0] == str(i)[-1]:
            ans += 3
        elif int(str(i)[0]) > int(str(i)[1]):
            ans += 2
    elif i <= 1000:
        if str(i)[0] == str(i)[-1]:
            ans += (int(str(i)[1])+3)*2-1
        elif int(str(i)[0]) > int(str(i)[-1]):
            ans += 22
        elif int(str(i)[0]) < int(str(i)[-1]):
            ans += 2
    elif i <= 10000:
        if str(i)[0] == str(i)[-1]:
            ans += (int(str(i)[1:3]) + 13)*2-1
        elif int(str(i)[0]) > int(str(i)[-1]):
            ans += 222
        elif int(str(i)[0]) < int(str(i)[-1]):
            ans += 22
    elif i <= 100000:
        if str(i)[0] == str(i)[-1]:
            ans += (int(str(i)[1:4]) + 113)*2-1
        elif int(str(i)[0]) > int(str(i)[-1]):
            ans += 2222
        elif int(str(i)[0]) < int(str(i)[-1]):
            ans += 222
    elif i <= 1000000:
        if str(i)[0] == str(i)[-1]:
            ans += (int(str(i)[1:5]) + 1113)*2-1
        elif int(str(i)[0]) > int(str(i)[-1]):
            ans += 22222
        elif int(str(i)[0]) < int(str(i)[-1]):
            ans += 2222
print(ans)

Solution (Python):
n = int(input())
n_list =[0 for i in range(101)]
count = 0
ans = 0
for i in range(1, n+1):
    if i >=10:
        n_list[int(str(i)[0])*10+int(str(i)[len(str(i))-1])] += 1
    elif i < 10:
        n_list[int(str(i)[0])] += 1
for i in range(1,10):
    ans += n_list[i]
for i in range(1,10):
    ans += (n_list[i]*n_list[i*11])*2
for i in range(11, 100):
    if int(str(i)[1]) != 0:
        b = int(str(i)[1])*10+int(str(i)[0])
        ans += n_list[i]*n_list[b]
print(ans)

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

def handstand(a, b):
  cnt_ = 0
  if a == b:
    cnt_ += 1
  k = 0
  while (a+1) * (10 ** (k+1)) + b-10 <= n:
    cnt_ += (10 ** k)
    k += 1
  cnt_ += max(0, (n - (a * (10 ** (k+1)) + b) + 10) // 10)
  return cnt_  

if n < 10:
  print(n)
  exit()
for i in range(1, 10):
  for j in range(1, 10):
    cnt += handstand(i, j) * handstand(j, i)
print(cnt)

Solution (Python):
n = int(input())
m = len(str(n))
ans = 0



for i in range(n + 1):
    A = str(i)
    first = A[0]
    end = A[-1]

    if end == '0':
        continue
    if first == end:
        ans += 1


    if m >= 3:
        for j in range(m - 2):
            ans += 10 ** (j)
    mini = int(end + '0' * (m - 2) + first)
    maxi = int(end + '9' * (m - 2) + first)
    
    if n < mini:
        continue
    elif mini <= n <= maxi:
        
        if int(str(n)[-1]) >= int(first):
            ans += int(str(n)[1:-1]) + 1
            
        else:
            ans += int(str(n)[1:-1])
            
    else:
        ans += (10 ** (m - 2))

print(ans)

Solution (Python):
N = int(input())
d = {}
ans = 0
for n in range(1, N+1):
  l = n%10
  u = n
  while u >= 10:
    u //= 10
  if l > 0:
    if (u,l) in d:
      d[(u,l)] += 1
    else:
      d[(u,l)] = 1
for i in range(1, 10):
  if (i, i) in d:
    a = d[(i, i)]
    ans += a*(a-1)+a
  for j in range(i+1, 10):
    a, b = (0, 0)
    if (i, j) in d:
      a = d[(i, j)]
    if (j, i) in d:
      b = d[(j, i)]
    ans += a*b*2
print(ans)

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

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

for i in range(1,n+1) :
	hed = i % 10;
	i_str = str(i);
	foot = int(i_str[0]);
	hedfoot[foot][hed] += 1;

ans = 0;
for i in range(1,n+1) :
	hed = i % 10;
	i_str = str(i);
	foot = int(i_str[0]);
	ans += hedfoot[hed][foot];

print(ans);

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

if 0 <= N <= 9:
    print(N)
else:
    ans = 9
    for i in range(11, N+1):
        if i % 10 == 0:
            continue
        if i < 100:
            if int(str(i)[0]) == i % 10:
                ans += 3  
            elif (i % 10)*10+(i//10) <= N:
                ans += 1
            continue
        if int(str(i)[0]) == i % 10:
            ans += 5  
            ans += ((i - (int(str(i)[0]) *
                          (10 ** (len(str(i)) - 1)))) // 10) * 2
            
            
            for j in range(1, len(str(i)) - 2):
                ans += (10 ** j) * 2

        elif int(str(i)[0]) > i % 10:
            for k in range(len(str(i)) - 1):
                ans += (10 ** k) * 2
        else:
            for h in range(len(str(i)) - 2):
                ans += (10**h)*2
    print(ans)

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

arr = [[list() for _ in range(10)] for _ in range(10)]
count = 0

for i in range(1,n+1):
  arr[int(str(i)[0])][int(str(i)[-1])].append(i)

for i in range(1,10):
  for j in range(1,10):
    arr[i][j] = len(arr[i][j])

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

Solution (Python):
a = int(input())
ans = 0
for i in range(1,a+1):
    s = str(i)
    if s[-1]== '0':continue
    elif len(s) == 1:
        ans+=1
    elif s[0] == s[1] and len(s)==2:
        ans+=3
    elif int(s[0])>int(s[1]) and len(s)==2 and '0' not in s:
        ans+=2
    elif len(s)==2:continue
    elif s[0] == s[2] and len(s)==3:
        ans += 3+2*(int(s[1])+1)
    elif len(s) == 3 and int(s[0])>int(s[2]):
        ans +=22
    elif len(s) == 3 and s[2] != '0':
        ans +=2 

    elif len(s) == 4 and s[0] == s[-1]:
        ans += 3+2*(10+int(s[1]+s[2])+1)
    elif len(s) == 4 and int(s[0])>int(s[-1]):
        ans += 222
    elif len(s)==4:
        ans +=22

    elif len(s) == 5 and s[0] == s[-1]:
        ans += 3+2*(110+int(s[1]+s[2]+s[3])+1)
    elif len(s) == 5 and int(s[0])>int(s[-1]):
        ans += 2222
    elif len(s)==5:
        ans +=222

    elif len(s) == 6 and s[0] == s[-1]:
        ans += 3+2*(1110+int(s[1]+s[2]+s[3]+s[4])+1)
    elif len(s) == 6 and int(s[0])>int(s[-1]):
        ans += 22222
    elif len(s)==6:
        ans +=2222
print(ans)

Solution (Python):
a = int(input())
ans = 0
for i in range(1,a+1):
    s = str(i)
    if s[-1]== '0':continue
    elif len(s) == 1:
        ans+=1
    elif s[0] == s[1] and len(s)==2:
        ans+=3
    elif int(s[0])>int(s[1]) and len(s)==2 and '0' not in s:
        ans+=2
    elif len(s)==2:continue
    elif s[0] == s[2] and len(s)==3:
        ans += 3+2*(int(s[1])+1)
    elif len(s) == 3 and int(s[0])>int(s[2]):
        ans +=22
    elif len(s) == 3 and s[2] != '0':
        ans +=2 

    elif len(s) == 4 and s[0] == s[-1]:
        ans += 3+2*(10+int(s[1]+s[2])+1)
    elif len(s) == 4 and int(s[0])>int(s[-1]):
        ans += 222
    elif len(s)==4:
        ans +=22

    elif len(s) == 5 and s[0] == s[-1]:
        ans += 3+2*(110+int(s[1]+s[2]+s[3])+1)
    elif len(s) == 5 and int(s[0])>int(s[-1]):
        ans += 2222
    elif len(s)==5:
        ans +=222

    elif len(s) == 6 and s[0] == s[-1]:
        ans += 3+2*(1110+int(s[1]+s[2]+s[3]+s[4])+1)
    elif len(s) == 6 and int(s[0])>int(s[-1]):
        ans += 22222
    elif len(s)==6:
        ans +=2222
print(ans)

Solution (Python):
import sys

n = sys.stdin.readline().rstrip()
len_n = len(n)
m = n[1:-1]
m = int(m) if m else 0
l = int(n[0])
r = int(n[-1])
n = int(n)

def main():
    res = [[0] * 10 for _ in range(10)]

    for i in range(1, 10):
        for j in range(1, 10):
            if i == j:
                if i <= n:
                    res[i][j] += 1
            for k in range(len_n-2+1):
                if k < len_n - 2:
                    res[i][j] += 10 ** k
                else:
                    if i < l:
                        res[i][j] += 10 ** k
                    elif i == l:
                        if j <= r:
                            res[i][j] += m + 1
                        else:
                            res[i][j] += m
                    else:
                        pass

    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += res[i][j] * res[j][i]
    return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):
n = int(input())
memo1 = 0
memo2 = 0
ans = 0
stra = ""
strb = ""
str1 = ""
str2 = ""

for a in range(1,10):
    for b in range(1,10):
        stra = str(a)
        strb = str(b)
        for i in range(1, n+1):
            str1 = str(i)[0]
            str2 = str(i)[-1]
            if str1 == stra and str2 == strb:
                memo1 += 1
            if str1 == strb and str2 == stra:
                memo2 += 1
        ans += memo1 * memo2
        memo1 = 0
        memo2 = 0



print(ans)

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

a = [0] * 100
for i in range(n+1):
    if i % 10 == 0:
        continue
    k = int(str(i)[0]) * 10 + i % 10
    a[k] += 1

result = 0
for i in range(11,100):
    if i % 10 != 0:
        result += a[i] * a[int(str(i)[::-1])]

print(result)

Solution (Python):
N=int(input())
b=[]
for i in range(9):
	b.append([0]*9)
for i in range(N):
	c=str(i+1)
	d=int(c[0])
	e=int(c[-1:])
	if d*e==0:
		continue
	b[d-1][e-1]+=1
f=0
for j in range(9):
	f+=b[j][j]**2
for i in range(8):
	for j in range(i+1):
		f+=b[j][i+1]*b[i+1][j]*2
print(f)

Solution (Python):
n=int(input())
s=str(n)
m=len(s)
ans=0
if n<=9:
    print(n)
    exit()
if n<=99:
    for i in range(1,n+1):
        for j in range(1,n+1):
            if str(i)[-1]=="0":
                continue
            if str(i)[0]==str(j)[-1] and str(j)[0]==str(i)[-1]:
                ans+=1
    print(ans)
    exit()
y=int(s[1:m-1])
for i in range(1,n+1):
    a,b=str(i)[0],str(i)[-1]
    if b=="0":
        continue
    if a==b:
        ans+=1
    dig=1
    for j in range(m-2):
        ans+=dig
        dig*=10
    if int(b)==int(s[0]):
        if int(a)>int(s[-1]):
            ans+=y
        else:
            ans+=y+1
    elif int(b)<int(s[0]):
        ans+=dig
print(ans)

Solution (Python):
sn=input()
l=len(sn)
n=int(sn)
s=0
t=[[0 for i in range(9)]for j in range(9)]

if l==1:
    print(n)
else:
    s1=0
    for i in range(l-2):
        s1+=(10**i)
    s1+=1
    for i in range(9):
        t[i][i]=s1

    s2=0
    for i in range(l-2):
        s2+=(10**i)

    for i in range(9):
        for j in range(9):
            if i!=j:
                t[i][j]=s2
    for i in range(9):
        for j in range(9):
            if int(sn[0])<i+1:
                s1=s2
            elif int(sn[0])==i+1:
                if int(sn[l-1])<j+1:
                    if len(sn[1:l-1])!=0:
                        t[i][j]+=int(sn[1:l-1])
                else:
                    if len(sn[1:l-1])!=0:
                        t[i][j]+=int(sn[1:l-1])+1
                    else:
                        t[i][j]+=1
            else:
                t[i][j]+=10**(l-2)

    for i in range(9):
        for j in range(9):
                s+=t[i][j]*t[j][i]
    print(s)

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




ans = 0

ansDict = {}
for i in range(10, 100):
    inAns = 0
    head = int(i / 10)
    tail = int(i % 10)

    
    if tail == 0:
        ansDict[i] = 0
        continue

    
    if(head == tail):
        inAns = inAns + 1


    B = tail * 10 + head
    keta = 1
    while(B <= N):

        inAns = inAns + 1
        next = B + 10
        if(int(str(next)[0]) != tail):
            keta = keta + 1
            B = tail * (10 ** keta) + head
        else:
            B = next

    ansDict[i] = inAns


for i in range(1, N + 1):
    head = int(str(i)[0])
    tail = int(str(i)[-1])
    ans = ans + ansDict[head * 10 + tail]

print(ans)

Solution (Python):
n = int(input())
cnt = 0
ls = []
"""
def chk(a,b):
    if a[len(a)-1] == '0' or b[len(b)-1] == '0':
        return 0
    elif a[0] == b[len(b)-1] and a[len(a)-1] == b[0] and a == b:
        return 1
    elif a[0] == b[len(b)-1] and a[len(a)-1] == b[0]:
        return 2
    else:
        return 0
"""
ls = []
dic = {}
for i in range(1,10):
    for j in range(1,10):
        ls.append([str(i),str(j)])

for i in range(81):
    dic[i] = 0


if n <= 50000:
    for i in range(1,n+1):
        a = str(i)
        if a[len(a)-1] != '0':
            
            if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
                
                
                cnt += 1
            elif a[0] == a[len(a)-1]:
                
                
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
            elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
                
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
            dic[ls.index([a[0],str(i)[len(a)-1]])] += 1

elif n <= 100000:
    dic = {0: 1112, 1: 1111, 2: 1111, 3: 1111, 4: 1111, 5: 1111, 6: 1111, 7: 1111, 8: 1111, 9: 1111, 10: 1112, 11: 1111, 12: 1111, 13: 1111, 14: 1111, 15: 1111, 16: 1111, 17: 1111, 18: 1111, 19: 1111, 20: 1112, 21: 1111, 22: 1111, 23: 1111, 24: 1111, 25: 1111, 26: 1111, 27: 1111, 28: 1111, 29: 1111, 30: 1112, 31: 1111, 32: 1111, 33: 1111, 34: 1111, 35: 1111, 36: 111, 37: 111, 38: 111, 39: 111, 40: 112, 41: 111, 42: 111, 43: 111, 44: 111, 45: 111, 46: 111, 47: 111, 48: 111, 49: 111, 50: 112, 51: 111, 52: 111, 53: 111, 54: 111, 55: 111, 56: 111, 57: 111, 58: 111, 59: 111, 60: 112, 61: 111, 62: 111, 63: 111, 64: 111, 65: 111, 66: 111, 67: 111, 68: 111, 69: 111, 70: 112, 71: 111, 72: 111, 73: 111, 74: 111, 75: 111, 76: 111, 77: 111, 78: 111, 79: 111, 80: 112}
    cnt = 25000008
    for i in range(50001,n+1):
        a = str(i)
        if a[len(a)-1] != '0':
            
            if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
                
                
                cnt += 1
            elif a[0] == a[len(a)-1]:
                
                
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
            elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
                
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
            dic[ls.index([a[0],str(i)[len(a)-1]])] += 1
elif n <= 150000:
    dic = {0: 1112, 1: 1111, 2: 1111, 3: 1111, 4: 1111, 5: 1111, 6: 1111, 7: 1111, 8: 1111, 9: 1111, 10: 1112, 11: 1111, 12: 1111, 13: 1111, 14: 1111, 15: 1111, 16: 1111, 17: 1111, 18: 1111, 19: 1111, 20: 1112, 21: 1111, 22: 1111, 23: 1111, 24: 1111, 25: 1111, 26: 1111, 27: 1111, 28: 1111, 29: 1111, 30: 1112, 31: 1111, 32: 1111, 33: 1111, 34: 1111, 35: 1111, 36: 1111, 37: 1111, 38: 1111, 39: 1111, 40: 1112, 41: 1111, 42: 1111, 43: 1111, 44: 1111, 45: 1111, 46: 1111, 47: 1111, 48: 1111, 49: 1111, 50: 1112, 51: 1111, 52: 1111, 53: 1111, 54: 1111, 55: 1111, 56: 1111, 57: 1111, 58: 1111, 59: 1111, 60: 1112, 61: 1111, 62: 1111, 63: 1111, 64: 1111, 65: 1111, 66: 1111, 67: 1111, 68: 1111, 69: 1111, 70: 1112, 71: 1111, 72: 1111, 73: 1111, 74: 1111, 75: 1111, 76: 1111, 77: 1111, 78: 1111, 79: 1111, 80: 1112}
    cnt = 100000008
    for i in range(100001,n+1):
        a = str(i)
        if a[len(a)-1] != '0':
            
            if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
                
                
                cnt += 1
            elif a[0] == a[len(a)-1]:
                
                
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
            elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
                
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
            dic[ls.index([a[0],str(i)[len(a)-1]])] += 1
else:
    dic = {0: 6112, 1: 6111, 2: 6111, 3: 6111, 4: 6111, 5: 6111, 6: 6111, 7: 6111, 8: 6111, 9: 1111, 10: 1112, 11: 1111, 12: 1111, 13: 1111, 14: 1111, 15: 1111, 16: 1111, 17: 1111, 18: 1111, 19: 1111, 20: 1112, 21: 1111, 22: 1111, 23: 1111, 24: 1111, 25: 1111, 26: 1111, 27: 1111, 28: 1111, 29: 1111, 30: 1112, 31: 1111, 32: 1111, 33: 1111, 34: 1111, 35: 1111, 36: 1111, 37: 1111, 38: 1111, 39: 1111, 40: 1112, 41: 1111, 42: 1111, 43: 1111, 44: 1111, 45: 1111, 46: 1111, 47: 1111, 48: 1111, 49: 1111, 50: 1112, 51: 1111, 52: 1111, 53: 1111, 54: 1111, 55: 1111, 56: 1111, 57: 1111, 58: 1111, 59: 1111, 60: 1112, 61: 1111, 62: 1111, 63: 1111, 64: 1111, 65: 1111, 66: 1111, 67: 1111, 68: 1111, 69: 1111, 70: 1112, 71: 1111, 72: 1111, 73: 1111, 74: 1111, 75: 1111, 76: 1111, 77: 1111, 78: 1111, 79: 1111, 80: 1112}
    cnt = 225000008
    for i in range(150001,n+1):
        a = str(i)
        if a[len(a)-1] != '0':
            
            if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
                
                
                cnt += 1
            elif a[0] == a[len(a)-1]:
                
                
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
            elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
                
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
            dic[ls.index([a[0],str(i)[len(a)-1]])] += 1


print(cnt)

Solution (Python):
N = int(input())
import numpy as np
ans = 0
lis = [[0]*9]*9
lis = np.array(lis)
for i in range(1,N+1):
    a = i%10
    b = i//(10**(len(str(i))-1))
    if a != 0:
        lis[a-1][b-1] += 1
for j in range(9):
    for k in range(9):
        ans += lis[k][j]*lis[j][k]
print(ans)

Solution (Python):
import numpy as np
N = int(input())

t = np.array([[0] * 10] * 10)

ans = 0
for i in range(1, N + 1):
    if i % 10 == 0:
        continue
    else:
        a = int(str(i)[0])
        b = int(str(i)[-1])
        t[a][b] = t[a][b] + 1

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

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

cnt = 0
for i in range(1, n+1):
    i_str = str(i)
    if len(i_str) == 1:
        cnt += 1
        continue
    if len(i_str) == 2:
        if int(i_str[0]) == int(i_str[1]):
            cnt += 3
        elif int(i_str[1]) != 0 and int(i_str[0]) > int(i_str[1]):
            cnt += 2
        continue
    if len(i_str) == 3:
        if int(i_str[0]) == int(i_str[2]):
            cnt += 4 + (2 * (int(i_str[1])+1) - 1)
        elif int(i_str[2]) != 0 and int(i_str[0]) > int(i_str[2]):
            cnt += 22
        elif int(i_str[0]) < int(i_str[2]):
            cnt += 2
        continue
    if len(i_str) == 4:
        if int(i_str[0]) == int(i_str[3]):
            cnt += 24 + 20 * int(i_str[1]) + 2 * (int(i_str[2])+1) - 1
        elif int(i_str[3]) != 0 and int(i_str[0]) > int(i_str[3]):
            cnt += 222
        elif int(i_str[0]) < int(i_str[3]):
            cnt += 22
        continue
    if len(i_str) == 5:
        if int(i_str[0]) == int(i_str[4]):
            cnt += 224 + 200 * int(i_str[1]) + 20 * int(i_str[2]) + 2 * (int(i_str[3])+1) - 1
        elif int(i_str[4]) != 0 and int(i_str[0]) > int(i_str[4]):
            cnt += 2222
        elif int(i_str[0]) < int(i_str[4]):
            cnt += 222
        continue
    if len(i_str) == 6:
        if int(i_str[0]) == int(i_str[5]):
            cnt += 2224 + 2000 * int(i_str[1]) + 200 * int(i_str[2]) + 20 * int(i_str[3]) + 2 * (int(i_str[4])+1) - 1
        elif int(i_str[5]) != 0 and int(i_str[0]) > int(i_str[5]):
            cnt += 22222
        elif int(i_str[0]) < int(i_str[5]):
            cnt += 2222
        continue

print(cnt)

Solution (Python):
words = lambda t : list(map(t, input().split()))
n = int(input())
n_lst = n % 10
n_hd = n
n_d = 1
while n_hd // 10 != 0:
    n_hd //= 10
    n_d += 1

ans = 0
for i in range(1,n+1):
    if i % 10 == 0:
        continue
    d = 2
    b_hd = i % 10
    b_lst = i
    while b_lst // 10 != 0:
        b_lst //= 10
    if b_hd == b_lst:
        if b_hd <= n:
            
            ans += 1
    while 10**(d-1) < n:

        if d < n_d:
            
            ans += 10**(d-2)
        elif d == n_d:
            if b_hd < n_hd:
                
                ans += 10**(d-2)
            elif b_hd == n_hd:
                
                
                ans += (n - (n_hd)*(10**(d-1)) - (n_lst)) // 10
                if b_lst <= n_lst:
                    
                    ans += 1
        d += 1

print(ans)

Solution (Python):
import sys


def solve(inp):
    N = int(inp.readline().strip())

    nums = [[0 for i in range(11)] for i in range(11)]

    for s in range(1, 10):
        for e in range(0, 10):
            n = 0
            
            if s == e and s <= N:
                
                n += 1
            for d in (10, 100, 1000, 10000, 100000, 1000000):
                start = end = s * d + e
                if start > 200000:
                    continue
                d2 = int(d / 10)
                while d2 >= 10:
                    end += 9 * d2
                    d2 = int(d2 / 10)
                end = min(end, 200000)
                
                if end <= N:
                    n += int(d / 10)
                else:
                    j = start
                    while j <= N:
                        n += 1
                        j += 10
            
            
            nums[s][e] = n

    r = 0
    for s in range(1, N + 1):
        if s % 10 != 0:
            r += nums[s % 10][int(str(s)[0])]

    return str(r)


def solve1(inp):
    N = int(inp.readline().strip())

    r = 0
    for i in range(1, N + 1):
        first = int(str(i)[0])
        last = i % 10
        for j in range(1, N + 1):
            if int(str(j)[0]) == last and j % 10 == first:
                r += 1

    return str(r)


def main():
    result = solve(sys.stdin)
    if result:
        print(result)


if __name__ == '__main__':
    main()