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 Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N = Z()
    maxl = len(str(N))
    output = 0
    for n in range(1, N+1):
        if n%10 == 0: continue
        s = str(n)
        if s[0] == s[-1]:
            for j in range(1, maxl+1):
                if j == 1:
                    output += 1
                    continue
                if j == 2:
                    if 10*int(s[-1]) + int(s[0]) <= N:
                        output += 1
                    continue
                if j < maxl:
                    output += 10 ** (j-2)
                    continue
                
                if int(s[-1]) < int(str(N)[0]):
                    output += 10 ** max(0, j-2)
                elif s[-1] == str(N)[0]:
                    output += int(str(N)[1:maxl-1]) + 1
                    if int(s[0]) > int(str(N)[-1]): output -= 1
        else:
            for j in range(2, maxl+1):
                if j < maxl:
                    output += 10 ** (j-2)
                    continue
                
                if int(s[-1]) < int(str(N)[0]):
                    output += 10 ** (j-2)
                if s[-1] == str(N)[0]:
                    if j == 2:
                        if 10*int(s[-1]) + int(s[0]) <= N:
                            output += 1
                    else:
                        output += int(str(N)[1:maxl-1]) + 1
                        if int(s[0]) > int(str(N)[-1]): output -= 1
    print(output)
    return

if __name__ == '__main__':
    main()

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

def pair(x):
    a=x%10
    b=int(str(x)[0])
    return (a,b)
dic={}
for i in range(1,n+1):
    p=pair(i)
    if(dic.get(p)==None):dic[p]=1
    else:dic[p]+=1
ans=0
for i in range(1,n+1):
    a,b=pair(i)
    p=(b,a)
    if(not dic.get(p)==None):ans+=dic[p]
print(ans)

Solution (Python):
N = input()
l = len(N)
if l == 1:
    print(N)
    exit()
nf = N[0]
ne = N[-1]
nm = 0
base = 1
if l > 2:
    nm = int(N[1:l-1:])
for i in range(1,l-2):
    base += 10**i
ans = 0

for i in range(int(N)+1):
    i = str(i)
    f = i[0]
    e = i[-1]
    if e != "0":
        if f == e:
            ans += 1
        if e < nf:
            ans += base
            if l > 2:
                ans += 10**(l-2)
        elif e == nf:
            if f <= ne:
                ans += base
                if l>2:
                    ans += nm + 1
            else:
                ans += base
                ans += nm
        else:
            if l >2:
                ans += base
print(ans)

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

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

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

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

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

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

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

for i in range(1,N+1):
    if i%10==0:
      continue
    s=str(i)
    up=int(s[0])
    down=int(s[-1])
    cnt[up][down]+=1

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

print(ans)

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

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

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

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

print(ans)

Solution (Python):
import sys
from collections import Counter
from collections import deque
import math
import bisect
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
grid=[[0]*10 for i in range(10)]
for i in range(1,n+1):
    a=int(str(i)[0])
    b=int(str(i)[-1])
    grid[a][b]+=1
ans=0
for i in range(10):
    for k in range(10):
        ans+=grid[i][k]*grid[k][i]
print(ans)

Solution (Python):
import sys
from collections import Counter
from collections import deque
import math
import bisect
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
grid=[[0]*10 for i in range(10)]
for i in range(1,n+1):
    a=int(str(i)[0])
    b=int(str(i)[-1])
    grid[a][b]+=1
ans=0
for i in range(10):
    for k in range(10):
        ans+=grid[i][k]*grid[k][i]
print(ans)

Solution (Python):
import collections
n = int(raw_input())
def g( a, b , n):
	count = 0
	
	if a == b and a <=n : 
		count +=1
	
	if a *10 + b <= n:
		count +=1
	
	if len(str(n)) <=2:
		return count

	
	s = str(n)
	if len(s) - 3 >= 1:
		count += 10 ** (len(s) - 3)

	

	s = str(n)
	if a == int(s[0]):
		m = s[1:-1]

		if m != '':

			
			count += int(m)
			if b <= int(s[-1]):
				count +=1
	elif a < int(s[0]):
		count += 10 ** (len(s) - 2)

	return count


h = collections.Counter()
for k in range(1, n+1):
	ks = str(k)
	a,b = int(ks[0]), int(ks[-1])
	h[(a,b)] +=1
s= 0
for a in range(1,10):
	for b in range(1,10):
		s += h[(a,b)] * h[(b,a)]

print s

Solution (Python):
n=int(input())
def func(i,j,m): 
  ret=0
  s=len(str(m))
  m0=int(str(m)[0])
  if s==1:
    if i==j and i<=m:
      return 1
    else:
      return 0
  if s==2:
    if 10*i+j<=m:
      return 2 if i==j else 1
    else:
      return 1 if i==j else 0
  if i<m0:
    for si in range(s-1):
      ret+=pow(10,si)
  elif i==m0:
    if j<=int(str(m)[-1]):
      ret+=int(str(m)[1:-1])+1
    else:
      ret+=int(str(m)[1:-1])
    for si in range(s-2):
      ret+=pow(10,si)
  elif i>m0:
    for si in range(s-2):
      ret+=pow(10,si)
  return ret if i!=j else ret+1
ans=0
d={}
for i in range(1,10):
  for j in range(1,10):
    a=func(i,j,n)*func(j,i,n)
    ans+=a
    
    

print(ans)

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

from collections import defaultdict

def f(x):
    return (int(str(x)[0]), int(str(x)[-1])) 
def main():
    N = int(readline())
    df = defaultdict(int)
    for i in range(1, N+1):
        df[f(i)] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans +=  df[(i, j)]*df[(j, i)]
    print(ans)
if __name__ == '__main__':
    main()

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

from collections import defaultdict

def f(x):
    return (int(str(x)[0]), int(str(x)[-1])) 
def main():
    N = int(readline())
    df = defaultdict(int)
    for i in range(1, N+1):
        df[f(i)] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans +=  df[(i, j)]*df[(j, i)]
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
n = int(input())
p = [[0 for i in range(10)] for j  in range(10)]
for i in range(1,10):
    for j in range(1,10):
        if i == j:
            if i <= n:
                p[i][j] += 1
        now = ""
        d = deque()
        d.append("")
        while d:
            now = d.popleft()
            for k in range(10):
                d.append(now+str(k))
            if int(str(i) + now + str(j)) <= n:
                p[i][j] += 1
            else:
                break
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=int(input())
count=[[0]*9 for _ in range(9)]
for i in range(1,n+1):
  if i%10>0:  
    i=str(i)
    x,y=int(i[0])-1,int(i[-1])-1
    count[x][y]+=1
    
ans=0
for i in range(9):
  for j in range(9):
      ans+=count[i][j]*count[j][i]
print(ans)

Solution (Python):
def culc(x):
    a = x % 10
    b = x
    while x:
        b = x
        x //= 10
    return a, b

n = int(input())
l = [[0 for i in range(9)] for j in range(9)]
ans = 0

for i in range(1,n+1):
    a,b = culc(i)
    if a != 0 and b != 0:
        l[a-1][b-1] += 1

for i in range(1,n+1):
    a,b = culc(i)
    if a != 0 and b != 0:
        ans += l[b-1][a-1]
print(ans)

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

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

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

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

Solution (Python):

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


def aggregate(N):
    pair_agg = {}
    for i in range(1, N + 1):
        pair = toHeadTail(i)
        if pair in pair_agg.keys():
            pair_agg[pair] += 1
        else:
            pair_agg[pair] = 1
    return pair_agg

N = int(input())
pair_agg = aggregate(N) 
count = 0
for i in range(1, N + 1):
    pair = toHeadTail(i)
    pair_inverse = (pair[1], pair[0])
    if pair_inverse in pair_agg.keys():
        count += pair_agg[(pair[1], pair[0])]
print(count)

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

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

Solution (Python):
N = int(input())
A = [[0]*10 for _ in range(10)]
for i in range(1,N+1):
  a = i%10 
  b = i // (10**(len(str(i))-1))
  if a!= 0:
    A[a][b] += 1
t = 0
for i in range(1,10):
  s = A[i][i]
  if s != 0:
    t += int(s + (s*(s-1)))
for i in range(1,9):
  for j in range(i+1,10):
    t += A[i][j] * A[j][i]*2
print(t)

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):
def main():
    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)


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    N = int(input())
    d = len(str(N))
    ansL = [[0 for _ in range(9)] for _ in range(9)]
    for i in range(1,10):
        for j in range(1,10):
            tmp = 0
            
            if i == j and i <= N:
                tmp += 1
            
            if i*10+j <= N:
                tmp += 1
            
            if d > 2:
                for k in range(3,d):
                    tmp += 10**(k-2)

                
                if int(str(N)[0]) > i:
                    tmp += 10**(d-2)
                elif int(str(N)[0]) == i:
                    tmp += int(str(N)[1:-1])
                    if int(str(N)[-1]) >= j:
                        tmp += 1
            ansL[i-1][j-1] = tmp
    
    ans = 0
    for i in range(9):
        for j in range(9):
            ans += ansL[i][j] * ansL[j][i]
    print(ans)
                

if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
lis = [[0 for i in range(10)] for i in range(10)]
for i in range(1,N+1):
    fir = i//(10**(len(str(i))-1))
    las = i%10
    lis[fir][las]+=1
sum=0
for i in range(0,10):
    for j in range(i,10):
        if i==j:
            sum+=lis[i][j]*(lis[i][j]-1)+lis[i][j]
        else:
            sum+=lis[i][j]*lis[j][i]*2


print(sum)

Solution (Python):
N=int(input())
lis = [[0 for i in range(10)] for i in range(10)]
for i in range(1,N+1):
    fir = i//(10**(len(str(i))-1))
    las = i%10
    lis[fir][las]+=1
sum=0
for i in range(0,10):
    for j in range(i,10):
        if i==j:
            sum+=lis[i][j]*(lis[i][j]-1)+lis[i][j]
        else:
            sum+=lis[i][j]*lis[j][i]*2


print(sum)

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

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

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

for j in range(9):
  for k in range(j+1):
    if k == j:
      ans += (ho[k][k])**2
    else:
      ans += (ho[j][k])*(ho[k][j])*2
print(ans)

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

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

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

print(ans)

Solution (Python):
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())

l = [ [ 0 for i in range(9) ] for j in range(9) ]



for k in range(N):
    x = str(k+1)
    i = int(x[0])
    j = int(x[-1])

    

    if i == 0 or j == 0:
        continue
    l[i-1][j-1] += 1

ans = 0
for i in range(9):
    for j in range(i,9):
        if i == j:
            n = l[i][j]
            ans += n**2
        else:
            ans += l[i][j] * l[j][i] * 2
            

print(ans)

Solution (Python):
import numpy as np

N = int(input())

l = np.zeros((9,9), dtype=np.int)


for k in range(N):
    x = str(k+1)
    i = int(x[0])
    j = int(x[-1])

    

    if i == 0 or j == 0:
        continue
    l[i-1][j-1] += 1

ans = 0
for i in range(9):
    for j in range(i,9):
        if i == j:
            n = l[i][j]
            ans += n**2
        else:
            ans += l[i][j] * l[j][i] * 2
            

print(ans)

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

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

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

print(ans)

Solution (Python):
import sys















sys.setrecursionlimit(10000001)
INF = 10 ** 10
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    n = ni()
    mat = [[0 for _ in range(10)] for _ in range(10)]

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

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

    print(ans)




if __name__ == '__main__':
    main()

Solution (Python):
import sys















sys.setrecursionlimit(10000001)
INF = 10 ** 10
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    n = ni()
    mat = [[0 for _ in range(10)] for _ in range(10)]

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

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

    print(ans)




if __name__ == '__main__':
    main()

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

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

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



def input_int():
    return int(input())



def int1(x):
    return int(x) - 1



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



def input_to_int_tuple():
    return tuple(map(int, input().split()))



def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    n = input_int()


    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

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

    return ret


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

Solution (Python):
import sys


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


def main():
    N = int(input())
    C = [[0] * 10 for _ in range(10)]
    for i in range(10):
        for j in range(10):
            for n in range(1, N + 1):
                L = len(str(n)) - 1
                a = n // (10 ** L)
                b = n % 10
                if a == i and b == j:
                    C[i][j] += 1
    answer = 0
    for i in range(10):
        for j in range(10):
            answer += C[i][j] * C[j][i]
    print(answer)


if __name__ == "__main__":
    main()

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

check = [[0] * 9 for i in range(9)]

if n <= 10:
    print(n)
else:
    for i in range(1, n+1):
        I = str(i)
        if I[-1] != '0':
            check[int(I[0]) - 1][int(I[-1]) - 1] += 1
    ans = 0
    for i in range(9):
        for j in range(9):
            ans += check[i][j] * check[j][i]
    print(ans)

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

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

for i in range(1, N+1):
    s = str(i)
    head, tail = int(s[0]), int(s[-1])
    if head > 0 and tail > 0:
        d[(head, tail)] += 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):
c = [[0]*10 for i in range(10)]
for i in range(1,int(input())+1):
  i = str(i)
  c[int(i[0])][int(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())

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

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

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

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    n = II()
    D = [[0]*10 for _ in range(10)]

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

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

if __name__ == '__main__':
    solve()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

import numpy as np

def solve():
    n = II()
    D = np.zeros((10, 10))

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

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

if __name__ == '__main__':
    solve()

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

for i in range(1, N+1):
    end = i%10
    head = int(str(i)[0])
    c[head][end] += 1

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

print(ans)

Solution (Python):




from collections import defaultdict

N = int(input())


"""
愚直解だと、Aを1~Nまで動かした時、各Aについて何個Bを取れるかを足し合わせると答えが出る。が、これはN<=2*10**5なのでO(N^2)は間に合わない。

事前に先頭がxで末尾がyの個数を、1~Nまで数え上げておくと、各Aについて先頭と末尾を抜き出して、それに対応するBの個数を足し合わせるとO(N)で求められる
"""
d = defaultdict(int)



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


ans = 0
for a in range(1,N+1):
    x = int(str(a)[0])
    y = int(str(a)[-1])
    ans += d[(y,x)]

print(ans)

Solution (Python):
n=int(input())
d={}
for i in range(1,n+1):
  f=i//(10**(len(str(i))-1))
  l=i-(i//10)*10
  if l!=0: 
    c=f*10+l
    if c in d:
      d[c]+=1
    else:
      d[c]=1
ans=0
for i in d:
  if int(str(i)[::-1]) in d:
    ans+=d[i]*d[int(str(i)[::-1])]
print(ans)

Solution (Python):
def main():
    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[i*10+j] *d[j*10+i]

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    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[i*10+j] *d[j*10+i]

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    d = {}
    for i in range(0,10):
        for j in range(0,10):
            d[i*10+j] = 0
    
    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[i*10+j] *d[j*10+i]

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    d = {}
    for i in range(0,10):
        for j in range(0,10):
            d[i*10+j] = 0
    
    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[i*10+j] *d[j*10+i]

    print(ans)

if __name__ == '__main__':
    main()

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

t = [0]*(100)
ans = 0
for k in range(1,N+1):
    if k%10 != 0:
        t[10*int(str(k)[0])+int(str(k)[-1])] += 1
for k in range(11,100):
    if k%10 != 0:
        ans += t[k]*t[10*(k%10)+k//10]
print(ans)

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

for i in range(10):
    for j in range(i,10):
        if(i==j):
            ans+=count[i][j]*count[j][i]
        else:
            ans+=2*(count[i][j]*count[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):
n = int(input())

all_count = 0
options = [str(i) for i in range(10)]

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

for i in range(n+1):
    checking = str(i)
    maeato[int(checking[0])][int(checking[-1])]+=1
all_count = 0
for i in range(1,10):
    for j in range(1,10):
        for k in range(1,10):
            for p in range(1,10):
                if i == p and j == k:
                    all_count+=maeato[i][j]*maeato[k][p]
print(all_count)

Solution (Python):
from sys import exit

n = int(input())
cs = [[0]*10 for i in range(10)]

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

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

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

print(ans)

Solution (Python):
def main():
    N = int(input())
    C = [[0]*10 for _ in range(10)]
    for num in range(1, N+1):
        s = str(num)
        px = int(s[0])
        sf = int(s[-1])
        C[px][sf] += 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__':
    main()

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    dictType = dict()
    for i in range(1, 10):
        for j in range(1, 10): dictType[(i, j)] = 0
    for i in range(1, N + 1):
        bottom = i % 10
        if bottom == 0: continue
        top = i
        while top >= 10: top //= 10
        dictType[(top, bottom)] += 1

    ans = 0
    for i in range(1, N + 1):
        bottom = i % 10
        if bottom == 0: continue
        top = i
        while top >= 10: top //= 10
        ans += dictType[(bottom, top)]
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

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

for i in range(1, N+1):
    f = int(str(i)[0])
    l = int(str(i)[-1])
    cnt[f][l] += 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())
num = [[0 for _ in range(10)] for j in range(10)]
for i in range(1,N+1):
    s = str(i)
    f = int(s[0])
    l = int(s[-1])
    if l == 0:
        continue
    num[f][l] += 1

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

print(ans//2)

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

N = int(input())

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

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

ans = 0
for i in range(1, N + 1):
    s = str(i)
    x = int(s[0])
    y = int(s[-1])
    ans += lst[y][x]

print (ans)

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

n = int(input())

from collections import defaultdict

d = defaultdict(int) 

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

ans = 0

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

print(ans)

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

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

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

print(ans)

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

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

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

Solution (Python):
from collections import defaultdict

N = int(input())

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

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

print(ans)

Solution (Python):

import math
import numpy as np
n = int(input())
num = np.zeros((10, 10))
for i in range(1, n + 1):
    a = i // pow(10, (int(math.log10(i))))
    b = i % 10
    num[b][a] += 1
ans=0
for i in range(10):
    for j in range(10):
        ans += num[i][j] * num[j][i]
print(int(ans))

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    default = 0

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

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

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

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
import math
import numpy as np
n = int(input())
ans = 0
num = np.zeros((10,10))
for i in range(1,n+1):
    ma = i // pow(10,int(math.log10(i)))
    mi = i % 10
    num[mi][ma] += 1
for i in range(10):
    for j in range(10):
        ans += num[i][j] * num[j][i]
print(int(ans))

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    lis=[[0]*9 for _ in range(9)]
    for i in range(1,n+1):
        si=str(i)
        if int(si[-1])>0:
            lis[int(si[0])-1][int(si[-1])-1]+=1
    cunt=0
    for i in range(9):
        for j in range(9):
            cunt+=lis[i][j]*lis[j][i]
    print(cunt)
    
    

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    lis=[[0]*9 for _ in range(9)]
    for i in range(1,n+1):
        si=str(i)
        if int(si[-1])>0:
            lis[int(si[0])-1][int(si[-1])-1]+=1
    cunt=0
    for i in range(9):
        for j in range(9):
            cunt+=lis[i][j]*lis[j][i]
    print(cunt)
    
    

if __name__=='__main__':
    main()

Solution (Python):
from collections import defaultdict
N = int(input())
ans = 0
c = defaultdict(int)

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

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

Solution (Python):
import itertools

n = int(input())

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

for i in range(1, n + 1):
    I = str(i)
    first_d = int(I[0])
    last_d = int(I[-1])
    if first_d == 0 or last_d == 0:
        continue
    counter[first_d][last_d] += 1

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