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 sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
INF = 10**9
mod = 10**9+7

N = i()
table = [[0]*9 for _ in range(9)]
for i in range(N):
    s = str(i+1)
    a,b = int(s[0]),int(s[-1])
    if b == 0:
        continue
    table[a-1][b-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):
import sys
from io import StringIO
import unittest
import os
import math


sys.setrecursionlimit(999999999)



def digit_val(number, n) -> int:
    return number // 10 ** n % 10


def max_digit_val(number) -> int:
    return number // 10 ** int(math.log10(number)) % 10


def min_digit_val(number) -> int:
    return number // 10 ** 0 % 10


def int_len(number) -> int:
    return int(math.log10(number) + 1)



def resolve(test_def_name=""):
    n = int(input())

    
    target_list = []
    ignore_cnt = 0
    for i in range(1, n + 1):
        ignore_cnt += 1
        if ignore_cnt == 10:
            ignore_cnt = 0
            continue
        target_list.append(i)

    
    cnt_dict = {}
    for i in target_list:
        if int_len(i) == 1:
            
            cnt_dict[i * 10 + i] = cnt_dict.get(i * 10 + i, 0) + 1
        else:
            val = max_digit_val(i) * 10 + min_digit_val(i)
            cnt_dict[val] = cnt_dict.get(val, 0) + 1

    
    ans = 0
    for i in target_list:
        if int_len(i) == 1:
            ans += cnt_dict.get(i * 10 + i, 0)
        else:
            val = min_digit_val(i) * 10 + max_digit_val(i)
            ans += cnt_dict.get(val, 0)

    print(ans)



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """25"""
        output = """17"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """1"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """100"""
        output = """108"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """2020"""
        output = """40812"""
        self.assertIO(test_input, output)

    def test_input_5(self):
        test_input = """200000"""
        output = """400000008"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.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=i%10
    if i<10:
        b=i
    c=i
    while c>=10:
        b=c//10
        c=b
    L[b][a]+=1
num=0
for i in range(10):
    for u in range(10):
        if L[i][u]!=0:
            num+=L[i][u]*L[u][i]
print(num)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

for i in range(1, n + 1):
    s = str(i)
    le = len(s)
    t1 = s[0]
    t2 = s[le - 1]
    memo[int(t1)][int(t2)] += 1

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

print(ans)

Solution (Python):


import copy
import random
import bisect 
import fractions 
import math
import sys
import collections
from decimal import Decimal 

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))


N = int(input())


ht = {}
for i in range(1, 10):
    for j in range(1, 10):
        ht[(i, j)] = 0

for i in range(N + 1):
    a = str(i)
    h, t = map(int, [a[0], a[-1]])
    if (h,t) in ht.keys():
        ht[(h,t)] += 1


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

print(ans)

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

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

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

def f(l, r):
  cnt = 0
  if l == r and l <= N:
    cnt += 1
  if l*10 + r <= N:
    cnt += 1
  for c in range(1, 5):
    x = l*10**(c+1) + r
    i = 0
    I = int("9"*c)
    while x <= N and i <= I:
      cnt += 1
      x += 10
      i += 1
  return cnt

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

for i in range(9):
  for j in range(9):
    C[i][j] = f(i+1, j+1)

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

print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
d = {(str(p),str(q)): 0 for p in range(1,10) for q in range(1,10)}
for i in range(1, n+1):
    c = str(i)
    if c[-1]!="0":
        d[c[0], c[-1]] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        if i==j:
            v = d[str(i), str(j)]
            ans += v * v
        else:
            v1 = d[str(i), str(j)]
            v2 = d[str(j), str(i)]
            ans += v1*v2
print(ans)

Solution (Python):
n=int(input())
c=[[0]*10 for i in range(10)]
for a in range(1,n+1):
  a=str(a)
  c[int(a[-1])][int(a[0])]+=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):
from itertools import product

n = int(input())

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

for i in range(n + 1):
    a, b = int(str(i)[0]), i % 10
    n_num[a][b] += 1

answer = sum(n_num[a][b] * n_num[b][a] for a, b in product(range(1, 10), repeat=2))
print(answer)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    vec = [[0] * 10 for _ in range(10)]
    for n in range(1, N + 1):
        s = str(n)
        vec[int(s[0])][int(s[-1])] += 1

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

    print(ans)
    return


if __name__ == '__main__':
    main()

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

for i in range(1, N+1):
  ss = str(i)
  hd = int(ss[0])
  tl = int(ss[-1])
  numss[hd][tl] += 1
  
  
ans = 0
for x in range(1, 10):
  for y in range(1, 10):
    ans += numss[x][y] * numss[y][x]

print(ans)

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

for i in range(1, n + 1):
    temp = str(i)
    if temp[0] == '0' or temp[-1] == '0':continue 
    hort[int(temp[0]) - 1][int(temp[-1]) - 1] += 1

res = 0
for i in range(9):
    for j in range(9):
        if i != j:
            temp = hort[i][j] * hort[j][i] * 2
        else:
            temp = hort[i][j] * (hort[i][j] - 1) + hort[i][j]
            
        hort[i][j] = 0
        res += temp

print(res)

Solution (Python):
n = int(input())
table = [[0]*10 for _ in range(10)]
for i in range(1, n+1):
  start = int(str(i)[0])
  end = int(str(i)[-1])
  table[start][end] += 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())
L = len(str(N))
if L == 1:
    print(N)
    quit()
    
pair = [[(10**(L-2)-1)//9]*9 for _ in range(9)]
for i in range(9):
    pair[i][i] += 1

for i in range(10**(L-1), N+1):
    pair[int(str(i)[0])-1][int(str(i)[L-1])-1] += 1 if i%10 else 0
ans = 0
for i in range(9):
    for j in range(9):
        ans += pair[i][j] * pair[j][i]
print(ans)

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

cnt = [[0] * 10 for i in range(10)]
for n in range(1, N + 1):
    cnt[int(str(n)[0])][int(str(n)[-1])] += 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 sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
 
N = INT()

dic = defaultdict(int)

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

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

print(ans)

Solution (Python):
n = int(input())
tbl = [[0]*10 for _ in [0]*10]
k = 10
for i in range(1, n+1):
    if i // k: k *= 10
    l = i*10//k
    r = i%10
    tbl[l][r] += 1
    
ans = 0
for l, _t in enumerate(tbl):
    for r, t in enumerate(_t):
        if l == r:
            ans += t*t
        else:
            ans += t*tbl[r][l]
print(ans)

Solution (Python):
import math
n = int(input())
ans=0
k = [[0]*10 for i in range(10)]

for i in range(1,n+1):
  j = str(i)
  l = int(j[0])
  r = int(j[-1])
  k[l][r]+=1
  
for i in range(10):
  for j in range(10):
    if i==0 or j==0:
      continue
    if i==j:
      ans += k[i][j]*k[i][j]
    else:
      ans += k[i][j]*k[j][i]
print(ans)

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

Solution (Python):


from collections import defaultdict
n = int(input())


dic = defaultdict(int)


for k in range(1,n+1):
    left = str(k)[0]
    right = str(k)[-1]
    dic[(left,right)] += 1
                
ans = 0
items = list(dic.items())
for (left,right),cnt in items:
    ans += cnt * dic[(right,left)]

print(ans)

Solution (Python):

n = int(input())

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

for i in range(10):
    for j in range(10):
        for k in range(1,n+1):
            if str(k)[0] == str(i) and str(k)[-1] == str(j):
                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):
from collections import defaultdict
N = int(input())
d = defaultdict(int)

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

  if s != 0 and t != 0:
    if s > t:
      s, t = s, t
    d[(s, t)] += 1

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

print(ans)

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

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

for i in range(1, N+1):
  if i % 10 != 0:
    si = str(i)
    start = int(si[0])
    fin = int(si[-1])
    dp[start-2][fin-2] += 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())

numss = [[0]*(10) for _ in range(10)]
for i in range(1, N+1):
    ss = str(i)
    hd = int(ss[0])
    tl = int(ss[-1])
    numss[hd][tl] += 1

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

print(ans)

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

Solution (Python):
n = int(input())
cou = 0
for i in range(1,n+1):
  i = str(i)
  if i[0]!="0" and i[-1]!="0":
    if int(i[0])>int(i[-1]):
      cou +=  (10**(len(i)-1)-1)//9
    elif int(i[0])<int(i[-1]):
      cou += (10**(len(i)-2)-1)//9
  

cou *= 2
    
l = [0 for i in range(9)]
for i in range(1,n+1):
  i = str(i)
  if i[0]==i[-1]:
    l[int(i[0])-1] += 1
l1 = [i*i for i in l]
print(sum(l1)+cou)

Solution (Python):


n = int(input())
c = [[[] for i in range(9)] for i in range(9)]

for i in range(1,n+1):
  s = str(i)
  if s[-1] == '0':
    continue
  c[int(s[0])-1][int(s[-1])-1].append(i)

ans = 0

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

print(ans)

Solution (Python):
n=int(input())
count=0
ab=[[0]*10 for i in range(10)]
for i in range(1,n+1):
  k=str(i)
  a=int(k[0])
  b=int(k[-1])
  ab[b][a]+=1

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

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

ans = 0
for i in range(1,n+1)[::-1]:
	head = str(i)[0]
	tail = str(i)[-1]
	length = len(str(i))
	if tail == '0':
		continue
	if head == tail:
		if length == 1:
			ans += 1
		elif length == 2:
			ans += 3
		else:
			ans += 3
			for j in range(3,length+1):
				if length > j:
					ans += 10**(j-2) * 2
				else:
					ans += 2 * (int(str(i)[1:length-1]) + 1)
	elif head < tail:
		if length > 2:
			ans += int('1' * (length-2)) * 2
	else:
		if length > 1:
			ans += int('1' * (length-1)) * 2
print(ans)

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

table = {}

for num in range(1, n + 1):
    l = num % 10
    tmp = num
    while tmp >= 10:
        tmp //= 10
    m = tmp % 10

    if (l, m) not in table:
        table[(l, m)] = 1
    else:
        table[(l, m)] += 1

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

print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())

cnt = defaultdict(int)
for i in range(1, N + 1):
    i = str(i)
    l = i[0]
    r = i[-1]
    cnt[(l, r)] += 1
    cnt[(r, l)] += 0

ans = 0
for (l, r), c in cnt.items():
    ans += cnt[(r, l)] * c

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 = sum(count[i][j] * count[j][i] for i in range(1, 10) for j in range(1, 10))
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque


from collections import Counter

from itertools import accumulate,combinations,permutations

from heapq import heapify,heappop,heappush


from functools import lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    

    N = int(input())
    
    
    
    
    count=[[0]*10 for i in range(10)]
    for i in range(1,N+1):
        si=str(i)
        head=int(si[0])
        tail=int(si[-1])
        count[head][tail]+=1
    ans=0
    for a in range(1,10):
        for b in range(1,10):
            ans+=count[a][b]*count[b][a]
    print(ans)







if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
l = [0]*100
ans = 0

for i in range(1, n + 1):
    s = str(i)
    l[int(s[0])*10 + int(s[-1])] += 1

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

print(ans)

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

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import decimal


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True

    @staticmethod
    def fact(N):
        res = {}
        tmp = N
        for i in range(2, int(N ** 0.5 + 1) + 1):
            cnt = 0
            while tmp % i == 0:
                cnt += 1
                tmp //= i
            if cnt > 0:
                res[i] = cnt
        if tmp != 1:
            res[tmp] = 1
        if res == {}:
            res[N] = 1
        return res


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N = Scanner.int()
    memo = [[0 for _ in range(10)] for _ in range(10)]
    for i in range(1, N + 1):
        s = str(i)
        memo[int(s[0])][int(s[-1])] += 1

    ans = 0
    for i in range(1, N + 1):
        s = str(i)
        t = memo[int(s[-1])][int(s[0])]
        ans += t
    print(ans)


def main():
    
    
    
    
    
    solve()


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
N = int(input())
P = []
Q = []
for L in range(1, N+1):
    L = str(L)
    a = int(L[0])
    b = int(L[-1])
    if a == 0 or b == 0:
        continue
    P.append((a, b))
C = Counter(P)

ans = 0
for i in range(len(P)):
    q = (P[i][-1], P[i][0])
    ans += C[q]

print(ans)

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

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

for n in range(1,N+1):
    head = int(str(n)[0])
    tail = int(str(n)[-1])
    M[head][tail] += 1

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

print(ans)

Solution (Python):
n=int(input())
d=[0]*100
for i in range(n+1):
  s=str(i)
  d[int(s[0])*10+int(s[-1])]+=1

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

Solution (Python):
def MAP(): return map(int, input().split())
def LIST(): return list(MAP())
 
N = int(input())
count = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, N + 1):
    string_i = str(i)
    initial = int(string_i[0])
    end = int(string_i[-1])
    count[initial][end] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += count[i][j] * count[j][i]
print(ans)

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



def calculate(n):
    result = {}

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

        i = int(str(index)[0])
        j = int(str(index)[-1])
        if result.get((i, j)) == None:
            if i != 0:
                result.__setitem__((i, j), 1)
        else:
            result.__setitem__((i, j), result.get((i, j)) + 1)

    res = 0
    for i in range(10):
        for j in range(10):
            s1 = result.get((i, j))
            s2 = result.get((j, i))

            s1 = 0 if s1 == None else s1
            s2 = 0 if s2 == None else s2

            res += s1 * s2

    print(res)


calculate(N)

Solution (Python):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
a = [[0] * 10 for i in range(10)]
for i in range(1,N+1):
    s = int(str(i)[0])
    g = int(str(i)[-1])
    a[s][g] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += a[i][j] * a[j][i]
print(ans)

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

Solution (Python):
import numpy as np
def main():
    n = int(input())
    count = np.zeros((10, 10))
    for i in range(1, n + 1):
        i = str(i)
        s = int(i[0])
        e = int(i[-1])
        count[s, e] += 1
    print(np.sum(count * count.T, dtype = np.int))

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

N = int(input())
dp = np.zeros((10, 10)).astype(int)
dp[1, 1] = 1
ans = 1
for i in range(2, N + 1):

    i_str = str(i)
    i_start = int(i_str[0])
    i_end = int(i_str[-1])
    
    if i_start == i_end:
        ans += 1
    ans += dp[i_end, i_start]*2
    dp[i_start, i_end] += 1
    
print(ans)

Solution (Python):
N = int(input())
D = {i: 0 for i in range(100)}
for i in range(1, N+1):
    if i % 10 != 0:
        s = str(i)
        D[int(s[0])*10+int(s[-1])] += 1
S = 0
for i in range(10):
    for j in range(10):
        S += D[i*10+j] * D[j*10+i]
print(S)

Solution (Python):
N = int(input())
D = {i: 0 for i in range(100)}
for i in range(1, N+1):
  if i % 10 != 0:
    s = str(i)
    D[int(s[0])*10+int(s[-1])] += 1
S = 0
for i in range(10):
  for j in range(10):
    S += D[i*10+j] * D[j*10+i]
print(S)

Solution (Python):
N = int(input())
D = {i: [0] for i in range(100)}
for i in range(1, N+1):
  if i % 10 != 0:
    s = str(i)
    D[int(s[0])*10+int(s[-1])][0] += 1
    D[int(s[0])*10+int(s[-1])].append(i)
S = 0
for i in range(10):
  for j in range(10):
    S += D[i*10+j][0] * D[j*10+i][0]
print(S)

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

Solution (Python):
def top(n):
    return int(str(n)[0])

def bottom(n):
    return int(str(n)[-1])

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

for i in range(1,n+1):
    c[top(i)][bottom(i)] += 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())
ct = [[0] * 10 for _ in range(10)]


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

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

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

D={(x//10,x%10):0 for x in range(100)}
for i in range(1,N+1):
    if i<10:
        D[(i,i)]=1
    else:
        Q=int(str(i)[0])
        R=i%10
        D[(Q,R)]+=1

S=0
for x in range(1,10):
    for y in range(1,10):
        S+=D[(x,y)]*D[(y,x)]

print(S)

Solution (Python):


def cmb(n, r):
    import math
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))






def solve(N):
    ABs = {}
    ABs_visited = {}
    for h in range(1, 10):
        for t in range(1, 10):
            ABs.setdefault((h, t), 0)
            ABs_visited.setdefault((h, t), False)

    for n in range(1, N + 1):
        s = str(n)
        h, t = int(s[0]), int(s[-1])
        if not (h == 0 or t == 0):
            ABs[(h, t)] += 1

    ans = 0
    for k in ABs:
        if ABs_visited[k]:
            continue
        h, t = k
        ABs_visited[(h, t)] = True
        

        
        
        
        
        ans += ABs[(h, t)] * ABs[(t, h)]
        
        
    

    print(ans)


if __name__ == '__main__':
    N = int(input())
    solve(N)

Solution (Python):
N=int(input())
ls=[[0]*9 for _ in range(9)]
for i in range(1,N+1):
  a=i
  while a//10!=0:
    a//=10
  b=i%10
  if b!=0:
    ls[a-1][b-1]+=1 
ans=0    
for i in range(1,10):
  for j in range(1,10):
    ans+=ls[i-1][j-1]*ls[j-1][i-1]
print(ans)

Solution (Python):
N=int(input())
ls=[[0]*9 for _ in range(9)]
for i in range(1,N+1):
  a=i
  while a//10!=0:
    a//=10
  b=i%10
  if b!=0:
    ls[a-1][b-1]+=1 
ans=0    
for i in range(1,10):
  for j in range(1,10):
    ans+=ls[i-1][j-1]*ls[j-1][i-1]
print(ans)

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

div = 1
pairs = [[0] * 10 for i in range(10)]

for i in range(1,N+1):
    if i // div >= 10:
        div *= 10
        f = 1
    else:
        f = i//div
    pairs[f][i%10] += 1

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

Solution (Python):
import numpy as np

n = int(input())
table,ans = np.zeros((9,9)),0

for k in range(n):
    num = str(k+1)
    if int(num[0])!=0 and int(num[-1])!=0:
        table[int(num[0])-1][int(num[-1])-1]+=1

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

Solution (Python):
n = int(input())
c = [[0]*10 for i in range(10)]
for i in range(1,n+1):
    s = str(i)
    l = int(s[0])
    r = int(s[-1])
    c[l][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())
cnt = [[0] * 10 for i in range(10)]
for i in range(1, n + 1):
    cnt[int(str(i)[0])][int(str(i)[-1])] += 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 math
import numpy as np
N = int(input())
tb = [[0]*10]*10
tb = np.array(tb)
for i in range(1,N+1):
    s = str(i)
    tb[int(s[0])][int(s[-1])] =tb[int(s[0])][int(s[-1])] +1
 
point =0
 
for i in range(0,10):
    for j in range(0,10):
        point += tb[i][j] * tb[j][i]
 
print(point)

Solution (Python):
N = int(input())
li = [[0]*9 for i in range(9)]
counter = 0

for i in range(1,N+1):
    I = str(i)
    fast = int(I[0])
    last = int(I[-1])
    if not last==0:
        li[fast-1][last-1] += 1

for i in range(9):
    for k in range(9):
        counter += li[i][k]*li[k][i]


print(counter)

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

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

k = ni()

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

for i in range(k+1):
  s = [int(_) for _ in str(i)]
  p[s[0]][s[-1]]+=1

ans=0

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

print(ans)

Solution (Python):
mod = 1e9+7
def add(a, b):
    c = a + b
    if c >= mod:
        c -= mod
    return c

def main():
    m = {}
    N = int(raw_input())
    
    for i in range(1, N+1):
        s = str(i)
        first = s[0]
        last = s[-1]
        key = first + last
        m[key] = m.get(key, 0) + 1
    
    ans = 0
    
    
    for k in m:
        if k[0] == k[1]:
            ans += m[k] * m[k]
        else:
            swap = k[1] + k[0]
            ans += m[k] * m.get(swap, 0)
    """
    for key1 in m:
        for key2 in m:
            
            
            if key1[0] == key2[1] and key1[1] == key2[0]:
                ans += m[key1] * m[key2]
    """
    
    print(ans)
    
    
    

main()

Solution (Python):
mod = 1e9+7
def add(a, b):
    c = a + b
    if c >= mod:
        c -= mod
    return c

def main():
    m = {}
    N = int(raw_input())
    
    for i in range(1, N+1):
        s = str(i)
        first = s[0]
        last = s[-1]
        key = first + last
        m[key] = m.get(key, 0) + 1
    
    ans = 0
    
    
    
    for key1 in m:
        for key2 in m:
            
            
            if key1[0] == key2[1] and key1[1] == key2[0]:
                ans += m[key1] * m[key2]
    
    print(ans)
    
    
    

main()

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

for i in range(1,n+1):
    s=str(i)
    s1 = int(s[0]) - 1
    s2 = int(s[-1]) - 1
    if s2 == -1 : continue
    check[s1][s2] += 1

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

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

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

for i in range(1, n+1):
  aaa[int(str(i)[0])][i%10] += 1

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

Solution (Python):
from collections import defaultdict

n = int(input())
dic = defaultdict(int)

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

point = 0

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

print(point)