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())
sn = str(N)

ans = 0
dp = [[[0] * 2 for i in range(10)] for j in range(10)]
dp[0][0][0] = 1
for nketa in range(len(str(N))):

    ndp = [[[0] * 2 for i in range(10)] for j in range(10)]

    for top in range(10):

        for app in range(10):

            for fl in range(2):

                for nadd in range(10):

                    if top == 0 and  fl == 0 and int(sn[nketa]) == nadd:
                        ndp[nadd][nadd][0] += dp[top][app][fl]
                    if top == 0 and fl == 0 and  int(sn[nketa]) > nadd:
                        ndp[nadd][nadd][1] += dp[top][app][fl]
                    if top == 0 and fl == 1:
                        ndp[nadd][nadd][1] += dp[top][app][fl]

                    if top != 0 and fl == 0 and int(sn[nketa]) == nadd:
                        ndp[top][nadd][0] += dp[top][app][fl]
                        
                    if top != 0 and fl == 0 and int(sn[nketa]) > nadd:
                        ndp[top][nadd][1] += dp[top][app][fl]

                    if top != 0 and fl == 1:
                        ndp[top][nadd][fl] += dp[top][app][fl]
                        
    dp = ndp

    
    

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

print (ans)

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

pattern = {str(h) + str(t): 0 for h in range(1, 10) for t in range(1, 10)}
for a in range(1, n + 1):
  a_str = str(a)
  a_head = a_str[0]
  a_tail = a_str[-1]
  if a_tail != "0":
    pattern[a_head + a_tail] += 1

ans = 0
for k, v in pattern.items():
  ans += v * (pattern.get(k[::-1]) or 0)

print(ans)

Solution (Python):
n = int(input())
ans = 0
c = {}
for i in range(1,n+1):
  s = str(i)
  if s[-1] == 0:
    continue
  k = (s[-1],s[0])
  k_ = (s[0],s[-1])
  pre = 0
  if k_ in c:
    pre = c[k_]
    ans += pre*2
  if k in c:
    v = c[k]
    v += 1
    c[k] = v
  else:
    c[k] = 1
  if k_ in c:
    ans += c[k_]-pre
print(ans)

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

Solution (Python):
import collections

N = int(input())
ans = 0
d = collections.defaultdict(int)
for i in range(1, N+1):
    str_i = str(i)
    key = str_i[0] + str_i[-1]
    if "0" in key:
        continue
    d[key] += 1

for k, v in d.items():
    rev_k = k[::-1]
    rev_v = d.get(rev_k)
    if rev_v is None:
        continue
    ans += v * rev_v
print(ans)

Solution (Python):
N = int(input())
a = [[[] for i in range(10)] for j in range(10)]
ans = 0
for i in range(1, N+1):
  a[int(str(i)[0])][int(str(i)[-1])].append(i)

for i in range(1, 10):
  ans += len(a[i][i]) ** 2
for i in range(1, 9):
  for j in range(i+1, 10):
    ans += len(a[i][j]) * len(a[j][i]) * 2
print(ans)

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

m = n
lsn = []
d = 0
while m > 0:
    lsn.append(m%10)
    m //= 10
    d += 1
lsn.reverse()

ans = 0
for i in range(1, n+1):
    ls = []
    while i > 0:
        ls.append(i%10)
        i //= 10
    
    sentou = ls[0]
    matsubi = ls[len(ls)-1]

    if sentou == 0 or matsubi == 0:
        continue

    for j in range(1, d+1):
        if j == 1:
            if sentou == matsubi:
                ans += 1
        elif j < d:
            ans += 10**(j-2)
        else:
            dp = [[0 for k in range(2)] for l in range(d+1)]
            dp[0][0] = 1

            for k in range(d):
                for l in range(2):
                    if k == 0:
                        if l == 1 or sentou < lsn[0]:
                            dp[k+1][1] += dp[k][l]
                        elif sentou <= lsn[0]:
                            dp[k+1][0] += dp[k][l]
                    elif k == d-1:
                        if l == 1 or matsubi < lsn[d-1]:
                            dp[k+1][1] += dp[k][l]
                        elif matsubi <= lsn[d-1]:
                            dp[k+1][0] += dp[k][l]
                    else:
                        lim = lsn[k]
                        if l == 1:
                            lim = 9
                        for o in range(lim+1):
                            if l == 1 or o < lsn[k]:
                                dp[k+1][1] += dp[k][l]
                            elif o <= lsn[k]:
                                dp[k+1][0] += dp[k][l]
            
            ans += dp[d][0]+dp[d][1]

print(ans)

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

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

print(ans)

Solution (Python):
from bisect import bisect_right, bisect_left
from itertools import groupby, permutations, combinations
from fractions import gcd
import numpy as np
from sys import stdin, stderr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def main():
    N = int(input())
    st = dict()
    for i in range(1, N+1):
        str_i = str(i)
        s = int(str_i[-1])
        t = int(str_i[0])
        if s == 0:
            continue
        
        
        num = s if s == t else s + t*10
        if num not in st:
            st[num] = 0
        st[num] += 1
    
    ret = 0
    for k in st.keys():
        v = st[k]
        if k < 10:
            ret += v*v
        else:
            kk = (k % 10) * 10 + (k // 10)
            if kk in st:
                ret += v * st[kk]
            
            

    return ret


if __name__ == "__main__":
    print(main())

Solution (Python):
def main():
    strn = input()
    n = int(strn)
    if n < 10:
        print(n)
        return
    m = [0] * 81

    for i in range(1, n + 1):
        a = int(str(i)[0])
        b = int(str(i)[-1])
        
        if b == 0:
            continue
        else:
            key = 9 * (a - 1) + b - 1
            
            m[key] += 1
    ans = 0
    
    for i in range(1, 10):
        for j in range(1, 10):
            key = 9 * (i - 1) + j - 1
            oppose = 9 * (j - 1) + i - 1
            ans += m[key] * m[oppose]
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
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):
def examA():
    N, M = LI()
    if N==M:
        print("Yes")
    else:
        print("No")
    return

def examB():
    A, B = LI()
    if A>B:
        ans = str(B)*A
    else:
        ans = str(A)*B
    print(ans)
    return

def examC():
    N = I()
    P = LI()
    minP = N+1
    ans = 0
    for i in range(N):
        if minP>P[i]:
            minP=P[i]
            ans +=1
    print(ans)
    return

def examD():
    N = I()
    ans = 0
    keep = [[0]*10 for _ in range(10)]
    for i in range(1,N+1):
        l = int(str(i)[0]); r = int(str(i)[-1])
        keep[l][r]+=1

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

    print(ans)
    return

def examE():
    def gcd(x, y):
        if y == 0:
            return x
        while (y != 0):
            x, y = y, x % y
        return x
    def lcm(x, y):
        return x * y // gcd(x, y)
    N = I()
    A = LI()


    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examD()

"""

"""

Solution (Python):
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]


n = ni()
m = [[0]*10 for _ in range(10)]
for i in range(1, n+1):
    i = str(i)
    f, e = int(i[0]), int(i[-1])
    m[f][e] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += m[i][j]*m[j][i]
print(ans)

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

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

for i in range(n):
    x=i+1
    l = x % 10
    f = x
    while f // 10 > 0:
        f = f // 10

    t[f][l] = t[f][l] + 1

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

print("{}".format(count))

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

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())
d = len(str(n))
ans = 0
cnt = 0
for a in range(1, n + 1):
    s = str(a)
    if s[-1] != '0':
        if d >= 3:
            l, r = 0, n + 1
            while r - l > 1:
                m = (l + r) // 2
                m -= 1
                st = str(m).zfill(d - 2)
                b = int(s[-1] + st + s[0])
                if b <= n:
                    l = m + 1
                else:
                    r = m + 1
            ans += l
            t = 10
            for i in range(d - 3):
                ans += t
                t *= 10
        if int(s[-1] + s[0]) <= n:
            ans += 1
        if s[0] == s[-1] and int(s[0]) <= n:
            ans += 1
print(ans)

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

count = 0
d = 1
mat = [[0] * 10 for _ in range(10)]
for a in range(1, n+1):
    
    if a >= 10 ** d: d += 1
    x = a // (10 ** (d - 1))
    y = a % 10
    mat[x][y] += 1

for i in range(10):
    
    count += mat[i][i] ** 2

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

print(count)

Solution (Python):
N=int(input())
Atamasiri = [[0 for i in range(10)] for j in range(10)]
cursaijou = 1
for n in range(1,N+1):
    if n>=cursaijou*10:
        cursaijou *= 10
    atama = n//cursaijou
    siri = n%10
    Atamasiri[atama][siri] += 1
ans = 0
for head in range(10):
    for ketsu in range(10):
        ans += Atamasiri[head][ketsu]*Atamasiri[ketsu][head]
print(ans)

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

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

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

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

print(ans)

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


m = defaultdict(int)

for i in range(1, N+1):
    s = str(i)
    if s[-1] != "0":
        m[s[0] + s[-1]] += 1

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


print(ans)

Solution (Python):
def main():
    N = int(input())
    ls = [[0 for _ in range(10)] for _ in range(10)]
    ans = 0
    for i in range(1,N+1):
        b,a = int(str(i)[0]),int(str(i)[-1])

        if a==b:
            ans += 2*ls[a][b]+1
            
        else:
            ans += 2*ls[a][b]

        ls[b][a] += 1

    print(ans)

        
if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
C = [[0 for i in range(10)] for j in range(10)]
for i in range(1,N+1):
    s = str(i)
    i1,i2 = int(s[0]),int(s[-1])
    C[i1][i2] += 1
ans = 0
for i in range(10):
    for j in range(10):
        if i == 0 or j == 0:
            continue
        ans += C[i][j]*C[j][i]
print(ans)

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

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

for i in range(1, N+1):
  DP[int(str(i)[0])][int(str(i)[-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):
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    n = II()
    sento = [[0 for i in range(10)] for i in range(10)]

    vs = [1, 10, 100, 1000, 10000, 100000]
    for i in range(1, n+1):
        m = i % 10
        for v in vs:

            if i // v < 10:

                s = i // v
                break
        
        sento[m][s] += 1

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







if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
L = len(str(N))
A,B=int(str(N)[L-1]),int(str(N)[0])
c = 0
for i in range(1,N+1):
    I = str(i)
    l = len(I)
    a,b = int(I[0]),int(I[l-1])
    if a == 0 or b == 0:
        continue
    else:
        if a == b:
            c += 1
        for m in range(max(L-2,0)):
            c += 10**m
        if L == 1:
            continue
        if b > B:
            continue
        elif b < B:
            c += 10**(L-2)
        elif L > 2 and a <= A:
            c += int(str(N)[1:L-1])+1
        elif L > 2:
            c += int(str(N)[1:L-1])
        elif L == 2:
            c += 1
print(c)

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

d = {}
d2 = {}
for i in range(1, N + 1):
    s = str(i)
    key = (s[0], s[-1])
    key2 = (s[-1], s[0])
    if key not in d:
        d[key] = 1
    else:
        d[key] += 1

    if key2 not in d2:
        d2[key2] = 1
    else:
        d2[key2] += 1


kosuu = 0
for key in d:
    val = d[key]
    val2 = d.get((key[1], key[0]))
    if val2 is not None:
        kosuu += val * val2

print(kosuu)

Solution (Python):
n = int(input())
grid = [[0 for i in range(9)] for j in range(9)]
ans = 0

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

for i in range(9):
    for j in range(i, 9):
        if i == j:
            ans += grid[i][j] * grid[j][i]
        else:
            ans += (grid[i][j] * grid[j][i])*2

print(ans)

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

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

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

Solution (Python):

import sys


def solve(N: int):
    x = [0] * 100

    for i in range(1, N+1):
        a = i%10
        b = int(str(i)[0])
        x[a + b*10] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1,10):
            if i == j:
                ans += x[i*11] ** 2
                continue
            ans += x[i*10 +j] * x[j*10 + i]
    print(ans)
    return




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

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
cnts = [[0] * 10 for _ in range(10)]
ans = 0
for i in range(N + 1):
    if i % 10 == 0:
        continue
    s = str(i)
    cnts[int(s[0])][int(s[-1])] += 1

for i in range(N + 1):
    if i % 10 == 0:
        continue
    s = str(i)
    ans += cnts[int(s[-1])][int(s[0])]

print(ans)

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

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

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

ans = 0
for a in range(1, 10):
    for b in range(1, 10):
        ans += lst[a][b] * lst[b][a]

print (ans)

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

FN = [0] * 100


for i in range(1, N+1):
  first_num = int(str(i)[0])
  last_num = i % 10
  FN[first_num*10 + last_num] += 1


cnt = 0
for i, fn in enumerate(FN):
  if i < 11:
    continue
  rev = (i // 10) + (i % 10) * 10
  cnt += FN[rev] * fn
  

print(cnt)

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

def calc(n):
	counter = [[0 for _ in range(10)] for _ in range(10)]

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

	cnt = 0
	for i in range(1, 10):
		for j in range(1, 10):
			if i == j:
				if counter[i][j] >= 1:
					cnt += counter[i][j] ** 2
			else:
				cnt += counter[i][j] * counter[j][i]
	return cnt

print(calc(n))

Solution (Python):
N = int(input())
lst = [[0 for i in range(10)]for j in range(10)]
ans = 0
for i in range(1,N+1):
    tmp = list(str(i))
    lst[int(tmp[0])][int(tmp[-1])]+=1
for i in range(10):
    for j in range(10):
        if i == 0 or j == 0:
            continue
        if i == j:
            ans+=lst[i][j]**2
        else:
            ans+=lst[i][j]*lst[j][i]
print(ans)

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


table = collections.defaultdict(list)

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


def get_initial(n):
    while(n // 10 > 0):
        n = n // 10
    return(n % 10)


for i in range(1, N+1):
    k = get_initial(i)
    table[k][i % 10] += 1
ans = 0
for i in range(1, 10):
    if i not in table:
        continue
    for j in range(1, 10):
        ans += table[i][j] * table[j][i]

print(ans)

Solution (Python):
l = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]]
n = int(input())
c = 0
for i in range(1,n+1):
    li = len(str(i))
    a = i%10
    b = i//(10**(li-1))
    l[a][b] = l[a][b]+1

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

Solution (Python):

N=int(input())
L=[[0 for i in range(10)] for j in range(10)]

for i in range(N):
    num=i+1
    s_num=str(num)
    t=int(s_num[0])
    b=int(s_num[-1])
    L[t][b]+=1

ans=0
for i in range(N):
    num=i+1
    s_num=str(num)
    t=int(s_num[0])
    b=int(s_num[-1])
    ans+=L[b][t]
    
print(ans)

Solution (Python):
n = int(input())
digit = len(str(n))

score = 0

for i in range(1,n+1):
    if str(i)[-1] != '0':
        first = str(i)[-1]
        end = str(i)[0]
        if first == end:
            count = 1
        else:
            count = 2
        while count < digit:
            if count <= 2:
                score += 1
            else:
                score += 10**(count - 2)
            count += 1
        if first < str(n)[0]:
            score += 10**(count - 2)
        elif first == str(n)[0]:
            if len(str(n)) == 1:
                score += 1
            elif end > str(n)[-1]:
                score += int(str(n)[1:])//10
            else:
                score += int(str(n)[1:])//10 + 1
print(score)

Solution (Python):
import math

n = int(input())
top_bottom_num = {str(i): {str(j): 0 for j in range(10)} for i in range(10)}
for i in range(1, n + 1):
    str_num = str(i)
    top_bottom_num[str_num[0]][str_num[-1]] += 1
ans = 0
for i in range(10):
    for j in range(10):
        i_j = top_bottom_num[str(i)][str(j)]
        j_i = top_bottom_num[str(j)][str(i)]
        ans += i_j * j_i
print(ans)

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

ans=0
for i in range(1,n+1):
    l=list(str(i))
    if l[-1]!='0':
        r=l[::-1]
        if l==r:
            ans-=0
        al[int(l[0])-1][int(l[-1])-1]+=1
for i in range(9):
    for j in range(9):
        k=al[i][j]
        if i==j:
            ans+=k*k
        else:
            ans+=al[i][j]*al[j][i]
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)

N = int(input())

ret = 0
for i in range(1, N + 1) :
  l = str(i)[0]
  r = str(i)[-1]
  
  if r == '0' :
    continue
    
  if l == r and int(l) <= N :
    ret += 1
    
  for j in range(0, len(str(N)) - 2) :
    ret += pow(10, j)
  
  if str(N)[0] == r :
    if len(str(N)) >= 2 :
      ret += int(str(N)[1:]) // 10
      if int(str(N)[-1]) >= int(l) :
        ret += 1
  elif str(N)[0] > r :
    ret += pow(10, len(str(N)) - 2)
    
print(ret)

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

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

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

ANS=0
for i in range(10):
    for j in range(10):
        ANS+=BL[i][j]*BL[j][i]

print(ANS)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    cnt=[[0]*10 for _ in range(10)]
    for x in range(1,n+1):
        l=int(str(x)[0])
        r=x%10
        cnt[l][r]+=1
    ans=0
    for l in range(1,10):
        for r in range(1,10):
            ans+=cnt[l][r]*cnt[r][l]
    print(ans)

main()

Solution (Python):
N = int(input())
ans = 0
for i in range(1, N+1):
    s = str(i)
    L = len(s)-2
    l = int(s[0])
    r = int(s[-1])
    if r == 0:
        continue
    if l > r:
        ans+=(10**(L+1)-1)//9*2
    elif l<r:
        ans+=(10**L-1)//9*2
    else:
        if L<0:
            ans+=1
        else:
            ans+=(10**L-1)//9*2+2
            ans+=((i-int(s[0]+'0'*L+s[-1]))//10+1)*2-1
print(ans)

Solution (Python):
def g(n):
    D = [[0]*10 for i in range(10)]
    for i in range(1,n+1):
        si = str(i)
        D[int(si[0])][int(si[-1])] += 1

    res = 0
    for i in range(10):
        for j in range(10):
            res += D[i][j]*D[j][i]
    return res
print(g(int(input())))

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

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

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

    m[s][e]+=1

ans=0

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

print(ans-1)

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

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

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

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

print(ans)

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

list_n = [str(i) for i in range(n+1)] 
list_f = [ int(t[0]) for t in list_n]
list_e = [ int(t[-1]) for t in list_n]

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

for i in range(n):
    a[list_f[i+1]][list_e[i+1]] += 1
    b[list_e[i+1]][list_f[i+1]] += 1

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

print(ans)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

N = int(input())

def brute(N):
    ans = []
    for a in range(1, N + 1):
        for b in range(1, N + 1):
            a2 = str(a)
            b2 = str(b)
            if a2[0] == b2[-1] and a2[-1] == b2[0]:
                ans.append((a, b))

    return ans

def faster(N):
    arr = defaultdict(list)
    c = Counter()
    for n in range(1, N + 1):
        s = str(n)
        first = s[0]
        last = s[-1]
        arr[first + last].append(n)
        c[first + last] += 1

    ans = 0
    for key in c:
        key2 = key[-1] + key[0]
        ans += c[key] * c[key2]

    for n in range(11, 100, 11):
        s = str(n)
        ans -= len(arr[n]) * len(arr[n])

    return ans

print(faster(N))

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

def nasu(N):
  D = []
  t = N
  while t:
    D = [t % 10] + D
    t //= 10
  return len(D), D[0], D[-1]

L, M, P = nasu(N)
Q = (N - (M * (10 ** (L - 1))) - P) // 10
ans = 0
for i in range(1, N + 1):
  if i % 10 == 0: continue
  l, x, y = nasu(i)
  if x == y and x <= N:
    ans += 1
  if y < M:
    for j in range(L - 1):
      ans += 10 ** j
  elif y > M:
    for j in range(L - 2):
      ans += 10 ** j
  else:
    for j in range(L - 2):
      ans += 10 ** j
    t = y * 10 + x
    if t <= N:
      ans += 1
    if L >= 2:
      if x > P:
        ans += Q - 1
      else:
        ans += Q

print(ans)

Solution (Python):
def get_val(x):
    left = x
    while True:
        tmp = left // 10
        if tmp == 0:
            break
        left = tmp
    return left, x % 10


N = int(input())

counter = dict()
for x in range(1, N+1):
    key = get_val(x)
    if key not in counter:
        counter[key] = 0
    counter[key] += 1

res = 0
for i in range(1, N+1):
    left, right = get_val(i)
    if right == 0:
        continue
    if (right, left) in counter:
        res += counter.get((right, left))

print(res)

Solution (Python):
N = int(input())
ans = 0
for i in range(1,N+1):
    n = str(i)
    if n[-1]=="0":continue
    if n[0]==n[-1]:
        ans+=1
    for j in range(5):
        a = int(n[-1]+"0"*j+n[0])
        b = int(n[-1]+"9"*j+n[0])
        if b<=N:
            ans+=10**j
        elif a<=N and N<b:
            ans+=(N-a)//10+1
        else:break
print(ans)

Solution (Python):


s=input()
n=int(s)

d={}

for i in range(1,n+1):
	ts=str(i)
	hs=ts[0]+" "+ts[len(ts)-1]

	if(hs in d):
		d[hs]+=1
	else:
		d[hs]=1



ans=0

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

print(ans)

Solution (Python):
def f(n) : 
    while n >= 10:  
        n /= 10 
    return int(n)
n = int(input())
l = list(list(0 for i in range(10)) for j in range(10))
for i in range(1, n+1):
	last = i%10
	if last:
		l[f(i)][last] += 1
ans = 0
for i in range(10):
	for j in range(10):
		ans += l[i][j] * l[j][i]
print(ans)

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

A=[[0 for i in range(9)] for j in range(9)]
keta=1
for i in range(N+1):
  a=i%10
  if i>=keta*10:
    keta=keta*10
  b=i//(keta)
  
  if a!=0 and b!=0:
    A[a-1][b-1]+=1

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

Solution (Python):
n = int(input())
R = len(str(n))
def topend(i):
  return [int(str(i)[0]),int(str(i)[len(str(i))-1])]
cnt = [[0]*10 for i in range(10)]

for i in range(1,n+1):
  l = topend(i)
  if l[0]*l[1]:
    cnt[l[0]][l[1]] += 1
ans = 0
for i in range(1,10):
  ans += cnt[i][i]*cnt[i][i]
for i in range(1,9):
  for j in range(i+1,10):
    ans += cnt[i][j]*cnt[j][i]*2
print(ans)

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

ans = [[0 for i in range(14)]for j in range(15)]
for i in range(1,10):
    for j in range(1,10):
        for keta in range(1,8):
            if keta == 1:
                if i == j and i <= n:
                    ans[i][j] += 1
            elif keta == 2:
                if i*10 + j <= n:
                    ans[i][j] += 1
            else:
                mid = keta - 2
                nummin = i*10**(keta-1) + j
                if nummin > n:
                    continue
                nummax = nummin + 10**(keta-1) - 10
                if nummax <= n:
                    ans[i][j] += 10**mid
                else:
                    tmp = n - nummin
                    ans[i][j] += tmp//10 + 1

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

Solution (Python):
import collections
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
dic = collections.defaultdict(int)
for i in range(1, n + 1):
    s = str(i)
    a = s[0]
    b = s[-1]
    if b != '0':
        dic[(a, b)] += 1
res = 0
for i in range(1, 10):
    for j in range(1, 10):
        i = str(i)
        j = str(j)
        res += dic[(i, j)] * dic[(j, i)]
print(res)

Solution (Python):
N = int(input())
dic = {}
for i in range(1,10):
    for j in range(1,10):
        x = str(i) + str(j)
        dic[x] = 0

for i in range(1,N+1):
    a = i % 10
    for j in range(7):
        b  = i // 10 ** (6 - j)
        if b != 0 :
            break
    z = str(b) + str(a)
    if a != 0 and b != 0:
        dic[z] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        x = str(i) + str(j)
        y = str(j) + str(i)
        ans += dic[x] * dic[y]
print(ans)

Solution (Python):
def read():
    N = int(input().strip())
    return N,

def solve(N):
    table = [[0 for i in range(0, 10)] for j in range(0, 10)]
    for i in range(1, N+1):
        strn = str(i)
        head, tail = int(strn[0]), int(strn[-1])
        table[head][tail] += 1
    count = 0
    for i in range(1, N+1):
        strn = str(i)
        head, tail = int(strn[0]), int(strn[-1])
        count += table[tail][head]
    return count


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Solution (Python):
n=int(input())
ans=0
for i in range(n):
  I=str(i+1)
  s=I[0];o=I[-1]
  if o=="0":continue
  if o==s:
    ans+=1
  a=(n-int(s))//10
  
  for j in range(6):
    chk=int(o)*(10**j)
    if chk<=a:
      if chk+10**j<=a:ans+=10**j
      else:
        ans+=a-chk+1
print(ans)

Solution (Python):
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N = int(input())

d = dd(int)
res = 0
for i in range(1, N + 1):
  x = int(str(i)[0])
  y = int(str(i)[-1])
  d[x * (10 ** 6) + y] += 1
for i in range(1, N + 1):
  x = int(str(i)[0])
  y = int(str(i)[-1])
  res += d[y * (10 ** 6) + x]

print(res)