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):
import math
N = input()
d = list(map(int, list(N)))
N = int(N)

comb = [0] * 10 * 10
for i in range(1,N + 1):
  top = str(i)[0]
  bot = str(i)[-1]
  pair = 0
  pair = int(str(i)[0]) * 10 + int(str(i)[-1])
  comb[pair] += 1

ans = 0

for i, var in enumerate(comb):
  top = int(str(i)[0])
  bot = int(str(i)[-1])
  if top == bot:
    ans += var ** 2
  else:
    ans += comb[top * 10 + bot] * comb[top + bot * 10]
    

print(ans)

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

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

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

print(ans)

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

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def dfs(d, tight, str_N, pre):
    if d >= len(str_N) - 1:
        if int(pre) <= int(str_N[-1]):
            return 1
        else:
            return int(not tight)

    ans = 0
    if tight:
        for i in range(0, int(str_N[d]) + 1):
            ans += dfs(d + 1, str(i) == str_N[d], str_N, pre)
    else:
        for i in range(0, 10):
            ans += dfs(d + 1, False, str_N, pre)

    return ans


def solve():
    N = int(input())
    str_N = str(N)
    D = len(str_N)

    memo = [-1] * 10
    ans = 0
    for A in range(1, N + 1):
        a = str(A)
        pre = a[0]
        post = a[-1]

        if pre == "0" or post == "0":
            continue

        for d in range(1, D + 1):
            if d == 1:
                if pre == post:
                    ans += 1
            elif d != D:
                ans += 10 ** (d - 2)
            else:
                
                if post == str(N)[0]:
                    if memo[int(pre)] == -1:
                        memo[int(pre)] = dfs(1, True, str_N, pre)
                    ans += memo[int(pre)]
                elif int(post) < int(str_N[0]):
                    ans += 10 ** (d - 2)

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())


cnt = {}
for i in range(1,N+1):
    N_str = str(i)

    num = N_str[0]+N_str[-1]
    if N_str[-1] == '0':
        pass
    else:
        if num in cnt.keys():
            cnt[num] +=1
        else:
            cnt[num] = 1

ans = 0
for i in cnt.keys():
    try:
        cnt_inv = cnt[i[::-1]]
        ans += cnt[i] * cnt_inv
    except:
        pass
print(ans)

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

for i in range(1,n+1):
  x=int(str(i)[0])
  y=int(str(i)[-1])
  if x!=0 and y!=0:
    a[x-1][y-1]+=1
  
ans=0
  
for i in range(9):
  for j in range(9):
    if i!=j:
      
      ans+=a[i][j]*a[j][i]
      
    else:
      ans+=(a[i][i]*a[i][i])
      
print(ans)

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

Solution (Python):
from collections import deque

N = input()
digit = len(N)
f_digit = int(N[0])
l_digit = int(N[-1])
N = int(N)
cnt = 0

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

for i in range(1, 10):
    for j in range(1, 10):
        g_cnt[i][j] = 0
        if i == j:
            if i <= N:
                g_cnt[i][j] += 1
            if i*10+i <= N:
                g_cnt[i][j] += 1
        else:
            if i*10+j <= N:
                g_cnt[i][j] += 1

        if digit >= 3:
            num = deque([str(k) for k in range(10)])
            while num:
                q = num.popleft()
                for k in range(10):
                    if int(str(i)+q+str(k)+str(j)) <= N:
                        num.append(q+str(k))
                if int(str(i)+q+str(j)) <= N:
                    g_cnt[i][j] += 1

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

print(cnt)

Solution (Python):
from collections import Counter
from itertools import combinations
n = int(input().rstrip())

c = Counter()

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

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

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

print(ans)

Solution (Python):
N=int(input())
d={}
for i in range(1,10):
  for j in range(1,10):
    d[i*10+j]=0
for i in range(1,min(10,N+1)):
  d[i*10+i]=1
for i in range(11,N+1):
  i=str(i)
  h,t=int(i[0])*10,int(i[-1])
  if t!=0:
    d[h+t]+=1
ans=0
for i in range(1,9):
  for j in range(i+1,10):
    ans+=(d[i*10+j]*d[j*10+i])*2
for i in range(1,10):
  ans+=d[i*10+i]**2
print(ans)

Solution (Python):
N=int(input())
d={}
for i in range(1,10):
  for j in range(1,10):
    d[i*10+j]=0
for i in range(1,min(10,N+1)):
  d[i*10+i]=1
p,h,t,c=10,10,1,1
for i in range(11,N+1):
  if t==10:
    t=1
  else:
    d[h+t]+=1
    t+=1
  if p==c:
    h+=10
    c=0
  if h==100:
    h=10
    p*=10
  c+=1
ans=0
for i in range(1,9):
  for j in range(i+1,10):
    ans+=(d[i*10+j]*d[j*10+i])*2
for i in range(1,10):
  ans+=d[i*10+i]**2
print(ans)

Solution (Python):
n = input()

if int(n)<10:
    print(n)
    exit()

matubi=int(n[-1])
sentou=int(n[0])
keta=len(n)

if n[1:keta-1]=="":
    aida=0
else:
    aida=int(n[1:keta-1])

n=int(n)
def cnt(saisyo,saigo):
    kotae=(10**(keta-2)-1)/9
    if saisyo<sentou:
        kotae+=10**(keta-2)
    elif saisyo==sentou:
        tmp=saisyo*10**(keta-1)+saigo
        while True:
            if str(tmp)[0]==str(saisyo) and tmp<=n:
                kotae+=1
            else:
                break
            tmp+=10
    if saisyo==saigo:
        kotae+=1
    return kotae

ans=0

import itertools
for seq in itertools.product(range(1,10),range(1,10)):
    ans+=cnt(seq[0],seq[1])*cnt(seq[1],seq[0])

print(int(ans))

Solution (Python):
n=int(input())
s=[]
t=[]
for i in range(9):
    s.append([])
for i in range(100):
    t.append([[],[]])
for i in range(1,n+1):
    a=str(i)
    b=max(int(a[0]),int(a[len(a)-1]))
    c=min(int(a[0]),int(a[len(a)-1]))
    if b!=0 and c!=0:
        if b==c:
            s[b-1].append(i)
        elif int(a[0])<int(a[len(a)-1]):
            t[b*9+c][0].append(i)
        else:
            t[b*9+c][1].append(i)
ans=0
for i in range(9):
    x=len(s[i])
    ans+=x**2
for i in range(100):
    x=len(t[i][0])
    y=len(t[i][1])
    ans+=2*x*y
print(ans)

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

from collections import defaultdict
dic = defaultdict(int)

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

ans = 0
for key in dic.keys():
  a,b = key
  if (b,a) in dic:
    ans += dic[(a,b)] * dic[(b,a)]
  
print(ans)

Solution (Python):
import sys

def f(fl, i, j):
    return sum(1 for v in fl if v[0] == i and v[1] == j)


def first_last(N):
    for n in range(1, N + 1):
        last = n % 10

        while n >= 10:
            n, _ = divmod(n, 10)
        first = n
        yield first, last


def resolve(in_):
    N = int(next(in_).strip())
    fl = tuple(first_last(N))
    
    ans = sum(f(fl, i, j) * f(fl, j, i) for i in range(10) for j in range(10))
    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

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

c = Counter()
for i in range(1, N+1):
  a = str(i)
  c[(a[0], a[-1])] += 1

ans = 0
for k, v in c.items():
  ans += v*c[(k[1], k[0])]
print(ans)

Solution (Python):
N = int(input())
A=[0]*100
for i in range(1,N+1):
  first=int(str(i)[0])
  last=int(str(i)[-1])
  A[first*10+last]+=1
score=0
for i in range(10):
  for j in range(10):
    score+=A[i*10+j]*A[j*10+i]
print(score)

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

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
ans = 0
n_ketasuu = len(str(n))
sta_max = int(str(n)[0])
end_max = int(str(n)[n_ketasuu-1])
for i in range(1,n+1):
    sta = int(str(i)[len(str(i))-1])
    end = int(str(i)[0])
    if sta == 0:
        continue
    
    if sta == end:
        ans +=1
    
    if n_ketasuu >= 2 and sta*10 + end <= n:
        ans += 1
    
    if n_ketasuu > 3 or (n_ketasuu == 3 and sta < sta_max):
        ans += 10
    elif n_ketasuu == 3 and sta == sta_max:
        ans += int(str(n)[1:n_ketasuu-1])
        if end <= end_max:
            ans += 1
    
    if n_ketasuu > 4 or (n_ketasuu == 4 and sta < sta_max):
        ans += 100
    elif n_ketasuu == 4 and sta == sta_max:
        ans += int(str(n)[1:n_ketasuu-1])
        if end <= end_max:
            ans += 1
    
    if n_ketasuu > 5 or (n_ketasuu == 5 and sta < sta_max):
        ans += 1000
    elif n_ketasuu == 5 and sta == sta_max:
        ans += int(str(n)[1:n_ketasuu-1])
        if end <= end_max:
            ans += 1
    
    if n_ketasuu > 6 or (n_ketasuu == 6 and sta < sta_max):
        ans += 10000
    elif n_ketasuu == 6 and sta == sta_max:
        ans += int(str(n)[1:n_ketasuu-1])
        if end <= end_max:
            ans += 1

print(ans)

Solution (Python):
def main():
    n = int(input())
    dp = [[0 for i in range(10)] for i in range(10)]
    for i in range(1,n+1):
        h,t = int(str(i)[0]), int(str(i)[-1])
        dp[h][t] += 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):
N = int(input())
count = [0,1]
if N == 1:
  print(1)
  exit()
for i in range(2,N+1):
  digit = len(str(i))
  head = int(str(i)[0])
  tail = int(str(i)[-1])
  plus = 0
  if digit == 1:
    plus=1
  elif digit == 2:
    if head == tail:
      plus = 3
    elif head>tail and tail>0:
      plus = 2
  elif tail>0:
    for j in range(digit-2):
      plus += 10**j*2
    if head == tail:
      mid = str(i)[1:-1]
      while len(mid)>1 and mid[0]=='0':
        mid = mid[1:]
      mid = int(mid)
      plus += 2*mid+1
      plus += 2
    elif head > tail:
      plus += 10**(digit-2)*2
  count.append(count[-1]+plus)
print(count[N])

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

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

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

Solution (Python):
import sys
import math




def count(a, b, N):

    keta = len(str(N))

    cnt = 0

    
    if a == b:
        cnt += 1 if a <= N else 0

    if keta == 1:
        return cnt

    
    if keta == 2:
        cnt += 1 if b * 10 + a <= N else 0
        return cnt

    
    for k in range(2, keta):
        cnt += 10**(k - 2)

    
    s = N // (10**(keta - 1))
    t = N % 10
    if b > s:
        return cnt
    elif b < s:
        cnt += 10**(keta - 2)
    else:
        tempN = (N - s * (10**(keta - 1)) - t) // 10
        
        cnt += (tempN + 1)  
        if a > t:
            cnt -= 1

    return cnt


def count_aa(a, N):

    keta = len(str(N))

    cnt = 0

    
    if keta == 1:
        return 1 if a <= N[-1] else 0

    cnt += 1  

    
    if keta == 2:
        return cnt + 1 if a <= N[-1] else 0

    
    
    for k in range(2, keta):
        cnt += 10**(k - 2)

    
    s = N // (10**(keta - 1))
    t = N % 10
    if b > s:
        return cnt
    elif b < s:
        cnt += 10**(keta - 2)
    else:
        tempN = (N - s * (10**(keta - 1)) - t) // 10
        
        cnt += (tempN + 1)  
        if a > t:
            cnt -= 1

    return cnt


N = int(input())

c = 0
for i in range(1,N+1):
    a = int(str(i)[0])
    b = i % 10
    
    if b == 0:
        continue
    c += count(a,b,N)

print(c)

Solution (Python):
N = int(input())
results = {(str(i), str(j)): 0 for i in range(1, 10) for j in range(1, 10)}


for i in range(N+1):
    s = str(i)
    if (s[0], s[-1]) in results.keys():
        results[(s[0], s[-1])] += 1

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

print(cnt)

Solution (Python):
n = int(input())
d = [[0] * 10 for _ in range(10)]
for i in range(1, n + 1):
    d[int(str(i)[0])][i % 10] += 1
ret = 0
for s in range(10):
    for e in range(10):
        ret += d[s][e] * d[e][s]
print(ret)

Solution (Python):
n = int(input())
d = dict()
for i in range(1, n+1):
    t = i
    tail = t % 10
    while t >= 10:  
        t //= 10
    head = t
    d[(head, tail)] = d.get((head, tail), 0) + 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        temp = d.get((i,j), 0) * d.get((j,i), 0)
        
        ans += temp
print(ans)

Solution (Python):
N = int(input())
M = [0] * 100
for i in range(1, N+1):
    i = str(i)
    l, r = int(i[0]), int(i[-1])
    M[l*10+r] += 1
ans = 0
for i, cnt in enumerate(M):
    l, r = divmod(i, 10)
    ans += cnt * M[r*10+l]
print(ans)

Solution (Python):
n = int(input())
t = [0] * 100
for i in range(11, 100):
  msb = i // 10
  lsb = i % 10
  if lsb == 0:
    continue
  for j in range(1, n+1):
    if j % 10 == lsb and int(str(j)[0]) == msb:
      t[i] += 1

ans = 0
for i in range(11, 100):
  msb = i // 10
  lsb = i % 10
  if lsb == 0:
    continue
  if msb == lsb:
    ans += t[i] ** 2
  else:
    ans += t[i] * t[lsb * 10 + msb]
print(ans)

Solution (Python):
from collections import defaultdict
n = int(input())
d = defaultdict(int)
for i in range(1, n+1):
    if str(i)[-1] == '0':
        continue
    d[int(str(i)[0] + str(i)[-1])] += 1
ans = 0
for i in range(11, 100):
    ans += d[i] * d[int(str(i)[1] + str(i)[0])]
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
d = defaultdict(int)
for i in range(1, N+1):
    s = str(i)
    top = int(s[0])
    end = int(s[-1])
    d[(top, end)] += 1
counter = 0
for i in range(1, N+1):
    s = str(i)
    top = int(s[0])
    end = int(s[-1])
    if (end, top) in d:
        counter += d[(end, top)]
print(counter)

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

Solution (Python):
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)


input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    
    
    
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    
    return X.flatten().tolist()[0]

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        
        
        
        
        


def soinsu(n):
    ret = defaultdict(int)
    for i in range(2, int(math.sqrt(n) + 2)):
        if n % i == 0:
            while True:
                if n % i == 0:
                    ret[i] += 1
                    n //= i
                else:
                    break

    if not ret:
        return {n: 1}
    return ret


def solve():
    n = getN()

    sen = [defaultdict(int) for i in range(10)]
    ref = []
    for i in range(1, n+1):
        c = str(i)
        a, b = int(c[0]), int(c[-1])
        sen[a][b] += 1
        ref.append((a,b))

    
    ans = 0
    for a, b in ref:
        if a != b:
            if a in sen[b]:
                ans += sen[b][a]
        else:
            ans += sen[b][a]
        

    print(ans)







def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    
    solve()

Solution (Python):
def main():
    N = int(input())
    ans = 0
    def c(i, j):
        res = 0
        str_i = str(i)
        str_j = str(j)
        for k in range(1, N+1):
            if str(k)[0] == str_i and str(k)[-1] == str_j:
                res += 1
        return res

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

    print(ans)
main()

Solution (Python):
def main():
    N = int(input())
    ans = 0
    def c(i, j):
        res = 0
        str_i = str(i)
        str_j = str(j)
        for k in range(1, N+1):
            if str(k)[0] == str_i and str(k)[-1] == str_j:
                res += 1
        return res

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

    print(ans)
main()

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

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

ans = 0
for a in range(1, N + 1):
    s = str(a)
    head = int(s[0])
    tail = int(s[-1])
    ans += count[tail][head]
print(ans)

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

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

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

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):
def resolve():
    N = int(input())
    C = [[0 for _ in range(10)] for __ in range(10)]
    for i in range(1, N+1):
        c = str(i)
        C[int(c[0])][int(c[-1])] += 1
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += C[i][j] * C[j][i]
    print(ans)

if '__main__' == __name__:
    resolve()

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

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

a = input()

n = int(a)

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

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

ans = 0



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

print(ans)

Solution (Python):
def main():
    N = int(input())
    comb_count = [[0 for i in range(10)] for j in range(10)]
    res = 0

    for i in range(1, N + 1):
        s = str(i)
        max, min = int(s[0]), int(s[-1])
        comb_count[max][min] += 1

    for l in range(1, 10):
        for r in range(1, 10):
            res += comb_count[l][r] * comb_count[r][l]
        
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
memo = [[0] * 10 for i in range(10)]

def kazu(start_num,end_num):
    global memo
    if memo[start_num][end_num] > 0:
        
        return memo[start_num][end_num]
    else:
        ans = 0
        
        if start_num == end_num:
            if start_num <= n:
                
                ans += 1
        
        if start_num * 10 + end_num <= n:
            
            ans += 1
        
        for i in range(10):
            if start_num * 100 + i * 10 + end_num <= n:
                
                ans += 1
            else:
                break
        
        for i in range(100):
            if start_num * 1000 + i * 10 + end_num <= n:
                
                ans += 1
            else:
                break
        
        for i in range(1000):
            if start_num * 10000 + i * 10 + end_num <= n:
                
                ans += 1
            else:
                break
        
        for i in range(10000):
            if start_num * 100000 + i * 10 + end_num <= n:
                
                ans += 1
            else:
                break
        memo[start_num][end_num] = ans
        
        return ans

n = int(input())
str_n = str(n)
kotae = 0

for a in range(1,n+1):
    if a%10!=0:
        str_a = str(a)
        
        kotae += kazu(int(str_a[-1]),int(str_a[0]))
        
print(kotae)

Solution (Python):
memo = [[0] * 10 for i in range(10)]

def kazu(start_num,end_num):
    global memo
    if memo[start_num][end_num] > 0:
        
        return memo[start_num][end_num]
    else:
        ans = 0
        
        if start_num == end_num:
            if start_num <= n:
                
                ans += 1
        
        if start_num * 10 + end_num <= n:
            
            ans += 1
        
        for i in range(10):
            if start_num * 100 + i * 10 + end_num <= n:
                
                ans += 1
            else:
                break
        
        for i in range(100):
            if start_num * 1000 + i * 10 + end_num <= n:
                
                ans += 1
            else:
                break
        
        for i in range(1000):
            if start_num * 10000 + i * 10 + end_num <= n:
                
                ans += 1
            else:
                break
        
        for i in range(10000):
            if start_num * 100000 + i * 10 + end_num <= n:
                
                ans += 1
            else:
                break
        memo[start_num][end_num] = ans
        
        return ans

n = int(input())
str_n = str(n)
kotae = 0

for a in range(1,n+1):
    if a%10!=0:
        str_a = str(a)
        
        kotae += kazu(int(str_a[-1]),int(str_a[0]))
        
print(kotae)

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


def digit(i, j):
    """
    1 <= k <= N となる k で、先頭の桁が i , 末尾の桁が j となる物の数を返す
    """
    count = 0
    for k in range(1, N+1):
        if str(k)[0] == str(i) and k % 10 == j:
            count += 1
    return count


seen = {}
for i in range(1, 10):
    for j in range(1, 10):
        seen[(i, j)] = digit(i, j)

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


print(res)

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

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

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

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

print(ans)

Solution (Python):
N = int(input())
res = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, N + 1):
    if i % 10:  res[int(str(i)[0]) - 1][i % 10 - 1] += 1
ans = 0
for i in range(1, N + 1):
    if i % 10:  ans += res[i % 10 - 1][int(str(i)[0]) - 1]
print(ans)

Solution (Python):

N = int(input())

dp = [[0] * 10 for _ in range(10)]
for n in range(1, N + 1):
    
    i = n % 10

    
    while n >= 10:
        n //= 10
    j = n % 10

    dp[i][j] += 1

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

print(ans)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)


N = int(input())

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

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

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

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

ans=0

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

Solution (Python):
def solve():
    N = int(input())
    cnt = [[0]*10 for i in range(10)]
    for i in range(1, N+1):
        target_str = str(i)
        cnt[int(target_str[0])][int(target_str[-1])] += 1

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

if __name__ == "__main__":
    solve()

Solution (Python):
import sys
import collections
import bisect


def main():
    n = int(input())
    ac, bc = collections.Counter([str(i + 1)[0] + str(i + 1)[-1] for i in range(
        n)]), collections.Counter([str(i + 1)[-1] + str(i + 1)[0] for i in range(n)])
    print(sum(ac[i] * bc[i] for i in ac.keys()))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import collections
import bisect


def main():
    n = int(input())
    a = [str(i + 1)[0] + str(i + 1)[-1] for i in range(n)]
    b = [str(i + 1)[-1] + str(i + 1)[0] for i in range(n)]
    ac, bc = collections.Counter(a), collections.Counter(b)
    print(sum(ac[i] * bc[i] for i in ac.keys()))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import collections
import bisect


def main():
    n = int(input())
    a = [str(i + 1)[0] + str(i + 1)[-1] for i in range(n)]
    b = [str(i + 1)[-1] + str(i + 1)[0] for i in range(n)]
    ac, bc = collections.Counter(a), collections.Counter(b)
    acKeys = ac.keys()
    bcKeys = bc.keys()
    cnt = 0
    for i in acKeys:
        cnt += ac[i] * bc[i]
    print(cnt)


if __name__ == '__main__':
    main()

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

for i in range(1, n+1):
    if i >= 10**5:
        tmp1 = i//10**5
    elif i >= 10**4:
        tmp1 = i//10**4
    elif i >= 10**3:
        tmp1 = i//10**3
    elif i >= 10**2:
        tmp1 = i//10**2
    elif i >= 10:
        tmp1 = i//10
    else :
        tmp1 = i
    tmp2 = i % 10
    a[tmp1][tmp2] += 1

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

Solution (Python):
def main():

    N = int(input())

    d = dict()
    for i in range(1, N+1):
        u = int(str(i)[0])
        v = int(str(i)[-1])
        d[(u, v)] = d.get((u, v), 0) + 1

    ans = 0
    for u, v in d:
        if (v, u) in d:
            ans += d[(u, v)] * d[(v, u)]
    return ans


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

Solution (Python):

n = int( input() )

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


for k in range( n + 1 ):
    head = int( str( k )[ 0 ] )
    foot = int( str( k )[ -1 ] )
    cnt[ head ][ foot ] += 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())

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)