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

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
#import numpy as np
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
#S = str(input())

N = int(input())

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

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

print (ans)

Submission status: Accepted

Solution code:
n = int(input())

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

c = [[0 for _ in range(9)] for _ in range(9)]
for  i in range(1, n+1):
    first_digit = str(i)[0]
    last_digit = str(i)[-1]
    if first_digit == '0' or last_digit == '0':
        continue
    c[int(first_digit)-1][int(last_digit)-1] += 1
ans = 0
for i in range(1,10):
    for j in range(1, 10):
        ans += c[i-1][j-1]* c[j-1][i-1]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
count = 0
c = [[0] * 10 for i in range(10)]
for num in range(N):
    s = str(num+1)
    start = int(s[0])
    end = int(s[len(s)-1])
    c[start][end] += 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:
n=int(input())
cnt=[[0 for i in range(10)] for j in range(10)]

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

ans=0
for h in range(0,10):
  for t in range(0,10):
    ans+=cnt[h][t]*cnt[t][h]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
ce = [[0] * 10 for i in range(10)]
for i in range(1,n+1):
    tmp = str(i)
    a = int(tmp[0]); b = int(tmp[-1])
    ce[a][b] += 1
res = 0
for i in range(10):
    for j in range(10):
        res += ce[i][j] * ce[j][i]
print(res)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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



print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = 0
for i in range(1, n+1):
    t = str(i)
    ans += counter[(t[-1], t[0])]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

ab = {}
for i in range(1, n+1):
    t = str(i)
    if (t[0], t[-1]) in ab:
        ab[(t[0], t[-1])] += 1
    else:
        ab[(t[0], t[-1])] = 1
#print(ab)
ans = 0
for i in range(1, n+1):
    t = str(i)
    if (t[-1], t[0]) in ab:
        ans += ab[(t[-1], t[0])]
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())

    total = 0
    data = [[0] * 9 for i in range(9)]
    for i in range(1, n + 1):
        str_num = str(i)
        top = str_num[0]
        bottom = str_num[-1]
        if bottom == '0':
            continue
        else:
            data[int(top) - 1][int(bottom) - 1] += 1

    for i in range(9):
        for j in range(9):
            total += (data[i][j] * data[j][i])

    print(total)

Submission status: Accepted

Solution code:
n = int(input())
key = [[0]*9 for i in range(9)]

ans = 0
for v in range(1, n+1):
  l = str(v)
  if v%10 == 0:
    continue
  k1, k2 = int(l[0]), int(l[-1])
  key[k1-1][k2-1] += 1
  ans += key[k2-1][k1-1]*2
  for i in range(len(l)):
    if l[i] != l[-1-i]:
      continue
    if i == len(l)-1:
      ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
max_l = len(str(N))
cnt = 0
for n in range(1, N+1):
    str_n = str(n)
    f_n, l_n = str_n[0], str_n[-1]
    if(f_n == '0' or l_n == '0'):
        continue
    if(f_n == l_n):
        cnt += 1
    i = 0
    while(int(l_n +'0'*i + f_n)<=N):
        cnt += min((N-int(l_n +'0'*i + f_n))//10 + 1, 10**i)
        i += 1

print(cnt)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    c = [[0] * 10 for i in range(10)]
    for i in range(n+1):
        b = i % 10
        for j in range(6):
            if i//(10**(j+1)) == 0:
                a = i//(10**j)
                break
        c[a][b] += 1
    ans = 0
    for i in range(1,10):
        for j in range(1,10):
            ans += c[i][j] * c[j][i]
    print(ans)
resolve()

Submission status: Accepted

Solution code:
N = int(input())
count =[[0 for x in range(10)] for z in range(10)]
for x in range(1,N+1):
    F = str(x)[0]
    E = str(x)[-1] 
    count[int(F)][int(E)] +=1
ans = 0
for x in range(10):
    for y in range(10):
        ans += count[x][y] *count[y][x]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import math

N = int(input())

c = np.zeros((10, 10), dtype=int)
d = np.zeros((10, 10), dtype=int)

for n in range(1, N+1):
    k = math.floor(math.log10(n)) + 1
    #print('桁数: ', k)
    i = n// (10 ** (k-1))
    #print('1番大きな桁の数: ', i)
    j = n%10
    #print('1の位の数: ', j)
    c[i][j] += 1
    d[j][i] += 1

ans = np.sum(c*d)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lis = [[0] * 10 for i in range(10)]
for i in range(1, n+1):
    val = list(str(i))
    lis[int(val[-1])][int(val[0])] += 1
#     print(val[-1],val[0])
#     print(val,lis)
    
ans = 0
for i in range(10):
    for j in range(10):
        ans += lis[i][j] * lis[j][i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ans = 0

"""
def c_i_j(n, i, j):
    counter = 0
    for num in range(1, n + 1):
        if int(str(num)[0]) == i and int(str(num)[-1]) == j:
            counter += 1
    return counter

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

print(ans)
"""
table = list([0] * 10 for i in range(10))

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

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

print(ans)

Submission status: Accepted

Solution code:
#d
n=int(input())

c=[[0]*9 for _ in range(9)]
for i in range(1,n+1):
    stri = str(i)
    head = int(stri[0])
    tail = int(stri[-1])
    if tail!=0:
        c[head-1][tail-1]+=1

print( sum(c[i][j] * c[j][i] for i in range(9) for j in range(9)) )

Submission status: Accepted

Solution code:
n = int(input())
lis = [[0] * 10 for i in range(10)]
for i in range(1, n + 1):
    val = list(str(i))
    lis[int(val[-1])][int(val[0])] += 1

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

Submission status: Accepted

Solution code:
N = int(input())
def get_num(a,b,N):#aで始まり、bで終わるN以下の数の個数
    cnt = 0
    if(a == b and N >= a):
        cnt += 1#1keta
    if(a*10+b <= N):
        cnt += 1#2keta
    #3keta~
    if(100<=N<1000):
        c = 100*a + b
        while(c <= N and c < 100*a + 90 + b + 1):
            cnt += 1
            c += 10
    elif(1000<=N<10000):
        c = 1000*a+b
        cnt += 10
        while(c <= N and c < 1000*a + 990 + b + 1):
            cnt += 1
            c += 10
    elif(10000<=N<100000):
        c = 10000*a + b
        cnt += 110
        while(c <= N and c < 10000*a + 9990 + b + 1):
            cnt += 1
            c += 10
    elif(100000<=N<=200000):
        c = 100000*a + b
        cnt += 1110
        while(c <= N and c < 100000*a + 99990 + b + 1):
            cnt += 1
            c += 10
    return cnt
sum = 0
for i in range(1,10):
    for j in range(1,10):
        sum += get_num(i,j,N)*get_num(j,i,N)
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
import bisect as bi
from operator import mul
from functools import reduce
from math import factorial

def main():
    n = int(input())
    c = {(i,j): 0 for i in range(1,10) for j in range(1,10)}

    for i in range(1, n+1):
        s = str(i)
        if s[-1] == "0":
            continue
        h, t = map(int, (s[0], s[-1]))
        c[h, t] += 1

    print(sum(c[i,j] * c[j,i] for i in range(1,10) for j in range(1, 10)))


main()

Submission status: Accepted

Solution code:
n = int(input())
c = {i: {j: 0 for j in range(1, 10)} for i in range(1, 10)}
for i in range(1, n + 1):
    i = str(i)
    if i[-1] == "0":
        continue
    j, k = map(int, [i[0], i[-1]])
    c[j][k] += 1

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

print(result)

Submission status: Accepted

Solution code:
N = int(input())
count = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, N + 1):
    if str(i)[0] != "0" and str(i)[-1] != "0":
        count[int(str(i)[0]) - 1][int(str(i)[-1]) - 1] += 1
ans = 0
for i in range(0, 9):
    for k in range(0, 9):
        ans += count[i][k] * count[k][i]
print(ans)

Submission status: Accepted

Solution code:
def handstand2(n):
    c = 0
    c_list = [[0 for j in range(10)] for i in range(10)]

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

    for i, _ in enumerate(c_list):
        for j, c_i_j in enumerate(_):
            c += c_i_j*c_list[j][i]
    return c

n = int(input())

print(handstand2(n))

Submission status: Accepted

Solution code:
n = int(input())
dic = {i:0 for i in range(100)}
for i in range(1,n+1):
    num = str(i)
    dic[int(num[0])*10+int(num[-1])] += 1
count = 0
for i in range(1,10):
    count += dic[i*11]**2
    for j in range(i+1,10):
        count += dic[i*10+j]*dic[j*10+i]*2
print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

d = defaultdict(int)
N = int(input())

for n in range(1, N + 1):
    n = str(n)

    a = n[0]
    b = n[-1]

    d[(a, b)] += 1


s = 0
for i in range(10):
    for j in range(10):

        s += d[(str(j), str(i))] * d[(str(i), str(j))]

print(s)

Submission status: Accepted

Solution code:
import math
N = int(input())

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

for i in range(N):
    i += 1
    top = int(str(i)[0])
    bottom = int(str(i)[-1])
    counter[top][bottom] += 1

c = 0
for i in range(10):
    for j in range(10):
        if i == j:
            t = counter[i][j]
            # iとjを入れ替えてダブルカウントするので1/2する
            c += t**2
        else:
            c += counter[i][j] * counter[j][i]

print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
cnt = 0

for i in range(1, int(N)+1):
    
    if i % 10 == 0:
        continue
    
    if i % 10 < int(N[0]):
        for j in range(len(N)-1):
            cnt += 10 ** j
    
    elif i % 10 == int(N[0]):
        if len(N) >= 3:
            for j in range(len(N)-2):
                cnt += 10 ** j
            cnt += int(N[1:-1])+1
        
        if len(N) == 2:
            cnt += 1
        
        if int(str(i)[0]) > int(N[-1]):
            cnt -= 1
    
    else:
        for j in range(len(N)-2):
            cnt += 10 ** j
        
    if str(i)[0] == str(i)[-1]:
        cnt += 1
    
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30
from collections import defaultdict

def main():
    n = int(input().strip())
    d = defaultdict(int)
    for i in range(1, n+1):
        ds = str(i)
        d[(ds[0], ds[-1])] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            si = str(i)
            sj = str(j)
            ans += d[(si, sj)] * d[(sj, si)]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
memo=[[0]*10 for _ in range(10)]
for i in range(1,n+1):
  a=int(str(i)[0])
  b=int(str(i)[-1])
  memo[a][b]+=1
ans=0
for i in range(1,n+1):
  b=int(str(i)[0])
  a=int(str(i)[-1])
  ans+=memo[a][b]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())

"""
A, Bの桁数でfor文を回して大爆死しました
単純にN以下の自然数で先頭の数がa, 末尾の数がbになる数の個数C[(a, b)]を洗い出しておけば
求める個数は
    ¥Sum_{0<=a, b<=9} C[(a, b)] * C[(b, a)]
"""

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
arr = [[0] * 10 for _ in range(10)]
for i in range(1, n + 1):
    num = str(i)
    head, tail = int(num[0]), int(num[-1])
    arr[head][tail] += 1
for i in range(10):
    for j in range(10):
        tmp = arr[i][j] * arr[j][i]
        ans += tmp
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1, n+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    
    C[a][b] += 1
        
answer = sum([C[i][j]*C[j][i] for i in range(10) for j in range(10)])

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
cnt_list = [[0 for _ in range(9)] for _ in range(9)]

for A in range(1, N+1):
    A_high = A//pow(10, len(str(A))-1)
    A_low = A%10
    if A_low != 0:
        cnt_list[A_high-1][A_low-1] += 1

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

Submission status: Accepted

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

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

print(sum([c[i][j]*c[j][i] for i in range(10) for j in range(10)]))

Submission status: Accepted

Solution code:
N = int(input())
C = [[0 for i in range(9)] for j in range(9)]


def top(n):
    while 10 <= n:
        n = n // 10
    return n


for n in range(N):
    c = n + 1
    i = c % 10
    j = top(c)
    if i and j:
        C[i - 1][j - 1] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=[[0]*9 for i in range(9)]
for i in range(1,N+1):
    if i%10:
        t=str(i)
        s[int(t[0])-1][int(t[-1])-1]+=1
a=0
for i in range(9):
    for j in range(i):
        a+=s[i][j]*s[j][i]*2
    a+=s[i][i]**2
print(a)

Submission status: Accepted

Solution code:
n= int(input())

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

for i in range(1,n+1):
  a,b = int(str(i)[0]), i%10
  mat[a][b] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [[0]*9 for _ in range(9)]
for i in range(n):
    if str(i+1)[0] != '0' and str(i+1)[-1] != '0':
        a[int(str(i+1)[0])-1][int(str(i+1)[-1])-1] += 1
        
ans = 0
for i in range(9):
    for j in range(9):
        ans += a[i][j]*a[j][i]
        
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
input = sys.stdin.buffer.readline
def inputS(): return input().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

from collections import defaultdict

N = int(input())

counter = defaultdict(int)

for i in map(str, range(1, N+1)):
  # 桁数の先頭、末尾を代入
  head = i[0]
  tail = i[-1]
  counter[(head, tail)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = [[0 for i in range(10)] for j in range(10)]
for i in range(1,N+1):
    top = int(str(i)[0])
    bot = int(str(i)[-1])
    c[top][bot]+=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:
N = int(input())
N_str = list(str(N))
length = len(str(N))

#1<=k<=Nで 先頭がiで末尾がjの総和
c = [[0]*9 for i in range(9)]
'''
#C(i,j)を求めるのは無理

def count(i, j):
    pass

for i in range(9):
    for j in range(9):
        c[i][j] = count(i, j)
'''

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
cnt = [ [0]*10 for _ in range(10)]

for i in range(1, n+1):
  strI = str(i)

  t = strI[0]
  b = strI[-1]
  cnt[int(t)][int(b)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = [[0]*9 for _ in range(9)]
count = 0
for i in range(1,N+1):
    li = i//10**(len(str(i))-1) 
    ri = i%10
    if ri == 0:
        continue
    c[li-1][ri-1] += 1

for i in range(9):
    for j in range(9):
        count += c[i][j]*c[j][i]

print(count)

Submission status: Accepted

Solution code:
def get_first_end(n):
    n = str(n)
    return (int(n[0]), int(n[-1]))


n = int(input())
ans = 0

ans_dict = {}
for i in range(1, n+1):
    p = get_first_end(i)
    if not(p in ans_dict):
        ans_dict[p] = 0
    ans_dict[p] += 1

for i in range(1, n+1):
    p = get_first_end(i)
    reverse = (p[1], p[0])
    if reverse in ans_dict:
        ans += ans_dict[reverse]
print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    sN = input()
    N = int(sN)
    firts_digit = int(sN[0])
    last_digit = int(sN[-1])
    num_candidates = max(min(firts_digit,last_digit),firts_digit -1 )
    max_inter_digits = len(sN) - 2
    

    ans = 0
    #for i in range(10**(len(sN)-1),N+1):
    max_inter_digits = len(sN) - 2
    intermediate = 0
    for _ in range(max_inter_digits):
        intermediate = intermediate*10+1
    # print(intermediate)
    if len(sN) == 1:
        print(N)
        return
    elif len(sN) == 2:
        ans = 9
        for i in range(1,10):
            tmp = i
            if i * 11 <= N:
                ans += 1
        for i in range(11,N+1):
            if i % 10 == 0:
                continue
            if i % 11 == 0:
                ans += 1
            if (i % 10) * 10 + i//10 <= N:
                ans += 1
            # print(i,ans)
        print(ans)
        return 

    first_N = int(sN[0])
    last_N = N % 10
    for i in range(1,N+1):

        # print(i,ans,intermediate)
        if i%10 == 0:
            continue
        si = str(i)
        #print(intermediate)
        if si[0] != si[-1]:
            ans += intermediate
        else:
            ans += intermediate + 1
            # ans += 
        # print(i,ans,intermediate)
        first_B = i % 10
        last_B = int(si[0])
        # first_N = int(sN[0])
        # last_N = i % 10



        
        if first_B <  first_N:
            ans += pow(10,max(len(sN)-2,0))
        elif first_B == first_N:
            # print(i) if i == 1 or i == 11 else None
            sm = sN[1:-1]
            m = (N % pow(10,len(sN)-1)) // 10 + 1 #if len(sN) > 2 else (N % pow(10,len(sN)-1))
            # print('i=',i,'m=',m)
            if last_B <= last_N:
                ans += m
            else:
                ans += max(m-1,0)
            # print(i,m) if i == 1 or i == 11 else None

            
            
    print(ans)
    '''


        N = 2500
        2012
        2
        22
        2?2
        2xy2 - 2492
            x: 0...4
            y: 0...9
            sm = 2492 = sN[:-2]+si[0]
            m = int(m)
            if m > N:
                sm = str(m-10)
            intermediate += (sm[1]+1)*....*(sm[-2]+1)
            

        N = 90000
        i = 65432
        26
        2?6
        2??6
        2???6 29996
            first_i = int(si[-1])
            first_N = int(N[0])
            if first_i <  first_N:
                intermediate = intermediate *10 +1
                ans += intermediate
            elif first_i == first_N:
                sm = sN[:-2]+si[0]
                tmp = 1
                for c in sm[1:-2]:
                    tmp*= c+1
                ans += tmp
            
                
                



        N = 30512
        i = 503
        i[-1] == N[0]
        35
        3?5
        3??5
        3???5 
        sm =  30505 = sN[0:-1] + str(i[0])
        intermediate += (s[1]+1)*...*(s[-2]+1)
        if m < N:
            m -=10
        '''

    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
D={}
for i in range(1,n+1):
   s=(str(i)[0],str(i)[-1])
   if s in D:
      D[s]+=1
   else:
      D[s]=1

ans=0
for aj,bj in D:
   if (bj,aj) in D:
      ans+=D[(aj,bj)]*D[(bj,aj)]
print(ans)

Submission status: Accepted

Solution code:
d=dict()
n=int(input())
ans=0
special=0
for i in range(1,n+1):
  ii=str(i)
  if (ii[0],ii[-1]) in d.keys():
    d[(ii[0],ii[-1])]+=1
  else:
    d[(ii[0],ii[-1])]=1
  if (ii[-1],ii[0]) in d.keys():
    ans+=d[(ii[-1],ii[0])]
  if (ii[0]==ii[-1]):
    special+=1
  #print(i,ans)
#print(special)
print(2*(ans-special)+special)

Submission status: Accepted

Solution code:
n = int(input())
dp = [[0] * 10 for _ in range(10)]
ans = 0

for i in range(1, n+1):
    top = int(str(i)[0])
    end = int(str(i)[-1])
    ans += 2 * dp[end][top]
    if top == end:
        ans += 1
    dp[top][end] += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
cnt=[[0]*10 for _ in range(10)]
#print(cnt)
for i in range(1,n+1):
    tmp=str(i)
    a=int(tmp[0])
    b=int(tmp[-1])
    cnt[a][b]+=1

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

print(sum)

Submission status: Accepted

Solution code:
N = int(input())

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

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import itertools


n = int(input())

XY = {xy:0 for xy in itertools.product("0123456789", repeat = 2)}
for i in range(1, n + 1):
    x, y = str(i)[0], str(i)[-1]
    XY[(x, y)] += 1

answer = 0
for a in range(1, n + 1):
    x, y = str(a)[0], str(a)[-1]
    answer += XY[(y, x)]

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())


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

# 数字を文字にして、[0]と[-1]を使うアイデアが参考になる
for i in range(1,N+1):
    n = str(i)
    head = int(n[0])
    tail = int(n[-1])
    L[head][tail] += 1

ans = 0

for j in range(10):
    for k in range(10):
        ans += L[j][k] * L[k][j]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

cnt = [[0]*10 for i in range(10)]
# 先頭がi, 末尾がjであるような数の個数を計算
for n in range(1, N+1):
    n = str(n)
    i = int(n[0])
    j = int(n[-1])
    cnt[i][j] += 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
if n<10:
    print(1)
    exit()
count=[[0]*10 for _ in range(10)]

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

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import math
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

#############
# Main Code #
#############

N = int(input())

dic = [[0 for i in range(10)] for j in range(10)]
for i in range(1,N+1):
  top = int(str(i)[0])
  bottom = int(str(i)[-1])
  dic[top][bottom] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[[0 for i in range(10)]for s in range(10)]
for  i in range(n+1):
  if i%10:
    l[int(str(i)[0])-1][int(str(i)[-1])-1]+=1
ans=0
for i in range(9):
  for j in range(9):
    ans+=l[i][j]*l[j][i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

d = defaultdict(int)

N = int(input())

for i in range(1,N+1):
    i = str(i)
    H = i[0]
    T = i[-1]
    d[H,T]+=1

o = 0
for i in "0123456789":
    for j in "0123456789":
        o += d[i,j] * d[j,i]

print(o)

Submission status: Accepted

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

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(N+1):
  nstr = str(i)
  pre = nstr[0]
  suf = nstr[-1]
  if suf == '0':
    continue
  dic[(pre, suf)] =dic[(pre, suf)] + 1

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

print(cnt)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(1000000)
input = lambda: sys.stdin.readline().rstrip()

N = int(input())

cnt = 0
for A in range(1, N + 1):
    last_digit = A % 10
    if last_digit == 0:
        continue
    first_digit = int(list(str(A))[0])
    for l in range(1, 7):
        if l == 1:
            if last_digit == first_digit:
                cnt += 1
        elif l < len(str(N)):
            cnt += 10 ** (l - 2)
        elif len(str(N)) == l:
            if last_digit < int(list(str(N))[0]):
                cnt += 10 ** (l - 2)
            elif last_digit == int(list(str(N))[0]):
                maybe, ok = 1, 0
                for j in range(1, l - 1):
                    ok = ok * 10 + maybe * int(list(str(N))[j])
                if first_digit <= N % 10:
                    ok += maybe
                cnt += ok
print(cnt)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(1000000)
input = lambda: sys.stdin.readline().rstrip()

N = int(input())

cnt = 0
for A in range(1, N + 1):
    last_digit = A % 10
    if last_digit == 0:
        continue
    first_digit = int(list(str(A))[0])
    for l in range(1, 7):
        if l == 1:
            if last_digit == first_digit:
                cnt += 1
        elif l < len(str(N)):
            cnt += 10 ** (l - 2)
        elif len(str(N)) == l:
            if last_digit < int(list(str(N))[0]):
                cnt += 10 ** (l - 2)
            elif last_digit == int(list(str(N))[0]):
                maybe, ok = 1, 0
                for j in range(1, l - 1):
                    ok = ok * 10 + maybe * int(list(str(N))[j])
                if first_digit <= N % 10:
                    ok += maybe
                cnt += ok
print(cnt)