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


from collections import Counter

M1 = []
M2 = []
for i in range(1, N+1):
    m = str(i)
    m1 = m[0] + m[-1]
    m1 = int(m1)
    m2 = m[-1] + m[0]
    m2 = int(m2)
    M1.append(m1)
    M2.append(m2)

M1 = Counter(M1)
M2 = Counter(M2)
M3 = M1 & M2
answer = 0
for m in M3.keys():
    answer += M1[m]*M2[m]

print(answer)

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

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

print(ans)

Solution (Python):
n=int(input())
v=[0]*100
ans=0
for i in range(1,n+1):
    x=int(str(i)[0])
    y=int(str(i)[-1])
    v[x*10+y]+=1

for i in range(len(v)):
    x=int(str(i)[0])
    y=int(str(i)[-1])
    ans+=v[i]*v[10*y+x]
print(ans)

Solution (Python):
n=int(input())
a=[[0]*9 for _ in range(9)]
for i in range(1,n+1):
  s=str(i)
  l=int(s[0])
  r=int(s[len(s)-1])
  if l*r!=0:
    a[l-1][r-1]+=1
x=0
for i in range(9):
  for j in range(9):
    x+=a[i][j]*a[j][i]
print(x)

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

dic = {}
for i in range(1, n+1):
    s = str(i)
    i0 = int(s[0])
    i1 = int(s[-1])
    k = (i0, i1)
    if k in dic:
        dic[k] += 1
    else:
        dic[k] = 1

m = 0
for k, n in dic.items():
    i0, i1 = k
    kk = (i1, i0)
    if kk in dic:
        m += n * dic[kk]

print(m)

Solution (Python):
N = int(input())
memo = {}

def func(i, j):
    if (i, j) in memo:
        return memo[(i, j)]

    count = 0
    n = 1
    while n <= N:
        str_n = str(n)

        if n >= 10 and str_n[0] != str(j):
            n += 10 ** (len(str_n) - 1)
            continue

        if str_n[-1] == str(i) and str_n[0] == str(j):
            count += 1
            n += 10
            continue

        n += 1
    
    memo[(i,j)] = count
    return count

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

print(ret)

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

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

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

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

for i in range(1, n + 1):
    sentou = int(str(i)[0])
    matsubi = int(str(i)[-1])
    table[sentou][matsubi] += 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())
import numpy as np
l=np.zeros(81).reshape(9,9)
for i in range(1,1+n):
    first=int(str(i)[0])-1
    last=int(str(i)[-1])-1
    if last>=0:
        l[first][last]+=1
ans=0
for i in range(9):
    for j in range(i,9):
        ans+=l[i][j]*l[j][i]
        if i!=j:
            ans+=l[i][j]*l[j][i]
print(int(ans))

Solution (Python):


n=int(input())
dp=[[0]*10 for x in [0]*10]
ans=0
for i in range(1,n+1):
    start,end=int(str(i)[0]),int(str(i)[-1])
    dp[start][end]+=1
for i in range(10):
    for j in range(10):
        ans+=dp[i][j]*dp[j][i]
        
            
print(ans)

Solution (Python):

import numpy as np

def pair(N):
    b=int(N)%10
    a=int(N)
    k=a//10
    while k!=0:
        a=k%10
        k=k//10
    return a,b
N=int(input())
P=np.zeros((10,10),dtype=np.int32)
ans=0
for i in range(1,N+1,1):
    a,b=pair(i)
    P[a][b]+=1
for i in range(1,10,1):
    for j in range(1,10,1):
        ans+=P[i][j]*P[j][i]
print(ans)

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

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

num = [[0 for j in range(10)]for i in range(10)]
for i in range(1,N+1):
    NN = i
    kouho = []
    while NN>=1:
        kouho.append(int(NN%10))
        NN = NN/10
    if len(kouho) == 0:
        continue
    
    low_bit = int(kouho[0])
    high_bit = int(kouho[-1])
    
    if (not low_bit or not high_bit):
        continue
    num[low_bit][high_bit] += 1
ans = 0


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

Solution (Python):
n=int(input())
dp=[[0 for i in range(10)] for i in range(10)]
def iandj(n):
  j=n%10
  while n>=10:
    n//=10
  i=n
  return i,j
for k in range(1,n+1):
  i,j=iandj(k)
  dp[i][j]+=1
ans=0
for i in range(1,10):
  for j in range(1,10):
  	ans += dp[i][j]*dp[j][i]
print(ans)

Solution (Python):
N = int(input())
count = 0
baf = 0

if N <= 10:
    for i in range(1, N+1):
        for j in range(1, N+1):
            stri = str(i)
            strj = str(j)
            if stri[0] == strj[-1] and strj[0] == stri[-1]:
                
                count += 1
    print(count)
else:
    num = (N // 10) ** 2
    if N % 10 == 0:

        print(num + 8)
    else:
        
        
        for i in range(N // 10 * 10 ,N+1):
            for j in range(1, N+1):
                stri = str(i)
                strj = str(j)
                if stri[0] == strj[-1] and strj[0] == stri[-1]:
                    
                    count += 1
                    
        count *= 2
        for i in range(N // 10 * 10 + 1, N+1):
            for j in range(N // 10 * 10 + 1, N+1):
                stri = str(i)
                strj = str(j)
                if stri[0] == strj[-1] and strj[0] == stri[-1]:
                    
                    count -= 1
                    
        print(num+count + 8)

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
 
cnt = 0
for _ in range(1,N+1):
  a = int(str(_)[0])
  b = int(str(_)[-1])
  cnt += A[b][a]
  
print(cnt)

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

memory = dict()
result = 0

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

for i in range(1, N + 1):
    if i >= 100000:
        memory[(i//100000, i%10,)] += 1
    elif i >= 10000:
        memory[(i//10000, i%10,)] += 1
    elif i >= 1000:
        memory[(i//1000, i%10,)] += 1
    elif i >= 100:
        memory[(i//100, i%10,)] += 1
    elif i >= 10:
        memory[(i//10, i%10,)] += 1
    else:
        memory[(i, i,)] += 1

for i in range(10):
    for j in range(10):
        result += memory[(i,j,)] * memory[(j,i,)]

print(result)

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

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

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


print(count)

Solution (Python):

n = int(input())

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

for i in range(n+1):
    if i % 10 == 0:
        continue
    s = str(i)
    match[int(s[0])][int(s[len(s)-1])] += 1

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

print(ans)

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

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

Solution (Python):

ab = []
def solve0(n):
    ok = 0
    for a in range(1,n+1):
        for b in range(1,n+1):
            if str(a)[0] == str(b)[-1] and str(a)[-1] == str(b)[0]:
                ok += 1
                ab.append([a,b])
    return ok

def solve2(n,ab):
    ok = 0
    ok_a = 0
    ok_b = 0
    for a,b in ab:
        ok_a = count(n,a)
        ok_b = count(n,b)
        ok = ok + ok_a * ok_b
    return ok

def count(n,z):
    strN = str(n)
    strZ = str(z)
    if z > n:       
        return 0
    if len(strN) == 1: 
        return 1
    if len(strZ) == 1: 
        return 1

    
    ok = 0
    mlen = 0
    while mlen+2 < len(strN): 
        ok = ok + 10**mlen
        mlen = mlen + 1
    
    if int(strZ[0]+"9"*mlen+strZ[-1]) <= n:
        ok = ok + 10**mlen
    elif int(strZ[0]+strN[1:len(strN)-1]+strZ[-1]) <= n:
        ok = ok + int(strN[1:len(strN)-1])+1
    else:
        nMid = str(strN[1:len(strN)-1])
        if int(nMid) > 0:
            nMid = str(int(nMid)-1).zfill(len(strN)-2)
            if int(strZ[0]+nMid+strZ[-1]) <= n:
                ok = ok + (int(nMid))+1
    return ok


solve0(99)


n = int(input())
print(solve2(n,ab))

Solution (Python):

ab = []
nTop = 0
nBtm = 0
nMid = 0
mid = []

def solve0(n):
    ok = 0
    for a in range(1,n+1):
        for b in range(1,n+1):
            if str(a)[0] == str(b)[-1] and str(a)[-1] == str(b)[0]:
                ok += 1
                ab.append([a,b])
    return ok

def solve1(n):
    ok = 0
    for a in range(1,n+1):
        for b in range(1,n+1):
            if str(a)[0] == str(b)[-1] and str(a)[-1] == str(b)[0]:
                ok += 1
    return ok

def solve2(n,ab):
    ok = 0
    ok_a = 0
    ok_b = 0
    for a,b in ab:
        ok_a = count(n,a)
        ok_b = count(n,b)
        ok = ok + ok_a * ok_b
    return ok

def count(n,z):
    strN = str(n)
    strZ = str(z)
    if z > n:       
        return 0
    if len(strN) == 1: 
        return 1
    if len(strZ) == 1: 
        return 1

    
    ok = 0
    mlen = 0
    while mlen+2 < len(strN): 
        ok = ok + 10**mlen
        mlen = mlen + 1
    
    if int(strZ[0]+"9"*mlen+strZ[-1]) <= n:
        ok = ok + 10**mlen

    elif int(strZ[0]+strN[1:len(strN)-1]+strZ[-1]) <= n:
        ok = ok + int(strN[1:len(strN)-1])+1

    else:
        nMid = str(strN[1:len(strN)-1])
        if int(nMid) > 0:
            nMid = str(int(nMid)-1).zfill(len(strN)-2)
            if int(strZ[0]+nMid+strZ[-1]) <= n:
                ok = ok + (int(nMid))+1

    return ok


solve0(99)


n = int(input())
print(solve2(n,ab))

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

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

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

base_N = 0
if(N > 10):
	base_N  = int(str(N)[0])*(10**(len(str(N))-1))+int(str(N)[-1])

dig_N = len(str(N))

ans = 0
for x in range(1,10):
	for y in range(1,10):
		for dig_A in range(dig_N):
			for dig_B in range(dig_N):
				check = True
				ways_A, ways_B = 0, 0
				deduct = 0
				if(dig_A == 0 and x<=N):
					if(x==y): 
						ways_A = 1
					else: check = False
				elif(dig_A == 1 and x*10+y<=N):
					ways_A = 1
				elif((x+1)*10**dig_A <= N):
					ways_A = 10**(dig_A-1)
				elif(x*(10**dig_A) <= N):
					if(x*(10**dig_A)+y <= N):
						if(y > N%10): ways_A = (N - base_N)//10
						else : ways_A = (N - base_N)//10 + 1
					else : check = False
				else:
					ways_A = 0
					check = False

				if(dig_B == 0 and y<=N):
					if(x==y): ways_B = 1
					else: check = False
				elif(dig_B == 1 and y*10+x<=N):
					ways_B = 1
					if(dig_A!=dig_B) : ways_B
				elif((y+1)*10**dig_B <= N):
					ways_B = 10**(dig_B-1)
				elif(y*(10**dig_B) <= N):
					if(y*(10**dig_B)+x <= N):
						if(x > N%10): ways_B = (N - base_N)//10
						else: ways_B = (N - base_N)//10 + 1
					else : check = False
				else:
					ways_B = 0
					check = False
				tmp = ans
				ans += ways_B * ways_A - deduct

print(ans)

Solution (Python):
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
import itertools
sys.setrecursionlimit(200000)
input = sys.stdin.readline









N, = map(int, input().split())

m = {
    0: "",
    1: "0",
    2: "00",
    3: "000",
    4: "0000",
}
m2 = {
    0: "",
    1: "9",
    2: "99",
    3: "999",
    4: "9999",
}
def f(t):
    t = str(t)
    c = 0
    if len(t) > 1:
        if t[0] == t[-1]:
            c += 1

    if len(t) == 1:
        t = t * 2
        c += 1

    for i in range(0, 5):
        p = t[-1] + m[i] + t[0]
        p2 = t[-1] + m2[i] + t[0]

        if str(int(p)) == p and int(p) <= N:
            c += 1
            if len(p) > 2:
                n = 10 ** (len(p) - 2) - 1
                if N < int(p2):
                    n = (N - int(p)) // 10
                c += n

    return c

ans = 0
for i in range(1, N+1):
    ans += f(i)

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()

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

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

    l[int(a)][int(b)]+=1

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

  return ans


print(main())

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

array = [0] * 100

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


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

print(ans)

Solution (Python):
import numpy as np
cnt=0
N=int(input())
S=np.zeros((10,10),dtype=int)

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

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

Solution (Python):

import sys
import collections

def solve(N: int):
    c = collections.defaultdict(int)
    if N == 1:
        print(1)
        return
    for i in range(1, N+1):
        target = str(i)
        c[target[0], target[-1]] += 1
    result = 0
    for i in range(0, 10):
        for j in range(0, 10):
            i = str(i)
            j = str(j)
            result += c[i, j] * c[j, i]
    print(result)
    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())

c = {}

for n in range(1, N+1):
    n_list = list(str(n))
    first = n_list[0]
    end = n_list[-1]
    if first + end in c:
        c[first + end] += 1
    else:
        c[first + end] = 1

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

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok


def main():
    N=ii()
    d = defaultdict(int)
    for i in map(str, range(1, N+1)):
        i = str(i)
        a, b = i[0], i[-1]
        d[(a, b)] += 1
    print(sum(d[(a, b)] * d[(b, a)] for a in map(str, range(1,9+1)) for b in map(str, range(1,9+1))))

main()

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok


def main():
    N=ii()
    d = defaultdict(int)
    for i in range(1, N+1):
        i = str(i)
        a, b = i[0], i[-1]
        if a != 0 != b:
            d[(a, b)] += 1
    ans = 0
    for i in range(1, N+1):
        i = str(i)
        a, b = i[0], i[-1]
        ans += d[(b, a)]
    print(ans)


main()

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):
import numpy as np
N = int(input())
a=0
list1 = np.array([0] * N)
list2 = np.array([0] * N)
ans = np.array([[0] * 10 for i in range(10)])
for i in range(N):
    s=str(i+1)
    list1[i]=int(s[-len(s)])
    list2[i]=(i+1)%10

for i in range(N):
    ans[list1[i]][list2[i]]+=1

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

print(a)

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

Solution (Python):



def main():
    n = int(input())
    d = dict()
    pairs = list()

    
    
    
    
    for i in range(1, n + 1):
        s = str(i)
        first = s[0]
        second = s[-1]
        pairs.append((first, second))

        if (first, second) not in d.keys():
            d[(first, second)] = 1
        else:
            d[(first, second)] += 1

    ans = 0

    for first, second in pairs:
        if (second, first) in d.keys():
            ans += d[(second, first)]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
D = [[0 for x in range(10)] for y in range(10)]

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

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

Solution (Python):
l=[0]*100;r=range(10)
for i in range(int(input())):
    s=str(i+1);l[int(s[0]+s[-1])]+=1
print(sum([l[i*10+j]*l[j*10+i]for i in r for j in r]))

Solution (Python):
l=[0 for _ in range(100)];r=range(10)
for i in range(1,int(input())+1):
    s=str(i);l[int(s[0]+s[-1])]+=1
print(sum([l[i*10+j]*l[j*10+i]for i in r for j in r]))

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

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

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

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

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

print(ans)

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

    table = [0] * 100
    for i in range(n+1):
        i_str = str(i)
        table[int(i_str[0]) * 10 + int(i_str[-1])] += 1

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

    print(num)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
sample = [0] * 100

for i in range(1,N+1):

    num = str(i)
    first = num[0]
    final = num[-1]
    
    sample[int(first+final)] += 1

ans = 0

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

print(ans)

Solution (Python):
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys

sys.setrecursionlimit(10**9)
mod = 998244353
inf = 10**20

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()



n = I()
ans=0
c = [[0]*10 for i in range(10)]

for i in range(1,n+1):
    s = str(i)
    if s[0]=="0" or s[-1]=="0":
        continue
    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())
l = len(str(n))
ns = n//(10**(l-1))
nt = n-ns*10**(l-1)
ans = 0
if l>1:
  ans += (ns-1)*(ns-1)*10**(2*l-4)
  for i in range(2, l):
    ans += 2*(ns-1)*9*10**(l+i-4)
  ans += 2*(ns-1)*10**(l-2)
  if ns<=nt:
    ans += ((nt-ns)//10+1)*((nt-ns)//10+1)
    for i in range(1, ns):
      ans += 2*((nt-i)//10+1)*10**(l-2)
    for i in range(2, l):
      for j in range(1, min(nt+1, 10)):
        ans += 2*((nt-j)//10+1)*10**(i-2)
    ans += 2*((nt-ns)//10+1)
  else:
    ans += nt
    for i in range(2, l):
      ans += 2*nt*10**(i-2)
  for i in range(2, l):
    for j in range(2, l):
      ans += 9*9*10**(i-2+j-2)
    ans += 2*9*10**(i-2)
  ans += 9
else:
  ans += n
print(ans)

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

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


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



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

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())
k = [[0] * 10 for _ in range(10)]

for n in range(1, N+1):
  sn = str(n)
  k[int(sn[0])][int(sn[-1])] += 1

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

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

for i in range(1, n+1):
  start, end = str(i)[0], str(i)[-1]
  d[(start, end)] = d.get((start, end), 0) + 1
count = 0
for key, n_key in d.items():
  count += n_key * d.get(tuple(reversed(key)), 0)
print(count)

Solution (Python):

n = int(input())

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

for _ in range(1, n+1):

    s=str(_)
    i=int(s[0])
    j=int(s[-1])

    number[i][j] += 1

count = 0
for i in range(10):

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

print(count)

Solution (Python):
from collections import defaultdict

N = int(input())

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

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

for i in range(1,n+1):
    s = str(i)
    j = int(s[0])
    k = int(s[-1])
    Number[j][k] += 1
count = 0
for i in range(10):
    for j in range(10):
        count += Number[i][j] *Number[j][i]
print(count)

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

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

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

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

print(sum)

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

Solution (Python):


def main():
    from collections import defaultdict

    n = int(input())

    d = defaultdict(int)
    for x in range(1, n + 1):
        if x % 10 == 0:
            continue

        tail = x % 10
        head = 0
        while x:
            head = x
            x //= 10

        d[head, tail] += 1

    ret = 0
    for (head, tail), cnt in tuple(d.items()):
        
        
        
        ret += d[head, tail] * d[tail, head]
        
        

    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
strN = str(N)
order = len(strN)
Ns = int(strN[0])
Ne = int(strN[-1])

ces = [[0  for i in range(10) ] for j in range(10)]
cse = [[0  for i in range(10) ] for j in range(10)]
for n in range(1, N + 1):
  n_last = n % 10
  n_first = n
  while n_first >= 10:
    n_first = n_first // 10
  ces[n_last][n_first] += 1
  cse[n_first][n_last] += 1

count = 0
for e in range(1, 10):
  for s in range(1, 10):
    count += ces[e][s] * cse[e][s]
    
print(count)

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

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

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

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

from collections import OrderedDict
pairs = OrderedDict()

for k in range(1, N+1):
    first = int(str(k)[0])
    last = int(str(k)[-1])
    if (first, last) in pairs:
        pairs[(first, last)] += 1
    else:
        pairs[(first, last)] = 1

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

print(cnt)

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

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

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

Solution (Python):
from collections import defaultdict
def getLeft(num):
    while num >= 10:
        num = int(num / 10)
    return num

def getRight(num):
    return num % 10

n = int(input())
a = defaultdict(int)
for number in range(1,n+1):
    left = getLeft(number)
    right = getRight(number)
    a[(left,right)] += 1


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

Solution (Python):
n = int(input())
p = [[0]*10 for _ in range(10)]
x, y = int(str(n)[0]), int(str(n)[-1])
keta = len(str(n))
ans = 0

for a in range(1, 10):
    for b in range(1, 10):
        for k in range(1, keta+1):
            if k == 1:
                if a == b and a <= n:
                    p[a][b] += 1
            elif k == 2:
                if int(str(a)+str(b)) <= n:
                    p[a][b] += 1
            elif k >= 3 and k < keta:
                p[a][b] += 10 ** (k-2)
            else:
                if a < x:
                    p[a][b] += 10 ** (k-2)
                elif a == x and b <= y:
                    p[a][b] += int(str(n)[1:-1]) + 1
                elif a == x and b > y:
                    p[a][b] += int(str(n)[1:-1])
                    
for a in range(1, 10):
    for b in range(1, 10):
        ans += p[a][b] * p[b][a]
print(ans)

Solution (Python):

si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
snm = lambda: map(str, input().split())
nl = lambda: list(map(int, input().split()))
n=ni()
c=[[0]*10 for _ in range(10)]

for i in range(1,n+1):
    sl=str(i)
    sl_s=int(sl[0])
    sl_e=int(sl[-1])
    c[sl_s][sl_e]+=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 math,sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
lili=lambda:list(map(int,sys.stdin.readlines()))
li = lambda:list(map(int,input().split()))

I=lambda:int(input())
S=lambda:input().strip()
mod = 1000000007

a=[]
n=I()
for i in range(1,10):
    k=[]
    for j in range(1,10):
        k.append(0)
    a.append(k)
        

for i in range(1,n+1):
    if(i%10==0):
        continue
    k=str(i)
    p=int(k[0])-1
    q=int(k[-1])-1
    a[p][q]+=1
ans=0

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

Solution (Python):
import sys

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

    solve(N)

def solve(N: int):
    counts = [[0] * 10 for _ in range(10)]
    for i in range(10, N + 1):
        b = i % 10 
        while i >= 10:
            i //= 10 
        counts[i][b] += 1
    ret = 0
    for i in range(1, min(N + 1, 10)):
        ret += 1
        if counts[i][i] > 0:
            ret += counts[i][i] * 2
    for i in range(1, 10):
        for j in range(1, 10):
            ret += counts[i][j] * counts[j][i]
    print(ret)
    return


 
if __name__ == "__main__":
    preprocessing()

Solution (Python):
N = int(input())
Len = len(str(N))


C = [] 

for i in range(0,10):
    C.append([0,0,0,0,0,0,0,0,0,0])

for A in range(0,N):
    ac = str(A+1)
    ha = int(ac[0])
    ta = int(ac[-1])
    C[ha][ta] += 1

ans = 0
for head in range(0,10):
    for tail in range(0,10):
        ans += C[head][tail] * C[tail][head]

print(ans)