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())
map_ans = []
ans = 0
for i in range(10):
    map_ans.append([0] * 10)
for i in range(1, n + 1):
    pre = str(i)[0]
    suf = str(i)[-1]
    map_ans[int(pre)][int(suf)] += 1
for i in range(1, 10):
    for j in range(1, 10):
        ans += map_ans[i][j] * map_ans[j][i]
print(ans)

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

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

ans = 0

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

print(ans)

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

a_list= [[0 for i in range(10)] for j in range(10)]
b_list= [[0 for i in range(10)] for j in range(10)]
for i in range(N+1):
    a=int(str(i)[0])
    b=int(str(i)[-1])
    a_list[a][b]+=1
    b_list[b][a]+=1

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

print(count)

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

Solution (Python):

import re
import math


mod=int(10**9+7)


N=int(input())
A=[[0 for a in range(11)] for b in range(11)]
for a in range(1,N+1):
    sa=str(a)
    A[int(sa[0])][int(sa[-1])]+=1
ans=0
for b in range(1,10):
    for e in range(1,10):
        ans+=A[b][e]*A[e][b]
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))


def cmd(n,r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
 
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
 
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
 
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
 
    return result

n = inp()
d = defaultdict(int)
for i in range(1,n+1):
    if i%10==0:
        continue
    if i<10:
        d[i*10+i] += 1
        continue
    tmp = str(i)
    d[int(tmp[0]+tmp[-1])] += 1

res = 0
fin = set()
for key in list(d):
    
    if key%11==0:
        res += d[key]**2
    else:
        tmp = int(str(key)[-1] + str(key)[0])
        if tmp in fin:
            continue
        fin.add(key); fin.add(tmp)
        res += d[key] * d[tmp] * 2
print(res)

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

for i in range(1,N+1):
    S = str(i)
    A = int(S[0])
    B = int(S[-1])
    if A>0 and B>0:
        T[A-1][B-1]+=1

for i in range(9):
    for j in range(9):
        K[i][j] = T[i][j]*T[j][i]

output = 0
for i in range(9):
    output += sum(K[i])
print(output)

Solution (Python):
n = int(input())
count = 0
l = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1,n+1):
  ii = str(i)
  s = int(ii[0])
  t = int(ii[-1]) 
  l[s][t] += 1
for i in range(1,9):
  for j in range(i+1,10):
    count += l[i][j]*l[j][i]*2
for i in range(1,10):
  count += l[i][i]*l[i][i]
print(count)

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

Solution (Python):
from collections import defaultdict

N = int(input())

top_end = defaultdict(int)

ans = 0
for i in range(1, N + 1):
    stri = str(i)
    ans += top_end[(stri[-1], stri[0])] * 2
    top_end[(stri[0], stri[-1])] += 1
    if stri[0] == stri[-1]:
        ans += 1

print(ans)

Solution (Python):
n=int(input())
l=len(str(n))
dp=[[0]*9 for i in range(9)]
count=0
for i in range(1,n+1):
    x,y=int(str(i)[0]),int(str(i)[-1])
    if x!=0 and y!=0:
        dp[x-1][y-1]+=1
for i in range(9):
    for j in range(9):
        count+=dp[i][j]*dp[j][i]
print(count)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

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

for i in range(1,N+1):
    k = list(str(i))
    st = int(k[0])
    ls = int(k[-1])
    MAP[st][ls] += 1
ANS = 0
for i in range(1,10):
    k = MAP[i]
    for j in range(1,10):
        m = k[j]
        ANS += m*MAP[j][i]
print(ANS)

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

Solution (Python):
import math


def main():
    N = int(input())
    cnt = [[0] * 10 for _ in range(10)]
    for i in range(1, N+1):
        d = int(math.log(i, 10))
        cnt[i//(10**d)][i % 10] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += cnt[i][j] * cnt[j][i]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import bisect
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]



def A():
    n, m = LI()
    if n == m:
        print("Yes")
    else:
        print("No")

    return



def B():
    a, b = LI()
    if a <= b:
        ans = str(a) * b
    else:
        ans = str(b) * a
    print(ans)
    return



def C():
    n = I()
    p = LI()
    n_min = n + 1
    ans = 0
    for num in p:
        if num < n_min:
            ans += 1
            n_min = num
    print(ans)
    return



def D():
    n = I()
    cnt = [[0] * 10 for i in range(10)]
    ans = 0
    for i in range(1, n + 1):
        h = str(i)[0]
        t = str(i)[-1]
        cnt[int(h)][int(t)] += 1
    for i in range(1, 10):
        for j in range(1, 10):
            ans += cnt[i][j] * cnt[j][i]
    print(ans)
    return



def E():
    return



def F():
    return



if __name__ == "__main__":
    D()

Solution (Python):
import collections
import math
import operator as op
from functools import reduce
import numpy as np
import math
import bisect
import heapq


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


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


def main():
    N = get_input(int)
    A = np.zeros((10, 10), dtype=np.int32)
    for i in range(1, N+1):
        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)

    return


if __name__ == '__main__':
    main()

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

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

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

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

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

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

ans = 0

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

print(ans)

Solution (Python):
from collections import Counter

n = int(input().strip())

cnt = Counter()
result = 0
for i in range(1, n + 1):
    s = str(i)
    result += cnt[s[-1], s[0]] * 2
    if s[0] == s[-1]:
        result += 1
    cnt[s[0], s[-1]] += 1
print(result)

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

AB = [[[] for _ in range(10)] for _ in range(10)]
for i in range(1, N+1):
    if int(str(i)[0]) != 0 and int(str(i)[-1]) != 0:
        AB[int(str(i)[0])][int(str(i)[-1])].append(i)

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

print(ans)

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

combi=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
    num = str(i)
    sentou=num[0]
    matubi=num[-1]
    combi[int(sentou)][int(matubi)]+=1

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

print(ans)

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

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

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

for i in range(1, N + 1):
    if i % 10 == 0:
        continue
    s = str(i)
    x = int(s[-1])
    y = int(s[0])
    d[x-1][y-1] += 1

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

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
li=[[0]*10 for i in range(10)]
for i in range(n):
    tmp=str(i+1)
    li[int(tmp[0])][int(tmp[-1])]+=1

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

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

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N = I()

count = 0
d = defaultdict(int)
for i in range(1,10):
    for j in range(1,10):
        count = 0
        for k in range(1,N+1):
            if str(k)[0]==str(j) and str(k)[-1]==str(i):
                count += 1
        d[(i,j)] = count


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

Solution (Python):
from collections import defaultdict
import sys
d = defaultdict(set)
N=int(input())
for i in range(1,N+1):
    
    j=str(i)
    d[(int(j[0]),int(j[-1]))].add(i)

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

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


N = int(readline())
sN = str(N)

a = {}

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

    t = (f, l)
    if not t in a:
        a[t] = 0
    
    a[t] += 1

count = 0


for k, v in a.items():
    f, l = k
    if f == l:
        count += v * v
    else:
        counter = a.get((l, f), 0)
        count += counter * v


print(count)

Solution (Python):
from collections import defaultdict

N = int(input())
count = defaultdict(int)
for n in range(1, N+1):
    i = n
    while i >= 10:
        i //= 10
    j = n % 10
    if i != 0 and j != 0:
        count[(i, j)] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += count[(i, j)] * count[(j, i)]
print(ans)

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

for i in range(1, n + 1):
    strI = str(i)
    if strI[-1] == "0":
        continue
    tmp[int(strI[0]) -1 ][int(strI[-1]) - 1] += 1

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

print(ans)

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

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

ans = 0

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

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

print(ans)

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()

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

table = np.array(table)
answer = (table * table.T).sum()
print(answer)

Solution (Python):
import math
from collections import deque

s=input()
l=len(s)
n=int(s)
ans=0

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

for i in range(1,n+1):
    st=str(i)
    x=int(st[0])
    y=i%10
    dp[x][y]+=1

for i in range(1,n+1):
    st=str(i)
    x=int(st[0])
    y=i%10
    ans+=dp[y][x]

print(ans)

Solution (Python):
from itertools import product
from collections import defaultdict
N = int(input())
S = defaultdict(int)
for i in range(1, N+1):
    s = str(i)
    S[s[0]+s[-1]] += 1
ans = 0
for s, e in product("123456789", repeat=2):
    ans += S[s+e]*S[e+s]
print(ans)

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

for i in range(1,n+1):
    p=len(str(i))
    x=i//(10**(p-1))
    y=i%10
    c[x][y]+=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())


D={}
for i in range(1,N+1):
    I=str(i)
    if (I[-1],I[0]) not in D:
        D[(I[-1],I[0])]=1
    else:
        D[(I[-1],I[0])]+=1

ans=0
for j in range(1,N+1):
    J=str(j)
    if (J[0],J[-1]) in D:
        ans+=D[(J[0],J[-1])]
print(ans)

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

L={(i,q):0 for i in range(1,10) for q in range(1,10)}

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

ans=0
for i in range(1,9):
    for q in range(i+1,10):
        ans+=2*L[(i,q)]*L[(q,i)]

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

print(ans)

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

biao = dict()

for i in range(1, a+1):
    i = str(i)
    if i[0] not in biao:
        biao[i[0]] = {i[-1] : 1}
    else:
        if i[-1] not in biao[i[0]]:
            biao[i[0]][i[-1]] = 1
        else:
            biao[i[0]][i[-1]] += 1

count = 0
for i in range(1, 10):
    for j in range(1, 10):
        try:
            i = str(i)
            j = str(j)
            count += biao[i][j] * biao[j][i]
        except:
            pass

print(count)

Solution (Python):
import queue
import math
import copy
"""
N = int(input())



A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""

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

Solution (Python):
from collections import Counter

N = int(input())

A = [str(i) for i in range(1, N + 1)]

C = Counter()
for s in A:
    C[s[0] + s[-1]] += 1

ans = 0
for s in A:
    if s[0] != '0' and s[-1] != '0':
        ans += C[s[-1] + s[0]]

print(ans)

Solution (Python):
import collections
def main():

    N = int(input())

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

    ans = 0
    for a in d:
        b = (a[1], a[0])
        if b in d:
            ans += d[a] * d[b]
    return ans



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

Solution (Python):
N = int(input())
X = [[0]*10 for _ in range(10)]
for n in range(1, N+1):
    s = str(n)
    a = int(s[0])
    b = int(s[-1])
    X[a][b] += 1

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

Solution (Python):
def main():
    n = int(input())
    d = dict()
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            d[(i, j)] = 0
    for i in range(1, n+1):
        ii = str(i)
        start = int(ii[0])
        end = int(ii[-1])
        if start == 0:
            continue
        if end == 0:
            continue
        d[(start, end)] += 1
    for i in range(1, 10):
        for j in range(1, 10):
            if i != j:
                ans += d[(i, j)]*d[(j, i)]
    for i in range(1, 10):
        ans += d[(i, i)]*d[(i, i)]
    print(ans)


main()

Solution (Python):

import sys
input = sys.stdin.buffer.readline


from collections import defaultdict


def getlist():
	return list(map(int, input().split()))


def main():
	N = int(input())
	L = [[0] * 10 for i in range(10)]
	for i in range(1, N + 1):
		A = str(i)
		x = int(A[0])
		y = int(A[-1])
		L[x][y] += 1
	
	ans = 0
	for i in range(1, 10):
		for j in range(1, 10):
			ans += L[i][j] * L[j][i]

	print(ans)


if __name__ == '__main__':
	main()

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

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

for i in range(1, N + 1):
  s = str(i)
  if s[0] == s[-1]:
    same[int(s[0])] += 1
  else:
    diff[int(s[0])][int(s[-1])] += 1

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

print(answer)

Solution (Python):
def solve(n):
    cnt_dict = dict()
    for k in range(11, 100):
        if k % 10 != 0:
            cnt_dict[str(k)] = 0
    for i in range(1, n + 1):
        if i % 10 != 0:
            cnt_dict[str(i)[0] + str(i)[-1]] += 1
    res = 0  
    for k in range(11, 100):
        if k % 10 != 0:
            res += cnt_dict[str(k)] * cnt_dict[str(k)[1] + str(k)[0]]
    
    return res


def main():
    n = int(input())
    res = solve(n)
    print(res)


def test():
    assert solve(25) == 17
    assert solve(1) == 1
    assert solve(100) == 108
    assert solve(2020) == 40812
    assert solve(200000) == 400000008


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

Solution (Python):
def main():
    n = input()
    dic = {}
    for i in xrange(1, n + 1):
        s = str(i)
        key = (s[0], s[-1])
        try:
            dic[key] += 1
        except KeyError:
            dic[key] = 1
    ans = 0
    for a, b in dic:
        ans += dic[(a, b)] * dic.get((b, a), 0)
    print ans


main()

Solution (Python):
N = int(input())
A = [[0 for i in range(11)] for j in range(11)]
for i in range(1, N+1):
  A[int(str(i)[0])][int(str(i)[-1])] +=1

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

Solution (Python):
N = int(input())
arr = [[0] * 10 for _ in range(10)]
ans = 0
for i in range(1, N+1):
    s = str(i)
    head = int(s[0])
    tail = int(s[-1])
    if head == tail:
        ans += 1
    ans += arr[tail][head] * 2
    arr[head][tail] += 1
print(ans)

Solution (Python):
n = int(input())
data = [0 for i in range(100)]
ans = 0
for now in range(1,n+1):
    s = int(str(now)[0])
    t = int(str(now)[-1])
    q = 10*t + s
    p = 10*s + t
    data[p] += 1
    ans += 2*data[q]
    if s == t:ans -= 1

print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

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

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

ans = 0

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

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

for i in range(9):
    for j in range(9):
        if i==j:
            continue
        ans += t[i][j]*t[j][i]

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

print(ans)

Solution (Python):
N = int(input())
L = [[0 for j in range(10)] for i in range(10)]
for i in range(1,N+1):
    sentou = int(str(i)[0])
    matsubi = int(str(i)[-1])
    L[sentou][matsubi] += 1

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

print(ans)

Solution (Python):
N = int(input())
ans = [[0]*9 for i in range(9)]
for i in range(1,N+1):
    if i%10 == 0:
        continue
    X = str(i)
    A,B = int(X[0])-1,int(X[-1])-1
    ans[A][B]+=1
ret = 0
for i in range(9):
    for j in range(9):
        ret += ans[i][j]*ans[j][i]
print(ret)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()

dic = defaultdict(int)

for i in range(1, N+1):
	dic["{},{}".format(str(i)[0], str(i)[-1])] += 1



ans = 0
for n in dic.copy():
	s, e = n.split(",")
	if s == e:
		ans += dic[n]**2
	else:
		s_o, e_o = e, s
		ans += dic[n] * dic["{},{}".format(s_o, e_o)]
print(ans)

Solution (Python):


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read






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

n = int(input())
for i in range(1,n+1):
    q = i%10
    r = i
    while r:
        p = r%10
        r //= 10
    
    res[p][q] += 1        

ans = 0
for p in range(1,10):
    for q in range(1,10):
        ans += res[p][q]*res[q][p]

print(ans)

Solution (Python):
N = int(input())
ans = 0
cnt = [[0 for j in range(10)] for i in range(10)]
for i in range(1, N+1):
	s = str(i)
	a,b = int(s[0]), int(s[-1])
	cnt[a][b] += 1

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

Solution (Python):

n = int(input())
a = [[0] * 10 for _ in range(10)]
for i in range(1, n+1):
    tmp = str(i)
    mae = int(tmp[0])
    usiro = int(tmp[-1])

    a[mae][usiro] += 1
    



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

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

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

for i in range(n+1):
    s = str(i)[0]
    e = str(i)[-1]
    if s == "0" or e == "0":
        continue
    ac[int(s) - 1][int(e) - 1] += 1

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

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

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

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

for i in range(1, n + 1):
    str_i = str(i)
    top = int(str_i[0])
    bottom = int(str_i[-1])
    if bottom == 0:
        continue
    li[top-1][bottom-1] += 1

cnt = 0
for i in range(1, n + 1):
    str_i = str(i)
    top = int(str_i[0])
    bottom = int(str_i[-1])
    if bottom == 0:
        continue
    cnt += li[bottom-1][top-1]

print(cnt)

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

st = [[0]*10 for _ in range(10)]
for n in range(1,N+1):
    if n%10==0:
        continue
    u = n%10
    d = int(str(n)[0])
    st[u][d] += 1

ans = 0
for n in range(1, N+1):
    if n%10==0:
        continue
    u = n%10
    d = int(str(n)[0])
    ans += st[d][u]

print(ans)

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

a = [[0 for _ in range(10)] for __ in range(10)]

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

ans = 0

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

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

ans = ans

print(ans)

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    dp = [[0 for _ in range(10)] for _ in range(10)]
    ans = 0
    for i in range(1, n+1):
        s = str(i)
        l = int(s[0])
        r = int(s[-1])
        dp[l][r] += 1
    for i in range(1, n+1):
        s = str(i)
        l = int(s[0])
        r = int(s[-1])
        
        ans += dp[r][l]
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

    N = int(input())
    dic = defaultdict(int)
    for i_ in range(1, N+1):
        i = str(i_)
        dic[(int(i[0]), int(i[-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)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

N = int(input())

class BinomialCoefficient(object):

    def __init__(self, N=510000, MOD=10**9+7):
        self.fac = [1, 1]
        self.finv = [1, 1]
        self.inv = [0, 1]
        self.MOD = MOD
        for i in range(2, N + 1):
            self.fac.append((self.fac[-1] * i) % MOD)
            self.inv.append((-self.inv[MOD % i] * (MOD // i)) % MOD)
            self.finv.append((self.finv[-1] * self.inv[-1]) % MOD)

    def comb(self, n, r):
        if r < 0 or n < r or r < 0:
            return 0
        return self.fac[n] * self.finv[r] * self.finv[n - r] % self.MOD


bc = BinomialCoefficient(N)
d = dict()
for i in range(1, N + 1):
    key = str(i)[0] + str(i)[-1]
    if d.get(key) is None:
        d[key] = 1
    else:
        d[key] += 1

ans = 0
for k, v1 in d.items():
    v2 = d.get(k[1] + k[0])
    if v2 is not None:
        ans += v1 * v2

print(ans)

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

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

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

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




print(ans)

Solution (Python):

N = int(input())

memo = [0]*100

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

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

print(cnt)

Solution (Python):
import sys

stdin = sys.stdin

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

n = ni()
ct = 0
for i in range(1, n+1):
    head = i%10
    if head == 0:
        continue
    k = i
    tail = 0
    while k > 0:
        tail = k%10
        k //= 10
    if head == tail:
        if head <= n:
            ct += 1
    
    d = 10
    while d <= n:
        
        if n >= (head+1)*d:
            ct += d//10
        elif n < head*d:
            pass
        else:
            ct += (n-head*d-tail+10)//10
        d *= 10
print(ct)

Solution (Python):

import sys
from operator import itemgetter

def inpl(): return list(map(int, input().split()))

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

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

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

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

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

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

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

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

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

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

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

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

for i in range(1,N+1):
    sento = int(str(i)[0])
    ushiro = int(str(i)[-1])
    D[sento][ushiro] += 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())
tb = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1,n+1):
  t = int(str(i)[0])
  b = int(str(i)[-1])
  tb[t][b] += 1
ans = 0
for l in range(10):
  for m in range(10):
    ans += tb[l][m] * tb[m][l]
print(ans)

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

a = {}
for i in range(1, n + 1):
    k = str(i)
    j = (int(k[0]), int(k[-1]))
    if j in a:
        a[j] += 1
    else:
        a[j] = 1

def g(b):
    if b in a:
        return a[b]
    return 0
ans = 0
for i in range(10):
    for j in range(10):
        ans += g((i, j)) * g((j, i))
print(ans)

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

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

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

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()

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

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

ans = 0
for s in range(10):
    for e in range(10):
        ans += cnts[s][e] * cnts[e][s]

print(ans)