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
n = input()
first_n = n[0]
finish_n = n[-1]
digit = len(n)
cnt = defaultdict(lambda :0)

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

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()

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

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

table = [[0]*10 for _ in range(10)]
for i in range(1, n+1):
    a = i
    a_head = int(str(a)[0])
    a_tail = a % 10
    table[a_head][a_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):


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):
'''
ika tako
事前計算で1以上N以下の数字を走査し、
「先頭桁がiで末尾桁がjの数字の個数Ci,j 」をあらかじめ求めておく。
再度、1以上N以下の数字を走査し、
その数字が先頭桁がjで末尾桁がiなら答にCi,jを加算すればよい。
'''
def solve(n):
    tbl = [0] * 100
    for i in range(1, n + 1):
        st = str(i)
        tbl[int(st[0] + st[-1])] += 1
 
    ans = 0
    for i in range(1, n + 1):
        st = str(i)
        ans += tbl[int(st[-1] + st[0])]
    return ans
 
 
n = int(input())
print(solve(n))

Solution (Python):
N = int(input())
count = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, N + 1):
    st, ed = int(str(i)[0]), int(str(i)[-1])
    count[st][ed] += 1

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

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

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

base = 1
for i in range(1, n + 1):
    if i >= base * 10:
        base *= 10
    count[i // base][i % 10] += 1

total = 0
base = 1
for i in range(1, n + 1):
    if i >= base * 10:
        base *= 10
    total += count[i % 10][i // base]

print(total)

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

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

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

Solution (Python):
def judge(n):
    global count,num_list
    if n[-1]==0:
        judge(int(n)-1)
    else:
        for i in range(1,int(n)+1):
            i=str(i)
            if int(i[-1])-1!=-1:
                num_list[int(i[0])-1][int(i[-1])-1]+=1

        for i in range(1,int(n)+1):
            i=str(i)
            if int(i[-1])-1!=-1:
                count=count+num_list[int(i[-1])-1][int(i[0])-1]

N=int(input())
count=0
num_list=[[0]*9 for i in range(9)]
judge(str(N))
print(count)

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


d=defaultdict(lambda:defaultdict(int))

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

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

print(r)

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

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

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

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

print(r)

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

for i in range(1, N+1):
    x = i % 10
    y = i
    while y // 10:
        y //= 10
    if x == 0 or y == 0:
        continue
    t[x][y] += 1

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

Solution (Python):
def main(n):
    c=[[0]*10 for _ in range(10)]
    for k in range(1,n+1):
        n=str(k)
        i = int(n[0])
        j = int(n[-1])
        c[i][j] += 1
    ans =0
    for i in range(10):
        for j in range(10):
            ans+=c[i][j]*c[j][i]
    print(ans)
             
if __name__=='__main__':
    main(int(input()))

Solution (Python):
from functools import lru_cache
import sys
sys.setrecursionlimit(10**9)
@lru_cache(maxsize=None)
def f(n):
    if 0 < n and n  < 10:
        return n
    else:
        n = str(n)
        if int(n[-1]) == 0:
            return f(int(n)-1)
        if int(n[0]) < int(n[-1]):
            k = len(n) - 2
            n = int(n)
            if k > 0:
                return f(n-1) + int('1'*k)*2
            else:
                return f(n-1)
        elif int(n[0]) > int(n[-1]):
            k = len(n) - 1
            n = int(n)
            s = int('1'*k)*2
            return  f(n-1) + s
        else:
            k = len(n) - 2
            if k > 0:
                N = int(n)
                return f(N-1) + int('1'*k)*2  + int(n[1:-1])*2 + 3
            else:
                N = int(n)
                return f(N-1) + 3
                
p = int(input())
print(f(p))

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

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

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

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

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

ans = 0
for (a,b), v in d.items():
    if (b, a) in d:
        ans += d[(a, b)] * d[(b, a)]
print(ans)

Solution (Python):
n = int(input())
X = ['']
for i in range(1, 6):
    for j in range(10**i):
        X.append(str(j).zfill(i))
cnt = 0
for a in range(1, n+1):
    x = str(a)[0]
    y = str(a)[-1]
    if x == y:
        cnt += 1
    if y == '0':
        continue
    l, r = -1, len(X)-1
    while r-l > 1:
        c = (l+r)//2
        if int(y+X[c]+x) <= n:
            l = c
        else:
            r = c
    cnt += r
print(cnt)

Solution (Python):
from math import log
N = int(input())
li = [[0]* 10 for i in range(10)]
for i in range(1,N+1):
    u = i//10**int(log(i,10))
    l = i%10
    if l != 0:
        li[u][l] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += li[i][j] * li[j][i]
print(ans)

Solution (Python):
n = int(input())
a = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1,n+1):
	j = i%10
	while i>9:
		i//=10
	a[i][j]+=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())


HT = [[0 for i in range(10)] for j in range(10)] 
for i in range(1, n+1):  
    d = len(str(i)) 
    hnum = i // (10**(d-1))  
    tnum = i % 10   
    HT[hnum][tnum] += 1


ans = 0


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


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


print(ans)

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

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

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

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

print(ans)

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

l = [
  [len([n for n in range(1, N+1) if (str(n)[0]==str(i) and str(n)[-1]==str(j))])
     for j in range(1, 10)]
  for i in range(1, 10)
]

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


def calc(num):
    s = list(str(num))[::-1]
    s2 = int(s[-1])
    s1 = int(s[0])

    keta = 0
    while True:
        num = num // 10
        keta += 1
        if num == 0:
            break

    if s1 == s2:
        if keta == 1:
            return 1
        elif keta == 2:
            return 1 + 2
        else:
            ans = 0
            for i in range(keta - 3):
                ans += (10 ** (i+1)) * 2
            ans += (int(''.join(s[1:-1][::-1]))+1) * 2 + 3
            return ans

    elif s1 > s2:
        if keta == 2:
            return 0
        else:
            ans = 0
            for i in range(keta - 2):
                ans += 10 ** i
            return ans * 2
    else:
        if s1 == 0:
            return 0
        else:
            ans = 0
            for i in range(keta - 1):
                ans += 10 ** i
            return ans * 2




dp = [0] * (N+1)

for i in range(1, N+1):
    dp[i] = dp[i-1] + calc(i)


print(dp[N])

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

count_list = []
for i in range(10):
    count_list.append([0] * 10)

for i in range(1, n + 1):
    i_str = str(i)
    j = int(i_str[0])
    k = int(i_str[-1])
    count_list[j][k] += 1

ans = sum([count_list[i][j] * count_list[j][i] for i in range(1, 10) for j in range(1, 10)])
print(ans)

Solution (Python):

n = int(input())
table = [[0 for _ in range(10)] for _ in range(10)]
for i in range(n + 1):
    f, l = i // (1 * 10**(len(str(i)) - 1)), i % 10
    table[f][l] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += table[i][j] * table[j][i]
print(ans)

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

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

combination = [[0 for j in range(10)] for i in range(10)]
for i in range(1,N+1):
    s = str(i)
    start = int(s[0])
    end=int(s[-1])
    combination[start][end] += 1
count=0
for row in range(10):
    for column in range(10):
        count+=combination[row][column]*combination[column][row]

print(count)

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

for i in range(1,n+1):
    bot=i%10
    if bot==0:
        continue
    top=i
    while top>=10:
        top=top//10
    arr[top][bot]+=1

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

Solution (Python):



N = int(input())


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


for n in range(1, N+1):
    tmp = list(str(n))
    tmp = list(map(int, tmp))
    sento = tmp[0]
    ichi = tmp[-1]
    nums[sento][ichi] += 1


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


print(ans)

Solution (Python):
import sys
from itertools import permutations


def main():
    input = sys.stdin.readline
    n = int(input())
    cache = {}
    ans = 0

    for i in range(1, n+1):
        if (int(str(i)[0]), int(str(i)[-1])) in cache.keys():
            cache[(int(str(i)[0]), int(str(i)[-1]))] += 1
        else:
            cache[(int(str(i)[0]), int(str(i)[-1]))] = 1

    for k, v in cache.items():
        if k[0] == k[1] and v == 1:
            ans += v**2
        
        elif (k[1], k[0]) in cache.keys():
            ans += cache[(k[1], k[0])] * v

    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=[[0]*9 for i in range(9)]
sum=0
for i in range(1,N+1):
  s=str(i)
  if(int(s[0])!=0 and int(s[-1])!=0):
    A[int(s[0])-1][int(s[-1])-1]+=1
for i in range(9):
  for j in range(9):
    sum+=A[i][j]*A[j][i]
  

print(sum)

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

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

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


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

ans = 0


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

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

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

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

ans = 0

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

Solution (Python):
n=int(input())
List=[]
List+=[0]*100
for i in range(1,n+1):
  List[int(str(i)[0])*10+int(str(i)[-1])]+=1
sum=0
for i in range(1,10):
  sum+=List[11*i]**2
for i in range(1,10):
  for j in range(1,10):
    if i!=j:
      sum+=List[10*i+j]*List[10*j+i]
print(sum)

Solution (Python):
def solve(N):
    ret = 0
    counter = [[0 for _ in range(10)] for _ in range(10)]
    for n in range(1, N + 1):
        i, j = map(int, [str(n)[0], str(n)[-1]])
        counter[i][j] += 1

    for i in range(10):
        for j in range(10):
            ret += counter[i][j] * counter[j][i]
    return ret


if __name__ == '__main__':
    N = int(input())
    ret = solve(N)
    print(ret)

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

dp = [[0] * 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(10):
    for j in range(10):
        ans += dp[i][j] * dp[j][i]
print(ans)

Solution (Python):
n=int(input())
A=[[0 for i in range(9)] for i in range(9)]
for i in range(1,n+1):
  q=int(str(i)[0])
  w=i%10 
  if w==0 : continue
  A[q-1][w-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):
from collections import defaultdict

def main():
    N = int(input())
    combs = [(S[0], S[-1]) for S in map(str, range(1, N+1))]
    d = defaultdict(int)
    for pre, suf in combs:
        d[(pre, suf)] += 1
        d[(suf, pre)]

    ans = 0
    for key, val in d.items():
        pre, suf = key
        ans += val * d[(suf, pre)]
        
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict

def main():
    N = int(input())
    combs = [(S[0], S[-1]) for S in map(str, range(1, N+1))]
    d = defaultdict(int)
    for pre, suf in combs:
        if suf != "0":
            d[(pre, suf)] += 1
            d[(suf, pre)]

    ans = 0
    for key, val in d.items():
        pre, suf = key
        ans += val * d[(suf, pre)]
        
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
c = np.zeros((10, 10))
n = int(input())
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(int(ans))

Solution (Python):
N = int(input())
ans_lis = [[0] * 9 for i in range(9)]
for i in range(1,N+1):
  t = str(i)
  mae = int(t[0])
  ushiro = int(t[-1])
  if ushiro == 0:
    continue
  ans_lis[mae-1][ushiro-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    ans += ans_lis[i][j] * ans_lis[j][i]
print(ans)

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

    freq = {}
    for i in range(1, N+1):
        pair = str(i)[0] + str(i)[-1]
        freq.setdefault(pair, 0)
        freq[pair] += 1
    
    ans = 0
    freq_keys = freq.keys()
    for i in range(1, N+1):
        pair_rev = str(i)[-1] + str(i)[0]
        if pair_rev in freq_keys:
            ans += freq[pair_rev]
    
    print(ans)    


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N = int(input())
    freq = {}
    for i in range(1, N+1):
        pair = str(i)[0] + str(i)[-1]
        freq.setdefault(pair, 0)
        freq[pair] += 1
    
    ans = 0
    for i in range(1, N+1):
        pair_rev = str(i)[-1] + str(i)[0]
        if pair_rev in freq.keys():
            ans += freq[pair_rev]
    
    print(ans)
     
      
if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N = int(input())
    freq = {}
    for i in range(1, N+1):
        pair = str(i)[0] + str(i)[-1]
        if pair in freq.keys():
            freq[pair] += 1
        else:
            freq[pair] = 1
    
    ans = 0
    for i in range(1, N+1):
        pair_rev = str(i)[-1] + str(i)[0]
        if pair_rev in freq.keys():
            ans += freq[pair_rev]
    
    print(ans)

if __name__ == "__main__":
    main()

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

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

ans = 0

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


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

Solution (Python):


def main():
     n = int(input())
     matrix = [[0 for i in range(10)] for j in range(10)]
     ans = 0

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

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

if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict as d
dic=d(int)
for i in range(1,int(input())+1):
  dic[(int(str(i)[0]),i%10)]+=1
ans=0
for i in range(10):
  for j in range(10):
    ans+=dic[(i,j)]*dic[(j,i)]
print(ans)

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

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

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

ans = 0

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

print(ans)

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

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

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

sm = 0
for i in range(10):
    for j in range(10):
        sm += mp[i][j] * mp[j][i]

print(sm)

Solution (Python):
import math
import collections
import sys

input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]

n=I()
info=[]
for i in range(1,n+1):
    if i%10!=0:
        info.append(str(i)[0]+str(i)[-1])

c=collections.Counter(info)

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

Solution (Python):
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

n = n_in()
from collections import Counter
c = Counter()

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

from itertools import product

print(sum(c[(i,j)]*c[(j,i)] for i,j in product(range(1,10), repeat=2)))

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

for i in range(1, n+1):
    s = str(i)
    s1 = int(s[0]) - 1
    s2 = int(s[-1]) - 1
    if s2 == -1:
        continue 
    cheak_box[s1][s2] += 1

ans = 0
for p in range(9):
    for q in range(9):
        ans += cheak_box[p][q] * cheak_box[q][p]
        
print(ans)

Solution (Python):
from collections import defaultdict
n_str=input()
n_int=int(n_str)
cnt=[[0]*9 for i in range(9)]

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

ans=0
for i in range(9):
    for j in range(9):
        if cnt[i][j]==0:continue
        if i==j:
            temp=cnt[i][j]
            ans+=temp**2
        else:
            ans+=cnt[i][j]*cnt[j][i]
print(ans)

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

n = 1

M = N
while M // 10 != 0:
    M //= 10
    n += 1


c = N // (10 ** (n - 1))  
d = N % 10  


def f(a, b):  
    if a < c:
        if a == b:
            return ((10 ** (n - 1)) // 9) + 1
        else:
            return ((10 ** (n - 1)) // 9)
    elif a > c:
        if a == b:
            if n == 1:
                return 0
            else:
                return ((10 ** (n - 2)) // 9) + 1
        else:
            if n == 1:
                return 0
            else:
                return ((10 ** (n - 2)) // 9)
    else:
        if a == b:
            if n == 1:
                return 1
            elif n == 2:
                if b <= d:
                    return 2
                else:
                    return 1
            else:
                if b <= d:
                    return 2 + (10 ** (n - 2)) // 9 + (N - c * (10 ** (n - 1))) // 10
                else:
                    return 1 + (10 ** (n - 2)) // 9 + (N - c * (10 ** (n - 1))) // 10
        else:
            if n == 1:
                return 0
            elif n == 2:
                if b <= d:
                    return 1
                else:
                    return 0
            else:
                if b <= d:
                    return 1 + (10 ** (n - 2)) // 9 + (N - c * (10 ** (n - 1))) // 10
                else:
                    return 0 + (10 ** (n - 2)) // 9 + (N - c * (10 ** (n - 1))) // 10


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

print(ans)

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

n = int(readline())
ans = 0
memo = {}
for i in range(1, n + 1):
    i = str(i)
    if (i[-1], i[0]) not in memo:
        memo[(i[-1], i[0])] = 1
    else:
        memo[(i[-1], i[0])] += 1
for i in range(1, n + 1):
    i = str(i)
    if (i[0], i[-1]) in memo:
        ans += memo[(i[0], i[-1])]
print(ans)

Solution (Python):
import numpy as np
from itertools import product

n = int(input())

c = np.zeros((10, 10), dtype=np.int)
for i in range(1, n+1):
    first = int(str(i)[0])
    last = int(str(i)[-1])
    c[first, last] += 1

res = 0
for i, k in product(range(10), repeat=2):
    res += c[i, k] * c[k, i]

print(res)

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

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

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

ans = 0

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

print(ans)

Solution (Python):
n = int(input())
count = 0
work1 = [0,1,10,10**2,10**3,10**4,10**5]
work2 = [0,1,1,10,10**2,10**3,10**4]
for i in range(1, n+1):
    if i//10 == 0:
        count += 1
    elif int(str(i)[0]) > int(str(i)[-1]) and str(i)[-1] != '0':
        temp = len(str(i))
        count += sum(work1[:temp])*2
    elif int(str(i)[0]) < int(str(i)[-1]) and str(i)[-1] != '0':
        temp = len(str(i))-1
        count += sum(work1[:temp])*2
    elif str(i)[0] == str(i)[-1]:
        temp = len(str(i))
        count += sum(work2[:temp])*2+1
        if temp >= 3:
            count += int(str(i)[1:-1])*2
print(count)

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

def main():
    N = int(input())
    dic = {}
    for i in range(10):
        for j in range(10):
            dic[(i, j)] = 0
    for i in range(1, N+1):
        tmp = str(i)
        s, e = int(tmp[0]), int(tmp[-1])
        dic[(s, e)] += 1
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += dic[(i, j)] * dic[(j, i)]
    print(ans)

if __name__ == "__main__":
    main()

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

def main():
    N = int(input())
    dic = {}
    for i in range(1, N+1):
        tmp = str(i)
        s, e = int(tmp[0]), int(tmp[-1])
        if (s, e) in dic:
            dic[(s, e)] += 1
        else:
            dic[(s, e)] = 1
    
    ans = 0
    for (i, j), count in dic.items():
        tmp = 0
        if i == j:
            tmp = count**2
        else:
            if (j, i) in dic:
                tmp = count * dic[(j, i)]
        ans += tmp
        
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
N = int(input())

forward = np.zeros(100, dtype=int)
backward = np.zeros(100, dtype=int)

for i in range(1, N+1):
    top, bottom = int(str(i)[0]), i % 10
    forward[top * 10 + bottom] += 1
    backward[bottom * 10 + top] += 1
    
res = (forward * backward).sum()
print(res)

Solution (Python):
n = int(input())
dict_par = dict()
for i in range(1,10):
    i = str(i)
    for k in range(1,10):
        k = str(k)
        dict_par[i+k] = 0
        for num in range(1, n+1):
            num = str(num)
            if (num[0] == i) & (num[-1] ==k):
                dict_par[i+k] +=1
ans = 0
for i in range(1,10):
    i = str(i)
    for k in range(1,10):
        k = str(k)
        ans+= dict_par[i+k] * dict_par[k+i]
print(ans)

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

c = [[0 for i in range(10)] for i in range(10)]
      
for i in range(1, n+1):
    
    tail = i % 10
    
    while(i // 10 != 0):
        i = i // 10
    head = i

    c[head][tail] += 1

total = 0

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

print(total)

Solution (Python):
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
length=len(str(n))

ans=0
for i in range(1,10):
    if n<i:
        break
    s=0
    for j in range(1,length+1):
        if j<=1:
            s+=1
        elif j<length:
            s+=(10**(j-2))
        else:
            if not(length==6 and i>=2):
                for m in range(n,10**(length-1),-1):
                    if str(m)[0]==str(m)[-1]==str(i):
                        s+=1
    ans+=s*s
for i1 in range(1,10):
    for i2 in range(1,10):
        if i1==i2:
            break;
        s1=0
        s2=0
        for j in range(2,length+1):
            if j<length:
                s1+=10**(j-2)
                s2+=10**(j-2)
            else:
                if not(length==6 and i1>=2 and i2>=2):
                    for m in range(n,10**(length-1),-1):
                        if str(m)[0]==str(i1) and str(m)[-1]==str(i2):
                            s1+=1
                        elif str(m)[0]==str(i2) and str(m)[-1]==str(i1):
                            s2+=1
        ans+=s1*s2*2
print(ans)

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

C = [[0 for _ in range(10)] for _ in range(10)]
for k in range(1, N + 1):
    k = str(k)
    r = int(k[-1])
    l = int(k[0])
    C[l][r] += 1

a = 0
for l in range(10):
    for r in range(10):
        a += C[l][r] * C[r][l]

print(a)

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

Solution (Python):
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from functools import reduce
from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm(a, b):
  return a*b//gcd(a, b)


def main():
  n = int(input())
  head_tail = [[0]*10 for _ in range(10)]
  for i in range(1, n+1):
    s = str(i)
    h = int(s[0])
    t = int(s[-1])
    head_tail[h][t] += 1
  ans = 0
  for h in range(1,10):
    for t in range(1,10):
      ans += head_tail[h][t] * head_tail[t][h]
  print(ans)
  
  
if __name__ == '__main__':
  main()

Solution (Python):
import queue

N = int(input())


change=[[0] * (9) for _ in range(9)]
for i in range(1, 10):
    for j in range(1, 10):
        if  i == j and i <= N:
            cnt = 1
        else:
            cnt = 0
        q = queue.Queue()
        n = i * 10 + j
        while n <= N:
            cnt += 1
            for k in range(0, 10):
                q.put((n // 10) * 100 + k * 10 + n % 10)
            n = q.get()
        change[i-1][j-1] = cnt

ans = 0
for s in range(1, 10):
    for t in range(1, 10):
        ans += change[s-1][t-1] * change[t-1][s-1]
print(ans)

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

for n in range(1,N+1):
    i=n%10
    j=n
    while 10<=j:
        j=j//10
    c[i][j]=c[i][j]+1

for i in range(10):
    for j in range(10):
        ans=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)
  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())
s = str(n)
l= len(s)-1
ans = 0
temp = [[0]*10 for i in range(10)] 
for k in range(1,n+1):

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

Solution (Python):
import sys

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

N = ir()
count = [[0] * 10 for _ in range(10)] 
for i in range(1, N+1):
    s = str(i)
    head = int(s[0]); tail = int(s[-1])
    count[head][tail] += 1

answer = 0
for h in range(10):
    for t in range(10):
        answer += count[h][t] * count[t][h]
print(answer)

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


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


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):
    a, b = f(i)
    ans += freq[(b, a)]

print(ans)