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

from collections import defaultdict
from collections import namedtuple

P = namedtuple('P', ['front', 'end'])

def f(x):
    return P(str(x)[0], str(x)[-1]) 

def main():

    n = int(input())
    freq = defaultdict(int)

    for i in range(1, n+1):
        p = f(i)
        freq[p]+=1


    ans = 0
    for i in range(1, n + 1):
        p = f(i)
        q = (p.end, p.front)
        ans += freq[q]

    print(ans)

main()

Solution (Python):
n = int(input())
li_a = []
for i in range(9):
  li_a.append([0,0,0,0,0,0,0,0,0])
for i in range(1, n+1):
  k = str(i)
  k0 = int(k[0])
  k1 = int(k[-1])
  if k0*k1 != 0:
    li_a[k0-1][k1-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    ans += li_a[i][j]*li_a[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):

from collections import defaultdict

def f(x):
    return (str(x)[0], str(x)[-1]) 

def main():

    n = int(input())
    freq = defaultdict(int)

    for i in range(1, n+1):
        p = f(i)
        freq[p]+=1


    ans = 0
    for i in range(1, n + 1):
        p = f(i)
        q = (p[1], p[0])
        ans += freq[q]

    print(ans)

main()

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 = np.zeros((10, 10)).astype(int)
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):
n=int(input())
li = [0] * 100 
for i in range(1,n+1):
  top = str(i)[0]
  bottom = str(i)[-1]
  if top != 0 and bottom != 0:
    li[int(top+bottom)] += 1

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

print(ans)

Solution (Python):
n = int(input())
cnt = [[0 for _ in range(11)] for _ in range(11)]
for i in range(1, n + 1):
    x = i
    while x >= 10:
        x //= 10
    cnt[x][i % 10] += 1
ans = 0
for i in range(1, n + 1):
    x = i
    while x >= 10:
        x //= 10
    ans += cnt[i % 10][x]
print(ans)

Solution (Python):
n = int(input())
cnt = [[0 for _ in range(11)] for _ in range(11)]
for i in range(1, n + 1):
    x = i
    while x >= 10:
        x //= 10
    cnt[x][i % 10] += 1
ans = 0
for i in range(1, n + 1):
    x = i
    while x >= 10:
        x //= 10
    ans += cnt[i % 10][x]
print(ans)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return sorted([list(map(int, input().split())) for _ in range(n)])

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

if __name__ == "__main__":
    main()

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

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

    if first == 0 or last == 0:
        continue
    else:
        count[first][last] += 1

ans = 0
for first in range(1, 10):
    for last in range(1, 10):
        pattern = count[first][last] * count[last][first]
        ans += pattern

print(ans)

Solution (Python):
from collections import defaultdict

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

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

num = '123456789'
res = 0
for i in num:
    for j in num:
        res += d[(i, j)] * d[(j, i)]

print(res)

Solution (Python):


iim = lambda: map(int, input().rstrip().split())

def d10(n):
    a = []
    while n:
        n, m = divmod(n, 10)
        a.append(m)

    return a

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

    if N < 10:
        print(N)
        return

    k = N // 10
    A = d10(N)

    a = A[-1]
    b = A[0]

    ans = k ** 2 + 8
    if b:
        c = len(A)
        d = d0 = 0
        for i in range(c-1):
            d, d0 = 10 ** i + d, d

        if a > b:
            ans += 2 * d * b
        else:
            e = k - a*10**(c-2)
            ans += 2 * d * (a-1)
            ans += 2 * (d0 + e + 1) + 1
            ans += 2 * d0 * (b - a)
    print(ans)



if __name__ == "__main__":
    resolve()

Solution (Python):


iim = lambda: map(int, input().rstrip().split())

def d10(n):
    a = []
    while n:
        n, m = divmod(n, 10)
        a.append(m)

    return a

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

    if N == 1:
        print(1)
        return

    k = N // 10
    A = d10(N)

    a = A[-1]
    b = A[0]

    ans = k ** 2 + 8
    if b:
        c = len(A)
        d = d0 = 0
        for i in range(c-1):
            d, d0 = 10 ** i + d, d

        if a > b:
            ans += 2 * d * b
        else:
            e = k - a*10**(c-2)
            ans += 2 * d * (a-1)
            ans += 2 * (d0 + e + 1) + 1
            ans += 2 * d0 * (b - a)
    print(ans)



if __name__ == "__main__":
    resolve()

Solution (Python):
N=int(input())
A=[[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9]

for n in range(N+1):
    temp=str(n)
    r=int(temp[-1])
    l=int(temp[0])
    A[r-1][l-1]+=1

ans=0
for i in range(9):
    for j in range(9):
        ans+=A[i][j]*A[j][i]
if N<=9:
    print(N)
else:
    print(ans)

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

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

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

ans = 0
for i in range(10):
    for j in range(10):
        if(i == j):
            ans += table[i][i]**2
        else:
            ans += table[i][j]*table[j][i]
print(ans)

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

ans = 0
for i in range(1,n+1):
    s=str(i)
    if s[0]=="0" or s[-1]=="0":
        continue
    else:
        d[int(s[0])][int(s[-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())
c = [[0] * 10 for i in range(10)]

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

s = 0
for t in range(10):
  for b in range(10):
    s += c[t][b] * c[b][t]

print(s)

Solution (Python):
def d():
    N = int(input())
    a = {}
    count = 0

    for i in range(1, N+1):
        l, r = str(i)[0], str(i)[-1]
        a[l, r] = a.get((l, r), 0) + 1

    

    for i in range(1, N+1):
        l, r = str(i)[0], str(i)[-1]
        
        count += a.get((r, l), 0)

    print(count)



d()

Solution (Python):
n = int(input())
d = [[0]* 9 for _ in range(9)]
for num in range(1, n+1):
    str_num = str(num)
    if str_num[-1] != "0":
        start = int(str_num[0]) - 1
        end = int(str_num[-1]) - 1
        d[start][end] += 1

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

print(ans)

Solution (Python):
def d():
    N = int(input())
    a = {}
    count = 0

    for i in range(1, N+1):
        l, r = str(i)[0], str(i)[-1]
        a[l, r] = a.get((l, r), 0) + 1

    

    for i in range(1, N+1):
        l, r = str(i)[0], str(i)[-1]
        
        count += a.get((r, l), 0)

    print(count)



d()

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


def func(x):
    while x >= 10:
        x //= 10
    return x


cnt = [[0] * 10 for _ in range(10)]
for i in range(1, n + 1):
    digit_end = i % 10
    digit_start = func(i)
    cnt[digit_start][digit_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())

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


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

result = 0
for j in range(10):
    for k in range(10):
        result += cnt[j][k] * cnt[k][j]

print(result)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
data=[0]*100
for ni in range(1,n+1):
    a=str(ni)[0]; b=str(ni)[-1]
    a=int(a); b=int(b)
    data[a*10+b]+=1
for i in range(1,10):
    for j in range(1,10):
        ans+=data[i*10+j]*data[j*10+i]

print(ans)

Solution (Python):
def make_key(a, b):
    return str(a) + '-' + str(b)


N = int(input())
nums = {}
for i in range(1, 10):
    for j in range(1, 10):
        nums[make_key(i, j)] = 0

ans = 0
for n in range(1, N + 1):
    strn = str(n)
    if strn[-1] != '0':
        key = make_key(strn[0], strn[-1])
        nums[key] += 1


for key, value in nums.items():
    a, b = map(int, key.split('-'))
    if a == b:
        ans += value ** 2
    else:
        key2 = make_key(b, a)
        ans += value * nums[key2]

print(ans)

Solution (Python):
N = int(input())
l = map(str, range(1, N+1))
summary = [[0 for _ in range(9)] for _ in range(9)]

for i in l:
    if i[-1] != "0":
        summary[int(i[0])-1][int(i[-1])-1] += 1

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

print(c)

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
x = list(d.keys())
for i in x:
    ans+=d[i[::-1]]*d[i]
print(ans)

Solution (Python):
N = int(input())
numMap = {}
answer = 0
def StartEnd(x):
    start = str(x)[0]
    end = str(x)[-1]
    return start, end

for i in range(1, N+1):
    
    pair = StartEnd(i)
    if pair in numMap:
        numMap[pair] += 1
    else:
        numMap[pair] = 1

for i in range(1, N+1):
    key = StartEnd(i)
    rev = key[::-1]
    
    if rev in numMap:
        answer += numMap[rev]
    
print(answer)

Solution (Python):
def left(text, n):
  return text[:n]

def right(text, n):
  return text[-n:]

def mid(text, n, m):
  return text[n-1:n+m-1]
  
n = int(input())

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


for i in range(1,n+1):
	first = int(left(str(i),1))
	end = int(right(str(i),1))
	rangeSet[first][end] += 1

kumiawase = 0
for k in range(1,10):
	for l in range(1,10):
		tmp = rangeSet[k][l] * rangeSet[l][k]
		kumiawase += tmp	
print(kumiawase)

Solution (Python):
n = int(input())
ans = 0
 
def solve(x):
    df = str(x)[0]
    dl = str(x)[-1]
    return df, dl
 
numMap = {}
 
for i in range(1, n+1):
    
    
    pair = solve(i)
    if pair in numMap:
        numMap[pair] += 1
    else:
        numMap[pair] = 1
 
for i in range(1, n+1):
    key = solve(i)
    rev = key[::-1]
    
    
    if rev in numMap:
        ans += numMap[rev]
 

print(ans)

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

def solve(x):
    df = str(x)[0]
    dl = str(x)[-1]
    return df, dl

numMap = {}

for i in range(1, n+1):
    
    
    pair = solve(i)
    if pair in numMap:
        numMap[pair] += 1
    else:
        numMap[pair] = 1

for i in range(1, n+1):
    key = solve(i)
    rev = key[::-1]
    
    
    if rev in numMap:
        ans += numMap[rev]


print(ans)

Solution (Python):
import sys

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

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

    ans = 0
    for y in range(1,10):
        for z in range(1,10):
            ans += arr[y][z] * arr[z][y]

    print(ans)

main()

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

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

for i in range(1,n+1):
    q=i%10
    p = i
    while p>=10:
        p //=10
    r[p][q] +=1
res=0
for i in range(1,10):
    for j in range(1,10):
        res += r[i][j]*r[j][i]
print(res)

Solution (Python):
import collections
n = int(input())
d = collections.Counter(map(lambda x: str(x)[0]+" "+str(x)[-1], list(range(1,n+1))))
ans = 0
for k, v in d.items():
	a,b = map(int, k.split())
	if a==b:
		ans += v*v
	elif a<b:
		s = str(b) + " " + str(a)
		if s in d:
			u = d[s]
			ans += 2*v*u
print(ans)

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


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

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

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

print(ans)

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

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

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

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

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

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

print(ans)

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

count = [0]*100
for i in range(1, n+1):
    s = str(i)
    x = int(s[0] + s[len(s)-1])
    count[x] += 1

ans = 0
for i in range(1, n+1):
    s = str(i)
    x = int(s[len(s)-1] + s[0])
    ans += count[x]

print(ans)

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

ans = 0
C = [ [0 for i in range(10)] for i in range(10)]

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

for first_num in range(1,10):
  for last_num in range(1,10):
    ans += C[first_num][last_num] * C[last_num][first_num]

print(ans)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

C = list2d(10, 10, 0)
for a in range(1, N+1):
    s = int(str(a)[0])
    t = int(str(a)[-1])
    C[s][t] += 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().strip())

arr = ["1",
       "2",
       "3",
       "4",
       "5",
       "6",
       "7",
       "8",
       "9"
       ]
pairs = {}

for i in arr:
    for j in arr:
        pairs[i + "_" + j] = 0
for i in range(1, (n + 1), 1):
    si = str(i)
    if si[0] != "0" and si[-1] != "0":
        pairs[si[0] + "_" + si[-1]] += 1
ans = 0
for i in range(1, (n + 1), 1):
    si = str(i)
    if si[0] != "0" and si[-1] != "0":
        ans += pairs[si[-1] + "_" + si[0]]
print(ans)

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

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

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

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

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

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

print(ans)

Solution (Python):
n = int(input())
d = [[0]*10 for _ in range(10)]
ans = 0
for i in range(1, n+1):
    s = str(i)
    if s[0] == '0' or s[-1] == '0':
        continue
    d[int(s[0])][int(s[-1])] += 1
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())
table = [[0]*10 for _ in range(10)]

for x in range(1,n+1):
    if x%10!=0:
        str_x = str(x)
        l,r = int(str_x[0]), int(str_x[-1])
        table[l][r] += 1

ans = 0
for x in range(1, n+1):
    if x%10 != 0:
        str_x = str(x)
        l,r = int(str_x[0]), int(str_x[-1])
        if l==r:
            ans += table[l][r]
        else:
            ans += table[r][l]
print(ans)

Solution (Python):
N = int(input())
t = [[0 for _ in range(9)] for l in range(9)]
ans = 0
for i in range(1, N+1):
    if len(str(i)) == 1:
        t[i-1][i-1] += 1
    elif str(i)[-1] != "0":
        t[int(str(i)[0])-1][int(str(i)[-1])-1] += 1
for i in range(1, N+1):
    a = str(i)[0]
    b = str(i)[-1]
    if b == "0":
        continue
    elif a == b:
        ans += t[(int(a)-1)][int(a)-1]
    else:
        ans += t[(int(b)-1)][int(a)-1]
print(ans)

Solution (Python):
N = int(input())
C = [[0 for _ in range(10)] for _ in range(10)]
for n in range(1, N+1):
    s, e = int(str(n)[0]), int(str(n)[-1])
    C[s][e] += 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())
 
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())
tbl=[[0 for j in range(10)] for i in range(10)]
for i in range(1,n+1):
    s= str(i)
    h=int(s[0])
    b=int(s[-1])
    tbl[h][b]+=1
ans = 0
for i in range(10):
    for j in range(10):
        ans+=tbl[i][j]*tbl[j][i]
print(ans)

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

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

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

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

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

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

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

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

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

Solution (Python):
def main():
    N = int(input())
    cnt = 0
    '''
    i => Aの末尾の桁が Bの先頭の桁
    j => Bの末尾の桁が Aの先頭の桁
    '''
    cnt = [[0] * 10 for _ in range(10)]
    for i in range(N+1):
        tmp = str(i)
        first = int(tmp[0])
        last = int(tmp[-1])
        cnt[first][last] += 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):
n = int(input())

def getFirstLast(n):
    digs = []
    while n:
        digs.append(n % 10)
        n //= 10
    return digs[-1], digs[0]

cnt = [[0 for _ in range(10)] for _ in range(10)]
for val in range(1, n + 1):
    first, last = getFirstLast(val)
    cnt[first][last] += 1
ret = 0
for first in range(1, 10):
    for last in range(1, 10):
        ret += cnt[first][last] * cnt[last][first]
print(ret)

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

def hoge(m):
    
    return (m%10, int(str(m)[0]))

for k in range(1,n+1):
    a,b = hoge(k)
    cnt[a][b] += 1    

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

print(ret)

Solution (Python):
n=int(input())
def kazu(a):
    return (a%10, int(str(a)[0]))
cnt=[[0]*10 for i in range(10)]
for i in range(1,n+1):
    p,q=kazu(i)
    cnt[p][q]+=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
X=[[0]*10 for i in range(10)]
for i in range(1,n+1):
    t=(int)(str(i)[0])
    b=(int)(str(i)[-1])
    X[t][b]+=1

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

print(ans)

Solution (Python):


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

for x in range(1, n+1):
    beg = int(str(x)[0])
    end = x%10
    c[end][beg] += 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())

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

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

Solution (Python):

N = int(input())

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

for i in range(N+1):

    s = str(i)

    lis[int(s[0])][int(s[-1])] += 1

ans = 0

for i in range(9):

    i += 1

    for j in range(9):

        j += 1

        ans += lis[i][j] * lis[j][i]

print (ans)

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

for i in range(1, N+1):
  istr=str(i)
  x=int(istr[0])
  y=int(istr[-1])
  if y!=0:
    c[x-1][y-1]+=1
    
out=0
for i in range(9):
  for j in range(9):
    out+=c[i][j]*c[j][i]
print(out)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

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


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N):
    ans = 0

    d = Counter()
    for i in range(N+1):
        l = len(str(i))
        a = i // 10 ** (l-1)
        b = i % 10
        if a != 0 and b != 0:
            d[(a, b)] += 1

    ans = 0
    for a, b in product(range(1, 10), repeat=2):
        ans += d[(a, b)] * d[(b, a)]
    return ans


def main():
    N = read_int()
    print(slv(N))


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

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


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N):
    ans = 0

    d = Counter()
    for i in range(N+1):
        l = len(str(i))
        a = i // 10 ** (l-1)
        b = i % 10
        if a != 0 and b != 0:
            d[(a, b)] += 1

    ans = 0
    for a, b in product(range(1, 10), repeat=2):
        ans += d[(a, b)] * d[(b, a)]
    return ans


def main():
    N = read_int()
    print(slv(N))


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
d=[[0]*10 for i in range(10)]
ans=0
for i in range(1,n+1):
    a=i%10
    if a!=0:
        t=i
        while t>9:
            t//=10
        b=t
        d[b][a]+=1
for i in range(10):
    for j in range(10):
        ans+=d[i][j]*d[j][i]
print(ans)

Solution (Python):
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

from collections import defaultdict

n = ni()

def f(x):
    a = x % 10
    b = 0
    while x:
        b = x
        x //= 10
    return (a, b)

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

for i in range(1,n+1):
    p = f(i)
    freq[p[0]][p[1]] += 1

for i in range(1,n+1):
    p = f(i)
    q = (p[1], p[0])
    ans += freq[q[0]][q[1]]

print(ans)

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

A = [list(0 for i in range(9)) for _ in range(9)]

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

cnt=0
for i in range(9):
    for j in range(9):
        cnt+=A[i][j]*A[j][i]

print(cnt)

Solution (Python):
n = int(input())
d = {}
for i in range(1, n+1):
    c = str(i)
    cr = c[0]
    cl = c[-1]
    key = cr + cl
    if key in d:
        d[key] += 1
    else:
        d[key] = 1
ans = 0
for key in d:
    key_r = key[-1] + key[0]
    if key_r in d:
        ans += d[key] * d[key_r]
print(ans)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

from collections import defaultdict


N = int(read().rstrip())

counter = defaultdict(int)  

for x in range(1, N+1):
    first = str(x)[0]
    last = str(x)[-1]
    counter[(first, last)] += 1

ans = 0
items = list(counter.items())  
for (first, last), cnt in items:
    ans += cnt * counter[(last, first)]

print(ans)

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

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

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

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

for i in range(N):
    si = str(i+1)
    f,l = int(si[0]),int(si[len(si)-1])
    C[f][l] += 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
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int):
    def combination(n, r, mod=10**9+7):
        n1, r = n+1, min(r, n-r)
        numer = denom = 1
        for i in range(1, r+1):
            numer = numer * (n1-i) % mod
            denom = denom * i % mod
        return numer * pow(denom, mod-2, mod) % mod
    
    
    from collections import defaultdict
    d = defaultdict(int)
    for i in range(1,N+1):
        s = str(i)
        d[int(s[0]+s[-1])] += 1

    ans = 0


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

    print(ans)

    return



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

import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int):
    def combination(n, r, mod=10**9+7):
        n1, r = n+1, min(r, n-r)
        numer = denom = 1
        for i in range(1, r+1):
            numer = numer * (n1-i) % mod
            denom = denom * i % mod
        return numer * pow(denom, mod-2, mod) % mod
    
    
    from collections import defaultdict
    d = defaultdict(int)
    for i in range(N+1):
        s = str(i)
        d[int(s[0]+s[-1])] += 1

    ans = 0


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

    print(ans-1)

    return



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())
d = 1
c = 0

for x in range(1, n + 1):
  l = x % 10
  
  if l == 0:
    continue
    
  r = x // d
  k = 10
    
  if l == r:
    c += 1
  
  while k <= n:
    y = l * k + r
    z = n - y
    
    if k <= z:
      c += k // 10
    elif 0 <= z:
      a  = z % k // 10 + 1
      c += a if a != 0 else 1
      
    k *= 10
  
  if       9 == x: d =     10
  elif    99 == x: d =    100
  elif   999 == x: d =   1000
  elif  9999 == x: d =  10000
  elif 99999 == x: d = 100000

print(c)

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

print(ans)

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