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



Sample input:
25


Sample output:
17

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())

nums = []

for i in range(N):
    nums.append(i+1)

balls = []
for i in itertools.permutations(nums, 2):
    balls.append(i)

for i in range(N):
    balls.append((i+1, i+1))

count = 0
for ball in balls:
    a, b = ball
    if (str(a)[-1] == str(b)[0]) and (str(a)[0] == str(b)[-1]):
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

N = int(input())
C = product(range(1,N+1), repeat=2)
print(len([c for c in C if str(c[0])[0] == str(c[1])[-1] and str(c[1])[0] == str(c[0])[-1]]))

Submission status: Time Limit Exceeded

Solution code:
s_list = int(input())

f_list = []

for x in range(1, s_list+1):
	for y in range(1, s_list+1)[::-1]:
		if str(x)[0] == str(y)[-1] and str(y)[0] == str(x)[-1] and (x, y) not in f_list:
			f_list.append((x, y))
		elif str(x)[0] == str(y)[-1] and str(y)[0] == str(x)[-1] and (y, x) not in f_list:
			f_list.append((y, x))

print(len(f_list))

Submission status: Time Limit Exceeded

Solution code:
def main():
 n=int(input())
 N=[i for i in range(1,n+1)]

 count=0
 for a in N:
   for b in N:
     if str(a)[0]==str(b)[-1] and str(a)[-1]==str(b)[0]:
       count+=1

 print(count)

if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def check(src, numA, count):
    for i in range(1, src+1):
        preA = numA[0:1]
        sufA = numA[-1]

        numB = str(i)
        preB = numB[0:1]
        sufB = numB[-1]

        if sufA == preB and preA == sufB:
            count = count + 1
    return count

count = 0
for i in range(1, N+1):
    num = str(i)
    count = check(N, num, count)

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

result = 0
target_list = [i for i in range(1, n + 1) if i % 10 > 0]
for a in target_list:
    for b in target_list:
        head_a = str(a)[0]
        tail_a = str(a)[-1]
        head_b = str(b)[0]
        tail_b = str(b)[-1]
        if head_a == tail_b and tail_a == head_b:
            result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
cnt=0
if n==1:
  print(1)
else:
  for i in range(1,n+1):
    for j in range(1,n+1):
      I = str(i)
      J = str(j)
      if I[0]==J[-1] and I[-1]==J[0]:
        #print(I,J)
        cnt+=1
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
R = 0
for A in range(N):
    A = str(A + 1)
    for B in range(N):
        B = str(B + 1)
        if A[-1] == B[0] and B[-1] == A[0]:
            R += 1
print(R)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = len(str(N))
ans = 0
for n in range(1,N+1):
    first = str(n)[0]
    second = str(n)[-1]
    for m in range(1, N+1):
        _first = str(m)[-1]
        _second = str(m)[0]
        if first == _first and second == _second:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
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]
        # print(a, ans)
        continue
    
    patterns = 0
    for b in range(1, n+1):
        x2 = int(str(b)[0])
        y2 = b % 10
        if x == y2 and y == x2:
            patterns += 1
    memo[x][y] = patterns
    ans += patterns
    # print(a, ans)

    # patterns = 0
    # # 1桁
    # if x == y and n <= x:
    #     patterns += 1
    
    # for k in range(keta-1):

print(ans)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())
ans = 0

x = []
y = []

for i in range(1,n+1):
    for j in range(1,n+1):
        x = list(str(i))
        y = list(str(j))
        if x[0] == y[-1] and x[-1] == y[0]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
count = 0
for i in range(1, n+1 % 2):
  for j in range(1, n+1 % 2):
    if str(i)[0] == str(j)[-1]:
      if str(j)[0] == str(i)[-1]:
        count += 1
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())

count = 0
for nn1 in range(1, num + 1):
  for nn2 in range(1, num + 1):
    n1 = str(nn1).lstrip('0')
    n2 = str(nn2).lstrip('0')
    if n1[0] == n2[-1] and n2[0] == n1[-1]:
      count += 1
      
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
count = 0
for i in range(1, n+1):
  for j in range(1, n+1):
    if str(i)[0] == str(j)[-1]:
      if str(j)[0] == str(i)[-1]:
        count += 1
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
count = 0
for i in range(1, N + 1):
    for j in range(1, N + 1):
        if str(i)[0] == str(j)[-1] and str(i)[-1] == str(j)[0]:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
import calendar
import fractions
import itertools

n = int(input())

ans = 0

for i in range(1, n + 1):
    for j in range(1, n + 1):
        a = str(i)
        b = str(j)
        if a[0] == b[-1] and a[-1] == b[0]:
            ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(i+1,j+1,x[i][j])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
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()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
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()

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


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 = {}
    # n以下のaで始まり、bで終わる整数のそれぞれ求める
    # 計算量: O(log_n)
    for a in range(1, 10):
        for b in range(1, 10):
            cnt = 0
            # 1桁の整数が条件を満たすケース
            if a == b and a <= n:
                cnt += 1
            # 2桁以上の整数で条件を満たすケース
            i = 1
            while True:
                _min = a * (10**i) + b  # 整数が a0000b

                _max = a * (10**i) + b
                _i = i - 1
                while _i > 0:
                    _max += 9 * (10**_i)  # 整数が a999b
                    _i -= 1
                if _max <= n:
                    cnt += 10**(i - 1)
                    i += 1
                    continue
                elif _min > n:
                    break
                else:  # a0..0b <= n < a9..9b
                    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()

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


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
            # 1桁の整数が条件を満たすケース
            if a == b and a <= n:
                cnt += 1
            # 2桁以上の整数で条件を満たすケース
            i = 1
            while True:
                _min = a * (10**i) + b  # 整数が a0000b

                _max = a * (10**i) + b
                _i = i - 1
                while _i > 0:
                    _max += 9 * (10**_i)  # 整数が a999b
                    _i -= 1
                if _max <= n:
                    cnt += 10**(i - 1)
                    i += 1
                    continue
                elif _min > n:
                    break
                else:  # a0..0b <= b < a9..9b
                    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()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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]

#for i in range(10):
#    print(l[i])

print(ans)