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

dic = {}
for i in range(1, 10):
  for j in range(1, 10):
    dic[(str(i), str(j))] = 0

for i in range(1, n+1):
  if i % 10 == 0:
    continue
  front = str(i)[0]
  back = str(i)[-1]
  dic[(front, back)] += 1
ans = 0
for i in range(1, 10):
  for j in range(1, 10):
    ans += dic[(str(i), str(j))] * dic[(str(j), str(i))]
print(ans)

Solution (Python):
import math

def main():
	N = int(input())
	c = [[0] * 10 for i in range(10)]
	for i in range(1, N + 1):
		digit = int(math.log10(i)) + 1
		c[i // (10 ** (digit - 1))][i % 10] += 1

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

	print(cnt)



main()

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

Solution (Python):
N = int(input())
ptn = [[0 for _ in range(10)] for __ in range(10)]
for num in range(1, N+1):
  str_num = str(num)
  if str_num[-1] != 0:
    ptn[int(str_num[0])][int(str_num[-1])] += 1
answer = 0
for i in range(10):
  for j in range(i, 10):
    if i == j:
      answer += ptn[i][j]**2
    else:
      answer += ptn[i][j]*ptn[j][i]*2
print(answer)

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

n = int(input())

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

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

print(ans)

Solution (Python):
from collections import defaultdict

d1 = defaultdict(int)
d2 = defaultdict(int)

n = int(input())

for i in range(1,n+1):
    si = str(i)
    f = si[0]
    l = si[-1]
    if f == l:
        d1[(f,l)]+=1
    else:
        d2[(f,l)]+=1

ans = 0
for v in d1.values():
    if v == 1:
        ans += 1
    else:
        ans += v**2

for v in list(d2.keys()):
    ans += d2[v]*d2[(v[1],v[0])]*2
    d2[v] = 0

print(ans)

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

Solution (Python):

def main():
    import numpy as np

    N = int(input())

    cnt = np.zeros((10, 10), dtype=np.int64)
    for n in map(str, range(1, N + 1)):
        cnt[int(n[0])][int(n[-1])] += 1

    print(np.sum(cnt * cnt.T))


if __name__ == '__main__':
    main()

Solution (Python):

def main():
    import numpy as np

    N = int(input())

    cnt = np.zeros((10, 10), dtype=np.int64)
    for n in [str(x) for x in range(1, N + 1)]:
        cnt[int(n[0])][int(n[-1])] += 1

    print((cnt[:, :] * cnt.T[:, :]).sum())


if __name__ == '__main__':
    main()

Solution (Python):
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

def main():
    n = int(input())
    ans = 0
    for i in range(1, n + 1):
        s = str(i)
        if s[-1] == '0':
            continue
        if s[0] == s[-1]:
            ans += 1
        if i < 10:
            continue
        if s[0] == s[-1]:
            ans += 2
        l = len(s)
        for i in range(2, l):
            ans += 10 ** (i - 2) * 2
        if s[0] < s[-1]:
            continue
        if s[0] > s[-1]:
            ans += 10 ** (l - 2) * 2
        elif l > 2:
            dp = int(s[1])
            for i in range(2, l - 1):
                dp *= 10
                dp += int(s[i])
            ans += dp * 2
    print(ans)

main()

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    l = [[0]*10 for i in range(10)]
    for i in range(1,n+1):
        s = str(i)
        head = int(s[0])
        tail = int(s[-1])
        l[head][tail] += 1
    ans = 0

    for i in range(1,10):
        for j in range(i,10):
            if i == j:
                ans += l[i][j]**2
            else:
                one = l[i][j]
                two = l[j][i]
                ans += one*two*2
    print(ans)



if __name__=="__main__":
    main()

Solution (Python):
def main():
    N = int(input())
    dp = [[0] * 10 for _ in range(10)]
    for i in range(1,N+1):
        tmp = str(i)
        left = int(tmp[0])
        right = int(tmp[-1])
        if left == 0 or right == 0:
            continue
        dp[left][right] += 1

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict

n = int(input())
ns = defaultdict(int)

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

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

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

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

Solution (Python):
N=int(input())
m=0
x=[[0]*9 for i in range(9)]
for i in range(1,N+1):
    if i%10==0:
        continue
    b=int(str(i)[0])
    g=int(i%10)
    x[b-1][g-1]+=1
ans=0
for i in range(9):
    for j in range(9):
        ans+=x[i][j]*x[j][i]
        
print(ans)

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

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

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

print(ans)

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

for i in range(1,min(10,n+1),1):
    l[i][i]+=1

if len(str(n))>=2:
    for k in range(2,len(str(n)),1):
        for i in range(1,10,1):
            for j in range(1,10,1):
                l[i][j]+=10**(k-2)

    for i in range(1,int(str(n)[0]),1):
        for j in range(1, 10, 1):
            l[i][j] += 10**(len(str(n))-2)
    if len(str(n))>=3:
        for j in range(1, 10, 1):
            if j<=int(str(n)[-1]):
                l[int(str(n)[0])][j]+=int(str(n)[1:-1])+1
            else:
                l[int(str(n)[0])][j] += int(str(n)[1:-1])
    else:
        for j in range(1, 10, 1):
            if j<=int(str(n)[-1]):
                l[int(str(n)[0])][j]+=1

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

print(t)

Solution (Python):
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))

N=intput()
C=[[0]*10 for _ in range(10)]
for x in range(1,N+1):
    r=x%10
    t=int(str(x)[0])
    if r!=0:
        C[t][r]+=1
ans=0
for t in range(1,10):
    for r in range(1,10):
        ans+=C[t][r]*C[r][t]
print(ans)

Solution (Python):
import numpy as np
N = int(input())
list_all =np.zeros((9,9))
num_list = [str(i) for i in range(1,N+1)]
overlap = [0 for i in range(9)]
for i in range(N):
    head = int(num_list[i][0])
    length = len(num_list[i])
    tail = int(num_list[i][length -1])
    if (head == 0) or (tail == 0):
        continue
    list_all[head-1,tail -1] += 1
sum1 = 0
for i in range(9):
    for j in range(i+1):
        if i ==j:
            sum1 += (list_all[i,j] * list_all[j,i])
        else:
            sum1 += (list_all[i,j] * list_all[j,i]) *2
print(int(sum1))

Solution (Python):
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 main():
    mod=10**9+7
    N=I()
    
    def ht(x):
        t=x%10
        string=str(x)
        h=int(string[0])
        return (h,t)
        
    cnt=[[0]*10 for _ in range(10)]
    
    for i in range(1,N+1):
        h,t=ht(i)
        cnt[h][t]+=1
        
    ans=0
    for i in range(10):
        for j in range(10):
            a=cnt[i][j]
            b=cnt[j][i]
            ans+=a*b
            
    print(ans)
        

main()

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





def func(i,j,N):
    ans = []
    for x in range(1,N+1):
        x_ = str(x)
        if x_[0] == str(i) and x_[-1] == str(j):
            ans += [x]
    return ans





ans = 0
for i in range(10):
    for j in range(10):
        ans += len(func(i,j,N))*len(func(j,i,N))
print(ans)

Solution (Python):
N=int(input())
n=len(str(N))
res=0
for a in range(1,N+1):
    a0=str(a)[0]
    a1=str(a)[-1]
    if a0=='0' or a1=='0':
        a_res=res
        continue
    for i in range(1,n+1):
        if i==1:
            if a0==a1:
                res+=1
        elif i==2:
            if 10*int(a1)+int(a0)<=N:
                res+=1
        elif i==n:
            n0=str(N)[0]
            n1=str(N)[-1]
            if n0<a1:
                res+=0
            elif n0>a1:
                res+=10**(i-2)
            else:
                res+=(N-int(a1)*10**(i-1))//10+1
                if n1<a0:
                    res-=1
        else:
            res+=10**(i-2)
print(res)

Solution (Python):

import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys


readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N):
    c = Counter()
    for i in range(1, N+1):
        s = str(i)
        c[int(s[0])*10+int(s[-1])] += 1

    ans = 0
    for i, j in product(range(1, 10), repeat=2):
        ans += c[10*i+j] * c[10*j+i]

    return ans


def main():
    N = read_int()
    print(slv(N))


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys


readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def ep(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        ep(e - s, 'sec')
        return ret

    return wrap


class Bisect:
    def __init__(self, func):
        self.__func = func

    def bisect_left(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if self.__func(mid) < x:
                lo = mid+1
            else:
                hi = mid
        return lo

    def bisect_right(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if x < self.__func(mid):
                hi = mid
            else:
                lo = mid+1
        return lo

@mt
def slv(N):
    c = defaultdict(list)
    for i in range(1, N+1):
        s = str(i)
        c[int(s[0])*10+int(s[-1])].append(s)
    ans = 0
    for i, j in product(range(1, 10), repeat=2):
        a = len(c[10*i+j])
        b = len(c[10*j+i])
        ans += a * b

    return ans


def main():
    N = read_int()
    print(slv(N))


if __name__ == '__main__':
    main()

Solution (Python):
import sys



def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    d = {}
    
    
    for a in range(1, 10):
        for b in range(1, 10):
            cnt = 0
            
            if a == b and a <= n:
                cnt += 1
            
            i = 1
            while True:
                _min = a * (10**i) + b  

                _max = a * (10**i) + b
                _i = i - 1
                while _i > 0:
                    _max += 9 * (10**_i)  
                    _i -= 1
                if _max <= n:
                    cnt += 10**(i - 1)
                    i += 1
                    continue
                elif _min > n:
                    break
                else:  
                    cnt += ((n - _min) // 10) + 1
                    break
            d[10 * a + b] = cnt
    ans = 0
    for a in range(1, 10):
        for b in range(1, 10):
            ans += d[a * 10 + b] * d[b * 10 + a]
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
import sys



def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    d = {}
    for a in range(1, 10):
        for b in range(1, 10):
            cnt = 0
            
            if a == b and a <= n:
                cnt += 1
            
            i = 1
            while True:
                _min = a * (10**i) + b  

                _max = a * (10**i) + b
                _i = i - 1
                while _i > 0:
                    _max += 9 * (10**_i)  
                    _i -= 1
                if _max <= n:
                    cnt += 10**(i - 1)
                    i += 1
                    continue
                elif _min > n:
                    break
                else:  
                    cnt += ((n - _min) // 10) + 1
                    break
            d[10 * a + b] = cnt
    ans = 0
    for a in range(1, 10):
        for b in range(1, 10):
            ans += d[a * 10 + b] * d[b * 10 + a]
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
cnt = [[0]*9 for _ in range(9)]
for i in range(1,n+1):
    s = str(i)
    if s[-1]!="0":
        cnt[int(s[0])-1][int(s[-1])-1] += 1
ans = 0
for i in range(9):
    for j in range(9):
        ans += cnt[i][j]*cnt[j][i]
print(ans)

Solution (Python):

import sys
from itertools import chain


def solve(N: int):
    m = [0] * 100
    for n in range(N + 1):
        s = str(n)
        h = int(s[0])
        l = int(s[-1])
        if l != 0:
            m[h * 10 + l] += 1

    answer = 0
    for h in range(1, 10):
        for l in range(1, 10):
            answer += m[h * 10 + l] * m[l * 10 + h]
    return answer


def main():
    N = int(input())
    answer = solve(N)
    print(answer)


if __name__ == "__main__":
    main()

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

cnt = np.zeros((10,10))

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

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

print(int(ans))

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

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

for i in range(1,N+1):
    x = i
    while x >= 10:
        x //= 10
    cnt[x][i%10] += 1

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

print(ans)

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

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

for i in range(1,N+1):
  if int(str(i)[len(str(i))-1])==0:
    continue
  table[int(str(i)[0])-1][int(str(i)[len(str(i))-1])-1]+=1

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

Solution (Python):
N=int(input())
A=[str(i)for i in range(1,10)]
ans=0
for a in A:
    for b in A:
        countA=0
        if a==b and int(a)<=N:
            countA+=1
        for i in range(5):
            m=int(a+'0'*i+b)
            M=int(a+'9'*i+b)
            if m<=N:
                if M<=N:
                    countA+=10**i
                else:
                    while m<=N:
                        countA+=1
                        m+=10

        countB=0
        if b==a and int(b)<=N:
            countB+=1
        for i in range(5):
            m=int(b+'0'*i+a)
            M=int(b+'9'*i+a)
            if m<=N:
                if M<=N:
                    countB+=10**i
                else:
                    while m<=N:
                        countB+=1
                        m+=10

        ans+=countA*countB
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())

ans = 0
for i in range(1, N+1):
    a = list(map(int, str(i)))
    x = a[0]
    y = a[-1]
    if x == y:
        ans += 1
    if y != 0:
        for j in (5, 4, 3, 2, 1):
            z = (y+1)*(10**j)
            if z <= N:
                ans += 10**(j-1)
            elif y*(10**j) + x <= N and N < z:
                s = N-y*(10**j)
                ans += int(s/10)
                if N%10 >= x:
                    ans += 1

print(ans)

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

head_tail = defaultdict(int)
for i in range(1, N + 1):
    tail = i % 10
    head = int(str(i)[0])
    head_tail[(head, tail)] += 1

answer = 0
for i in range(1, N + 1):
    head = int(str(i)[0])
    tail = i % 10
    answer += head_tail[(tail, head)]
print(answer)

Solution (Python):
n = int(input())
dic = {}
def is_ok(a,b,p,m,n):
    
    d = a*(10**(m-1)) + p*10 + b
    if a*(10**(m-1)) + p*10 + b <= n:
        return True
    return False
def meguru_bisect(ng, ok):
    '''
    初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
    まずis_okを定義すべし
    ng ok は  とり得る最小の値-1 とり得る最大の値+1
    最大最小が逆の場合はよしなにひっくり返す
    '''
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(i,j,mid,m,n):
            ok = mid
        else:
            ng = mid
    return ok
for i in range(1,10):
    for j in range(1,10):
        ans = 0
        if i == j and i <= n:
            ans += 1
        if 10*i+j <= n:
            ans += 1
        if len(str(n)) >= 3:
            m = len(str(n))
            ans += (10**(m-2)-10)//9
            ng = 10**(m-2)
            ok = -1
            c = meguru_bisect(ng, ok)
            ans += c + 1
        dic[10*i+j] = ans
cnt = 0
for k in range(1,10):
    for l in range(1,10):
        cnt += dic[10*k+l]*dic[10*l+k]
print(cnt)

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

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

for i in range(1, N+1):
    i_str = str(i)
    cnt[int(i_str[0])][int(i_str[-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())
c=[0 for i in range(100)]

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

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

Solution (Python):
N=int(input())
l=[[0]*10 for i 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(10):
  for j in range(10):
    ans+=l[i][j]*l[j][i]
  
print(ans)

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

for i in range(1, N+1):
    s = len(str(i))
    if s == 1:
        l[i][i] += 1
    else:
        l[i//(10**(s-1))][i%10] += 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())
K = str(N)
l = len(str(N))

ans = 0

def tp(i,j,p):
    tmp = 0
    if p >= 2:
        if l == p:
            if i == int(K[0]):
                if j > int(K[l-1]):
                    tmp = (N-int(K[0])*10**(l-1)-int(K[l-1]))//10
                else:
                    tmp = (N-int(K[0])*10**(l-1)-int(K[l-1]))//10+1
            elif i >= int(K[0]):
                tmp = 0
            else:
                tmp = 10**(p-2)
        elif l > p:
            tmp = 10**(p-2)
        return tmp
    else:
        if i == j and i <= N:
            return 1
        else:
            return 0


for i in range(1,10):
    for j in range(1,10):
        for p in range(1,l+1):
            for q in range(1,l+1):
                ans += tp(i,j,p)*tp(j,i,q)

print(ans)

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

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N = int(readline())
    dp = [[0] * 10 for _ in range(10)]

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

        ans += 2 * dp[second][first]

        if first == second:
            ans += 1

        dp[first][second] += 1

    print(ans)


if __name__ == '__main__':
    main()

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

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

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

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

print(count)

Solution (Python):
import sys









MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_ints2(x):   return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)

def f(num):
    front = num
    back = num
    while front >= 10:
        front //= 10
    while back >= 10:
        back %= 10
    
    return front, back

def Main():
    n = read_int()
    cnt = [[0] * 10 for _ in range(10)]
    for x in range(1, n + 1):
        front, back = f(x)
        cnt[front][back] += 1
    
    ans = 0
    for x in range(10):
        for y in range(10):
            ans += cnt[x][y] * cnt[y][x]
    print(ans)

if __name__ == '__main__':
    Main()

Solution (Python):
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
Comlist = []
Ans = dict()
Answer = 0
for i in range(1,10):
    for j in range(1,10):
        M = i*10 + j
        Comlist.append(str(M))
        Ans[str(M)] = 0
for k in range(1,N+1):
    strk = str(k)
    if k < 10:
        Tar = strk + strk
        Ans[Tar] += 1
    else:
        Tar = strk[0] + strk[-1]
        if Tar in Ans:
            Ans[Tar] += 1
Answer = 0
for q in range(len(Comlist)):
    p = Comlist[q]
    pin = p[1] + p[0]
    Answer += Ans[p] * Ans[pin]
print(Answer)

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

l = [[0]*10 for i in range(10)]
for i in range(1, N+1):
  x = int(str(i)[0])
  y = int(str(i)[-1])
  l[x][y] += 1
  
ans = 0
for i in range(1, 10):
  for j in range(i, 10):
    if i == j:
      ans += l[i][j] * l[j][i]
    else:
      ans += 2 * l[i][j] * l[j][i]
print(ans)

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

for n in range(1,N+1):
  i=int(str(n)[0])
  j=int(str(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):
n = int(input())
a = [[0 for i in range(10)] for j in range(10)]
for i in range(1, n+1):
    s = str(i)
    if s[-1] != '0':
        a[int(s[0])][int(s[-1])] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += a[i][j] * a[j][i]
print(ans)

Solution (Python):
N = int(input())
A = [[0 for i in range(9)] for j in range(9)]
i = 92838
for i in range(1, N+1):
  if i % 10 != 0:
    A[int(str(i)[0])-1][i % 10 - 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):
import math

N = int(input())
N_log = math.floor(math.log(N, 10))
N_top = N // pow(10, N_log)
N_bottom = N % 10
N_rest = N % pow(10, N_log)


def n_b(b0, b1):
    if b0 == 0:
        return 0
    ret = 0
    for keta in range(N_log + 1):
        ret += n_b_keta(b0, b1, keta)
    return ret


def n_b_keta(b0, b1, keta):
    if keta == 0:
        return b0==b1
    elif keta != N_log:
        return pow(10, keta - 1)
    else:
        if b0 > N_top:
            return 0
        elif b0 < N_top:
            return pow(10, keta - 1)
        else:
            return 1 + (N_rest//10) - (b1>N_bottom)

ans = 0
pre = 0
for i in range(1, N + 1):
    ans += n_b(int(str(i)[-1]), int(str(i)[0]))
    
    pre = ans
print(ans)

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

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

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

import numpy as np

graph = np.array(graph)

print(np.trace(np.dot(graph, graph)))

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

Solution (Python):

import sys
import collections as cl


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 main():
    N = II()
    items = [[0 for i in range(9)]for j in range(9)]

    for i in range(N+1):
        start = int(str(i)[0])
        end = int(str(i)[-1])
        if end == 0:
            continue
        items[start - 1][end - 1] += 1

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

    print(ans)


main()

Solution (Python):
L = [[0]*10 for _ in range(10)]
S = 0
for i in range(1, int(input())+1):
  L[int(str(i)[0])][int(str(i)[-1])] += 1
for j in range(9):
  for i in range(j+1, 10):
    S += 2*L[j][i]*L[i][j]
for i in range(10):
  S += L[i][i]**2
print(S)