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

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

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

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

print(ans)

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

counts = {(i + 1, j + 1): [0, 0] for i in range(9) for j in range(i, 9)}
for i in range(n):
  num = str(i + 1)
  s = int(num[0])
  e = int(num[-1])
  if e == 0:
    continue
  if s <= e:
    counts[(s, e)][0] += 1
  else:
    counts[(e, s)][1] += 1

count = 0
for (a, b), (v1, v2) in counts.items():
  if a == b:
    count += v1 * v1
  else:
    count += v1 * v2 * 2

print(count)

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

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


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

print(ans)

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

count_dists = {}
for s in range(1,10):
    count_dists[s] = {}
    for t in range(1,10):
        count_dists[s][t] = 0

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

    if end_num == 0:
        continue

    count_dists[lead_num][end_num] = count_dists[lead_num][end_num] + 1

answer_count = 0

for u in range(1,10):
    for v in range(1,10):
        num1 = count_dists[u][v]
        num2 = count_dists[v][u]

        if u == v:
            answer_count += num1 ** 2
        else:
            answer_count += num1 * num2


print(answer_count)

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

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

a = [[0 for i in range(10)] for j in range(10)]
for i in range(1,n+1):
    s = str(i)
    a[int(s[0])][int(s[-1])] += 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):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n = int(input())
    p = [[0]*10 for _ in range(10)]
    res = 0
    for x in range(1, n+1):
        x = str(x)
        c, d = int(x[0]), int(x[-1])
        if c==0 or d==0:
            continue
        p[d][c] += 1
        
        
    for x in range(1, n+1):
        x = str(x)
        c, d = int(x[0]), int(x[-1])
        if c==0 or d==0:
            continue
        res += p[c][d]
        
        
    print(res)

    
    
    
    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

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

c = [[0 for i in range(10)] for j in range(10)]
for i in range(N+1):
    str_i = str(i)
    c[int(str_i[0])][int(str_i[-1])] += 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())

map = {i: {j: 0 for j in range(1, 10)} for i in range(1, 10)}

div = 1
for n in range(1, N + 1):
  h = n // div
  if h >= 10:
    div *= 10
    h = n // div
  t = n % 10
  if t == 0:
    continue
  map[h][t] += 1

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

print(total)

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

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

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

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

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

count = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, N + 1):
    s = str(i)
    prefix = int(s[0])
    surfix = int(s[-1])
    if surfix != 0:
        count[prefix - 1][surfix - 1] += 1

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

print(ans)

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

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

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

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

count = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, N + 1):
    s = str(i)
    prefix = int(s[0])
    surfix = int(s[-1])
    if surfix != 0:
        count[prefix - 1][surfix - 1] += 1

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

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

print(ans)

Solution (Python):
from collections import defaultdict


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


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


L = list(d.items())
 
ans = 0
 
for (start, end), i in L:
    ans += i * d[(end, start)]
 
print(ans)

Solution (Python):
from collections import defaultdict

d = defaultdict(int)

n = int(input())

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

L = list(d.items())

ans = 0

for (start, end), i in L:
    ans += i * d[(end, start)]

print(ans)

Solution (Python):
def total_pairs(N):
    hashmap = {}
    answer = 0
    for current in range(1, N+1):
        s = str(current)
        first, second = s[0], s[-1]
        total = first+second
        if not hashmap.get(total):
            hashmap[total] = 1
        else:
            c = hashmap[total]
            hashmap[total] = c + 1
            
    
    for k, v in hashmap.items():
        
        f_s, s_s = k[0], k[1]
        if f_s == s_s:
            answer += v*v 
        else:
            temp = s_s+f_s
            if hashmap.get(temp):
                value = hashmap[temp]
                answer +=(v*value)
        
    return answer

N = int(input())
print(total_pairs(N))

Solution (Python):
N=int(input())
cnt=0
for i in range(1,10):
    for k in range(1,10):
        a=0
        b=0
        for n in range(1,N+1):
            r=n%10
            l=n//(10**(len(str(n))-1))
            if k==r and i==l:
                a+=1
            if i==r and k==l:
                b+=1
        cnt+=a*b
print(cnt)

Solution (Python):

import sys


def solve(N: int):
    digits = [[0 for i in range(10)] for j in range(10)]
    d = 1
    for i in range(1, N+1):
        last = i % 10
        first = i//d
        if first >= 10:
            d *= 10
            first = i//d
        digits[last-1][first-1] += 1
    sum = 0
    for i in range(10):
        for j in range(10):
            sum += digits[i][j]*digits[j][i]
    print(sum)
    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):
dic={}
for i in range(1,10):
  for j in range(0,10):
    dic[(i,j)]=0

N=int(input())
for i in range(1,min([N+1,10])):
  dic[(i,i)]+=1
for i in range(10,min([N+1,100])):
  dic[(i//10,i%10)]+=1
for i in range(100,min([N+1,1000])):
  dic[(i//100,i%10)]+=1
for i in range(1000,min([N+1,10000])):
  dic[(i//1000,i%10)]+=1
for i in range(10000,min([N+1,100000])):
  dic[(i//10000,i%10)]+=1
for i in range(100000,N+1):
  dic[(i//100000,i%10)]+=1
  
ans=0
for i in range(1,10):
  for j in range(1,10):
    x=dic[(i,j)]
    y=dic[(j,i)]
    ans+=x*y
print(ans)

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

for k in range(N+1):
    sk = str(k)
    C[int(sk[0])][int(sk[-1])] += 1

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

print(ans)

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

for k in range(N+1):
    sk = str(k)
    C[int(sk[0])][int(sk[-1])] += 1

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

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
ns=str(n)
l=len(ns)
ans=0
for i in range(1,n+1):
    s=str(i)
    x=s[0]
    y=s[-1]
    if y=='0':
        continue
    if x==y:
        ans+=1
    if l==1:
        continue
    for j in range(l-2):
        ans+=10**j
    if int(ns[0])>int(y):
        ans+=10**(l-2)
    elif int(ns[0])<int(y):
        pass
    else:
        z=n-int(ns[0])*10**(l-1)
        if z>=int(x):
            w=z-int(x)
            ans+=w//10+1
print(ans)

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











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

Solution (Python):
def check(a, b):
    k = 1
    res = 0
    if a == b and (a <= N): res += 1
    while True:
        if (a * (10 ** k) + b) > N:
            break
        else:
            if len(str(N)) > (k+1) or str(N)[0] > str(a):
                res += 10 ** (k - 1)
                k += 1
                continue

            else :
                Q = N - (a * (10 ** k) + b)
                p = 1
                if len(str(Q)) > 1 :
                    p += (int(str(Q)[:-1]))
                res += p
                k += 1
    return res


def main():
    global N
    N = int(input())
    ans = 0
    for i in range(1, 10, 1):
        for j in range(1, 10, 1):
            a = check(i, j)
            b = check(j, i)
            ans += a * b
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
cnt = 0
l = len(str(n))
heads_tails = [[0 for i in range(9)] for j in range(9)]

for i in range(1, n+1):
    if i % 10 == 0:
        continue
    head = int(str(i)[0]) - 1
    tail = int(str(i)[-1]) - 1
    heads_tails[head][tail] += 1

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

print(cnt)

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



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

for i in range(1,n+1):
    i_str = str(i)
    mae = int(i_str[0])
    usi = int(i_str[-1])
    count[mae][usi] += 1

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

print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())

def pair(n):
    l = n % 10
    while n != 0:
        f = n
        n = n // 10
    return (f, l)

d = defaultdict(int)

for i in range(1, N + 1):
    d[pair(i)] += 1

ans = 0
for key, value in d.items():
    tmp1 = value
    l, f = key
    if (f, l) in d.keys():
        tmp2 = d[(f, l)]   
        ans += tmp1 * tmp2
print(ans)

Solution (Python):
N = int(input())
c = 0
cnt = [ [0]*10 for _ in range(10) ]
for i in range(1,N+1):
    a = str(i)
    top = int(a[0])
    end = int(a[-1])
    cnt[top][end] += 1

for i in range(10):
    for j in range(10):
        for k in range(10):
            for l in range(10):
                if i == l and j == k:
                    c += cnt[i][j] * cnt[k][l]
print(c)

Solution (Python):


from collections import defaultdict

def main():
    n = int(input())
    cnt = defaultdict(int)
    for i in range(1, n + 1):
        si = str(i)
        x = int(si[0])
        y = int(si[-1])
        cnt[(x, y)] += 1
    res = 0
    for x in range(1, 10):
        for y in range(1, 10):
            num_a = cnt[(x, y)]
            num_b = cnt[(y, x)]
            res += num_a * num_b
    print(res)

if __name__ == "__main__":
    main()

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

from collections import defaultdict
counter = defaultdict(int)

for k in range(1,N+1):
    temp = list(str(k))
    counter[(int(temp[0]),int(temp[-1]))] += 1

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

Solution (Python):
N=int(input())
def answer(i,j):
    i=str(i)
    j=str(j)
    kotae=0
    for k in range(1,N+1):
        if str(k)[0]==i and str(k)[-1]==j:
            kotae+=1
    return kotae
answers=0
for i in range(1,10):
    for j in range(1,10):
        answers+=answer(i,j)*answer(j,i)
print(answers)

Solution (Python):
N=int(input())
dp=[0]*100
for i in range(N):
    s=str(i+1)
    dp[int(s[0])*10+int(s[-1])]+=1
ans=sum(dp[i*10+j]*dp[j*10+i] for i in range(1,10) for j in range(1,10))
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())

s = [[0 for i in range(10)] for i in range(10)]


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

print(ans)

Solution (Python):
N = input()
l = len(N)
ans = 0
C = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1,int(N)+1):
    s = str(i)
    top = int(s[0])
    bot = int(s[-1])
    C[top][bot] += 1
for i in range(10):
    for j in range(10):
        ans += C[i][j]*C[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)
  f=int(S[0])
  b=int(S[-1])
  M[f][b]+=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 ())
cnt = [[0 for i in range (10)] for i in range (10)]
for i in range (1, n + 1) :
    p = int (str (i)[0])
    q = i % 10
    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())

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

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

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

print(ans)

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

c = Counter()
d = defaultdict(bool)

for i in range(1,N+1):
    s = str(i)
    c[(s[0],s[-1])] += 1
    d[(s[0],s[-1])] = True
    
k = list(c.keys())

ans = 0
for i,j in k:
    if d[(j,i)]:
        ans += (c[(i,j)]*c[(j,i)])
        d[(j,i)] = False
print(ans)

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

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

for i in range(1,n+1):
    sentou = int(str(i)[0])
    matsubi = int(str(i)[-1])
    t[sentou][matsubi] += 1
    
result = 0
for i in range(10):
    for j in range(10):
        result += t[i][j] * t[j][i]
        
print(result)

Solution (Python):
import math
import time


t = time.time()
def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

def fkc(keta):
    return 10**keta

def pat_b(a, n):
    tail = a % 10

    if tail == 0:
        return 0

    head = int(str(a)[0])

    ret = 0
    if tail == head: 
        ret += 1

    if n >= tail*10+head: 
        ret += 1

    for free_keta in range(1, 6): 
        mincase = 10**(free_keta+1) * tail + head

        if n < mincase:
            break
        if n >= 10**(free_keta+1) + mincase + head - 10:
            ret += fkc(free_keta)
            continue
        else:
            ret += int(int(n-mincase)/10) + 1
    return ret

def main():
    n = iip()
    result = 0
    for i in range(n+1):
        result += pat_b(i, n)
    print(result)


main()

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

C = [[0]*10 for _ in range(10)]
for n in range(1, N+1):
    s = str(n)
    C[int(s[0])][int(s[-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())
a = [[0]*9 for x in range(9)]
for i in range(1,n+1):
    s = str(i)
    if s[-1]=="0":
        continue
    a[int(s[0])-1][int(s[-1])-1] += 1
ans = 0
for i in range(9):
    for j in range(9):
        ans += a[i][j]*a[j][i]

print(ans)

Solution (Python):
def main():
    N = int(input())
    cnt = [[0] * 10 for _ in range(10)]
    for i in range(1,N+1):
        tmp = str(i)
        top = int(tmp[0])
        end = int(tmp[-1])
        cnt[top][end] += 1
    ans = 0
    for a in range(10):
        for b in range(10):
            for c in range(10):
                for d in range(10):
                    if a == d and b == c:
                        ans += cnt[a][b] * cnt[c][d]

    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):

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

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

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

count = 0
for k in range(10):
    for i in range(10):
        count += num[k][i] * num[i][k]

print(count)

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

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

for i in range(1, n + 1):
    mln = i % 10   
    mrn = int(str(i)[0])  
    a[mln][mrn] += 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):
n = int(input())

ans = 0
for i in range(1,10) :
  for j in range(i,10) :
    c_a = 0
    c_b = 0
    for k in range(1,n+1) :
      kl = len(str(k))
      if k//(10**(kl-1)) == i and k%10 == j :
        c_a += 1
      if k//(10**(kl-1)) == j and k%10 == i :
        c_b += 1
    if i == j :
      ans += c_a*c_b
    else :
      ans += c_a*c_b*2
      
print(ans)

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

for i in range (1, n + 1) :
    q = i % 10
    p = int (str (i)[0])
    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 ())
cnt = [[0 for i in range (10)] for i in range (10)]

for i in range (1, n + 1) :
    q = i % 10
    p = int (str (i)[0])
    cnt[p][q] += 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 head(x):
    r, q = divmod(x, 10)
    if r == 0:
        return q
    return head(r)
    

def tail(x):
    return x % 10


def submit():
    n = int(input())
    head_tail_count = {(i, j):0 for i in range(10) for j in range(10)}

    for x in range(n + 1):
        h = head(x)
        t = tail(x)
        head_tail_count[(h, t)] += 1

    ans = 0
    for x in range(1, 10):
        for y in range(1, 10):
            ans += head_tail_count[(x, y)] * head_tail_count[(y, x)]
    print(ans)


if __name__ == "__main__":
    submit()

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

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

def fl(x):
    f = 0
    l = x % 10
    while x != 0:
        f = x
        x = x // 10
    return (f, l)

d = defaultdict(int)

for i in range(1, N+1):
    d[fl(i)] += 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())

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

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

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

print(count)

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

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

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

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

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

print(ans)

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


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


for k in range(1, n + 1):
    arr = list(map(int,list(str(k))))
    c[arr[0]][arr[-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):
ls = [[0 for _ in range(10)] for _ in range(10)]
N = int(input())
for i in range(1,N+1):
    s = str(i)
    ls[int(s[-1])][int(s[0])] += 1
ans = 0
for i in range(10):
    for j in range(10):
        ans += ls[i][j]*ls[j][i]
print(ans)

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

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


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


ans=0

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

Solution (Python):
import sys

input = sys.stdin.buffer.readline







def main():
	li=[[0]*10 for i in range(10)]
	n=int(input())
	for i in range(1,n+1):
		i=str(i)
		li[int(i[0])][int(i[-1])]+=1

	ans=0

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

	print(ans)
















if __name__ == "__main__":
	main()

Solution (Python):

n = int(input())
length = len(str(n))
c = [[0] * 10 for _ in range(10)]
for k in range(1, n + 1):
    s = str(k)
    first = int(s[0])
    last = int(s[-1])
    c[first][last] += 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
from collections import defaultdict
read = sys.stdin.buffer.read


N = int(read())


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

answer = 0


items = list(counter.items())


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

print(answer)

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

L = [[0]*10 for j in range(10)]

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

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

Solution (Python):



n=int(input())
freq=dict()
ans=0

for i in range(1,n+1):
    s=str(i)
    p=(s[0],s[-1])
    freq[p]=freq.get(p,0)+1

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

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

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

if __name__ == '__main__':
    main()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n = INT()
l = len(str(n))
arr = []
for i in range(1, 10):
    for j in range(1, 10):
        arr.append([i, j])
arr3 = [0 for _ in range(len(arr))]
for i in range(1, n + 1):
    head = int(str(i)[0])
    foot = int(str(i)[-1])
    if head != 0 and foot != 0:
        idx = arr.index([head, foot])
        arr3[idx] += 1
ans = 0
for i in range(len(arr3)):
    idx = arr.index(list(reversed(arr[i])))
    ans += arr3[i] * arr3[idx]
print(ans)

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

for i in range(1, N + 1):
    s = str(i)
    row = int(s[0])
    column = int(s[-1])
    if row == 0 or column == 0:
        continue
    matrix[row - 1][column - 1] += 1

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

Solution (Python):
import sys

def solve():
    N = int(input())
    D = dict()
    for i in range(1, N + 1):
        front = i % 10
        back = i
        while back >= 10: back //= 10
        if front > 0:
            if (front, back) in D: D[(front, back)] += 1
            else: D[(front, back)] = 1
    Ans = 0
    for i in range(1, N + 1):
        back = i % 10
        if back > 0:
            front = i
            while front >= 10: front //= 10
            if (front, back) in D: Ans += D[(front, back)]
    print(Ans)

    return 0

if __name__ == "__main__":
    solve()

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

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

import sys
from itertools import product

def solve(N: int):
    def f(n):
        h = n
        while h // 10 > 0:
            h //= 10
        return h, n%10
    table = [[0] * 10 for _ in range(10)]
    for h, t in map(f, range(1, N+1)):
        table[h][t] += 1
    return sum(table[i][j] * table[j][i] for i, j in product(range(1, 10), repeat=2))


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

if __name__ == '__main__':
    main()

Solution (Python):
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 

enu = enumerate
def input():
    return sys.stdin.readline()[:-1]
def pri(x):
    if type(x) is list:
        print('\n'.join(map(str, x)))
    else:
        print(x)

sys.setrecursionlimit(10**7)
N = int(input())

nums = defaultdict(int)

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

cnt = 0
for st in range(1, 10):
    for en in range(1, 10):
        cnt += nums[(st, en)] * nums[(en, st)]

pri(cnt)

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

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

def start(n):
    return int(str(n)[0])
def end(n):
    return int(str(n)[-1])

for i in range(1,N+1):
    qq[start(i)][end(i)] += 1

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

print(ans)

Solution (Python):












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


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


for i in range(n+1):
    if i % 10 == 0:
        continue

    head = int(str(i)[0])
    tail = int(str(i)[-1])
    count += a[tail][head]

print(count)

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

n=ni()
d= [10*[0]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):
from collections import defaultdict
def main():
    def pair(n):
        a = n % 10
        b = str(n)[0]
        return '{},{}'.format(a, b)

    N = int(input())
    freq = defaultdict(int)
    for i in range(1, N+1):
        p = pair(i)
        freq[p] += 1
    
    res = 0
    for i in range(1, N+1):
        p = pair(i)
        q = ','.join(p.split(',')[::-1])
        res += freq[q]
    print(res)

main()

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