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):
from itertools import product


def get_ms_ls(i):
    temp = list(str(i))
    return int(temp[0]), int(temp[-1])


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

    dp = [[0] * 10 for _ in range(10)]
    for i in range(1, N + 1):
        msb, lsb = get_ms_ls(i)
        dp[msb][lsb] += 1

    ans = 0
    for (i, j) in product(range(10), repeat=2):
        x, y = dp[i][j], dp[j][i]
        ans += x * y

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    n = II()
    ans = 0
    nf = int(str(n)[0])
    ne = int(str(n)[-1])
    lenn = len(str(n))
    dp = [[0] * 10 for i in range(10)]
    for i in range(1, n + 1):
        f = int(str(i)[0])
        e = int(str(i)[-1])
        dp[f][e] += 1
    ans = 0
    for i in range(1, n + 1):
        f = int(str(i)[0])
        e = int(str(i)[-1])
        ans += dp[e][f]

    print(ans)
    return



if __name__ == '__main__':
    solve()

Solution (Python):
n = int(input())
matrix = [[0]*10 for _ in range(10)]
for i in range(1,n+1):
    s = str(i)
    matrix[int(s[0])][int(s[-1])] += 1
total = 0
for i in range(1,10):
    for j in range(1,10):
        total += matrix[i][j]*matrix[j][i]
print(total)

Solution (Python):
N = int(input())
ret = 0
item = [[0 for i in range(10)] for j in range(10)]
for i in range(1, N+1):
    x = str(i)
    item[int(x[-1])][int(x[0])] += 1
for i in range(10):
    for j in range(10):
        ret += item[i][j] * item[j][i]
print(ret)

Solution (Python):
def main():
  import sys
  input = sys.stdin.readline
  N = int(input())
  dic = {}
  for i in range(1,N+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    if (a,b) in dic:
      dic[(a,b)] += 1
    else:
      dic[(a,b)] = 1
  
  ans = 0
  for i in range(1,10):
    for j in range(1,10):
      if (i,j) in dic and (j,i) in dic:
        ans += dic[(i,j)]*dic[(j,i)]
  print(ans)
    

if __name__ == '__main__':
  main()

Solution (Python):


N = int(input())

ans = 0


for i in range(N):
    a = i + 1

    if a < 10:
        ans += 1
        continue

    if str(a)[-1] == '0':
        continue

    
    if str(a)[0] > str(a)[-1]:
        for j in range(len(str(a)) - 1):
            ans += (10 ** j) * 2

    
    elif str(a)[0] < str(a)[-1]:
        for j in range(len(str(a)) - 2):
            ans += (10 ** j) * 2

    
    else:
        
        if a >= 100:
            ans += int(str(a)[1:-1]) * 2

        
        ans += 1

        
        for j in range(len(str(a)) - 2):
            ans += (10 ** j) * 2

        
        ans += 2

print(ans)

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

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

for i in range(N):
    temp=list(str(i+1))
    temp=list(map(int,temp))
    lis[temp[0]][temp[-1]]+=1

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

Solution (Python):
import numpy as np
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(raw_input())
C = np.zeros(shape=(10,10))

for i in range(N+1):
    i_char = str(i)
    i_first = int(i_char[0])
    i_last = int(i_char[-1])

    C[i_first][i_last] = C[i_first][i_last] + 1

ans = 0





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

print int(ans)

Solution (Python):
N=int(input())
dp=[[0]*9 for _ in range(9)]
for i in range(N):
  a=int(str(i+1)[0])
  b=int(str(i+1)[-1])
  if 1<=a<=9 and 1<=b<=9:
    dp[a-1][b-1]+=1

ans=0
for i in range(9):
  for j in range(9):
    ans+=dp[i][j]*dp[j][i]
print(ans)

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

Solution (Python):
import numpy as np
N = int(input())
m = [[0 for j in range(10)] for i in range(10)]
for i in range(1, N+1):
  m[i%10][int(str(i)[0])] += 1

m = np.array(m)
mt = m.T
mm = m * mt
print(np.sum(mm))

Solution (Python):
N = int(input())
G = []
for i in range(9):
    G.append([])
for i in G:
    for j in range(9):
        i.append([])

for i in range(1,N+1):
    if i % 10 == 0:
        continue
    x = list(str(i))
    A = int(x[0])
    B = int(x[len(x)-1])
    G[A-1][B-1].append(i)

ans = 0
for i in range(9):
    for j in range(9):
        ans += len(G[i][j]) * len(G[j][i])

print(ans)

Solution (Python):
n=int(input())
keta=len(str(n))
def f(x,y):
  if keta==1:
    if x==y and x<=n:
      return 1
    else:
      return 0
  elif keta==2:
    if x==y and x*10+y<=n:
      return 2
    elif x==y:
      return 1
    elif x*10+y<=n:
      return 1
    else:
      return 0
  else:
    if x==y:
      ret=2
    else:
      ret=1
    for i in range(1,keta-2):
      ret+=10**i
    if x<int(str(n)[0]):
      ret+=10**(keta-2)
    elif x==int(str(n)[0]):
      if y<=int(str(n)[-1]):
        ret+=int(str(n)[1:keta-1])+1
      else:
        ret+=int(str(n)[1:keta-1])
    return ret

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

Solution (Python):
import sys
from collections import deque
from heapq import heapify, heappop, heappush
from itertools import combinations, permutations
from bisect import bisect, bisect_left

def solve():
    input = sys.stdin.readline
    N = int(input())
    strN = str(N)
    maxDig = len(strN)
    count = 0
    for i in range(1, N + 1):
        a = str(i)
        low = int(a[-1])
        if low == 0: continue
        high = int(a[0])
        if low == high: count += 1 
        digb = 2
        while digb < maxDig:
            count += 10 ** (digb - 2)
            digb += 1
        if N >= 10:
            if low > int(strN[0]): continue
            elif low < int(strN[0]): count += 10 ** (digb - 2)
            elif N >= 10:
                DP = [[0, 0] for _ in range(maxDig)]
                DP[0][0] = 1
                for j in range(1, maxDig - 1):
                    DP[j][0] = 1
                    DP[j][1] = DP[j-1][1] * 10 + DP[j-1][0] * (int(strN[j]))

                count += DP[maxDig - 2][1] 
                
                if int(strN[maxDig - 1]) >= high: count += 1

    print(count)
        
        

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
n=int(input())
check1=[[i,i,0] for i in range(1,10)]
check2=[[i,j,0] for i in range(1,10) for j in range(i+1,10)]
l=len(check2)
check3=[[check2[i][1],check2[i][0],0] for i in range(l)]

for i in range(1,n+1):
    s=str(i)
    s1=int(s[0])
    s2=int(s[-1])
    if s2!=0:
        if s1==s2:
            check1[s1-1][2]+=1
        elif s1<s2:
            x=[0,8,15,21,26,30,33,35]
            check2[x[s1-1]+(s2-s1-1)][2]+=1
        else:
            x=[0,8,15,21,26,30,33,35]
            check3[x[s2-1]+(s1-s2-1)][2]+=1
cnt=0
for i in range(9):
    cnt+=(check1[i][2])*(check1[i][2])
for i in range(l):
    cnt+=2*(check2[i][2]*check3[i][2])
print(cnt)

Solution (Python):






n = int(input())

d = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, n+1):
    temp = list(str(i))
    h = int(temp[0])
    t = int(temp[-1])
    if t != 0:
        d[h][t] += 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())


def msb(x):
    while x >= 10:
        x //= 10
    return x


n = [[0] * 9 for i in range(9)]

for i in range(1, N + 1):
    l = i % 10
    if l == 0:
        continue
    m = msb(i)
    n[l - 1][m - 1] += 1

ans = 0
for i in range(9):
    for j in range(9):
        ans += n[i][j] * n[j][i]
print(ans)

Solution (Python):
import sys
from functools import lru_cache

d={}

N=int(input())

for i in range(1,N+1):
    start=int(str(i)[0])
    end=int(str(i)[-1])
    tmp=d.get((start,end),0)
    d[(start,end)]=tmp+1

retval=0

for i in range(1,10):
    for j in range(1,10):
        retval+=d.get((i,j),0)*d.get((j,i),0)

print(retval)

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

num = [[0 for i in range(10)] for j in range(10)]

for i in range(1,N+1):
    if i % 10 > 0:
        num[int(str(i)[0])][i % 10] += 1



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

Solution (Python):
N = int(input())
table = [[0]*9 for _ in range(9)]
for n in range(1,N+1):
    tail = str(n)[-1]
    if tail == '0':
        continue
    head = str(n)[0]
    table[int(head)-1][int(tail)-1] += 1
ans = 0


for i in range(9):
    for j in range(i+1):
        
        if i == j:
            temp = table[i][j]*table[j][i]
        else:
            temp = 2*table[i][j] * table[j][i]
        ans += temp




print(ans)

Solution (Python):

import sys


def solve(N: int):
    x=[[0]*10 for _ in range(10)]
    for i in range(N):
        h=int(str(i+1)[0])
        t=(i+1)%10
        x[h][t]+=1

    ans=0
    for i in range(9):
        ans+=x[i+1][i+1]**2

    for i in range(9):
        for j in range(8-i):
            ans+=x[i+1][j+i+2]*x[j+i+2][i+1]*2

    print(ans)
    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):
import sys
sys.setrecursionlimit(10000000)

from bisect import *
from collections import *
from heapq import *



INF = 500000
mod = 10**9+7

N = int(input())
dic = defaultdict(lambda : 0)
for i in range(1, 10):
    for j in range(1, 10):
        if i == j:
            if i <= N:
                dic[(i, j)] = 1
        for k in range(1, 6):
            l = 10**k
            x = i*l + j
            if N < x:
                break
            if x + l-10 <= N:
                dic[(i, j)] += l//10
            else:
                n = N%10
                if n < j:
                    dic[(i, j)] += N%l//10
                else:
                    dic[(i, j)] += N%l//10+1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += dic[(i, j)] * dic[(j, i)]
print(ans)

Solution (Python):
import copy
from collections import defaultdict


num = int(input())

d = defaultdict(int)
for i in range(1, num+1):
    s = str(i)
    start = int(s[0])
    end = int(s[-1])
    d[(start, end)] += 1

ans = 0
queue = sorted(d.keys())
for (start, end) in queue:
    if start > end:
        continue
    elif start == end:
        ans += d[(start, end)] ** 2
    else:
        ans += (d[(start, end)] * d[(end, start)]) * 2

print(ans)

Solution (Python):
n = int(input())
strn = str(n)
keta = len(strn)
n_head = int(strn[0])
n_tail = int(strn[-1])
naka = strn[1:-1]
if len(naka)  == 0:
    nakan = 0
else:
    nakan = int(naka)

ans = 0

if keta <= 2:
    for i in range(1,n+1):
        for j in range(1,n+1):
            head1 = int(str(i)[0])
            tail1 = int(str(i)[-1])
            head2 = int(str(j)[0])
            tail2 = int(str(j)[-1])
            if head1 == tail2 and head2 == tail1:
                ans += 1
                
    print(ans)

else:
    hoj = []
    tmp = 1
    mog = 1
    for i in range(keta):
        hoj.append(mog)
        tmp = tmp*10
        mog = mog + tmp
    
    
    for i in range(1,n+1):
        head = int(str(i)[0])
        tail = int(str(i)[-1])
        if tail == 0:
            continue

        if tail < n_head:
            ans += hoj[keta-2]
        elif tail > n_head:
            ans += hoj[keta-3]
        else:
            ans += hoj[keta-3]
            if head <= n_tail:
                ans += nakan + 1
            else:
                ans += max(nakan ,0)

        if head == tail:
            ans += 1

        
    print(ans)

Solution (Python):
n=int(input())
k=len(str(n))
f=int(str(n)[0])
l=int(str(n)[-1])
if k==1:
    print(n)
    exit()
m=(n%(10**(k-1))-l)//10
ans=0
for i in range(1,10):
    if i<f:
        ans+=((10**(k-1)-1)//9+1)**2
    elif i==f:
        if i<=l:
            ans+=((10**(k-2)-1)//9+m+2)**2
        else:
            ans+=((10**(k-2)-1)//9+m+1)**2
    else:
        ans+=((10**(k-2)-1)//9+1)**2
for i in range(1,10):
    for j in range(1,10):
        if i!=j:    
            if i<f:
                a=(10**(k-1)-1)//9
            elif i==f:
                if j<=l:
                    a=(10**(k-2)-1)//9+m+1
                else:
                    a=(10**(k-2)-1)//9+m
            else:
                a=(10**(k-2)-1)//9
            if j<f:
                b=(10**(k-1)-1)//9
            elif j==f:
                if i<=l:
                    b=(10**(k-2)-1)//9+m+1
                else:
                    b=(10**(k-2)-1)//9+m
            else:
                b=(10**(k-2)-1)//9
            ans+=a*b
print(ans)

Solution (Python):
N = int(input())
str_N = str(N)
digits = len(str_N)
lim = min(str_N[0], str_N[-1])



count = 0
for a in range(1,N+1):
    str_a = str(a)
    a_s = str_a[0]
    a_e = str_a[-1]
    dig_a = len(str_a)
    
    if int(a_e) > 0:
        
        if a_s == a_e:
            count += 1
        
        for dig_b in range(2,digits):
            count += 10**(dig_b-2)
            
        reverse = int(a_e)*10**(digits-1) + int(a_s)
        if reverse <= N:
            if int(a_e) < int(str_N[0]):
                count += 10**(digits-2)
            else:
                count += (N - reverse)//10 + 1

    

print(count)

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

tab = [[0] * 9 for _ in range(9)]

for head in range(1, 10):
    for tail in range(1, 10):
        val = 0
        if head == tail and head <= N: val += 1
        
        N_tmp = head * 10 + tail
        for level in range(100):
            N_nxt = (N_tmp - tail) * 10 + tail
            if N_nxt > N:
                val += min(max(0, (N - N_tmp) // 10 + 1), 10 ** level)
            else:
                val += 10 ** level
            
            N_tmp = N_nxt
        
        tab[head - 1][tail - 1] = val
        



ans = 0
for head in range(1, 10):
    for tail in range(1, 10):
        ans += (tab[head - 1][tail - 1] * tab[tail - 1][head - 1])

print(ans)

Solution (Python):
def main():
    n = int(input())
    num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    nines = {(i, j): 0 for i in num for j in num}

    for i in range(1, n+1):
        if i % 10 == 0:
            continue
        s = str(i)
        ht = (int(s[0]), int(s[-1]))
        nines[ht] += 1

    print(sum(nines[i, j] * nines[j, i] for i in num for j in num))


if __name__ == '__main__':
    main()

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

cnt=[[0,0,0,0,0,0,0,0,0,0] for _ in range(10)]
ans = 0


for i in range(n+1):
    i = list(str(i))
    cnt[int(i[0])][int(i[-1])] += 1

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

print(ans)

Solution (Python):

n = int(input())

istart_jend = [[0 for _ in range(10)] for i in range(10)]
base = 1

for i in range(1,n+1):
    if i%(base*10)==0:
        base*=10
    istart_jend[i//base][i%10]+=1

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

Solution (Python):

n = int(input())

l = [[0 for i in range(10)] for j in range(10)]

ans = 0
for i in range(n):
    s = str(i+1)
    t = len(s)
    u = int(s[0])
    v = int(s[len(s)-1])

    if u == 0:
        continue
    if v == 0:
        continue

    if u == v:
        ans += 1

    z = 2 * l[v][u]
    ans += z

    l[u][v] += 1

print(ans)

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

def count(a1,a2):
    res = 0
    for b in range(1,N+1):
        sb = str(b)
        if sb[0]==a2 and sb[-1]==a1:
            res += 1
    return res

d = {}
for a1 in range(1,10):
    for a2 in range(10):
        d[(str(a1),str(a2))] = count(str(a1),str(a2))

ans = 0
for a in range(1,N+1):
    sa = str(a)
    a1 = sa[0]
    a2 = sa[-1]
    ans += d[(a1,a2)]

print(ans)

Solution (Python):


n = int(input())
mtrx = [[0 for _ in range(10)] for _ in range(10)]

for i in range(1, n + 1):
    num = str(i)
    s = int(num[0]) - 1
    e = int(num[-1]) - 1

    mtrx[s][e] += 1
    
sumv = 0
for i in range(10):
    for j in range(10):
        sumv += mtrx[i][j] * mtrx[j][i]
print(sumv)

Solution (Python):

import itertools
import sys


def solve(N: int):
    sN = str(N)
    lN = len(sN)
    if lN == 1:
        print(N)
        return
    l1 = int(sN[0])
    l2 = int(sN[-1])
    d = {}
    for k1, k2 in itertools.product(range(1, 10), repeat=2):
        p = 10 ** (lN - 2) // 9
        if k1 == k2:
            p += 1
        if k1 < l1:
            p += 10 ** (lN - 2)
        elif k1 == l1:
            p += int(sN[1:-1] or "0")
            if k2 <= l2:
                p += 1
        d[k1, k2] = p

    ans = 0
    for k1, k2 in itertools.product(range(1, 10), repeat=2):
        ans += d[k1, k2] * d[k2, k1]
    print(ans)


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
for i in range(1,n+1):
    a=str(i)[0]
    b=str(i)[-1]
    for digit in range(1,7):
        if digit == 1:
            if a==b:
                ans+=1
        elif b!='0':
            mn = int(b+'0'*(digit-2)+a)
            mx = int(b+'9'*(digit-2)+a)
            if mn>n:
                break
            if mx<=n:
                ans+=10**(digit-2)
            else:
                ns=str(n)
                for j in range(1,len(ns)-1):
                    ans+=int(ns[j])*10**(max(digit-2-j,0))
                if ns[-1]>=a:
                    ans+=1
                break


print(ans)

Solution (Python):
N = int(input())
a = {}
ans = 0
for i in range(1,10):
    for j in range(1,10):
        a[i,j] = 0

for i in range(1,N+1):
    top = int(str(i)[0])
    low = int(str(i)[-1])
    if top == 0 or low == 0:
        continue
    a[top, low] += 1
    if top == low:
        ans += a[low,top] * 2 -1
    else:
        ans += a[low,top] * 2


print(ans)

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

num_end_dic={}
for i in range(1,N+1):  
  digit_i=len(str(i))
  head=i//(10**(digit_i-1))
  tail=i%10
  
  if tail==0:
    continue
  elif (head,tail) not in num_end_dic:
    num_end_dic[(head,tail)]=1
  else:
    num_end_dic[(head,tail)]+=1
  

answer=0
for h,t in num_end_dic:
  if (t,h) in num_end_dic:
    answer+=num_end_dic[(h,t)]*num_end_dic[(t,h)]

print(answer)

Solution (Python):
import math

N = int(input())

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

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

print(ans)

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

BegEndTbl = []
for _ in range(10):
    BegEndTbl.append([0] * 10)

ans = 0
for i in range(1, N + 1):
    left = int(str(i)[0])
    right = i % 10
    if left == right:
        ans += 1
    ans += BegEndTbl[right][left] * 2
    BegEndTbl[left][right] += 1
print(ans)

Solution (Python):
N = int(input())
d = dict()
for i in range(1, 10):
    for j in range(1, 10):
        d[str(i) + str(j)] = 0

for i in range(1, N + 1):
    if str(i)[-1] != '0':
        d[str(i)[0] + str(i)[-1]] += 1

result = 0
for i in range(1, 10):
    for j in range(1, 10):
        result += d[str(i) + str(j)] * d[str(j) + str(i)]

print(result)

Solution (Python):
n=int(input())
a=[]
b=[]
c=0
for i in range(10):
    for j in range(10):
        a.append(0)
    b.append(a)
    a=[]
for i in range(n):
    b[int(str(i+1)[0])][int(str(i+1)[-1])]+=1
for i in range(10):
    for j in range(10):
        c+=b[i][j]*b[j][i]
print(c)

Solution (Python):
from fractions import gcd


import heapq
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7



def lcm(a, b):
    return a * b / fractions.gcd(a, b)


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors


def factorize(n):
    fct = []  
    b, e = 2, 0  
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct


def is_prime(n):
    if n == 1:
        return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True


def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

def modpow(a, n, mod):
    res = 1
    while n > 0:
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res


def modinv(a, mod):
    return modpow(a, mod - 2, mod)


def cnk(a, b):
    MOD = 10**9+7
    ret = 1
    for i in range(b):
        ret *= (a-i)
        ret %= MOD
        ret = ret * modinv(i+1, MOD) % MOD
    return ret




def main():
    
    
    
    
    
    
    
    
    
    
    
    
    
    n = int(input())
    d = defaultdict(int)
    for i in range(1,n+1):
        if i < 10:
            d[(i, i)] += 1
        else:
            d[(int(str(i)[-1]), int(str(i)[0]))] += 1
    
    ans = 0
    for i in range(1, n+1):
        if i < 10:
            ans +=d[(i, i)]
        else:
            if int(str(i)[-1]) == int(str(i)[0]):
                ans += d[(int(str(i)[0]), int(str(i)[-1]))]
            else:
                ans += d[(int(str(i)[0]), int(str(i)[-1]))]
    print(ans)

            


    




    


if __name__ == '__main__':
    main()

Solution (Python):
def up(m):
    while m>0:
        if m//10>0:
            m=m//10
        else:
            break
    return m


n=int(input())
a=[[0]*9 for x in range(9)]
for c in range(1,n+1):
    i=c%10
    if i==0:
        continue
    j=up(c)
    a[i-1][j-1]+=1
t=0
for i in range(9):
    for j in range(9):
        t+=a[i][j]*a[j][i]
print(t)

Solution (Python):
n=int(input())
C=[]
for i in range(9):
    C.append([0]*9)
for i in range(1,n+1):
    l=int(str(i)[0])
    r=int(str(i)[-1])
    if r!=0:
        C[l-1][r-1]+=1
ans=0
for i in range(9):
    for j in range(9):
        ans+=C[i][j]*C[j][i]
print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())
di = defaultdict(int)

for i in range(1, N+1):
    k = str(i)[0] + str(i)[-1]
    di[k] += 1


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

print(ans)

Solution (Python):
n = int(input())
table = [[0 for _ in range(9)] for _ in range(9)]

for i in range(1, n+1):
    s = str(i)
    f, l = s[0], s[-1]
    if "0" in [f, l]: continue
    table[int(f)-1][int(l)-1] += 1

ans = 0
for i in range(9):
    for j in range(9):
        ans += table[i][j] * table[j][i]

print(ans)

Solution (Python):
n=int(input())
lists=[[ [] for j in range(10)]for i in range(10)]
for i in range(1,n+1):
    a=str(i)
    lists[int(str(a[0]))][int(str(a[-1]))].append(i)
c=0
for i in range(1,10):
    for j in range(1,10):
        c+=len(lists[i][j])*len(lists[j][i])
print(c)

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

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

arr[1][1] += 1
arr[2][2] += 1
arr[3][3] += 1
arr[4][4] += 1
arr[5][5] += 1
arr[6][6] += 1
arr[7][7] += 1
arr[8][8] += 1
arr[9][9] += 1

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

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

sum_diag = 0
sum_other = 0

for i in range(1, 10):
  for j in range(1, 10):
    if i == j:
      sum_diag += arr[i][j]*arr[i][j]
    else:
      sum_other += arr[i][j] * arr[j][i] * 2



print(int(sum_diag + sum_other/2))

Solution (Python):
n=int(input())
ans=0
import itertools
for i in range(1,n+1):
    p,q=str(i)[0],str(i)[-1]
    if int(p)*int(q)==0:
        continue
    if len(str(i))>=3:
        if p<q:
            ans+=int("1"*(len(str(i))-2))*2
        elif p>q:
            ans+=int("1"*(len(str(i))-1))*2
        else:
            ans+=int("1"*(len(str(i))-2))*2
            ans+=(int(str(i)[1:-1])+1)*2
            ans+=1
    elif len(str(i))==1:
        ans+=1
    else:
        if p>q:
            ans+=2
        elif p==q:
            ans+=3
print(ans)

Solution (Python):
n=int(input())
a=[[0]*9 for i in range(9)]
for i in range(1,n+1):
    x,y =int(str(i)[0]), int(str(i)[-1])
    if y>0:
        a[x-1][y-1]+=1
count=0
for i in range(9):
    for j in range(9):
        if i>j:
            continue
        if i==j:
            count+=a[i][j]**2
        else:
            count+=a[i][j]*a[j][i]*2
print(count)

Solution (Python):
n = int(input())
if n<10:
  print(n)
  exit()
ans = 0
for i in range(10,n+1):
  if i%10 == 0:
    continue
  x,y = str(i)[0],str(i)[-1]
  x,y = int(x),int(y)
  k = len(str(n))
  for j in range(1,7):
    if (y+1)*10**j+x-10<=n:
      ans += 10**(j-1)
    else:
      if y*10**j+x>n:
        break
      else:
        ans += (n-y*10**j-x)//10+1
        break
for i in range(1,10):
  for j in range(1,7):
    if (i+1)*10**j+i-10<=n:
      ans += 10**(j-1)*2
    else:
      if i*10**j+i>n:
        break
      else:
        ans += ((n-i*10**j-i)//10+1)*2
        break
ans += 9
print(ans)

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

lN = int(str(N)[0])
rN = int(str(N)[-1])
dN = len(str(N))

ans = 0
for a in range(1, N+1):
    la = int(str(a)[0])
    ra = int(str(a)[-1])
    
    if la == 0 or ra == 0:
        continue
    
    if la == ra:
        ans += 1
        
    
    d = 2
    while d <= dN:
        if d == dN:
            if ra < lN:
                ans += 10**(d-2)
                
            elif ra == lN:
                if dN == 2:
                    ans += 1
                    
                elif dN > 2:
                    midN = int(str(N)[1:-1])
                    if rN >= la:
                        ans += midN + 1
                        
                    else:
                        ans += midN
                        
            else:
                ans += 0
                
            break
        ans += 10**(d-2)
        
        d +=1

print(ans)

Solution (Python):
n = int(input())
a = int(str(n)[0])
b = int(str(n)[-1])

l = [[-1]*9 for _ in range(9)]

c = len(str(n))-2

for i in range(1, 10):
    for j in range(1, 10):
        cnt = 0
        if c >= 1:
            a_b = int(str(n)[1:-1])
            for k in range(1, c+1):
                if k == c:
                    if a > i:
                        cnt += 10**k
                    elif a == i:
                        if b >= j:
                            cnt += a_b + 1
                        else:
                            cnt += a_b
                else:
                    cnt += 10**k

        if int(str(i)+str(j)) <= n:
            cnt += 1

        if i == j and i <=n:
            cnt += 1

        l[i-1][j-1] = cnt

ans = 0

for i in range(9):
    for j in range(9):
        ans  += l[i][j] * l[j][i]

print(ans)

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

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

memo = dict()
 
def func(s,f):
    ans = 0
    for i in range(1,n+1):
        if str(i)[0] == f and str(i)[-1] == s:
            ans += 1
    memo[s+" "+f] = ans
 
res = 0

for i in range(1,n+1):
    s = str(i)[0]
    f = str(i)[-1]

    if s + " " + f in memo:
        res += memo[s + " " + f]
    else:
        func(s,f)
        res += memo[s + " " + f]

print(res)

Solution (Python):
n=int(input())
ct=[[0]*10 for i in range(10)]
for i in range(1,10):
  for j in range(1,10):
    if n>=10:
      if i==j:
        ct[i][j]+=1
    if n>=10**2:
      ct[i][j]+=1
    if n>=10**3:
      ct[i][j]+=10
    if n>=10**4:
      ct[i][j]+=100
    if n>=10**5:
      ct[i][j]+=1000
    
    if n<10:
      if n>=i:
        ct[i][j]+=1
    
    elif 10<n<10**2:
      if n>=i*10+j:
        ct[i][j]+=1
    
    elif 10**2<n<10**3:
      if int(str(n)[0])>i:
        ct[i][j]+=10
      elif int(str(n)[0])==i:
        if int(str(n)[-1])>=j:
          ct[i][j]+=int(str(n)[1])+1
        else:
          ct[i][j]+=int(str(n)[1])

    elif 10**3<n<10**4:
      if int(str(n)[0])>i:
        ct[i][j]+=10**2
      elif int(str(n)[0])==i:
        if int(str(n)[-1])>=j:
          ct[i][j]+=int(str(n)[1:3])+1
        else:
          ct[i][j]+=int(str(n)[1:3])

    elif 10**4<n<10**5:
      if int(str(n)[0])>i:
        ct[i][j]+=10**3
      elif int(str(n)[0])==i:
        if int(str(n)[-1])>=j:
          ct[i][j]+=int(str(n)[1:4])+1
        else:
          ct[i][j]+=int(str(n)[1:4])
    
    elif n>10**5:
      if int(str(n)[0])>i:
        ct[i][j]+=10**4
      elif int(str(n)[0])==i:
        if int(str(n)[-1])>=j:
          ct[i][j]+=int(str(n)[1:5])+1
        else:
          ct[i][j]+=int(str(n)[1:5])
ans=0
for i in range(1,10):
  for j in range(1,10):
    ans+=ct[i][j]*ct[j][i]
print(ans)