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):
import numpy as np

N = int(input())

A = np.zeros((10,10))
 
for i in range(1, N+1):
    
    A[int(str(i)[0])][int(str(i)[-1])] += 1 
    
count = 0

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

print(int(count))

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

freq = {}

for i in range(10):
    for j in range(10):
        freq[str(i)+str(j)] = 0
 
for i in range(1, N+1):
    i_str = str(i)

    head_str = i_str[0]
    tail_str = i_str[-1]
    freq[head_str+tail_str] += 1

ans = 0

for i in range(1, N+1):
    i_str = str(i)

    head_str = i_str[0]
    tail_str = i_str[-1]
    ans += freq[tail_str+head_str] 

print(ans)

Solution (Python):
def mainFunc():
    N = int(input())
    matrix = [[0 for i in range(10)] for i in range(10)]
    for i in range(1, N + 1):
        start = int(str(i)[0])
        end = int(str(i)[-1])
        matrix[start - 1][end - 1] += 1
    
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += matrix[i][j] * matrix[j][i]

    print(ans)


mainFunc()

Solution (Python):
import numpy as np
N = int(input())
c = np.array([[0 for x in range(10)] for y in range(10)])
buf_1 = []
buf_2 = 0

def func(A) :
    A = str(A)
    return [int(A[0]),int(A[len(A)-1])]

for i in range(1,N+1) :
    buf_1 = func(i)
    
    c[buf_1[0],buf_1[1]] += 1

for i in range(10) :
    for j in range(10) :
        buf_2 += c[i][j] * c[j][i]

print(buf_2)

Solution (Python):
N = input()
m = len(N)

a0 = int(N[0])
a1 = int(N[-1])

ans = 0
if m == 1:
  ans = int(N)
else:
  mid = (int(N[1:]) - a1)//10
  t = 10**(m-2)
  ans = 9
  ans += ((a0-1)*t) * 2
  ans += (mid + (a1>=a0)) * 2
  ans += ((a0-1)*t)**2
  ans += ((mid*(a0-1) + min(a0-1, a1))*t) * 2
  ans += (mid + (a1>=a0))**2
  if m >= 3:
    ans += (t - 1) * 2
    ans += ((a0-1)*(t - 1)*t) * 2
    ans += (mid * (t - 1)) * 2
    ans += (a1 * (t - 1)//9) * 2
    ans += (t - 1)**2

print(ans)

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

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

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

ans = 0
for i in range(0, 10):
    for j in range(0, i+1):
        if i == j:
            ans += matrix[i][i] ** 2
        else:
            ans += matrix[i][j] * matrix[j][i] * 2

print(ans)

Solution (Python):
import math
N = int(input())
cnt = [[0] * 10 for _ in range(10)]
for i in range(1, N+1):
    d = int(math.log(i, 10))
    cnt[i//(10**d)][i % 10] += 1
ans = 0
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())
C = [[0]*10 for i in range(10)]

def get_ab(n):
    str_n = str(n)
    return map(int, [str_n[0], str_n[-1]])

for i in range(1, N + 1):
    a, b = get_ab(i)
    C[a][b] += 1


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

Solution (Python):
N = int(input())
mem=[[0] * 10 for _ in range(10)]

for i in range(1, N+1):
    s = str(i)
    mem[int(s[0])][int(s[-1])] += 1 
    
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += mem[i][j] * mem[j][i]
print(ans)

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

Solution (Python):
N = int(input())
mem = [[0]*10 for _ in range(10)]

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

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

Solution (Python):
N = int(input())
from collections import defaultdict
d=defaultdict(int)
for i in range(1,N+1):
    a,b=str(i)[0],str(i)[-1]
    d[(int(a),int(b))]+=1
r = 0
for i in range(1,10):
    for j in range(1,10):
        r += d[(i,j)] * d[(j,i)]
print(r)

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

from collections import defaultdict

counter = defaultdict(int)

for x in map(str, range(1, N+1)):
    head = x[0]
    tail = x[-1]
    counter[(head, tail)] += 1

ans = 0
items = list(counter.items())
for (head, tail), cnt in items:
    ans += cnt * counter[(tail, head)]

print(ans)

Solution (Python):
import numpy as np

N = int(input())
tb = np.zeros((10, 10), dtype=int)

for n in range(1, N+1):
    n = str(n)
    lhs, rhs = int(n[0]), int(n[-1])
    tb[lhs][rhs] += 1


ret = 0
for lhs in range(1, 10):
    for rhs in range(1, 10):
       ret += tb[lhs][rhs] * tb[rhs][lhs]

print(ret)

Solution (Python):
def main():
    n = int(input())
    count = 0
    m = dict()
    for i in range(1, n + 1):
        tmp = str(i)
        t = (tmp[0], tmp[-1])
        if t in m:
            m[t] += 1
        else:
            m[t] = 1
    for i in range(1, n + 1):
        tmp = str(i)
        t = (tmp[-1], tmp[0])
        if t in m:
            count += m[t]
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
n = int(input())
counter = defaultdict(int)
for x in map(str, range(1, n+1)):
  head = x[0]
  tail = x[-1]
  counter[(head, tail)] += 1
ans = 0
items = list(counter.items())
for (head, tail), cnt in items:
  ans += cnt * counter[(tail, head)]
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()



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


for i in range(1, n+1):
    s_i = str(i)
    
    first = int(s_i[0])
    last = int(s_i[-1])
    
    
    c[first][last] += 1
    


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

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

l = [0 for _ in range(81)]
for k in range(1, n + 1):
    i = int(str(k)[0])
    j = int(str(k)[-1])
    if i != 0 and j != 0:
        idx = 9 * (i - 1) + (j - 1)
        l[idx] += 1
    
    
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        idx = 9 * (i - 1) + (j - 1)
        idx2 = 9 * (j - 1) + (i - 1)
        ans += l[idx] * l[idx2]
        

print(ans)

Solution (Python):
def get_pair(x):
    s = int(str(x)[0])
    e = x % 10
    return s, e


N = int(input())
freq = {}
for i in range(1, N+1):
    s, e = get_pair(i)
    freq[(s, e)] = freq.get((s, e), 0) + 1
ans = 0
for s, e in freq:
    ans += freq.get((s, e), 0) * freq.get((e, s), 0)
print(ans)

Solution (Python):
import numpy as np
import math

N = int(input())

c = np.zeros((10, 10), dtype=int)
d = np.zeros((10, 10), dtype=int)

for n in range(1, N+1):
    k = math.floor(math.log10(n)) + 1
    
    i = n// (10 ** (k-1))
    
    j = n%10
    
    c[i][j] += 1
    d[j][i] += 1

ans = np.sum(c*d)
print(ans)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    
    d = defaultdict(int)
    for i in range(1,N+1):
        bot,top = str(i)[0],str(i)[-1]
        d[bot,top] += 1
    
    ans = 0
    for i in range(1,N+1):
        top,bot = str(i)[0],str(i)[-1]
        ans += d[bot,top]    
    print(ans)
if __name__ == '__main__':
    main()

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

C_ab = [[0 for i in range(10)] for j in range(10)]
for i in range(1,N+1):
    C_ab[int(str(i)[0])][int(str(i)[-1])] += 1

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

print(count)

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

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

c[0][0]
for x in range(1,N+1):
    X=str(x)
    c[int(X[0])][int(X[-1])]+=1

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

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

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

    for n in range(1, N + 1):
        str_n = str(n)
        top = int(str_n[0])
        bottom = int(str_n[-1])

        c[top][bottom] += 1

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

    print(ans)


main()

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['No', 'Yes']
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


def cmb(n, r):
    if n - r < r:
        r = n - r
    if r == 0:
        return 1
    if r == 1:
        return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


def main():
    N = I()
    NS = str(N)
    ans = 0

    dic = {}

    for j in range(1, 10):
        for k in range(1, 10):
            if j != k:
                dic[10 * j + k] = 0
            else:
                dic[j] = 0

    for i in range(1, N+1):
        if i < 10:
            dic[i] += 1
        else:
            b = int(str(i)[0])
            e = int(str(i)[-1])
            if b == e:
                dic[b] += 1
            elif e != 0:
                dic[10 * b + e] += 1

    
    for j in range(0, 10):
        for k in range(0, 10):
            if 10 * j + k in dic:
                if 10 * j + k < 10:
                    ans += dic[10 * j + k] * dic[10 * j + k]
                else:
                    ans += dic[10 * j + k] * dic[10 * k + j]
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):

import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()

dic = defaultdict(int)
for i in range(1, n+1):
    dic["{},{}".format(str(i)[0], str(i)[-1])] += 1



ans = 0
for n in dic.copy():
    s, e = n.split(",")
    
    
    
    
    ans += dic[n] * dic["{},{}".format(e, s)]
print(ans)        

exit()
c = [[0] * 10] * 10

Solution (Python):
def bunkai(n:int):
    ls=list(str(n))
    top,bottom=int(ls[0]),int(ls[-1])
    if n<=99:
        middle=0
    else:
        middle=int("".join(ls[1:-1]))
    return top, middle, bottom

n=int(input())
Nt,Nm,Nb = bunkai(n)

count=0
for i in range(1,n+1):
    t,m,b = bunkai(i)
    if b==0:continue
    if t==b:count+=1
    if n<=99:
        if int(str(b)+str(t))<=n:count+=1
    else:
        keta = len(str(n))
        for x in range(keta-2):
            count+=10**x
        x+=1
        if Nt>b:
            count+=10**x
        elif Nt==b:
            if Nb>=t:
                count+=Nm+1
            else:
                count+=Nm
print(count)

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

Solution (Python):
N = int(input())
count = [[0]*10 for _ in range(10)]
for i in range(1,N+1):
    s = str(i)
    count[int(s[0])][int(s[-1])] += 1
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):
N=int(input())
C=[[0]*9  for i in range(9)]
count=0

for n in range(1,N+1):
   if str(n)[-1]!="0":
      left=int(str(n)[0])-1
      right=int(str(n)[-1])-1
      C[left][right]+=1
      
for i in range(1,10):
   for j in range(1,10):
      count+=C[i-1][j-1]*C[j-1][i-1]

print(count)

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

def td(x):
    a = x % 10
    b = 0
    while(x):
        b = x
        x //= 10
    return (a, b)

pc = dict()
for i in range(N):
    p = td(i + 1)
    if p in pc:
        pc[p] += 1
    else:
        pc[p] = 1
ans = 0
for i in range(N):
    p = td(i + 1)
    q = (p[1], p[0])
    if q in pc:
        ans += pc[q]
print(ans)

Solution (Python):
import math

N = int(input())





def calc(A):
    A_str = str(A)
    
    m = len(A_str)    
    
    a = int(A_str[0])
    
    b = int(A_str[-1])

    
    
    
    if b == 0:
        
        return 0
    elif m==1:
        
        return 0
    elif m==2:
        
        if a < b:
            return 0
        else:
            return 1
    else:        
        
        if a < b:
            
            X = 0
            for i in range(0,m-2):
                X += 10**i

            return X
            
        elif a==b:
            
            X = int(A_str[1:-1])
            
            for i in range(0,m-2):
                X += 10**i

            
            X = X + 1
            return X
        else:
            
            X = 0
            for i in range(0,m-1):
                X += 10**i
                
            return X


def calc2(A):
    A_str = str(A)
    
    m = len(A_str)    
    
    a = int(A_str[0])
    
    b = int(A_str[-1])

    if m==1:
        return 1
    else:
        if a==b:
            return 1
        else:
            return 0

num1 = 0
num2 = 0
for A in range(1,N+1):
    num1 += calc(A)
    num2 += calc2(A)


print(num1*2 + num2)

Solution (Python):
import numpy as np
import math

N = int(input())

C = np.zeros([10,10], dtype = int)
D = np.zeros([10,10], dtype = int)

for n in range(1,N+1):
    k = math.floor(math.log10(n)) + 1
    i = n//10**(k-1)
    j = n%10
    C[i][j] += 1
    D[j][i] += 1

ans = np.sum(C*D)
print(ans)

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

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

for i in range(1,n+1):
    b = int(str(i)[0])
    c = int(str(i)[-1])
    a[b][c] += 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):
from collections import defaultdict
n = int(input())

def f(x):
    a1 = int(str(x)[0])
    a2 = x%10
    return (a1, a2)

count = defaultdict(int)
for x in range(1, n+1):
    key = f(x)
    count[key] += 1

ans = 0
for x in range(1, n+1):
    a0, a1 = f(x)
    if a1 != 0:        
        key = (a1, a0) 
        ans += count[key]

print(ans)

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

c = [[0 for _ in range(10)] for __ in range(10)]
for i in range(1, n+1):
    str_i = str(i)
    head = int(str_i[0])
    tail = int(str_i[-1])
    c[head][tail] += 1

cnt = 0
for i in range(10):
    for j in range(10):
        cnt += c[i][j] * c[j][i]

print(cnt)

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

Solution (Python):
N=int(input())
lenN=len(str(N))
lst=[]
lst2=[[0 for m in range(10)] for i in range(10)]

for i in range(11,100):
    if "0" not in str(i):
        lst.append(str(i))
for i in range(1,N+1):
    if "0" not in [int(str(i)[0]),int(str(i)[-1])]:
        lst2[int(str(i)[0])][int(str(i)[-1])]+=1

count=0
for a in lst:
    for b in lst:
        if a[0]==b[-1] and b[0]==a[-1]:
            b=lst2[int(a[0])][int(a[-1])]*lst2[int(b[0])][int(b[-1])]
            count+=(b) if b>1 else b
print(count)

Solution (Python):
from collections import defaultdict
n = int(input())

ans = 0

f = lambda x: (int(str(x)[0]), int(str(x)[-1]))

freq = defaultdict(int)
for i in range(1, n+1):
    p = f(i)
    freq[p] += 1

for i in range(1, n+1):
    p = f(i)
    q = (p[1], p[0])
    ans += freq[q]
print(ans)

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 += d[k]*d.get(k2,0)

print(ans)

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

c = [[0 for _ in range(9)] for _ in range(9)]

for n in range(1, N + 1):
  l = n % 10
  if l == 0:
    continue
  t = n // (10 ** (len(str(n)) - 1))
  c[t - 1][l - 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):
n = int(input())
c = [[0] * 10 for _ in range(10)]
for i in range(1, n + 1):
  p = int(str(i)[0])
  q = int(str(i)[-1])
  c[p][q] += 1

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

print(ans)

Solution (Python):
from collections import defaultdict

n=int(input())
counter=defaultdict(int)

for x in map(str,range(1,n+1)):
    head=x[0]
    tail=x[-1]
    counter[(head,tail)]+=1

ans=0
items=list(counter.items())
for (head, tail), cnt in items:
    ans+=cnt*counter[(tail, head)]

print(ans)

Solution (Python):
def resolve():

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

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

        str_k = str(k)

        i = int(str_k[0])
        j = int(str_k[-1])

        mat[i][j] += 1

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

    print(ans)
    
resolve()

Solution (Python):
def main():
    n = int(input())
    dic = {}
    for i in range(10):
        for j in range(10):
            dic[(i, j)] = 0
    for num in range(1, n+1):
        str_num = str(num)
        i = int(str_num[0])
        j = int(str_num[-1])
        dic[(i, j)] += 1

    total = 0
    for i in range(10):
        for j in range(10):
            total += dic[(i, j)] * dic[(j, i)]
    print(total)


if __name__ == "__main__":
    main()

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

    freq = np.zeros((10, 10))
    for i in range(1, N+1):
        s = str(i)
        head = int(s[0])
        tail = int(s[-1])
        freq[head][tail] += 1
        
    ans = 0
    for i in range(1, N+1):
        s = str(i)
        head = int(s[0])
        tail = int(s[-1])
        ans += freq[tail][head]      

    print(int(ans))
    

if __name__ == "__main__":
    resolve()

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

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



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



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

print(ans)

Solution (Python):
n = int(input())
le = [[0] * 10 for i in range(10)]
ri = [[0] * 10 for i in range(10)]
for i in range(1,n+1):
    le[int(str(i)[-1])][int(str(i)[0])] += 1
    ri[int(str(i)[0])][int(str(i)[-1])] += 1
ans = 0
for i in range(10):
    for j in range(10):
        ans += le[j][i] * ri[j][i]
print(ans)

Solution (Python):
def main():
    n = int(input())
    dic = {}
    for i in range(10):
        for j in range(10):
            dic[(i, j)] = 0
    for num in range(1, n+1):
        str_num = str(num)
        i = int(str_num[0])
        j = int(str_num[-1])
        dic[(i, j)] += 1

    total = 0
    for i in range(10):
        for j in range(10):
            total += dic[(i, j)] * dic[(j, i)]
    print(total)


if __name__ == "__main__":
    main()

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

from collections import defaultdict

m = defaultdict(lambda: 0)

for a in range(1, n + 1):
  s = str(a)
  f = int(s[0])
  l = int(s[-1])
  m[(f, l)] += 1

ans = 0
for b in range(1, n + 1):
  s = str(b)
  f = int(s[0])
  l = int(s[-1])
  ans += m[(l, f)]

print(ans)

Solution (Python):
from itertools import product

N = input()
l = len(N)
a = int(N[0])
b = int(N[-1])

if l == 1:
    print(l)
    exit()
elif l == 2:
    print(9 + min(a, b) ** 2 * 2)
    exit()

N = N[1:-1]
table = [[0] * 10 for _ in range(10)]
for i in range(1, 10):
    table[i][i] = 1

for i, j in product(range(1, 10), repeat=2):
    table[i][j] += sum(10 ** (l - i) for i in range(3, l + 1))
    if i < a:
        table[i][j] += 10 ** (l - 2)
    elif i == a:
        if b < j:
            table[i][j] += sum((int(N[-i - 1])) * 10 ** i for i in range(l - 2))
        else:
            table[i][j] += sum((int(N[-i - 1])) * 10 ** i for i in range(l - 2)) + 1

answer = 0
for i, j in product(range(1, 10), repeat=2):
    answer += table[i][j] * table[j][i]
    

print(answer)

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

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

ans = 0
for i in range(1,n + 1):
    s = str(i)
    if s[0] == "0" or s[-1] == "0":
        continue
    else:
        d[int(s[0])][int(s[-1])] += 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())
cnt=0

c=[[0 for _ in range(10)] for _ in range(10)]
for i in range(1,n+1):
  l=int(str(i)[0])
  r=int(str(i)[-1])
  c[l][r]+=1
'''
for i in range(10):
  for j in range(10):
    print(c[i][j], end=' ')
  print('')
'''
sum=0
for i in range(1,10):
  for j in range(1,10):
    sum+=c[i][j]*c[j][i]
print(sum)

Solution (Python):
from itertools import product

N = input()
l = len(N)
a = int(N[0])
b = int(N[-1])

if l == 1:
    print(l)
    exit()
elif l == 2:
    print(9 + min(a, b) ** 2 * 2)
    exit()

N = N[1:-1]
table = [[0] * 10 for _ in range(10)]
for i in range(1, 10):
    table[i][i] = 1

for i, j in product(range(1, 10), repeat=2):
    table[i][j] += sum(10 ** (l - i) for i in range(3, l + 1))
    if i < a:
        table[i][j] += 10 ** (l - 2)
    elif i == a:
        if b < j:
            table[i][j] += sum((int(N[-i - 1])) * 10 ** i for i in range(l - 2))
        else:
            table[i][j] += sum((int(N[-i - 1])) * 10 ** i for i in range(l - 2)) + 1

answer = 0
for i, j in product(range(1, 10), repeat=2):
    answer += table[i][j] * table[j][i]
    

print(answer)

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

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

for n in range(1, N+1):
    first = int(str(n)[0])
    last = int(str(n)[-1])
    ctable[first][last] += 1

for i in range(10):
    for j in range(10):
        ans += ctable[i][j] * ctable[j][i]

print(ans)

Solution (Python):
from collections import defaultdict

N=input()

L=defaultdict(lambda: 0) 
for i in range(1,N+1):
    i=str(i)
    begin=int(str(i)[0])
    end=int(str(i)[-1])
    L[(begin,end)]+=1


ans=0
for begin in range(1,10):
    for end in range(1,10):
        ans+=L[(begin,end)]*L[(end,begin)]

print ans

Solution (Python):
def resolve():
    N = int(input())
    c = [[0] * 10 for _ in range(10)]
    for k in map(str, range(1, N+1)):
        i = int(k[0])
        j = int(k[-1])
        c[i][j] += 1
    n = 0
    for i in range(10):
        for j in range(10):
             n += c[i][j] * c[j][i]
    print(n)

resolve()

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

c = []
for i in range(10):
    c.append([0] * 10)
    

for i in range(N):
    s = str(i+1)
    h = int(s[0])
    r = int(s[-1])
    c[h][r] += 1
    
    
ans = 0
for i in range(10):
  for j in range(10):
    ans += c[i][j]*c[j][i]
    
print(ans)

Solution (Python):
N = int(input())
c = [[0]*10 for _ in range(10)]
for k in range(1,N+1):
    tmp = str(k)
    i = int(tmp[0])
    j = int(tmp[-1])
    c[i][j] += 1
ans = 0
for i in range(10):
    for j in range(10):
        ans += c[i][j] * c[j][i]
print(ans)

Solution (Python):

N = int(input())
res = 0
import collections
cnt = collections.Counter()
for i in range(1, N+1):
    str_i = str(i)
    cnt[str_i[0] + str_i[-1]] += 1
res = 0

for k, v in cnt.items():
    if k[0] == k[-1]:
        res += v * v
    elif k[-1]+k[0] in cnt:
        res += v * cnt[k[-1]+k[0]]
print(res)

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

    c = [[0] * 10 for _ in range(10)]
    for k in range(1, N + 1):
        c[int(str(k)[0])][int(str(k)[-1])] += 1
    return sum([c[i][j] * c[j][i] for i in range(10) for j in range(10)])

print(d_handstand2())

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

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

for k in range(1,n+1):
    pre = k
    now = k
    j = k%10
    while now>=1:
        pre = now
        now //= 10
    c[pre][j] += 1    


ans = 0

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

Solution (Python):
n = int(input())
li = []
for i in range(9):
  li_2 = []
  for j in range(9):
    li_2.append(0)
  li.append(li_2)

ans = 0
for i in range(1,n+1):
  i = str(i)
  t = int(i[0])
  f = int(i[-1])
  if f != 0:
    li[t-1][f-1] += 1
for i in range(9):
  for j in range(9):
    cnt =  li[i][j]*li[j][i]
    ans += cnt
print(ans)

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

c = [[0 for i in range(10)] for j in range(10)]
for k in range(1, n+1):
    str_k = str(k)
    i = int(str_k[0])
    j = int(str_k[-1])
    c[i][j] += 1

answer = 0
for i in range(10):
    for j in range(10):
        answer += c[i][j] * c[j][i]

print(answer)

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

Solution (Python):
import numpy as np

N = int(input())
dis = np.zeros((10,10))

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

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

Solution (Python):
N=input()

L={}
for start in range(1,10):
    for end in range(1,10):
        t=0
        if start==end:
            n=start
            if n<=N: t+=1

        n=start*10**1+end
        if n<=N: t+=1

        for i in range(10):
            n=start*10**2+i*10**1+end
            if n<=N:
                t+=1

        for i in range(10):
            for ii in range(10):
                n=start*10**3+i*10**2+ii*10**1+end
                if n<=N:
                    t+=1

        for i in range(10):
            for ii in range(10):
                for iii in range(10):
                        n=start*10**4+i*10**3+ii*10**2+iii*10**1+end
                        if n<=N: t+=1


        for i in range(10):
            for ii in range(10):
                for iii in range(10):
                    for iiii in range(10):
                        n=start*10**5+i*10**4+ii*10**3+iii*10**2+iiii*10**1+end
                        if n<=N: t+=1
        L[(start,end)]=t
    

ans=0

for start in range(1,10):
    for end in range(1,10):
        ans+=L[(start,end)]*L[(end,start)]

print ans

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

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
candidate = []
ans = 0

import itertools

for i in range(1, n+1):
    if (i % 10) != 0:
        candidate.append(str(i))
for i, j in (itertools.product(['1', '2', '3', '4', '5', '6', '7', '8', '9'], repeat=2)):

    i_j = 0
    j_i = 0
    for k in candidate:
        start, end = k[0], k[-1]
        
        if start == i and end == j:
            i_j += 1
        if start == j and end == i:
            j_i += 1
    ans += (i_j * j_i)

print(ans)

Solution (Python):


n = int(input())

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



for i in range(1, n+1):
    s = str(i)
    
    head = int(s[0])
    
    tail = int(s[-1])
    
    cnt[head][tail] += 1



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