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):
def main():
    import sys
    from collections import defaultdict

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

    n = int(input())
    nd = defaultdict(int)
    for i in range(1, n+1):
        tmp = str(i)
        h, t  = int(tmp[0]), int(tmp[-1])
        nd[(h, t)] += 1
    
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += nd[(i, j)]*nd[(j, i)]
    print(ans)



    
if __name__ == '__main__':
    main()

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

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

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

d = 1
for i in range(10, n + 1):
    if i % (d * 10)== 0:
        d *= 10
    h = i // d
    f = i % 10
    count[h][f] += 1

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

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

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

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

for n in range(1,N+1):
    n=str(n)
    n_s=int(n[0])
    n_e=int(n[-1])
    
    C[n_s][n_e]+=1
    
ans=0
for c_1 in range(10):
    for c_2 in range(10):
        ans += C[c_1][c_2]*C[c_2][c_1]
        
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(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 _ 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):


"""
https://atcoder.jp/contests/abc152/tasks/abc152_d
Low Elements.
"""

def solve(n):
    res = 0

    for num in range(n+1):
        start = int(str(num)[0])
        end = int(str(num)[-1])

        for i in range(1, 10):
            for j in range(1, 10):
                if i == start and j == end: dp[i][j] += 1

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

    return res


if __name__ == "__main__":
    n = int(input())
    dp = [[0 for _ in range(10)] for _ in range(10)]

    print(solve(n))

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

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

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


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

Solution (Python):


"""
https://atcoder.jp/contests/abc152/tasks/abc152_d
Low Elements.
"""

def solve(n):
    res = 0

    for num in range(n+1):
        start = int(str(num)[0])
        end = int(str(num)[-1])

        for i in range(10):
            for j in range(10):
                if i == start and j == end: dp[i][j] += 1

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

    return res


if __name__ == "__main__":
    n = int(input())
    dp = [[0 for _ in range(10)] for _ in range(10)]

    print(solve(n))

Solution (Python):
from math import factorial
n=int(input())
d=[[0]*10 for _ in range(10)]
for i in range(1,n+1):
  j=i
  while True:
    if j//10:j//=10
    else:break
  d[j][i%10]+=1
ans=0
for i in range(10):
  for j in range(10):
    ans+=d[i][j]*d[j][i]
print(ans)

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

dp=[[0 for n in range(10)] for n in range(10)]
dp1=dp.copy()

for n in range(x+1):
    v=str(n)
    p=int(v[0])
    q=int(v[-1])
    for a in range(10):
        for b in range(10):
            if a==p and b==q:
                dp[a][b]+=1

res=-1

for n in range(10):
    for k in range(10):
        res+=dp[n][k]*dp[k][n]

print(res)

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    C = [[0]*10 for i in range(10)]
    N = read_int()
    for n in range(1, N+1):
        first = last = n%10
        while n != 0:
            first = n%10
            n //= 10
        C[first][last] += 1
    answer = 0
    for i in range(0, 10):
        for j in range(0, 10):
            answer += C[i][j]*C[j][i]
    return answer


if __name__ == '__main__':
    print(solve())

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

N = int(readline())

from collections import defaultdict
dic = defaultdict(int)

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

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

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

for i in range(n+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    if a == 0 or b == 0:
        continue
    cnt[a][b] += 1

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

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

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

for i in range(n+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    if a == 0 or b == 0:
        continue
    cnt[a][b] += 1

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

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

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

for i in range(1, n + 1):
    now = str(i)
    pre, pos = int(now[0]), int(now[-1])
    if pos == 0: continue
    count[pre][pos] += 1

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):
n = int(input())

li = [0]*100
for i in range(n+1):
    li[int(str(i)[0]+str(i)[-1])]+=1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += li[int(str(i)+str(j))] * li[int(str(j)+str(i))]

print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())

cnts = defaultdict(int)
for i in range(1, N + 1):
    if i % 10 == 0:
        continue
    digits = []
    while i > 0:
        i, r = divmod(i, 10)
        digits.append(r)
    digits = digits[::-1]
    cnts[(digits[0], digits[-1])] += 1

ans = 0
for (head, tail), c1 in cnts.items():
    c2 = cnts.get((tail, head), 0)
    ans += c1 * c2
print(ans)

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

Solution (Python):
from collections import defaultdict
N = int(input())
dic = defaultdict(int)
for n in range(1,N+1):
    s = str(n)
    dic[(int(s[0]), int(s[-1]))] += 1

res = 0
for key, value in dic.items():
    a, b = key
    cnt = dic.get((b,a),0)
    res += value * cnt

print(res)

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

Solution (Python):
N = int(input())
A = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, N+1):
    S = str(i)
    if S[-1] == '0':
        continue
    A[int(S[0])-1][int(S[-1])-1] += 1
ans = 0
for i in range(9):
    for j in range(9):
        ans += A[i][j]*A[j][i]
print(ans)

Solution (Python):

def main():
    N = int(input())
    NN = [[0]*10 for _ in range(11)]
    for i in range(11,100):
        As = str(i)[0]
        Ae = str(i)[-1]
        cnt = 0
        for j in range(1,N+1):
            if str(j)[-1]== As and str(j)[0] == Ae:
                cnt +=1
        NN[int(As)][int(Ae)] = cnt


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

    print(ans)

if __name__ == '__main__':
        main()

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


def check(min_, max_, x):
  ret = sum([10**gap for gap in range(len(str(x))-2)])
  if min_ <= x:
    ret += int((min(max_, x) - min_)/10) + 1
  return ret


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

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B

  print(sum_)


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

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


def check(min_, max_, x):
  if x < min_:
    return 0
  else:
    return int((min(max_, x) - min_)/10) + 1


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

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      

  print(sum_)


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

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


def check(min_, max_, x):
  if max_ <= x:
    
    return int((max_ - min_)/10) + 1
  elif min_ <= x:
    
    
    
    
    return int((x - min_)/10) + 1
  else:
    return 0


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

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      

  print(sum_)


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

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


def check(min_, max_, x):
  if max_ <= x:
    return 10**max(0, len(str(min_))-2)
  elif min_ <= x:
    ret = max(1, int(str(x)[1:-1]) if len(str(x)) > 2 else 1)
    if int(str(min_)[-1]) <= int(str(x)[-1]):
      ret += 1
    return ret
  else:
    return 0


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

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      

  print(sum_)


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

Solution (Python):
N = int(input())
NN = [[0]*10 for _ in range(11)]
for i in range(11,100):
    As = str(i)[0]
    Ae = str(i)[-1]
    cnt = 0
    for j in range(1,N+1):
        if str(j)[-1]== As and str(j)[0] == Ae:
            cnt +=1
    NN[int(As)][int(Ae)] = cnt


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

print(ans)

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = [[0]*10 for i in range(10)]

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

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

print(ans)

Solution (Python):
from collections import defaultdict

def main():
    n = int(input())
    d = defaultdict(int)
    for i in range(1, n+1):
        s = str(i)
        a = s[0]
        b = s[-1]
        d[a, b] += 1
    ans = 0
    d_items = list(d.items())
    for (a, b), v in d_items:
        ans += v * d[b,a]
    print(ans)
    
if __name__ == '__main__':
    main()

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

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

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

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

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

for n in range(1,N+1):
  y = str(n)
  A[int(y[0])][int(y[-1])] += 1
  


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

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

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

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

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

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

m = {}
for i in range(1, N + 1):
  x = str(i)
  if '0' in [x[0], x[-1]]:
    continue
  key = str([x[0], x[-1]])
  add = 1
  
  if key not in m:
    m[key] = add
  else:
    m[key] += add

ans = 0
for i in range(1, 10):
  for j in range(1, 10):
    key = str([str(i), str(j)])
    if key in m:
      d = m[key]
      if i == j:
        ans += d ** 2
      elif str([str(j),str(i)]) in m:
        ans += d * m[str([str(j),str(i)])]
    
print(ans)

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

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

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

Solution (Python):
from collections import Counter, defaultdict
import sys

sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools
from copy import deepcopy
import queue
from collections import deque





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

    data = [[0 for i in range(10)] for j in range(10)]
    for i in range(1, num + 1):
        now_str = str(i)
        data[int(now_str[0])][int(now_str[-1])] += 1

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

    
    print(ans)




if __name__ == '__main__':
    main()

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


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

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



ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        
        

        
        ans += h_and_t[i][j] * h_and_t[j][i]
print(ans)

Solution (Python):
n=int(input())
ans=0
for i in range(1,n+1):
    i2 = str(i)
    if int(i2[-1])==0:
        pass
    elif int(i2[0]) > int(i2[-1]):
        for j in range(len(i2)-1):
            ans+= (10**j)*2
    elif int(i2[0]) < int(i2[-1]):
        for j in range(len(i2)-2):
            ans+= (10**j)*2
    elif len(i2)>2:
        for j in range(len(i2)-2):
            ans+= (10**j)*2
        ans+=3
        ans+=int(i2[1:-1])*2
    else:
        ans+=len(i2)*2-1
print(ans)

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

n = int(input())

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

Solution (Python):
import math
from collections import Counter

N = int(input())

c = Counter()

ans = 0
for n in range(1, N + 1):
    left = n // (10 ** int(math.log10(n)))
    right = n % 10
    c[(left, right)] += 1

for left, right in c.keys():
    ans += c[(left, right)] * c[(right, left)]

print(ans)

Solution (Python):
n=int(input(""))
le=len(str(n))
ten=10**(le-1)
one=int(str(n)[-1])

b=[]
for k in range(9):
    a=[]
    for i in range(9):
        a+=[0]
    b+=[a]
    
if(le==1):
    print(1)
else:
    
    for i in range(9):
            s=0
            if(ten*(i+2)<=n):
                s=10**(le-2)
                for k in range(9):
                    b[i][k]+=s
            elif(ten*(i+1)<n):
                s=int((n-ten*(i+1))/10)+1
                if(one==0):
                    s-=1
                for k in range(9):
                    
                    b[i][k]+=s
                    if (k+1==one):
                        s-=1
    
    for i in range(9):
        b[i][i]=1+b[i][i]
        
    s=0    
    for i in range(2,le):
            s+=10**(i-2)
    
    c=0
    for i in range(9):
        for k in range(9):
            c+=(b[i][k]+s)*(b[k][i]+s)
    
            
        
        
    print(c)

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())
top_bottom = [[0]*10 for _ in range(10)]
for i in range(1, N + 1):
    s = str(i)
    top_bottom[int(s[0])][int(s[-1])] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += top_bottom[i][j] * top_bottom[j][i]
print(ans)

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

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 copy
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)]


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


def solve():
    N = Scanner.int()
    cnt = [[0 for _ in range(10)] for _ in range(10)]
    for i in range(1, N + 1):
        front = int(str(i)[0])
        back = int(str(i)[-1])
        cnt[front][back] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += cnt[i][j] * cnt[j][i]
    print(ans)


def main():
    
    
    
    
    
    
    solve()


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
dic = {}
for i in range(1,n+1):
    if str(i)[-1] == 0:
        continue
    key = str(i)[0]
    key += "+" + str(i)[-1]
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1
res = 0
for i in range(1,n+1):
    if str(i)[-1] == 0:
        continue
    key = str(i)[-1]
    key += "+" + str(i)[0]
    if key in dic:
        res += dic[key]
print(res)

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

Solution (Python):
from collections import defaultdict
strN=input()
lenN=len(strN)
N=int(strN)

if N<=9:print(N)
else:
  count = defaultdict(int)
  for i in range(10,N+1):
    a,b=int(str(i)[0]),int(str(i)[-1])
    if b==0:continue
    count[(a,b)] += 1

  ans=0
  for i in range(1,10):
    count[(i,i)] += 1
  count = dict(count)
  for key in count.keys():
    if key[0]<key[1] and count.get((key[1],key[0])):
      ans += count[key] * count[(key[1],key[0])] * 2
    if key[0]==key[1]:
      ans += count[key]**2
  print(ans)

Solution (Python):
N = int(input())
C = {(i,j):0 for i in range(1,10) for j in range(1,10)}
num = list(range(1,10))
for k in range(1,N+1):
    k = str(k)
    i = int(k[0])
    j = int(k[-1])
    if i in num and j in num:
        C[(i,j)] += 1
cnt = 0
for i in range(1,10):
    for j in range(1,10):
        cnt += C[(i,j)]*C[(j,i)]
print(cnt)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = [[0]*10 for _ in range(10)]

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

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

print(ans)

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

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

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

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

Solution (Python):
N=int(input())
l = [[] for _ in range(9)]
for i in range(0,9):
    for j in range(0,9):
        l[i].append(0)
for i in range(N+1):
    if (int(str(i)[0])!=0 and int(str(i)[-1])!=0):
        l[int(str(i)[0])-1][int(str(i)[-1])-1]+=1
t=0
for i in range(9):
    for j in range(9):
        t+=l[i][j]*l[j][i]
print(t)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    cnt = [[0] * 10 for _ in range(10)]
    for i in range(1, n + 1):
        i = str(i)
        head = int(i[0])
        foot = int(i[-1])
        cnt[head][foot] += 1

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


if __name__ == '__main__':
    resolve()

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

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


if __name__ == "__main__":
    resolve()

Solution (Python):



def main():
    num = int(input())
    counter = [[0] * 10 for _ in range(10)]
    for x in range(1, num + 1):
        x = str(x)
        head = int(x[0])
        tail = int(x[-1])
        counter[head][tail] += 1
    gen = (counter[head][tail] * counter[tail][head]
           for head in range(1, 10)
           for tail in range(1, 10))
    print(sum(gen))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N = NI()
    grid = make_grid(10, 10, 0)
    ans = 0
    for i in range(N+1):
        s = str(i)
        t = int(s[0])
        b = int(s[-1])
        grid[t][b] += 1
    for i in range(1, 10):
        for j in range(1, 10):
            ans += grid[i][j] * grid[j][i]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N = NI()
    grid = make_grid(10, 10, 0)
    ans = 0
    for i in range(N+1):
        s = str(i)
        t = int(s[0])
        b = int(s[-1])
        grid[t][b] += 1
    for i in range(1, 10):
        for j in range(1, 10):
            if i != j:
                ans += grid[i][j] * grid[j][i]
            else:
                ans += grid[i][j] ** 2
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
x=str(n)
keta=len(str(n))

l=[[0]*10 for i in range(10)]
for i in range(n+1):
    num=str(i)
    sento=int(num[0])
    matsubi=int(num[-1])
    if matsubi==0:
        continue
    l[sento][matsubi]+=1

sm=0
for i in range(n+1):
    num=str(i)
    sento=int(num[0])
    matsubi=int(num[-1])
    if matsubi==0:
        continue
    sm+=l[matsubi][sento]

print(sm)

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

n, s, c = int(input()), [[0 for i in range(10)] for j in range(10)], 0
for i in range(1, n+1): s[int(str(i)[0])][int(str(i)[-1])] += 1
print(sum([s[i][j] * s[j][i] for i in range(1, 10) for j in range(1, 10)]))

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

g = [[0] * (10) for _ in range(10)]
for i in range(1, N + 1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    g[a][b] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += g[i][j]*g[j][i]

print(ans)

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

C = [[0 for _ in range(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):
import sys
input = sys.stdin.buffer.readline

n = int(input())
dic = {}
cdic = {}
d = 0
for i in range(1,n+1):
    matsubi = i%10
    sentou = i//(10**d)
    if sentou >= 10:
        d += 1
        sentou = i//(10**d)
    

    if sentou == matsubi:
        if sentou not in cdic:
            cdic[sentou] = 0
        cdic[sentou] += 1
        continue

    sw = False
    if sentou > matsubi:
        sentou,matsubi = matsubi,sentou
        sw = True

    if (sentou,matsubi) not in dic:
        dic[(sentou,matsubi)] = [0,0]

    if not sw:
        dic[(sentou,matsubi)][0] += 1
    else:
        dic[(sentou,matsubi)][1] += 1

ans = 0

for item in cdic:
    ans += cdic[item]*(cdic[item])

for item in dic:
    ans += dic[item][0]*dic[item][1]*2

print(ans)

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

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

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

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

dp = [[0] * 10 for _ in range(10)]
for x in range(1, N + 1):
    j = x % 10
    while x >= 10:
        x //= 10
    i = x % 10
    dp[i][j] += 1
    
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += dp[i][j] * dp[j][i]
        
print(ans)

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

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

for num in range(1, n+1):
    s = str(num)
    x, y = int(s[0]), int(s[-1])
    if x != 0 and y != 0:
        d[x-1][y-1] += 1

for i in range(9):
    k = -1
    for j in range(9):
        if i == j:
            ans += pow(d[i][j], 2)
            k = 1
        elif k == 1:
            ans += 2*d[i][j]*d[j][i]

print(ans)

Solution (Python):
def II(): return int(input())
N=II()
def solve(n):
  tbl=[0]*100
  ans=0
  for i in range(1,n+1):
    i=str(i)
    tbl[int(i[0]+i[-1])]+=1
  for i in range(1,n+1):
    i=str(i)
    ans+=tbl[int(i[-1]+i[0])]
  return ans
a=solve(N)
print(a)

Solution (Python):
def II(): return int(input())
N=II()
tbl=[0]*101
for i in range(1,N+1):
  i=str(i)
  tbl[int(i[0]+i[-1])]+=1
ans=0
for i in range(1,N+1):
  i=str(i)
  ans+=tbl[int(i[-1]+i[0])]
print(ans)

Solution (Python):
def II(): return int(input())
N=II()
tbl=[0]*101
for i in range(1,N+1):
  i=str(i)
  tbl[int(str(i[0]+i[-1]))]+=1
ans=0
for i in range(1,N+1):
  i=str(i)
  ans+=tbl[int(str(i[-1]+i[0]))]
print(ans)