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:
N = int(input())
 
c = []
for i in range(10):
    c.append([0] * 10)
 
for i in range(N):
    s = str(i+1)
    i1 = int(s[0])
    i2 = int(s[-1])
    c[i1][i2] += 1
    
total = 0
for i in range(10):
    for j in range(10):
        total += c[i][j] * c[j][i]
        
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())

def cnt(i,j):
    ret=0
    si=str(i)
    for n in range(1,N+1):
        if n%10!=j:
            continue
        if str(n)[0]!=si:
            continue
        ret+=1
    return ret

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 = []
for i in range(10):
    c.append([0] * 10)

for i in range(N):
    s = str(i+1)
    i1 = int(s[0])
    i2 = int(s[-1])
    c[i1][i2] += 1
    
total = 0
for i in range(10):
    for j in range(10):
        total += c[i][j] * c[j][i]
        
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

a = list(range(1, 10))
z = list(range(1, 10))

az = []
az_r = []
az0 = []
az1 = []
for i in a:
    for j in z:
        az.append(i*10+j)
        az_r.append(i+j*10)
        az0.append(0)
        az1.append(0)

for i in range(1, n+1):
    if i < 10:
        j = i*10+i
    else:
        j = i
    str1 = str(j)
    str_index = int(str1[0])*10 + int(str1[-1])
    str_index_r = int(str1[0]) + int(str1[-1])*10
    if int(str1[-1]) != 0 and int(str1[0]) != 0:
        az0[az.index(str_index)] += 1
        az1[az.index(str_index_r)] += 1

ans = 0
for i in range(len(az0)):
    if az0[i] >0 and az1[i] >0:
        ans += az0[i]*az1[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
for i in range(1, N+1):
    head, tail = str(i)[0], str(i)[-1]
    if tail == "0":
        continue
    if head == tail:
        ans += 1
    if int(tail + head) <= N:
        ans += 1
    if len(str(N))-2 >= 1 or head == tail:
        num = len(str(N))-2
        if int(tail) <= int(str(N)[0]) and num > 0:
            if int(tail + "9"*(len(str(N))-2) + head) <= N:
                j = int("9"*(len(str(N))-2))
            else:
                if int(tail + str(N)[1:-1] + head) <= N:
                    j = int(str(N)[1:-1])
                else:
                    j = int(str(N)[1:-1]) - 1
            ans += int(j) + 1
        if num > 1:
            ans += int((num - 1) * "1") * 10
    # print(i, ans)
                    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
len_N = len(str(N))
str_N = str(N)
N_a = int(str_N[0])
N_b = int(str_N[-1])
ans = 0
if len_N == 1:
    print(N)
    exit()
if len_N == 2:
    ans += 9
    for i in range(11, N+1):
        str_i = str(i)
        if str_i[1] == '0':
            continue
        i_tmp = int(str(i)[1] + str(i)[0])
        if i_tmp <= N:
            ans += 1
    tmp = N // 11
    ans += tmp * 2
    print(ans)
    exit()

for i in range(1, N+1):
    tmp = str(i)
    tmp_a = int(tmp[0])
    tmp_b = int(tmp[-1])
    if tmp_b == 0:
        continue
    if tmp_a == tmp_b:
        ans += 1
 
    if tmp_b > N_a:
        ans += int('1' * (len_N - 2))

    elif tmp_b == N_a and tmp_a <= N_b:
        ans += int('1' * (len_N - 2))
        naka = str_N[1:-1]
        if naka == "":
            continue
        ans += int(naka) + 1

    elif tmp_b == N_a and tmp_a > N_b:
        ans += int('1' * (len_N - 2))
        naka = str_N[1:-1]
        if naka == "":
            continue
        ans += int(naka)

    else:
        ans += int('1' * (len_N - 1))

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N=int(input())
A=[0]*101
count=0
for i in range(1,N+1):
    T = str(i)
    K = len(T) - 1
    if i<10:
        A[i*10+i]+=1
    else:
        X=i%10
        Y=i//(10**K)
        if X!=0:
            A[Y*10+X]+=1

#print(A)

for i in range(1,N+1):
    T = str(i)
    K = len(T) - 1
    if i<10:
        count+=A[i*10+i]
    else:
        X=i%10
        Y=i//(10**K)
        if X!=0:
            count+=A[X*10+Y]
print(count)

Submission status: Accepted

Solution code:
N = input()

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

digits = 1
base = 10
for i in range(0, 20):
    if n < 10 ** i:
        digits = i
        break

smallest_digit = n % 10
largest_digit = n // (10 ** (digits - 1)) % 10
mid_digit_pattern = (n - largest_digit * (10 ** (digits - 1))) // 10 + 1

def count_pattern(a, b, dig):
    if dig == 1 and a != b:
        return 0
    elif dig == 1:
        return 1
    elif dig == digits:
        if a > largest_digit:
            return 0
        elif a < largest_digit:
            return 10 ** (dig - 2)
        elif b <= smallest_digit:
            return mid_digit_pattern
        elif b > smallest_digit:
            return mid_digit_pattern - 1
        else:
            assert False, "Can't reach here"
    else:
        return 10 ** (dig - 2)

result = 0
if digits == 1:
    result = n
else:
    counts = {}
    for i in range(1, 10):
        for j in range(1, 10):
            state = (i, j)
            counts[state] = sum([count_pattern(i, j, d) for d in range(1, digits + 1)])

    for state in counts.keys():
        result += counts[state] * counts[tuple(reversed(state))]

print(result)

Submission status: Accepted

Solution code:
import sys
from math import log10, floor, ceil
from itertools import combinations, product
n = int(input())
n_start = int(str(n)[0])
n_last = int(str(n)[-1])
n_keta = floor(log10(n)) + 1
ans = 0
dic = {(i[0], i[1]) : 0 for i in product(range(10), repeat=2)}
for i in range(n+1):
    _i = str(i)
    dic[(int(_i[0]), int(_i[-1]))] += 1
dic[(0, 0)] = 0
for a, b in product(range(10), repeat=2):
    ans += dic[(a, b)] * dic[(b, a)]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

c = [[0 for _ in range(10)] for _ in range(10)]
for k in range(1, n+1):
    s = str(k)
    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())
from collections import defaultdict
dic=defaultdict(int)

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

Submission status: Accepted

Solution code:
from functools import lru_cache

N = int(input())

@lru_cache(maxsize=None)
def c(i, j):
    return len([
        i
        for n in range(1, N + 1)
        if str(n)[0] == i
        if str(n)[-1] == j
    ])

def main():
    tmp = 0
    for i in "0123456789":
        for j in "0123456789":
            tmp += c(i, j) * c(j ,i)

    print(tmp)

main()

Submission status: Accepted

Solution code:
s = input().rstrip()
N = int(s)

ans = 0
i = 1
order = len(str(N))

params = {}

for i in range(1, N + 1):
    Akey = str(i)[-1] + str(i)[0] 
    if params.get(Akey) is not None:
        params[Akey] = params[Akey] + 1
    else:
        params[Akey] = 1

for i in range(1, N + 1):
    Bkey = str(i)[0] + str(i)[-1] 
    count = params.get(Bkey)
    if count is not None:
        ans =  ans + count;

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(n+1):
    hn = int(str(i)[0])
    tn = i % 10
    cc[hn][tn] = cc[hn][tn]+1
#    print("{} {}".format(hn, tn))
    
c = 0

for h in range(1,10):
    for t in range(1, 10):
        c = c + (cc[h][t]*cc[t][h])
#        print("{}".format(cc[h][t]))
        
print(c)

Submission status: Accepted

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())
cnt = [[0]*9 for _  in range(9)]
for idx in range(1,n+1):
    a = str(idx)
    if a[-1] == '0':
        continue
    cnt[int(a[0])-1][int(a[-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:
N = int(input())

c = [[0]*10 for i in range(10)]
for n in range(1,N+1):
    n_str = str(n)
    n_last = len(n_str)-1
    c[int(n_str[0])][int(n_str[n_last])] += 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:
import sys
from collections import defaultdict

def main():
    n = int(sys.stdin.readline().strip())
    # n = 25

    # 11
    # 12 21
    # 13 31
    # ...
    # 19 91

    # 22 22
    # 23 32
    # ...
    # 29 92

    # n = 100
    # 1-1: 2 => 2 ** 2
    # 1-2: 1
    # 1-3: 2
    # ...
    # 2-1: 1
    # 3-1: 1
    # ...
    # 1-2 and 2-1: 1 * 1 => 1


    # n = 2020
    # 1-1: 1 + 1 + 10 + 10 * 10 => 112
    # 1-2: 1 + 1 * 10 + (10 ** 2) * 1 => 222
    # 2-1: 1 + 2 * 10 + 2 [2011, 2001] => 23
    # => 222 * 23

    # 1-1 to 9-9: 81

    # n = 25
    # 1-1: 1
    # 1-2: 1
    # 2-1: 1
    # 1-3: 1
    # 1-4: 1
    # ...
    # 1-9: 1
    # 2-2: 2
    # 2-3: 1
    # 2-4: 1
    # 2-5: 1
    # 1 + 1 * 1 + 1 * 0 + ...

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

    for i in range(1, n+1):
        tmp = i
        while tmp >= 10:
            tmp //= 10
        dic[tmp][i % 10] += 1

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


if __name__ == '__main__':
    main()
    # count_i_j(1, 3, 25)

Submission status: Accepted

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

ans = 0

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

    if end != 0 :
        book[pre-1][end-1] += 1

for i in range(9) :
    for j in range(i, 9) :
        if i == j :
            ans += book[i][j] * book[j][i]
            #print('=', i, j, book[i][j] * book[j][i])
            continue
        if book[i][j] != 0 and book[j][i] != 0 :
            ans += book[i][j] * book[j][i] * 2
            #print('!', i, j, book[i][j] * book[j][i] * 2)

#for i in range(9) :
#    print(book[i])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(N):
	n = str(i+1)
	c[int(n[0])][int(n[-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())
ans = 0
Cnt = [[0]*10 for _ in range(10)]
for i in range(1, n+1):
    x, y = int(str(i)[0]), int(str(i)[-1])
    Cnt[x][y] += 1
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:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
N_str = str(N)
n_keta = len(N_str)

# 条件にあうやつを数え上げる。
# 条件に合えば、10の倍数は基本的に条件を満たすが
from collections import defaultdict
container = defaultdict(lambda: 0)

for n in range(N + 1):
    if n % 10 == 0:
        continue
    n_str = str(n)
    container[(int(n_str[0]), int(n_str[-1]))] += 1

ans = 0
from itertools import product
for i, j in product(range(1, 10), range(1, 10)):
    ans += container[(i, j)] * container[(j, i)]

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())

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

for i in range(1,n+1):
    sn = str(i)
    for ci in range(10):
        for cj in range(10):
            if sn[0]==str(ci) and sn[-1]==str(cj):
                c[ci][cj] += 1

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(read())

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
l=[[0]*10 for i in range(10)]
for i in range(n):
    t=str(i+1)
    l[int(t[0])][int(t[-1])]+=1
a=0
for i in range(10):
    for j in range(10):
        a+=l[i][j]*l[j][i]
print(a)

Submission status: Accepted

Solution code:
N= int(input())
lead_N = int(str(N)[0]) 
end_N = int(str(N)[-1])
ans = 0
for i in range(1, N + 1):
  lead = int(str(i)[0])
  end = int(str(i)[-1])
  # 一桁
  if lead == end:
    ans += 1
  #2桁以上ループ lead + 中間 + end　
  for j in range((len(str(N)) - 2) + 1):
    if end == 0 or (end > lead_N and j == (len(str(N)) - 2)):
      #末尾が0　または　生成する整数がNに収まらない場合
      continue
    elif end == lead_N and j == (len(str(N)) - 2):
      #生成する整数の一部がNに収まらない場合
      #中間の数字
      if len(str(N)) >= 3:
        ans += int(str(N)[1:-1])
      if lead <= end_N:
        ans += 1
    else:
      #何も気にしないときは組み合わせの分だけたす
      ans += 10 ** j

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
def f(x,y):
  ret=0
  for i in range(1,n+1):
    if i%10!=y:
      continue
    else:
      if x==int(str(i)[0]):
        ret+=1
  return ret
ans=0
T=[[0]*10 for _ in range(10)]
for i in range(1,10):
  for j in range(1,10):
    T[i][j]=f(i,j)

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

Submission status: Accepted

Solution code:
n=int(input())
l=[[0]*9 for j in range(9)]
for i in range(1,n+1):
    i=str(i)
    if i[0]!='0' and i[-1]!='0':
        l[int(i[-1])-1][int(i[0])-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:
N = int(input())
ans = 0
C = [[0]*10 for _ in range(10)]
for n in range(1, N+1):
    i, j = int(str(n)[0]), int(str(n)[-1])
    C[i][j] += 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:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    N = int(input())

    res = 0

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

    for k in range(1,N+1):
        s = str(k)
        fd = int(s[0])
        ld = int(s[-1])
        c[fd][ld] += 1

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

    print(res)



main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N = int(read())
l = len(str(N)) - 1
c_mat = [[0] * 10 for _ in range(10)]

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

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

Submission status: Accepted

Solution code:
import math

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(x, n+1, 10):
        x2 = int(str(b)[0])
        y2 = b % 10
        if x == y2 and y == x2:
            patterns += 1
    memo[x][y] = patterns
    ans += patterns

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

numbers = [[0 for i in range(10)] for i in range(10)]
for i in range(1, 10):
    for j in range(10):
        if i > N:
            continue
        tmp = 0
        if i == j:
            tmp += 1
        if i * 10 + j <= N:
            tmp += 1
        i_now = 100
        nine = 90
        nine_now = 10
        while i*i_now + nine + j <= N:
            tmp += nine_now
            i_now *= 10
            nine_now *= 10
            nine += 9 * nine_now

        if i*i_now <= N // i_now * i_now:
            if j <= N%10:
                tmp += (N%i_now)//10 + 1
            else:
                tmp += max(0, (N%i_now)//10)
        numbers[i][j] = tmp
ans = 0
for i in range(10):
    for j in range(10):
        ans += numbers[i][j]*numbers[j][i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

G = [[0 for tail in range(10)] for head in range(10)]

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

total = 0
for headA in range(10):
  for tailA in range(10):
    total += G[headA][tailA] * G[tailA][headA]
print(total)

Submission status: Accepted

Solution code:
n = int(input())
res_a = 0
res_b = 0
dic_len = {1:0,2:0,3:1,4:11,5:111,6:1111}
for i in range(1,n+1):
    istr = str(i)
    leni = len(istr)
    ifirst, ilast = istr[0], istr[-1]
    if ifirst == ilast:
        res_a += 1
        if i >= 10:
            res_b += 1
        if leni > 2:
            imid = istr[1:-1]
            res_b += max(int(imid),0)
    elif ifirst > ilast and ilast!='0':
        res_b += int(10**(leni-2))
    if ilast!='0':
        res_b += dic_len[leni]
print(res_a+2*res_b)

Submission status: Accepted

Solution code:

N = int(input())

C = [[0]*(10) for _ in range(10)]
for i in range(N+1):
    S = str(i)
    s = int(S[0])
    e = int(S[-1])
    C[s][e] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += C[i][j]*C[j][i]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
c = np.zeros((10,10))
for i in range(1,n+1):
  l = int(str(i)[0])
  r = int(str(i)[-1])
  c[l,r] += 1

count = 0
for l in range(10):
  for r in range(10):
    count += c[l,r]*c[r,l]
print(int(count))

Submission status: Accepted

Solution code:
n=int(input())
d=[10*[0]for _ in range(10)]
ans=0
for i in range(1,n+1):
  s=str(i)
  if s[0]=="0" or s[-1]=="0":
    continue
  d[int(s[0])][int(s[-1])]+=1
for i in range(1,10):
  for j in range(i,10):
    if i == j:
        ans+=d[i][j]*d[j][i]
    else:
        ans+=d[i][j]*d[j][i]*2
print(ans)

Submission status: Accepted

Solution code:
from pprint import pprint
N = int(input())
digit_cnt = list([0] * 9 for _ in range(9))
now = 1
for i in range(1, N + 1):
    str_i = str(i)
    row = int(str_i[0])
    column = int(str_i[-1])
    if row != 0 and column != 0:
        digit_cnt[row - 1][column - 1] += 1
# pprint(digit_cnt)

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

Submission status: Accepted

Solution code:
d=[0]*100
for i in range(int(input())+1):
  s=str(i)
  d[int(s[0])*10+int(s[-1])]+=1
r=range(1,10)
print(sum(d[i*10+j]*d[j*10+i]for i in r for j in r))

Submission status: Accepted

Solution code:
def fu(a,b):
  l=len(a)
  b=str(b)
  x=0;k=int(b[1:])-int(a[1:])
  if l<len(b) or a[0]<b[0]:
    return 10**(l-2)
  return (int('0'+b[1:l-1])+1-int(a[-1]>b[-1]))*(a[0]==b[0])

n = int(input())
b=str(n)
p = len(str(n))
x=min(9,n)
for i in range(10,n+1):
  l = len(str(i))
  k = str(i)[::-1]
  if k[0]!='0':
    x += fu(k,n)
    x += 2*(int('1'*max(l-2,1))+int(k[0]==k[-1])) if l>2 else 2*int(k[0]==k[-1])
print(x)

Submission status: Accepted

Solution code:
n = int(input())
c = [[0]*(10) for i in  range(10)]
for k in range(1,n+1):
    leng = len(str(k))-1 
    i = k//(10**leng)
    j = k%10
    c[i][j] +=1
ans = 0
for i in range(10):
    for j in range(10):
        ans+=c[i][j] *c[j][i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
cnt = [[0] * 9 for _ in range(9)]
tmp = 0
for i in range(1, n + 1):
    one = str(i)
    if one[-1] == '0':
        continue
    cnt[int(one[0]) - 1][int(one[-1]) - 1] += 1
for i in range(9):
    for j in range(9):
        ans += cnt[i][j] * cnt[j][i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

#N以下で先頭と末尾がkのやつ
TTS = [1,1,10,100,1000,10000,100000]
TTW = [0,1,10,100,1000,10000,100000]
def ttk(N,k):
    c = 0
    Nd = len(str(N))
    #print("Nd",Nd)
    for i in range(Nd-1):
        #print(TTS[i])
        c += TTS[i]
    if Nd >= 3:
        for i in range(10**(Nd-2)):
            a = int(str(k) + (Nd-2-len(str(i)))*"0" + str(i) + str(k))
            #print("a=",a)
            if a <= N:
                c += 1
            else:
                break
    elif Nd == 2:
        a = int(str(k)*2)
        if a <= N:
            c += 1
    elif Nd == 1:
        a = k
        if a <= N:
            c += 1
    return c

#(i)先頭末尾が同じもの
"""
y = 0
for i in range(1,10):
    y += ttk(N,i)**2
print(y)
"""


def ttpq(N,p,q):
    c = 0
    Nd = len(str(N))
    #print("Nd",Nd)
    for i in range(Nd-1):
        #print(TTW[i])
        c += TTW[i]
    if Nd >= 3:
        for i in range(10**(Nd-2)):
            a = int(str(p) + (Nd-2-len(str(i)))*"0" + str(i) + str(q))
            #print(a)
            if a <= N:
                c += 1
            else:
                break
    elif Nd == 2:
        a = int(str(p)+str(q))
        if a <= N:
            c += 1
    elif Nd == 1:
        c = 0
    return c
#(ii)先頭末尾が違うもの

y=0
for i in range(1,10):
    for j in range(i,10):
        # print("i:{},j,{}".format(i,j))
        if i == j:
            # print("   added",ttk(N,i)**2)
            y += ttk(N,i)**2
        else:
            # print("   added",ttpq(N,i,j)**2)
            y += ttpq(N,i,j)*ttpq(N,j,i)*2
        # print(y)
print(y)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    n = INT()
    ans = 0

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

    for i in range(1, n+1):
        s = str(i)
        a, b = s[0], s[-1]
        if "0" in [a, b]: continue
        lis[int(a)-1][int(b)-1] += 1


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

    print(ans)



if __name__=='__main__':
    main()

Submission status: Accepted

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

counter=defaultdict(int)

X=map(str,(range(1,N+1)))
for x in X:
    h=x[0]
    t=x[-1]
    counter[(h,t)]+=1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input()) 
d = [10*[0] for _ in range(10)]
for i in range(1, N+1):
    s = str(i)
    if s[0]=="0" or s[-1]=="0":
        continue
    d[int(s[0])][int(s[-1])]+=1   
ans = 0
for i in range(10):
    for j in range(10):
        ans+=d[i][j]*d[j][i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


n = int(input())

count = defaultdict(int)

for x in map(str, range(1, n + 1)):
    head = x[0]
    tail = x[-1]
    count[(head, tail)] += 1

ans = 0
# if do not, RuntimeError: dictionary changed size during iteration
items = list(count.items())
for (head, tail), c in items:
    ans += c * count[(tail, head)]

print(ans)

Submission status: Accepted

Solution code:
import unittest


class TestD(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(25), 17)

    def test_2(self):
        self.assertEqual(think(1), 1)

    def test_3(self):
        self.assertEqual(think(100), 108)

    def test_4(self):
        self.assertEqual(think(2020), 40812)

    def test_5(self):
        self.assertEqual(think(200000), 400000008)


def solve():
    n = read()
    result = think(n)
    write(result)


def read():
    return read_int(1)[0]


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n):
    count = 0
    dp = [[0 for last_digit in range(10)] for first_digit in range(10)]
    for i in range(1, n + 1):
        first_digit = first_digit_of(i)
        last_digit = last_digit_of(i)
        dp[first_digit][last_digit] += 1
    for first_digit in range(10):
        for last_digit in range(10):
            count += dp[first_digit][last_digit] * dp[last_digit][first_digit]
    return count


def write(result):
    print(result)


def first_digit_of(a):
    return int(str(a)[0])


def last_digit_of(a):
    return int(str(a)[-1])


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

counter = 0

lead = np.zeros((9, 9))

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

for i in range(1, N + 1):
    if (i % 10 != 0):
        moji = str(i)
        tmp = moji[-1]
        counter += lead[int(moji[-1]) - 1][int(moji[0]) - 1]

print(int(counter))

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
count = [[0]*10 for i in range(10)]
ans = 0
for i in range(1,N+1):
    s = str(i)
    l = int(s[0])
    r = int(s[-1])
    count[l][r] += 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:
N=int(input())
data=[[0]*10 for i in range(10)]
for u in range(N+1):
    data[int(str(u)[0])][int(str(u)[-1])]+=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:



N = int(input())

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


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

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


print(ans)

Submission status: Accepted

Solution code:
n = int(input())

grid = [[0 for _ in range(9)] for _ in range(9)]
ans = 0
for i in range(1, n + 1):
    if i % 10 == 0:
        continue
    else:
        s = str(i)
        grid[int(s[0]) - 1][int(s[-1]) - 1] += 1

for i in range(9):
    for j in range(9):
        ans += grid[i][j] * grid[j][i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

    c[s][e] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input()) 

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

for i in range(1, N+1):
    s = str(i)
    if s[0]=="0" or s[-1]=="0":
        continue
    d[int(s[0])][int(s[-1])]+=1
    
for i in range(10):
    for j in range(10):
        ans+=d[i][j]*d[j][i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
table = [[0 for _ in range(9)] for _ in range(9)]
 
for i in range(1, n+1):
    s = str(i)
    f, l = s[0], s[-1]
    if "0" in [f, l]: continue
    table[int(f)-1][int(l)-1] += 1
 
ans = 0
for i in range(9):
    for j in range(9):
        ans += table[i][j] * table[j][i]
 
print(ans)

Submission status: Accepted

Solution code:
N = input()
n_len = len(N)
N = int(N)
A = [[0 for i in range(10)] for _ in range(10)]

ans = 0
for n in range(1, N+1):
    l = int(str(n)[0])
    r = n % 10

    if r == 0:
        continue
    A[l][r] += 1

for l in range(1, 10):
    for r in range(1, 10):
        ans += A[l][r] * A[r][l]

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)