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())
def f(x,y):
  ret=0
  for i in range(1,n+1):
    if i%10!=y:
      continue
    else:
      if x==int(str(i)[0]):
        ret+=1
  return ret
ans=0
T=[[0]*10 for _ in range(10)]
for i in range(1,10):
  for j in range(1,10):
    T[i][j]=f(i,j)

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

Solution (Python):
n=int(input())
l=[[0]*9 for j in range(9)]
for i in range(1,n+1):
    i=str(i)
    if i[0]!='0' and i[-1]!='0':
        l[int(i[-1])-1][int(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())
def f(x,y):
  ret=0
  for i in range(1,n+1):
    if i%10!=y:
      continue
    else:
      if x==int(str(i)[0]):
        ret+=1
  return ret
ans=0
T=[[0]*10 for _ in range(10)]
for i in range(1,10):
  for j in range(1,10):
    T[i][j]=f(i,j)

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

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

Solution (Python):




import sys
input = sys.stdin.readline



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

    res = 0

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

    for k in range(1,N+1):
        s = str(k)
        fd = int(s[0])
        ld = int(s[-1])
        c[fd][ld] += 1

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

    print(res)



main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(read())
l = len(str(N)) - 1
c_mat = [[0] * 10 for _ in range(10)]

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

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

Solution (Python):
import math

n = int(input())

keta = int(math.log10(n)) + 1

memo = [ [None] * 10 for _ in range(10)]

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

    if x == 0 or y == 0:
        continue

    if memo[x][y] is not None:
        ans += memo[x][y]
        
        continue
    
    patterns = 0
    for b in range(x, n+1, 10):
        x2 = int(str(b)[0])
        y2 = b % 10
        if x == y2 and y == x2:
            patterns += 1
    memo[x][y] = patterns
    ans += patterns

print(ans)

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

numbers = [[0 for i in range(10)] for i in range(10)]
for i in range(1, 10):
    for j in range(10):
        if i > N:
            continue
        tmp = 0
        if i == j:
            tmp += 1
        if i * 10 + j <= N:
            tmp += 1
        i_now = 100
        nine = 90
        nine_now = 10
        while i*i_now + nine + j <= N:
            tmp += nine_now
            i_now *= 10
            nine_now *= 10
            nine += 9 * nine_now

        if i*i_now <= N // i_now * i_now:
            if j <= N%10:
                tmp += (N%i_now)//10 + 1
            else:
                tmp += max(0, (N%i_now)//10)
        numbers[i][j] = tmp
ans = 0
for i in range(10):
    for j in range(10):
        ans += numbers[i][j]*numbers[j][i]
print(ans)

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

G = [[0 for tail in range(10)] for head in range(10)]

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

total = 0
for headA in range(10):
  for tailA in range(10):
    total += G[headA][tailA] * G[tailA][headA]
print(total)

Solution (Python):
n = int(input())
res_a = 0
res_b = 0
dic_len = {1:0,2:0,3:1,4:11,5:111,6:1111}
for i in range(1,n+1):
    istr = str(i)
    leni = len(istr)
    ifirst, ilast = istr[0], istr[-1]
    if ifirst == ilast:
        res_a += 1
        if i >= 10:
            res_b += 1
        if leni > 2:
            imid = istr[1:-1]
            res_b += max(int(imid),0)
    elif ifirst > ilast and ilast!='0':
        res_b += int(10**(leni-2))
    if ilast!='0':
        res_b += dic_len[leni]
print(res_a+2*res_b)

Solution (Python):

N = int(input())

C = [[0]*(10) for _ in range(10)]
for i in range(N+1):
    S = str(i)
    s = int(S[0])
    e = int(S[-1])
    C[s][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 numpy as np
n = int(input())
c = np.zeros((10,10))
for i in range(1,n+1):
  l = int(str(i)[0])
  r = int(str(i)[-1])
  c[l,r] += 1

count = 0
for l in range(10):
  for r in range(10):
    count += c[l,r]*c[r,l]
print(int(count))

Solution (Python):
n=int(input())
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(i,10):
    if i == j:
        ans+=d[i][j]*d[j][i]
    else:
        ans+=d[i][j]*d[j][i]*2
print(ans)

Solution (Python):
from pprint import pprint
N = int(input())
digit_cnt = list([0] * 9 for _ in range(9))
now = 1
for i in range(1, N + 1):
    str_i = str(i)
    row = int(str_i[0])
    column = int(str_i[-1])
    if row != 0 and column != 0:
        digit_cnt[row - 1][column - 1] += 1


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

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 fu(a,b):
  l=len(a)
  b=str(b)
  x=0;k=int(b[1:])-int(a[1:])
  if l<len(b) or a[0]<b[0]:
    return 10**(l-2)
  return (int('0'+b[1:l-1])+1-int(a[-1]>b[-1]))*(a[0]==b[0])

n = int(input())
b=str(n)
p = len(str(n))
x=min(9,n)
for i in range(10,n+1):
  l = len(str(i))
  k = str(i)[::-1]
  if k[0]!='0':
    x += fu(k,n)
    x += 2*(int('1'*max(l-2,1))+int(k[0]==k[-1])) if l>2 else 2*int(k[0]==k[-1])
print(x)

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())
c = [[0]*(10) for i in  range(10)]
for k in range(1,n+1):
    leng = len(str(k))-1 
    i = k//(10**leng)
    j = k%10
    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):
n = int(input())
ans = 0
cnt = [[0] * 9 for _ in range(9)]
tmp = 0
for i in range(1, n + 1):
    one = str(i)
    if one[-1] == '0':
        continue
    cnt[int(one[0]) - 1][int(one[-1]) - 1] += 1
for i in range(9):
    for j in range(9):
        ans += cnt[i][j] * cnt[j][i]
print(ans)

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


TTS = [1,1,10,100,1000,10000,100000]
TTW = [0,1,10,100,1000,10000,100000]
def ttk(N,k):
    c = 0
    Nd = len(str(N))
    
    for i in range(Nd-1):
        
        c += TTS[i]
    if Nd >= 3:
        for i in range(10**(Nd-2)):
            a = int(str(k) + (Nd-2-len(str(i)))*"0" + str(i) + str(k))
            
            if a <= N:
                c += 1
            else:
                break
    elif Nd == 2:
        a = int(str(k)*2)
        if a <= N:
            c += 1
    elif Nd == 1:
        a = k
        if a <= N:
            c += 1
    return c


"""
y = 0
for i in range(1,10):
    y += ttk(N,i)**2
print(y)
"""


def ttpq(N,p,q):
    c = 0
    Nd = len(str(N))
    
    for i in range(Nd-1):
        
        c += TTW[i]
    if Nd >= 3:
        for i in range(10**(Nd-2)):
            a = int(str(p) + (Nd-2-len(str(i)))*"0" + str(i) + str(q))
            
            if a <= N:
                c += 1
            else:
                break
    elif Nd == 2:
        a = int(str(p)+str(q))
        if a <= N:
            c += 1
    elif Nd == 1:
        c = 0
    return c


y=0
for i in range(1,10):
    for j in range(i,10):
        
        if i == j:
            
            y += ttk(N,i)**2
        else:
            
            y += ttpq(N,i,j)*ttpq(N,j,i)*2
        
print(y)

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

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

def main():
    n = INT()
    ans = 0

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

    for i in range(1, n+1):
        s = str(i)
        a, b = s[0], s[-1]
        if "0" in [a, b]: continue
        lis[int(a)-1][int(b)-1] += 1


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

    print(ans)



if __name__=='__main__':
    main()

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

counter=defaultdict(int)

X=map(str,(range(1,N+1)))
for x in X:
    h=x[0]
    t=x[-1]
    counter[(h,t)]+=1

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

print(ans)

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


class TestD(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(25), 17)

    def test_2(self):
        self.assertEqual(think(1), 1)

    def test_3(self):
        self.assertEqual(think(100), 108)

    def test_4(self):
        self.assertEqual(think(2020), 40812)

    def test_5(self):
        self.assertEqual(think(200000), 400000008)


def solve():
    n = read()
    result = think(n)
    write(result)


def read():
    return read_int(1)[0]


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n):
    count = 0
    dp = [[0 for last_digit in range(10)] for first_digit in range(10)]
    for i in range(1, n + 1):
        first_digit = first_digit_of(i)
        last_digit = last_digit_of(i)
        dp[first_digit][last_digit] += 1
    for first_digit in range(10):
        for last_digit in range(10):
            count += dp[first_digit][last_digit] * dp[last_digit][first_digit]
    return count


def write(result):
    print(result)


def first_digit_of(a):
    return int(str(a)[0])


def last_digit_of(a):
    return int(str(a)[-1])


if __name__ == '__main__':
    
    solve()

Solution (Python):
import numpy as np

N = int(input())

counter = 0

lead = np.zeros((9, 9))

for i in range(1, N + 1):
    if (i % 10 != 0):
        moji = str(i)
        lead[int(moji[0]) - 1][int(moji[-1]) - 1] += 1

for i in range(1, N + 1):
    if (i % 10 != 0):
        moji = str(i)
        tmp = moji[-1]
        counter += lead[int(moji[-1]) - 1][int(moji[0]) - 1]

print(int(counter))

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

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

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

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

print(ans)

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

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

Solution (Python):



N = int(input())

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


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

grid = [[0 for _ in range(9)] for _ in range(9)]
ans = 0
for i in range(1, n + 1):
    if i % 10 == 0:
        continue
    else:
        s = str(i)
        grid[int(s[0]) - 1][int(s[-1]) - 1] += 1

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

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

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

    c[s][e] += 1

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

print(ans)

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

ans=0
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
    
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 _ in range(9)] for _ in range(9)]
 
for i in range(1, n+1):
    s = str(i)
    f, l = s[0], s[-1]
    if "0" in [f, l]: continue
    table[int(f)-1][int(l)-1] += 1
 
ans = 0
for i in range(9):
    for j in range(9):
        ans += table[i][j] * table[j][i]
 
print(ans)

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

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

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

Solution (Python):
N = input()
n_len = len(N)
N = int(N)
A = [[0 for i in range(10)] for _ in range(10)]

ans = 0
for n in range(1, N+1):
    l = int(str(n)[0])
    r = n % 10

    if r == 0:
        continue
    A[l][r] += 1

for l in range(1, 10):
    for r in range(1, 10):
        ans += A[l][r] * A[r][l]

print(ans)

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

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

for i in range(1,N+1):
    if i < 10:
        list[i-1][i-1] += 1
    else:
        long = len(str(i))
        first = int(i / (10**(long-1)))
        last = i % 10
        if first != 0 and last != 0:
            list[first-1][last-1] += 1
            

for i in range(9):
    for j in range(i,9):
        if i == j:
            tmp = list[i][j]
            result += tmp * tmp
        else:
            result += list[i][j] * list[j][i] * 2

print(result)

Solution (Python):
n = int(input())
dic = {}
for i in range(10, 100): dic[str(i)]=0
for i in range(1, n+1):
    s = str(i)
    dic[s[0]+s[-1]] += 1

ans = 0
for i in range(1, n+1):
    s = str(i)
    if s[-1]=="0": continue
    ans += dic[s[-1]+s[0]]
print(ans)

Solution (Python):
N = int(input())
start, end, both = dict(), dict(), dict()
for i in range(1, N+1):
    i_s = str(i)
    l, r = int(i_s[0]), int(i_s[-1])
    both[l, r] = both.get((l, r), 0) + 1

ans = 0
for i in range(1, N + 1):
    i_s = str(i)
    l, r = int(i_s[0]), int(i_s[-1])
    ans += both.get((r, l), 0)
print(ans)

Solution (Python):
import math
import numpy as np

N = int(input())
ans = 0
num_mat = np.zeros((9, 9))
for i in range(N+1):
  head = int(list(str(i))[0])
  tail = int(list(str(i))[-1])
  rev = ''.join(list(str(i))[::-1])
  if tail==0:
    continue
  num_mat[head-1, tail-1] += 1

for i in range(9):
  for j in range(i+1):
    if i==j:
      num = num_mat[i,j]
      ans += num + num * (num-1)
    else:
      ans += num_mat[i,j] * num_mat[j,i] * 2

print(int(ans))

Solution (Python):
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input())
ans_dict = {}
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans_dict[i*10+j]=0
for num in range(1,n+1):
    num_str =  str(num)
    i = int(num_str[len(num_str)-1])
    j = int(num_str[0])
    if (i!=0) & (j!=0):
        ans_dict[i*10+j] +=1
for key_ in ans_dict.keys():
    if str(key_)[0] == str(key_)[1]:
        ans += ans_dict[key_]**2
    else:
        key_s = key_//10+int(str(key_)[1])*10
        ans += ans_dict[key_] * ans_dict[key_s]*2
        ans_dict[key_s] = 0
print(ans)

Solution (Python):
N= int(input())
lead_N = int(str(N)[0]) 
end_N = int(str(N)[len(str(N)) - 1])
ans = 0

for i in range(1, N + 1):
  lead = int(str(i)[0])
  end = int(str(i)[len(str(i)) - 1])
  
  if lead == end:
    ans += 1
  
  for j in range((len(str(N)) - 2) + 1):
    if end == 0:
      
      continue
    elif end > lead_N and j == (len(str(N)) - 2) :
      
      continue
    elif end == lead_N and j == (len(str(N)) - 2):
      
      
      if len(str(N)) >= 3:
        ans += int(str(N)[1:len(str(N)) - 1])

      if lead <= end_N:
        ans += 1
      
      pass
    else:
      
      ans += 10 ** j

print(ans)

Solution (Python):
n=str(input())
H=n[0]
T=n[-1]
keta=len(n)
n=int(n)
ans=0
if keta==1:
    print(1)
    exit()
elif keta==2:
    for i in range(1,n+1):
        s=str(i)
        for j in range(1,n+1):
            S=str(j)
            if S[0]==s[-1] and S[-1]==s[0]:
                ans+=1    
else:
    for i in range(1,n+1):
        s=str(i)
        h=s[0]
        t=s[-1]
        if h!="0" and t!="0":
            ans+=(10**(keta-2)-1)//9
            if h==t:
                ans+=1
            if int(t)<int(H):
                ans+=(10**(keta-2))*(int(H)-int(t))
            elif int(t)==int(H):
                ans+=max(0,((n-10**(keta-1)*int(H)-int(h))//10)+1)
            
print(ans)

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

resultdist = {}

ketalist = [1,11,111,1111,11111]

def chukan(N):
    if(len(N) > 2):
        return int(N[1:-1])
    else:
        return 0

def retketa1(N):
    if(len(N) > 1):
        return ketalist[len(N)-2]
    else:
        return 0

def retketa2(N):
    if(len(N) > 2):
        return ketalist[len(N)-3]
    else:
        return 0

for i in range(1,10):
    for j in range(1,10):
        count = 1 if i==j else 0
        if(i < int(N[0])):
            count += retketa1(N)
        if(i > int(N[0])):
            count += retketa2(N)
        if(i == int(N[0])):
            count += retketa2(N)
            if(j > int(N[-1])):
                count += chukan(N)
            else:
                if(len(N) > 1):
                    count += chukan(N) + 1
                else:
                    count += chukan(N)

        resultdist[str(i)+str(j)] = count

resulutcount = 0

for i in range(1, int(N)+1):
    l = str(i)[0]
    f = str(i)[-1]
    if(f != "0"):
        resulutcount += resultdist[f+l]
print(resulutcount)

Solution (Python):
from collections import defaultdict
a = [list(map(int, s.split()))[0] for s in open(0)][0]
cnt = 0 
di = defaultdict(int)
for i in range(1, a + 1):
    if len(str(i)) == 1:
        cnt += 1 
        di[str(i)] += 1
    elif str(i) != 0:
        if str(i)[0] == str(i)[-1]:
            cnt += di[str(i)[-1]] * 2 + 1
            cnt += di[str(i)[0] + str(i)[-1]]*2
            di[str(i)[0] + str(i)[-1]] += 1
        else:
            cnt += di[str(i)[-1] + str(i)[0]]*2
            di[str(i)[0] + str(i)[-1]] += 1

print(cnt)

Solution (Python):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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

N=one_int()
N+=1

def naive_solver(N):
    output=[]
    for i in range(1,N):
        A=str(i)
        for j in range(1,N):
            B=str(j)
            if A[-1]==B[0] and B[-1]==A[0]:

                output.append([A,B])
    return output

def naive_solver2(N):
    dicts={str(i):{str(j):0 for j in range(10)} for i in range(10) }
    for i in range(1,N):
        temp=str(i)
        dicts[temp[0]][temp[-1]] += 1
    return dicts

dicts = naive_solver2(N)

import itertools as it

from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under



dicts = naive_solver2(N)

count = 0
for si in range(1,10):
    i=str(si)
    for sj in range(si,10):
        j = str(sj) 
        

        if j!=i:
            count+=dicts[i][j] * dicts[j][i] * 2

        else:
            count+=dicts[i][j]*dicts[j][i]


print(count)

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

m = dict()
for i in range(1, N + 1):
    n = str(i)
    s = n[0]
    t = n[-1]
    
    if t == 0:
        continue
    
    k = (s, t)
    if k in m:
        m[k] += 1
    else:
        m[k] = 1

ans = 0
for k, v in m.items():
    s, t = k
    if s == t:
        ans += v ** 2
    else:
        if (t, s) in m:
            ans += v * m[(t, s)]

print(ans)

Solution (Python):
from itertools import product
from functools import reduce
import operator
def prod(iterable):
    return reduce(operator.mul, iterable, 1)
def cnt_samelen(b,e,n_str):
  n0=int(n_str[0])
  if b>n0 or len(n_str)==1:
    return 0
  if b<n0:
    return b*int(10**(len(n_str)-2))
  
  return (int(n_str[1:-1])+1 if len(n_str)>2 else 1)-(1 if e>int(n_str[-1]) else 0)
def cnt(b,e,n_str):
  res=0
  l=len(n_str)
  if b==e and int(n_str)>=b:
    res+=1
  for i in range(l-2):
    res+=10**(i)
  res+=cnt_samelen(b,e,n_str)
  return res
def sol():
  n_str=input()
  return sum(cnt(b,e,n_str)*cnt(e,b,n_str) 
             for b,e in product(range(1,10),repeat=2))

print(sol())

Solution (Python):
N = int(input())
P = list(range(1,N+1))
ans = 0
keta = len(str(N))
for p in P:
    first = str(p)[0]
    last = str(p)[-1]
    if last == "0":
        pass
    else:
        temp = 0
        for k in range(1,keta+1):
            if k == 1:
                if first == last:
                    temp += 1
            elif k == 2:
                if int(last+first) <= N:
                    temp += 1
            elif k == keta:
                if int(last) > int(str(N)[0]):
                    pass
                elif int(last) < int(str(N)[0]):
                    temp += 10**(k-2)
                else:
                    temp += int(str(N)[1:-1])+1
                    if int(str(N)[-1]) < int(first):
                        temp -= 1
            else:
                temp += 10**(k-2)
        ans += temp
print(ans)

Solution (Python):
import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop



readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

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

'''
while(True):
    Nstr = str(N)
    l = len(Nstr) - 1

    if Nstr[0] == Nstr[l]:
        break

    N -= 1
'''




for i in range(N+1):
    s = str(i)
    l = len(s) - 1

    if s[l] == '0':
        continue
    A[int(s[0])][int(s[l])] += 1


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


print(ans)

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

if N < 10:
    print(N)
else:
    count = 9
    import numpy as np
    counter = np.zeros((9,9), dtype = int)
    for i in range(11, N+1):
        if i % 10 == 0:
            pass
        else:
            s = list(str(i))
            i1 = int(s[0]) - 1
            i2 = int(s[-1]) - 1
            counter[i1][i2] += 1
    
    for i in range(9):
        count += counter[i][i] ** 2 + counter[i][i] * 2
    
    for i in range(8):
        for j in range(i+1, 9):
            
            count += counter[i][j] * counter[j][i] * 2
    print(count)

Solution (Python):
import sys
from math import log10, floor, ceil
from itertools import combinations, product
n = int(input())
n_start = int(str(n)[0])
n_last = int(str(n)[-1])
n_keta = floor(log10(n)) + 1
dic = {i:0 for i in product(range(10), repeat=2)}

memo_ls = [[[0 for k in range(2)] for j in range(10)] for i in range(n_keta)]
if n_keta == 1:
    print(n_start)
else:
    for i in range(n_start + 1):
        if i < n_start:
            memo_ls[0][i][1] = 1
        else:
            memo_ls[0][i][0] = 1
    for i, x in enumerate(str(n)[1:-1], 1):
        x = int(x)
        for j in range(10):
            for k in range(10):
                if j < x:
                    memo_ls[i][k][1] += memo_ls[i-1][k][0] + memo_ls[i-1][k][1]
                elif j == x:
                    memo_ls[i][k][0] = memo_ls[i-1][k][0]
                    memo_ls[i][k][1] += memo_ls[i-1][k][1]
                else:
                    memo_ls[i][k][1] += memo_ls[i-1][k][1]
            memo_ls[i][j][1] += 1
    score = 0
    for i in range(10):
        for j in range(10):
            if j <= n_last:
                score = sum(memo_ls[-2][i])
            else:
                score = memo_ls[-2][i][1]
            dic[(i, j)] = score
    for i in range(10):
        dic[(i, i)] += 1
        dic[(0, i)] = 0
    ans = 0
    for i in product(range(10), repeat=2):
        ans += dic[i] * dic[(i[1], i[0])]
    print(ans)

Solution (Python):
def add_num(n):
    str_n = str(n)
    lower = int(str_n[-1])
    upper = int(str_n[0])
    digit_num = len(str_n)
    ret_val = 0
    if lower == 0:
        return 0
    if digit_num == 1:
        return 1
    elif digit_num == 2:
        if upper < lower:
            return 0
        if upper == lower:
            ret_val += 1
        for d in range(digit_num-1):
            add_val = (10**d) * 2
            ret_val += add_val
        return ret_val

    if upper <= lower:
        for d in range(digit_num-2):
            add_val = (10**d) * 2
            ret_val += add_val
        if upper == lower:
            ret_val += 1
    else:
        for d in range(digit_num-1):
            add_val = (10**d) * 2
            ret_val += add_val
    if upper == lower:
        ret_val += 2
        
        ret_val += int(str_n[1:-1])*2
    return ret_val


def main():
    N = int(input())
    ans = 0
    for n in range(1, N+1):
        
        ans += add_num(n)
    print(ans)


if __name__ == "__main__":
    main()
    add_num(11)

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

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

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

r = dict()
for i in range(1, N + 1):
    ii = str(i)
    a = ii[0]
    b = ii[-1]
    if b == '0':
        continue
    a = int(a)
    b = int(b)
    if (a, b) not in r:
        r[(a, b)] = 0
    r[(a, b)] += 1

result = 0
for i in range(1, 10):
    for j in range(1, 10):
        if (i, j) not in r:
            continue
        if (j, i) not in r:
            continue
        result += r[(i, j)] * r[(j, i)]
print(result)

Solution (Python):
import itertools
N = int(input())
Nketa = len(str(N))

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


for p in range(1,N+1):
    mae = int(str(p)[0])
    ato = int(str(p)[-1])
    range_count[mae][ato] += 1

    

count = 0
for j in range(1,N+1):
    a_mae = int(str(j)[0])
    a_ato = int(str(j)[-1])
    
    b_mae = a_ato
    b_ato = a_mae
    
    if b_mae == 0:
        continue
    else:
        count += range_count[b_mae][b_ato]
print(count)

Solution (Python):
n = int(input())
m = [0]*100

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

res = sum([m[i]*m[int(str(i)[::-1])] for i in range(100)])

print(res)

Solution (Python):
n = int(input())
cnt=0
for i in range(n):
  a = i+1
  k = str(a)
  l = len(k)
  k1, k2 = int(k[0]), int(k[l-1])
  flag=0
  s = 10
  if k2==k1:
    cnt+=1
  if k2!=0:
    while(flag==0):
      if k2*s+k1<=n:
        if (k2+1)*s+k1<n:
          cnt+=(s//10)
        else:
          cnt+=((n-(k2*s+k1))//10+1)
        s*=10
      else:
        flag=1
print(cnt)