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())
s0=list(str(n))
ans=0

if len(s0)==1:
    print(n)
    exit()

if len(s0)==2:
    for i in range(1,n+1):
        s1=list(str(i))
        if s1[-1]=='0':
            continue
        if s1[0]==s1[-1]:
            ans+=1
        if int(s1[-1])*10+int(s1[0])<=n:
            ans+=1
    print(ans)
    exit()

for i in range(1,n+1):
    s1=list(str(i))
    if s1[-1]=='0':
        continue
    if s1[0]==s1[-1]:
        ans+=1
    for j in range(2,len(s0)):
        ans+=10**(j-2)
    if int(s0[0])>int(s1[-1]):
        ans+=10**(len(s0)-2)
    elif s0[0]==s1[-1]:
        ans+=int(''.join(s0[1:len(s0)-1]))+1
        if int(s0[-1])<int(s1[0]):
            ans-=1
print(ans)

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

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

def check(v,n):
  if len(n) > len(v):
    return 10**(len(v)-2)
  if len(n) < len(v):
    return 0
  if n[0] > v[-1]:
    return 10**(len(v)-2)
  if n[0] == v[-1]:
    x=1 if v[0] > n[-1] else 0
    return max(0, int(n[1:-1])-x+1)
  if n[0] < v[-1]:
    return 0

for i in range(1,n+1):
  st=str(i)
  nst=str(n)
  if st[-1]=='0':
    continue
  if len(st) == 1:
    cnt+=1
    continue
  if len(st)==2:
    if int(st[-1]+st[0]) <= n:
      cnt += 1
  else:
    cnt+= check(st,nst)
    for i in range(len(st)-2):
      xst=st[-1]+'9'*i+st[0]
      cnt += check(xst,nst)*2
  if st[0]==st[-1]:
    cnt += 2

print(cnt)

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):
import sys
import  math
from heapq import *
import fractions
from collections import defaultdict
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
n_l=[[0]*10 for i in range(10)]
for i in range(1,N+1):
    if(1<=i and i<10):
        n_l[i][i]+=1
    elif(10<i and i<10**2):
        n_l[i%10][i//10]+=1
    elif(10**2<i and i<10**3):
        n_l[i%10][i//100]+=1
    elif(10**3<i and i<10**4):
        n_l[i%10][i//1000]+=1
    elif(10**4<i and i<10**5):
        n_l[i%10][i//10000]+=1
    elif(10**5<i):
        n_l[i%10][i//100000]+=1
    

ans=0
for i in range(1,10):
    for k in range(1,10):
        if(i!=k):
            ans+=(n_l[i][k])*((n_l[k][i]))

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

Solution (Python):
import sys
import itertools

import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(input())

c = [[0 for i in range(10)] for _ in range(10)]
def f(n):
    x = 0
    while n > 0:
        n //= 10
        x +=1
    return x

for i in range(1, N + 1):
    d = f(i)
    front = i // (10 ** (d - 1)) 
    back = i % 10
    c[front][back] += 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 collections import Counter
n = int(input())
li = [(int(str(x)[0]), int(str(x)[-1])) for x in range(1, n + 1)]

c = dict(Counter(li).items())

cnt = 0
for k, v in c.items():
    a, b = k
    if a == b:
        cnt += v**2
    elif (b, a) in c:
        v2 = c[(b, a)]
        cnt += v * v2

print(cnt)

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

import copy

raw=[0]*10
a=[]

for i in range(10):
  a.append(copy.deepcopy(raw))

ans=0


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



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

print(ans)

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

def hs(k):
    ks = str(k)
    lk = len(ks)
    a = int(ks[0])
    b = int(ks[len(ks) - 1])
    if lk == 1:
        return 1
    if b == 0:
        return 0
    s = 0
    if (a > b) & (lk > 1):
        s += 10 ** (lk - 2) * 2
    elif a == b:
        if lk == 2:
            s += 1
        else:
            s += ((k % (a * 10**(lk - 1))) // 10 + 1) * 2 - 1
    if lk >= 2:
        for i in range(lk - 1):
            if lk - i - 3 >= 0:
                s += 10 ** (lk - 3 - i) * 2
            elif a == b:
                s += 2
    return s

ans = 0
for i in range(1, n + 1):
    ans += hs(i)
print(ans)

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

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
class Counter(dict):
    def __missing__(self, i):
        return 0


n = int(input())

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

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

print(ans)

Solution (Python):
import sys
from collections import defaultdict

def solve():
    input = sys.stdin.readline
    N = int(input())
    ht = defaultdict(int)

    for i in range(1, N + 1):
        k = i
        head = k % 10
        while k >= 10:
            k //= 10
        tail = k
        if head > 0: ht[(head, tail)] += 1
    ans = 0
    for i in range(1, N + 1):
        k = i
        tail = k % 10
        while k >= 10:
            k //= 10
        head = k
        if tail > 0: ans += ht[(head, tail)]
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

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

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

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

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

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

cnt = 0
for i in range(10):
    for j in range(i, 10):
        if i == j:
            cnt += lst[i][j] * lst[j][i]
        else:
            cnt += lst[i][j] * lst[j][i] * 2
print(cnt)

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


dig = [[0 for _ in range(10)] for i in range(10)]

for i in range(1,N+1):
    n = str(i)
    f = int(n[0])
    l = int(n[-1])
    dig[f][l] += 1


ans = 0

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

print(ans)

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

Solution (Python):
N = int(input())
rem = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
for i in range(1,N+1):
    sti = str(i)
    rem[int(sti[0])][int(sti[-1])] += 1
count = 0
for k in range(10):
    for j in range(10):
        count += rem[k][j]*rem[j][k]
        
print(count)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import defaultdict


N = int(read())
counter = defaultdict(int)
for x in map(str,range(1,N+1)):
    head = x[0]
    tail = x[-1]
    counter[(head, tail)] += 1
ans = 0
items = list(counter.items())
for (head, tail), cnt in items:
    ans += cnt * counter[(tail, head)]
print(ans)

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

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

for i in range(1, N+1):
  if i%10==0: continue
  strI = str(i)
  f,l = strI[-1], strI[0]
  dp[int(f)][int(l)] += 1

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

print(res)

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):
N = int(input())
N_ = str(N)
f_N, l_N = int(N_[0]), int(N_[-1])
digit = len(N_)
if digit > 2:
    inter_N = int(N_[1:digit-1])
cnt = 0
for num in range(1, N+1):
    num = str(num)
    f, l = int(num[-1]), int(num[0])
    if f == 0:
        continue
    for i in range(1, digit+1):
        if i == 1:
            if f == l:
                cnt += 1
            else:
                continue
        else:
            if i != digit:
                cnt += 10**(i-2)
            elif digit == 2 and i == digit:
                if f*10+l <= N:
                    cnt += 1
                else:
                    continue
            else:
                if f > f_N:
                    continue
                elif f == f_N:
                    if l > l_N:
                        cnt += inter_N
                    elif l <= l_N:
                        cnt += (inter_N+1)
                else:
                    cnt += 10**(i-2)
print(cnt)

Solution (Python):

c = [[0] * 10 for _ in [0] * 10]
for i in range(1, int(input()) + 1):
    c[int(str(i)[0])][int(str(i)[-1])] += 1

print(sum(c[i][j] * c[j][i] for i in range(10) for j in range(10)))

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

c = [[0 for _ in range(10)] for __ in range(10)]
for x in range(1, N+1):
    check = str(x)
    it = int(check[0])
    jt = int(check[-1])
    c[it][jt] += 1

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

print(ans)

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

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

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

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

Solution (Python):
N=int(input())
A=[[0 for i in range(10)] for j in range(10)]
for i in range(1,N+1):
  i=str(i)
  ii=len(i)
  A[int(i[0])][int(i[ii-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):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
import numpy as np

Mod = 1000000007
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1] 
for i in range(2, 10**5 + 1):
    fact.append((fact[-1] * i) % Mod)
    inv.append((-inv[Mod % i] * (Mod // i)) % Mod)
    factinv.append((factinv[-1] * inv[-1]) % Mod)
    
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n - r] % p
    
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[y] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main():  
    n = int(input())
    ls = [[0] * 10 for i in range(10)]
    for i in range(1, n + 1):
        val = list(str(i))
        ls[int(val[-1])][int(val[0])] += 1
    
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += ls[i][j] * ls[j][i]

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

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

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

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

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

def count_ab(start, end):
    l = len(str(n))
    if start == 0:
        return 0
    if l == 1:
        if start == end:
            if n >= start:
                return 1
            else:
                return 0
        else:
            return 0
    else:
        if start == end:
            cnt = 1
        else:
            cnt = 0
        if int(str(n)[0]) > start:
            for i in range(1, l):
                cnt += 10 ** i // 10
        if int(str(n)[0]) == start:
            for i in range(1, l - 1):
                cnt += 10 ** i //10
            cnt += int(str(n)[1:]) // 10
            if int(str(n)[-1]) >= end:
                cnt += 1
        if int(str(n)[0]) < start:
            for i in range(1, l - 1):
                cnt += 10 ** i //10
        return cnt

ans = 0
for i in range(10):
    for j in range(10):
        A = count_ab(i, j)
        B = count_ab(j, i)
        ans += A * B

print(ans)

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

Solution (Python):
c=[[0 for i in range(9)] for i in range(9)]

n=int(input())
for i in range(1,n+1):
    I=str(i)
    if I[0]!="0" and I[len(I)-1]!="0":
        c[int(I[0])-1][int(I[len(I)-1])-1]+=1

s=0
for i in range(9):
    for j in range(9):
        s+=c[i][j]*c[j][i]

print(s)

Solution (Python):
n=int(input())
c = [[0]*10 for i in range(10)]
n_len = len(str(n))
for k in range(1,n+1):
  k_str = str(k)
  l,r=int(k_str[0]),int(k_str[-1])
  c[l][r]+=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 collections import defaultdict
"""
N 以下の全ての整数に対して, (先頭, 末尾) を数える.
(先頭, 末尾), (末尾, 先頭)の組は, 独立しているので掛け算で求められる.
"""
n = int(input())
ansl = defaultdict(lambda: 0)
for i in range(1,n+1):
	sentou = int(str(i)[0])
	ushiro = int(str(i)[-1])
	ansl[(sentou, ushiro)] += 1
ans = 0
for i in range(1, 10):
	for j in range(1, 10):
		ans += ansl[(i, j)]*ansl[(j, i)]
print(ans)

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

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

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

print(res)

Solution (Python):
n = input()
l = len(n)

table = [[0] * 9 for _ in range(9)]
for i in range(9):
    for j in range(9):
        if l == 1:
            if i + 1 <= int(n) and i == j:
                table[i][j] += 1
        elif l == 2:
            if 10 * (i + 1) + (j + 1) <= int(n):
                table[i][j] += 1
            if i == j:
                table[i][j] += 1
        else:
            if i + 1 < int(n[0]):
                for k in range(l-1):
                    table[i][j] += 10 ** k
            elif i + 1 == int(n[0]):
                if j + 1 <= int(n[-1]):
                    table[i][j] += int(n[1:l-1]) + 1
                else:
                    table[i][j] += int(n[1:l-1])
                for k in range(l-2):
                    table[i][j] += 10 ** k
            else:
                for k in range(l-2):
                    table[i][j] += 10 ** k
            if i == j:
                table[i][j] += 1

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

print(res)

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

for i in range(1,N+1):
  i = str(i)
  if i[0] == '0' or i[-1] == '0':
    pass
  else:
    l[int(i[0])][int(i[-1])] += 1

ans = 0

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

print(ans)

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

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

for i in range(n+1):
  if i % 10 != 0:
    str_i = str(i)
    mat[int(str_i[0])][int(str_i[-1])] += 1
c = 0
for y in range(1, 10):
  for x in range(y, 10):
    if y == x:
      c += mat[y][x] * mat[x][y]
    else:
      c += mat[y][x] * mat[x][y] * 2
print(c)

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

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

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


print(ans)

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

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

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

ans = 0

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

print(ans)

Solution (Python):

printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def foo(n,x,y):
    l = len(n)
    sm = 0
    if x==y and (l>1 or n[0]>=x):
        sm = 1
    if l==1:
        return sm
    for i in range(2,l):
        sm += 10**(i-2)
    if x==n[-1]:
        sm += bar(n[:-1],y)
    elif x<n[-1]:
        sm += 10**(l-2)
    
    return sm

def bar(n,y):
    l = len(n)
    if l==1:
        return 1 if n[0]>=y else 0
    sm = (n[-1])*10**(l-2) + bar(n[:-1],y)
    return sm

nn = inn()
nnn = nn
n = []
while nn>0:
    n.append(nn%10)
    nn //= 10
sm = 0
for a in range(1,nnn+1):
    y = a%10
    if y==0:
        continue
    aa = a
    while aa>0:
        x = aa
        aa //= 10
    sm += foo(n,y,x)
print(sm)

Solution (Python):
n = int(raw_input())
f = []
for i in range(10):
	f.append([0 for _ in range(10)])
ans = 0

for a in range(1, n + 1):
	val = a
	first, last = 0, val % 10
	while val != 0:
		first = val % 10
		val = val / 10
	f[first][last] += 1

for a in range(1, n + 1):
	val = a
	first, last = 0, val % 10
	while val != 0:
		first = val % 10
		val = val / 10
	ans += f[last][first]

print ans

Solution (Python):
n=input()
dic={}
for i in range(1,int(n)+1):
  wkstr=str(i)[0]+str(i)[len(str(i))-1]
  if wkstr in dic:
    dic[wkstr]+=1
  else:
    dic[wkstr]=1
ans=0
for key in dic.keys():
  revkey=key[len(key)-1]+key[0]
  if key==revkey:
    ans+=dic.get(key)*dic.get(revkey)
  else:
    ans+=dic.get(key)*dic.get(revkey,0)
print(ans)

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

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

Solution (Python):
import sys
N = int(sys.stdin.readline().rstrip())

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

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

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

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


if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):
from collections import defaultdict
def main():
    n = int(input())
    d = defaultdict(int)
    for i in range(n):
        num = str(i + 1)
        head = num[0]
        tail = num[-1]
        d[(head, tail)] += 1

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

if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = lambda: sys.stdin.readline().strip()

def C(first, last, ndigs, N):
    ans = 0
    curr = first * 10 ** (ndigs - 1) + (last if ndigs > 1 else 0)
    incs = 0
    while curr <= N and incs < 10 ** (ndigs - 1):
        ans += 1
        curr += 10
        incs += 10
    return ans

def solve(N: int):
    ndigs = len(str(N))
    ans = 0
    for ndigsA in range(1, ndigs + 1):
        for ndigsB in range(1, ndigs + 1):
            for first in range(1, 10):
                for last in range(1, 10):
                    if (ndigsA > 1 and ndigsB > 1) or first == last:
                        ans += C(first, last, ndigsA, N) * C(last, first, ndigsB, N)
    print(ans)

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    solve(N)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
lis = [[0 for i in range(10)] for j in range(10)]
for i in range(1, n+1):
    s = str(i)
    l = int(s[0])
    r = int(s[-1])
    lis[l][r] += 1
ans = 0
for i in range(10):
    for j in range(10):
        if i == j:
            ans += lis[i][j] ** 2
        else:
            ans += lis[i][j] * lis[j][i]
print(ans)

Solution (Python):
n = int(input())
lis = [[0 for i in range(10)] for j in range(10)]
for i in range(1, n+1):
    s = str(i)
    l = int(s[0])
    r = int(s[-1])
    lis[l][r] += 1
ans = 0
for i in range(10):
    for j in range(10):
        if i == j:
            ans += lis[i][j] ** 2
        else:
            ans += lis[i][j] * lis[j][i]
print(ans)

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

    res = 0
    numbers = [[0] * 10 for _ in range(10)]
    for i in range(1, n + 1):
        ss = str(i)
        numbers[int(ss[0])][int(ss[-1])] += 1

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

    print(res)


if __name__ == '__main__':
    resolve()

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

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

Solution (Python):
n=int(input())
ans=0
currentpower=0
for i in range(1,n+1):
  if i>=10**(currentpower+1):
    currentpower+=1
  b=i%10
  a=(i-i%(10**currentpower))//(10**currentpower)
  if b==0:
    continue
  if a==b:
    ans+=1
    if currentpower>0:
      ans+=2
    r=max(0,((i-(a*(10**currentpower))-b)//10))
    ans+=2*r
    for j in range(0,currentpower-1):
      ans+=2*(10**j)
  elif a<b:
    for j in range(0,currentpower-1):
      ans+=2*(10**j)
  else:
    for j in range(0,currentpower):
      ans+=2*(10**j)
print(ans)

Solution (Python):
from collections import defaultdict

def abc152_d():
    N = int(input())
    freq = defaultdict(int)
    for i in range(1, N+1):
        s = str(i)
        freq[(s[0], s[-1])] += 1  

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

abc152_d()

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

Solution (Python):
def main():
    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 a in range(10):
        for b in range(10):
            ans += cnt[a][b]*cnt[b][a]
    print(ans)

main()

Solution (Python):
n=int(input())
c=[[0]*10 for _ in range(10)]
ans=0
for i in range(1,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 main():
    N = int(input())
    
    
    
    
    
    
    if N<=9:
        print(N)
    else:
        count = 9
        for n in range(10,N+1):
            n = str(n)
            nlen = len(n)
            if n[-1] == '0':
                continue
            if n[0]<n[-1]:
                tmp = int('1'*(nlen-2)) if nlen>2 else 0
                count += tmp*2
            elif n[0]>n[-1]:
                tmp = int('1'*(nlen-1)) if nlen>2 else 1
                count += tmp*2
            else:
                
                tmp = int('1'*(nlen-2))+int(n[1:-1])+1 if nlen>2 else 1
                count += tmp*2-1
                
                count += 2
        print(count)

main()

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,N+1):
    s = str(i)
    ans += d[(s[-1], s[0])]


print(ans)

Solution (Python):
import itertools

N = int(input())

comb = itertools.combinations_with_replacement([str(i) for i in range(1, 10)], 2)
count = 0
for c in comb:
    A = c[0]
    B = c[1]
    count_1 = 0
    count_2 = 0
    for i in range(1, N+1):
        if str(i)[0] == A and str(i)[-1] == B:
            count_1 += 1
        elif str(i)[0] == B and str(i)[-1] == A:
            count_2 += 1
    if A == B:
        count += count_1 ** 2
    else:
        count += count_1 * count_2 * 2
print(count)

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

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

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

print(cnt)

Solution (Python):
import sys
import numpy as np
input = lambda: sys.stdin.readline().rstrip()


def solve():
    N = int(input())
    d = np.zeros((9, 9), dtype='i8')

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

    ans = 0
    for i in range(9):
        for j in range(9):
            if i == j:
                ans += d[i][j] * (d[i][j] - 1) + d[i][j]
            else:
                ans += d[i][j] * d[j][i]

    
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
A= int(input())
l=[[0]*10 for _ in range(10)]
for i in range(1,A+1):
   n=str(i)
   if n[0] == "0" or n[-1] == "0":
      continue
   l[int(n[0])][int(n[-1])]+=1
ans=0
for i in range(1,10):
   for j in range(1,10):
      ans+=l[i][j] * l[j][i]
print(ans)

Solution (Python):
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

N = int(input())

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

ans = 0
for a in range(1, 10):
    for b in range(1, 10):
        ans += dp[a][b] * dp[b][a]

print(ans)

Solution (Python):
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

N = int(input())
sN = str(N)
l = len(sN)

ans = 0
for n in range(1, min(10, N+1)):
    sn = str(n)
    ans += 1
    
    cnt = 1
    for i in range(2, l):
        ans += cnt
        cnt *= 10

    if l == 2:
        if sN >= sn[-1] + sn[0]:
            ans += 1
    elif l > 2 and sN[0] >= sn[-1]:
        if sN[0] == sn[-1]:
            hoge = int(sN[1:-1])
            if sN[-1] < sn[0]:
                hoge -= 1
            ans += hoge + 1
        elif sN[0] > sn[-1]:
            ans += cnt

for n in range(11, N+1):
    sn = str(n)
    if sn[-1] != "0":
        if n > 10 and sn[0] == sn[-1]:
            ans += 1


        cnt = 1
        for i in range(2, l):
            ans += cnt
            cnt *= 10

        if l == 2:
            if sN >= sn[-1] + sn[0]:
                ans += 1
        elif l > 2 and sN[0] >= sn[-1]:
            if sN[0] == sn[-1]:
                hoge = int(sN[1:-1])
                if sN[-1] < sn[0]:
                    hoge -= 1
                ans += hoge + 1
            elif sN[0] > sn[-1]:
                ans += cnt

print(ans)

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))
n = ii()

def get_head(x):
    while x:
        b = x%10
        x //= 10
    return b

dp = [[0] * 10 for i in range(10)]
for i in range(1,n+1):
    h = get_head(i)
    t = i%10
    dp[h][t] += 1

for i in range(10):
    dp[0][i] = 0
    dp[i][0] = 0

ans = 0
for i in range(1,n+1):
    h = get_head(i)
    t = i%10
    ans += dp[t][h]

print(ans)

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

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

for i in range(10):
    for j in range(10):
        res += cif[i][j]*cif[j][i]
print(res)

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

num_cnt = [ [0]*9  for _ in range(9)]
for i in range(n):
    num = i+1
    num_s = str(num)
    top = int(num_s[0]) -1
    last = int(num_s[-1]) -1
    if top == -1 or last == -1:
        continue
    num_cnt[top][last] += 1

ans = 0
for x in range(0,9):
    for y in range(0,9):
        ans += num_cnt[x][y] * num_cnt[y][x]
print(ans)