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

Solution (Python):
n = int(input())
cnt = [[0] * 10 for i in range(10)]
for i in range(1,n+1):
  i = str(i)
  cnt[int(i[0])][int(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 sys
n = int(sys.stdin.readline().rstrip())

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

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

r = 0
for i in range(10):
    for j in range(10):
        r += s[i][j] * s[j][i]

print(r)

Solution (Python):
import numpy as np

N = int(input())

def solve():
    arr = [0] * 100
    ans = 0
    
    for i in range(1, N + 1):
        strNum  = str(i)
        arr[int(strNum[0] + strNum[-1])] += 1
        
    for i in range(1, N + 1):
        strNum = str(i)
        ans += arr[int(strNum[-1] + strNum[0])]
    
    return ans

print(solve())

Solution (Python):
import numpy as np
import math

def solve(num):
  table = np.zeros((9,9))

  for n in range(1, num+1):
    order = len(str(n))
    upper = n // 10**(order-1)
    lower = int(str(n)[-1])
    if lower == 0:
      continue
    table[lower-1][upper-1] += 1

  ans = 0
  for n in range(1, num+1):
    order = len(str(n))
    upper = n // 10**(order-1)
    lower = int(str(n)[-1])
    if lower == 0:
      continue
    ans += table[upper-1][lower-1]
  return int(ans)

if __name__ == "__main__":
  _n = int(input())
  ans = solve(_n)
  print(ans)

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

ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        c_ij = 0
        c_ji = 0
        for k in range(1, n + 1):
            temp = str(k)
            if temp[0] == str(i) and temp[-1] == str(j):
                c_ij += 1
            if temp[0] == str(j) and temp[-1] == str(i):
                c_ji += 1
        ans += c_ij * c_ji
print(ans)

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

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

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

ans = 0

for j in range(10):
    for k in range(10):
        ans += lst[j][k] * lst[k][j]

print(ans)

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

def get_first(num):
    log = 1
    k=num
    while True:
        if k < 10:
            break
        k = k//10
        log+=1
    
    return (k,log)

for i in range(1,n+1):
    f,_ = get_first(i)
    l   = i%10
    if l==0:
        continue
    b[f-1][l-1] +=1


for i in range(9):
    for j in range(9):
        count+=b[i][j]*b[j][i]


print(count)

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

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

for i in range(1,N+1):
    S = str(i)
    cnt[int(S[0])][int(S[-1])] += 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):
def Int ():
    return int(input())

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

for i in range(1,N+1):
    S = str(i)
    cnt[int(S[0])][int(S[-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):
N = int(input())
cnt = [[0] * 10 for i in range(10)]

for i in range(1,N+1):
    S = str(i)
    cnt[int(S[0])][int(S[-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):
N = int(input())
d = [[0] * 10 for _ in range(10)]
ans = 0

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

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

print(ans)

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

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

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

Solution (Python):
N = int(input())
X = [[0 for j in range(10)] for i in range(10)]

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

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=[[0]*10 for _ in range(10)]
    for i in range(1,n+1):
        i=str(i)
        s=int(i[0]); t=int(i[-1])
        A[s][t]+=1

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

    print(ans)
resolve()

Solution (Python):

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

    if N <= 10:
        return N

    
    numberCount = [0] * 100
    for i in range(10):
        numberCount[i] = 1
        numberCount[i*10+i] = 1

    ans = 9
    for i in range(11, N+1):

        
        if str(i)[-1] == "0":
            continue

        
        if str(i)[0] == str(i)[-1]:
            ans += 1

        
        ans += numberCount[int(str(i)[-1])*10 + int(str(i)[0])] * 2

        
        numberCount[int(str(i)[0])*10 + int(str(i)[-1])] += 1

    return ans


print(solve())

Solution (Python):
n=input()
intn=int(n)
ln=len(n)

if intn<10:
  print(n)
  quit()

def count(i,j):
  res=0
  if i==j:
    res+=1
  if ln==2:
    if 10*i+j<=intn:
      res+=1
  else:
    res+=(10**(ln-2)-1)//9
    if i<int(n[0]):
      res+=10**(ln-2)
    elif i==int(n[0]):
      tn=int(n[1:-1])
      if j<=intn%10:
        res+=tn+1
      else:
        res+=tn
  return res

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

Solution (Python):
import numpy as np
N = int(input())
nn = np.zeros((9,9), dtype=int)

for i in range(1,N+1):
  istr = str(i)
  a = int(istr[0])
  b = int(istr[-1])
  if (a != 0) and (b != 0): nn[a-1, b-1] += 1
    
ans = 0
for a in range(9):
  for b in range(9):
    ans += nn[a, b] * nn[b, a]
print(ans)

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

for i in range(1,n+1):
  s = str(i)
  if s[-1] != '0':
    a += 2*l[(int(s[-1]),int(s[0]))]
    l[(int(s[0]),int(s[-1]))] += 1
    if s[-1] == s[0]:
      a += 1

print(a)

Solution (Python):


from itertools import product
from math import floor, ceil
import numpy as np

N = int(input())
d = floor(np.log10(N)) + 1  
N_sento = N // (10 ** (d - 1))  

n = np.zeros((9, 9), dtype='int')


for i, j in product(range(9), range(9)):
    
    

    
    if i == j and N >= (i + 1):
        n[i, j] += 1

    
    for k in range(2, d):
        n[i, j] += 10 ** (k - 2)
    
    
    if d < 2:
        pass
    elif N_sento > (i + 1):
        
        n[i, j] += 10 ** (d - 2)
    elif N_sento == (i + 1):
        
        n[i, j] += (N - N_sento * 10**(d-1)) // 10 + 1
        if (j + 1) > N % 10:
            n[i, j] -= 1

print(np.einsum('ij,ji', n, n))

Solution (Python):
n = int(input())
d = {}

for a in range(1, n+1):
    a = str(a)
    f, l = a[0], a[-1]
    if '0' in [f, l]:
        continue
    if (f, l) in d:
        d[(f, l)] += 1
    else:
        d[(f, l)] = 1

ans = 0
for b in range(1, n+1):
    b = str(b)
    f, l = b[-1], b[0]
    if (f, l) in d:
        ans += d[(f, l)]
print(ans)

Solution (Python):

from sys import stdin




def _li(): return list(map(int, stdin.readline().split()))
def _li_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def _lf(): return list(map(float, stdin.readline().split()))
def _ls(): return stdin.readline().split()
def _i(): return int(stdin.readline())
def _f(): return float(stdin.readline())
def _s(): return stdin.readline()[:-1]


N = _i()
table = {}
for i in range(1, 10):
    for j in range(1, 10):
        count = 0
        if i == j:
            count += 1
        a, b = str(j), str(i)
        for order in range(len(str(N)) - 1):
            min_x = int(a + "0" * order + b)
            if min_x > N:
                break
            max_x = int(a + "9" * order + b)
            if max_x <= N:
                count += 10 ** order
            else:
                assert str(N)[0] == a
                offset = 1 if str(N)[-1] < b else 0
                count += int(str(N)[1:-1]) - offset + 1
        table[(i, j)] = count

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

print(ans)

Solution (Python):
from itertools import product


def solve(string):
    d = len(string)
    if d == 1:
        return string
    s0, ss, sn = int(string[0]), int(string[1:-1] or 0), int(string[-1])
    t = [[0] * 10] + [[int("1" * (d - 2) or 0)] * 10 for _ in range(9)]
    for i in range(1, 10):
        t[i][i] += 1
    for i, j in product(range(1, s0), range(10)):
        t[i][j] += 10**(d - 2)
    for j in range(10):
        t[s0][j] += ss + 1 if j <= sn else ss
    return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
from itertools import product


def solve(string):
    d = len(string)
    if d == 1:
        return string
    s0, ss, sn = int(string[0]), int(string[1:-1] or 0), int(string[-1])
    t = [[0] * 10] + [[int("1" * (d - 2) or 0)] * 10 for _ in range(9)]
    for i in range(1, 10):
        t[i][i] += 1
    for i, j in product(range(1, s0), range(10)):
        t[i][j] += 10**(d - 2)
    for j in range(sn + 1):
        t[s0][j] += ss + 1
    for j in range(sn + 1, 10):
        t[s0][j] += ss
    return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

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

if n > 10:
  for i in range(1,10):
    l[(i,i)] = 1
  a = 9
  for i in range(10,n+1):
    s = str(i)
    if s[-1] != '0':
      a += 2*l[(int(s[-1]),int(s[0]))]
      l[(int(s[0]),int(s[-1]))] += 1
      if s[-1] == s[0]:
        a += 1
else:
  a = n

print(a)

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

def CheckDigit():
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += digit[i][j] * digit[j][i]
    return ans

def ArrayRotation(n):
    for i in range(1, n+1):
        tmp = str(i)
        digit[int(tmp[0])][int(tmp[-1])] += 1

if N <= 11:
    print(N)
else:
    ArrayRotation(N)
    print(CheckDigit())

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

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

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

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

print(ans)

Solution (Python):
n = int(input())
di = [[0]*9 for _ in range(9)]
for i in range(1, n+1):
    p = list(str(i))
    if p[-1] == '0':
        continue
    di[int(p[0])-1][int(p[-1])-1]+=1
result = 0
for i in range(9):
    for j in range(9):
        c = di[i][j]
        d = di[j][i]
        result+=c*d
print(result)

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

pairs={}

for n in range(1, N+1):
  str_n = str(n)
  pair=str_n[0]+str_n[-1]
  if pair in pairs:
    pairs[pair]+=1
  else:
    pairs[pair]=1
ans = 0

for i in pairs.keys():
  if i[::-1] in pairs:
    ans+=pairs[i]*pairs[i[::-1]]

print(ans)

Solution (Python):



N = int(input())


heads_and_tails = {}
for head in range(1, 10):
    for tail in range(1, 10):
        heads_and_tails[(head, tail)] = 0


for num in range(1, N+1):
    if num%10 == 0:
        continue
    head = int(str(num)[0])
    tail = int(str(num)[-1])
    heads_and_tails[(head, tail)] +=1


count = 0
for head in range(1, 10):
    for tail in range(1, 10):
        count += heads_and_tails[(head, tail)] * heads_and_tails[(tail, head)]

print(count)

Solution (Python):

import sys


def solve(N: int):
    count = {}
    for i in range(1, N+1):
        p = get_pair(i)
        key = p[0] + '_' + p[1]
        if not count.get(key):
            count[key] = 0
        count[key] += 1
    ans = 0
    for i in range(1, N+1):
        p = get_pair(i)
        key = p[1] + '_' + p[0]
        if count.get(key):
            ans += count[key]
    
    print(ans)
    return


def get_pair(n: int):
    nf = str(n)[0]
    nl = str(n)[len(str(n)) - 1]
    return [nf, nl]



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())
arr = [0]*100
for num in range(1,N+1):
  arr[int(str(num)[0]+str(num)[-1])] += 1
print(sum([arr[i*10+j]*arr[j*10+i]  for i in range(10) for j in range(10)]))

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

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

for i in range(1,N+1):
    a = i % 10
    b = 0
    while i > 0:
        b = i
        i = i // 10
    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())

nlist=list(range(1,N+1))
import math
def getmax(n):
    logscale=int(math.log10(n))
    kurai=int(n/(10**logscale))
    return kurai
def getmin(n):
    return int(n%10)

profile=[]
for n in nlist:
    profile.append([getmin(n),getmax(n)])

counter=0
for i in range(1,10):
    for j in range(1,10):
        ij=profile.count([i,j])
        ji=profile.count([j,i])
        counter+=ij*ji

print(counter)

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

c = [[0] * 10 for _ in range(10)] * n
for i in range(1, n + 1):
    i1 = i // 10 ** (len(str(i)) - 1)
    i2 = i % 10
    c[i1][i2] += 1

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

Solution (Python):
from sys import stdin
def solve():
    n=int(stdin.readline())
    '''ln_n=len(str(n))
    cnt=9
    for i in range(11,n+1):
        s=str(i)
        if s[-1]=='0':
            continue
        ln=len(s)
        if ln==ln_n:
            if ln_n!=2:
                cnt+=int(s[1:-1])
            else:
                if int(s[::-1])<=n:
                    cnt+=1
        else:
            cnt+=10**(ln-2)
    print(cnt)'''
    d={}
    for i in range(1,n+1):
        s=str(i)
        if s[-1]!='0':
            k=(s[0],s[-1])
            d.setdefault(k,0)
            d[k]+=1
    cnt=0
    for i in d:
        for j in d:
            if i[0]==j[1] and i[1]==j[0]:
                cnt+=(d[i]*d[j])
    
    print(cnt)
                
solve()

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

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

for i in range(1,n+1):
    t=str(i)
    c[int(t[0])][int(t[-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())

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

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

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

def ht(n):
    str_n = str(n)
    head = int(str_n[0])
    tail = int(str_n[-1])
    return (head, tail)

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

for i in range(1, N+1):
    h,t = ht(i)
    htmap[h][t] += 1

ans = 0
for i in range(1, N+1):
    h,t = ht(i)
    ans += htmap[t][h]

print(ans)

Solution (Python):
from collections import defaultdict

n = int(input())
d = defaultdict(int)

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

ans = 0
for i in range(1,n+1):
    s = str(i)
    k = (s[-1],s[0])
    ans += d[k]
print(ans)

Solution (Python):
import numpy as np

n = int(input())

mat = np.zeros((10,10))

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

total = (mat*(mat.T)).sum()
print(int(total))

Solution (Python):

class Solution:

    def solve(self, N: int) -> int:

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

        base = 1
        for n in range(1, N+1):
            if n % (10 * base) == 0:
                base *= 10

            first = n // base
            last = n % 10

            c[first][last] += 1

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

        return answer


if __name__ == '__main__':

    
    N = int(input())

    
    solution = Solution()
    print(solution.solve(N))

Solution (Python):
import collections
N = int(input())
li = [i for i in range(1, N+1)]
l1 = list(map(lambda x:str(x)[0]+str(x)[-1], li))
l2 = list(map(lambda x:str(x)[-1]+str(x)[0], li))
c1 = collections.Counter(l1)
c2 = collections.Counter(l2)

num = 0
for l in set(l1):
    num += c1[l] * c2[l]
print(num)

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

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

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

    c[start][stop] += 1

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


print(r)

Solution (Python):
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        c1 = 0
        c2 = 0
        d = 1
        for x in range(1,N+1):
            if x % (d *10) == 0:
                d = d * 10
        
            if x % 10 == i and x // d == j:
                c1 += 1
            if x % 10 == j and x // d == i:
                c2 += 1
        ans += c1 * c2

print (ans)

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

d = {}
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 i % 10 == 0:
        continue
    i = str(i)
    d[i[0]+i[-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):
import os, sys

def msd(n):
  d = 0
  while n > 0:
    d = n % 10
    n //= 10
  return d

def main():
  N = int(input())
  c = [[0] * 10 for _ in range(10)]
  for n in range(1, N + 1):
    l = msd(n)
    r = n % 10
    c[l][r] += 1
  
  ans = 0
  for i in range(1, 10):
    for j in range(1, 10):
      ans += c[i][j]*c[j][i]
  print(ans)


if __name__ == '__main__':
  if sys.platform == 'ios':
    sys.stdin = open(os.path.splitext(__file__)[0] + '.txt')
  sys.setrecursionlimit(10**6)
  main()

Solution (Python):
import math

n = int(input())

def N(n, i, j):
    num = 0
    for x in range(1,n+1):
        low = int(x%10)
        high = int(x/(pow(10, int(math.log10(x)))))
        if((high == i)and(low == j)):
            num = num + 1
    return num

s = 0
for i in range(1,10):
    s = s + pow(N(n, i, i), 2)

for i in range(2,10):
    for j in range(1,i):
        s = s + 2*N(n, i, j)*N(n, j, i)

print(s)

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

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

cnt = 0

for i in range(1, N+1):
    head = int(str(i)[0])
    tail = int(str(i)[len(str(i)) - 1])
    list[head][tail] += 1

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

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline






















def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	poole = {}

	for i in range(1,N+1):
		ii = str(i)
		start = ii[0]
		end = ii[-1]

		if (start,end) in poole:
			poole[(start,end)] += 1
		else:
			poole[(start,end)] = 1

	ans = 0
	for i in range(1,N+1):
		ii = str(i)
		start = ii[0]
		end = ii[-1]

		if (end,start) in poole:
			tmp = poole[(end,start)]
			ans += tmp

	print(ans)







if __name__ == "__main__":
	main()

Solution (Python):
from sys import stdin

N = int(stdin.readline().rstrip())
ans = 0
d = 0
M = [[0] * 10 for _ in range(10)]

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

for i in range(10):
    for j in range(10):
        if i == j:
            d += M[i][j] * M[j][i]
        else:
            ans += M[i][j] * M[j][i]

print(ans + d)

Solution (Python):
N=int(input())
count=[[0]*9 for i in range(9)]
for i in range(N+1):
  j=str(i)
  if j[0] != '0' and j[-1] != '0':
    count[int(j[0])-1][int(j[-1])-1]+=1
ans=0
for i in range(9):
  for j in range(9):
    ans+=count[i][j]*count[j][i]
print(ans)

Solution (Python):
"""
108345237845804
498073591
are an example match, because (1,4) and (4,1)
each integer can be encoded as this kind of pair
if there is also
4768281
then we have 2 matches so far, because the
first matches with the second and third
considering 1 and 11 and 111 and 1111 and 11111
each are encoded as (1,1), so we get 9 matches from just these 5...
solution :
just count how many integers get encoded as a certain pair, then calculate
the number of pairs that would be made from them
eg if (1,4) = 10 and (4,1) = 5 then there are 5*10 = 50 pairs
How many encodings? no leading zero, so also ending zero wont get a match,
so 1 to 9 and 1 to 9, is 9*9=81 encodings
"""


def handstand(N):
    
    count_encoding = {(x, y): 0 for x in range(1, 10) for y in range(1, 10)}
    for i in range(1, N+1):
        string_convert = str(i)
        start_val = int(string_convert[0])
        end_val = int(string_convert[-1])
        if end_val == 0: continue
        count_encoding[start_val, end_val] += 1
    
    matches_count = 0
    for x in range(1, 10):
        for y in range(1, 10):
            matches_count += count_encoding[x, y]*count_encoding[y, x]
    return matches_count


if __name__ == "__main__":
    test1 = "25"
    test2 = "1"
    test3 = "100"
    test4 = "2020"
    test5 = "200000"
    test6 = "16"
    test = test4
    
    in_val = int(input())
    print(handstand(in_val))

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


def head_tail(num):
    if len(str(num)) == 1:
        return (num, num)
    top = int(str(num)[0])
    tail = int(str(num)[-1])
    return (top, tail)


pairs = {}
for i in range(1, N+1):
    p = head_tail(i)
    if p not in pairs:
        pairs[p] = 1
    else:
        pairs[p] += 1

ans = 0
for i in range(1, N+1):
    pair = head_tail(i)
    re = (pair[1], pair[0])
    ans += pairs.get(re, 0)
print(ans)

Solution (Python):
def main():
    N = int(input())
    l = [[0] * 10 for _ in range(10)]
    for i in range(1,N + 1):
        i = str(i)
        t = i[0]
        b = i[-1]
        if t == 0 or b == 0:
            continue
        l[int(t)][int(b)] += 1
    r = 0
    for i in range(1, 10):
        for j in range(1, 10):
            r += l[i][j] * l[j][i]
    return r

print(main())

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

def gutyoku(head, tail):
    result = 0
    for i in range(tail, n + 1, 10):
        if int(str(i)[0]) == head:
            result += 1
    return result

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

Solution (Python):
import itertools
import numpy as np
N = int(input())
log_n = int(np.log10(N))

vals = []

for a,b in itertools.product(range(1,10), range(1,10)):
    res = 0
    if a == b and a <= N:
        res += 1
    for i in range(1, log_n + 1):
        mini = a * (10 ** i) + b
        if N - mini < 0:
            break
        if 10 * (10 ** (i - 1) - 1) <= N - mini:
            res += 1 + 10 * (10 ** (i - 1) - 1) // 10
        else:
            res += 1 + np.floor((N - mini) / 10)
    vals.append(((a, b), int(res)))
table = {k:v for k,v in vals}
res = 0
for x in list(itertools.combinations(range(1,10),2)):
    res += 2 * (table[x] * table[(x[1],x[0])])
for x in range(1,10):
    res += table[(x,x)] ** 2
print(res)

Solution (Python):
def main():
    n = int(input())
    flg = 0
    val=0
    ans=0
    ns = str(n)
    nl = len(ns)
    tmp = [[0]*10 for i in range(11)]
    for i in range(1,n+1):
        s = str(i)
        top = int(s[0])
        btm = int(s[-1])
        tmp[top][btm] += 1

    for i,a in enumerate(tmp):
        for j,b in enumerate(a):
            if b != 0:
                ans += b*tmp[j][i]
    print(ans)





if __name__=='__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    flg = 0
    val=0
    ans=0
    ns = str(n)
    nl = len(ns)
    tmp = [[0]*10 for i in range(11)]
    for i in range(1,n+1):
        s = str(i)
        top = int(s[0])
        btm = int(s[-1])
        tmp[top][btm] += 1

    for i,a in enumerate(tmp):
        for j,b in enumerate(a):
            if b != 0:
                ans += b*tmp[j][i]
    print(ans)





if __name__=='__main__':
    main()

Solution (Python):
N = int(input())
lists = [str(i) for i in range(1,N+1)]
count = 0
for i in range(1,10):
    for j in range(i,10):
        s_cnt, t_cnt = 0, 0
        si, sj = str(i), str(j)
        for k in lists:
            if k[0] == si and k[-1] == sj:
                s_cnt += 1
            elif k[0] == sj and k[-1] == si:
                t_cnt += 1
        if i != j:
            count += s_cnt * t_cnt * 2
        elif i == j:
            count += s_cnt ** 2
print(count)

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

pattern = {}

for nn in range(1,n + 1):
    f = int(nn / (10 ** (len(str(nn) ) -1)))
    l = nn % 10  
    p = f * 10 + l
    if f == l:
        p = f
    
    if  p in pattern:
       pattern[p] += 1
    else:
        pattern[p] = 1


s = 0
for p, v in pattern.items():

    f = int(p / (10 ** (len(str(p) ) -1)))
    l = p % 10
    p2 = l * 10 + f
    if l == 0:
        continue      
    elif p < 10: 
        s +=  v * v
    elif p2 in pattern:
        s += v * pattern[p2]
print(s)

Solution (Python):
n=int(input())
A=[[0 for i in range(10)] for j in range(10)]

for i in range(1,n+1):
    num=str(i)
    A[int(num[0])][int(num[-1])] +=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())
num = [[0 for i in range(10)] for j in range(10)]

ans = 0

for _ in range(n):
    i = _ + 1
    if i < 10:
        num[i][i] += 1
    elif i % 10 != 0:
        moji = str(i)
        num[int(moji[0])][int(moji[len(moji)-1])] += 1
    else:
        pass

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

print(ans)

Solution (Python):
def ser_c(N):
    rec = [[0]*10 for i in range(10)]
    for l in range(1, N+1):
        k = str(l)
        i = int(k[0])
        j = int(k[-1])
        rec[i][j] += 1 
    return rec
N = int(input())
cnt = 0
rec = ser_c(N)
for i in range(10):
    for j in range(10):
        cnt += rec[i][j]*rec[j][i]
print(cnt)

Solution (Python):
import sys
from collections import defaultdict
N = int(sys.stdin.readline())
cnt = defaultdict(int)
for i in range(1, N+1):
  str_i = str(i)
  cnt[(str_i[0], str_i[-1])] += 1
 
ans = 0
for d1 in range(1, 10):
  for d2 in range(1, 10):
    d1, d2 = str(d1), str(d2)
    ans += cnt[(d1, d2)] * cnt[(d2, d1)]
print(ans)

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


print(a)

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

Solution (Python):
N = int(input())
C = {}

def firstd(num):
    if num < 10:
        return num
    prev = 0
    while num > 0:
        num = num // 10
        if num == 0:
            return prev
        prev = num
    return prev

assert firstd(123) == 1
assert firstd(321) == 3

for x in range(1, N + 1):
    first = firstd(x)
    last = x % 10
    C[(first,last)] = C.get((first,last), 0) + 1

ans = 0
for x in range(1, N + 1):
    first = firstd(x)
    last = x % 10

    ans += C.get((last,first), 0)
print(ans)

Solution (Python):
import numpy as np

n = int(input())
count = np.zeros((10, 10), dtype=np.int)
for i in range(1, n+1):
  str_i = str(i)
  head = int(str_i[0])
  tail = int(str_i[-1])
  count[head, tail] += 1
print(np.sum(count.T * count))

Solution (Python):
import math
from collections import defaultdict

N = int(input())

rr = defaultdict(lambda: 0)
for i in range(1, N + 1):
    x = int(i / (10 ** int(math.log10(i))))
    y = i % 10
    rr[(x, y)] += 1

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r += rr[(u, l)] * rr[(l, u)]
print(r)

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

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

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

for n in range(1, N + 1):
  _last = int(str(n)[0])
  _first = int(str(n)[-1])
  answer += nmap[_first][_last]
  
print(answer)

Solution (Python):
from collections import defaultdict

def getHeadTail(n):
    tail = n%10
    while n//10 > 0:
        n //= 10
    return n, tail

def main():
    N = int( input())
    d = defaultdict( int)
    for i in range(1, N+1):
        a, b = getHeadTail(i)
        if a == 0 or b == 0:
            continue
        d[(a,b)] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += d[(i,j)]*d[(j,i)]

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

Solution (Python):
strN = input()
N = int(strN)
p = {i: {j: 0 for j in range(1, 10)} for i in range(1, 10)}
for k in range(1, N+1):
    strk = str(k)
    i = int(strk[0])
    j = int(strk[-1])
    if i == 0 or j == 0:
        continue
    p[i][j] += 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):
N, = list(map(int,input().split()))
c = [[0 for e in range(10)] for s in range(10)]
for i in range(1, N+1):
    c[int(str(i)[0])][int(str(i)[-1])] += 1
r = 0
for s in range(1, 10):
    for e in range(1, 10):
        r += c[s][e] * c[e][s]
print(r)

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