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

ans = 0
ten_lst = [(10 ** i - 1) // 9 for i in range(1, 7)]
for i in range(1, N + 1):
    str_i = str(i)
    l = len(str_i)
    if str_i[-1] == '0':
        continue
    elif str_i[0] == str_i[-1]:
        # 131など
        if l == 1:
            ans += 1
        elif l == 2:
            ans += 3
        else:
            ans += 3 + 2 * (ten_lst[l - 3] + int(str_i[1:-1]))
    elif int(str_i[0]) > int(str_i[-1]):
        # 312など
        ans += 2 * ten_lst[l - 2]
    elif l >= 3:
        # 213など
        ans += 2 * ten_lst[l - 3]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
nf=int(str(N)[0])
nl=len(str(N))
if nl==1:
  print(N)
  exit()

ans=0
for i in range(1,N+1):
  a=i
  f=int(str(a)[0])
  l=int(str(a)[-1])
  if l!=0:
    if l<nf: 
      for j in range(2,nl+1):
        ans+=10**(j-2)
      if f==l:
        ans+=1
    elif l>nf:
      if nl-3>=0:
        for j in range(3,nl+1):
          ans+=10**(j-3)
        if f==l:
          ans+=1
      else:
        if f==l:
          ans+=1
    else:
      if int(str(N)[-1])>=f:
        if nl>=3:
          ans+=int("".join(str(N)[1:-1]))+1
          for j in range(3,nl+1):
            ans+=10**(j-3)
          if f==l:
            ans+=1
        else:
          ans+=1
          if f==l:
            ans+=1
      else:
        if nl>=3:
          ans+=int("".join(str(N)[1:-1]))
          for j in range(3,nl+1):
            ans+=10**(j-3)
          if f==l:
            ans+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
n_digit = len(str(n))
count = 0
for i in range(1, 10):
    for j in range(1, 10):
        # a:i[0-9]*j
        # b:j[0-9]*i
        a_count = 0
        b_count = 0
        for k in range(j, n+1, 10):
            k_str = str(k)
            if(i == int(k_str[0])):
                a_count += 1
        for k in range(i, n+1, 10):
            k_str = str(k)
            if(j == int(k_str[0])):
                b_count += 1
        count += a_count * b_count
print(count)

Submission status: Accepted

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

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

r=0
for j in range(9):
    for k in range(9):
        if j==k:
            r+=L[j][j]*L[j][j]
        else:
            r+=L[j][k]*L[k][j]
print(r)

Submission status: Accepted

Solution code:
N = int(input())
# P = list(map(int, input().split()))

if N <= 10:
    print(N)
    exit()

dic = {}
for i in range(11,100):
    if i % 10 == 0:
        continue
    if i % 11 == 0:
        dic[i] = 1
    else:
        dic[i] = 0

for i in range(11, N+1):
    if i % 10 == 0:
        continue
    tmp = str(i)[0] + str(i)[-1]
    dic[int(tmp)] += 1

res = 0
for current, num in dic.items():
    if current % 11 == 0:
        res += num * num
    else:
        tmp = int(str(current)[::-1])
        res += num * dic[tmp]

print(res)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


#mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np


def main():
	n=int(input())
	
	ans1=0
	ans2=0
	
	for i in range(1,n+1):
		a=int(str(i)[0])
		b=i%10
		
		if b==0:
			continue
		k=len(str(i))
		if k==1:
			ans2+=1
		elif a>b:
			ans1+=(10**(k-1)-1)//9
		elif a<b:
			ans1+=(10**(k-2)-1)//9
		else:
			ans1+=(10**(k-2)-1)//9+1+(int(str(i)[1:])-a)//10
			ans2+=1
	print(ans1*2+ans2)
	
	
	
	
	


if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
c = len(str(n))
if n <= 10:
    print(min(n, 9))
else:
    for i in range(1, 10):
        for j in range(1, 10):
            pa = 0
            pa2 = 0
            if i == j:
                pa += 1
                pa2 += 1
            temp = n - (i * 10**(c-1) + j)
            temp2 = n - (j * 10**(c-1) + i)
            if c > 2:
                if temp >= 0:
                    pa += min(10**(c-2), temp//10 + 1)
                if temp2 >= 0:
                    pa2 += min(10**(c-2), temp2//10 + 1)
            else:
                if temp >= 0:
                    pa += 1
                if temp2 >= 0:
                    pa2 += 1
            for k in range(0, c-2):
                pa += 10**k
                pa2 += 10**k
            ans += pa*pa2
            #print(i, j, pa*pa2)
    print(ans)

Submission status: Accepted

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

for i in range(9):
  for j in range(9):
    x=v[i][j]
    y=v[j][i]
    ans+=x*y



print(ans)

Submission status: Accepted

Solution code:
n=int(input())
cnt=0
data=[[0 for j in range(10)] for i in range(10)]
for ii in range(1,n+1):
    i=str(ii)
    if i[-1]!=0:
        data[int(i[-1])][int(i[0])]+=1

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

Submission status: Accepted

Solution code:
n=int(input())
ans=0
n_len=len(str(n))-1



for i in range(1,n+1):
    #print("i:",i)
    if i%10==0:
        continue
    tmp=str(i)
    tmp=tmp[-1]+tmp[0]
    check=0
    if int(tmp)<=n:
        check+=1
    if tmp[0]==tmp[-1]:
        check+=1
    
    for j in range(1,n_len):
        
        cal_min=int(tmp[0]+"0"*j+tmp[-1])
        cal_max=int(tmp[0]+"9"*j+tmp[-1])
        #print("cal:",cal_min,",",cal_max)
        if cal_min>n:
            continue
        if cal_max<=n:
            check+=10**j
            continue
        cal_n=str(n)[1:-1]
        check+=int(cal_n)+1
        if int(str(n)[-1])<int(tmp[-1]):
            check-=1
    #print("check:",check)
    ans+=check
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

for n in range(1, N + 1):
    # nの先頭桁
    n_head = int(str(n)[0])
    # nの末尾桁
    n_tail = int(str(n)[-1])
    matrix[n_head][n_tail] += 1

answer = 0
for n in range(1, N + 1):
    # nの先頭桁
    n_head = int(str(n)[0])
    # nの末尾桁
    n_tail = int(str(n)[-1])
    answer += matrix[n_tail][n_head]

print(answer)

Submission status: Accepted

Solution code:
n = int(input())
d = [str(i) for i in range(1, n+1)]
data = [0] * 100
answer = 0
for i in range(1, n+1):
    a = int(list(str(i))[0])
    b = int(list(str(i))[-1])
    data[a*10 + b] += 1
for i in range(1, 10):
    for j in range(1, 10):
        answer += data[i*10 + j] * data[j*10 + i]
print(answer)

Submission status: Accepted

Solution code:
N_str = input().strip()
N = int(N_str)
Ns = int(N_str[0])
Ne = int(N_str[-1])
w = len(str(N)) - 2
if w > 0:
	Nm = int(N_str[1:-1])
else:
	Nm = 0

count = 0
for A in range(1, N + 1):
	A_str = str(A)
	As = int(A_str[0])
	Ae = int(A_str[-1])
	if Ae == 0:
		continue

	if w > 0:
		for i in range(w - 1):
			count += 10 ** (i + 1)
		
		if Ae < Ns:
			count += 10 ** w
		elif Ae == Ns:
			if As > Ne:
				count += Nm
			else:
				count += Nm + 1

	if Ae == As:
		count += 1
	
	if Ae * 10 + As <= N:
		count += 1
	
print(count)

Submission status: Accepted

Solution code:
N = int(input())
d = len(str(N))
s = [0 for k in range(100)]

for k in range(1,N+1):
    l = len(str(k))
    s[10*(k//(10**(l-1)))+k%10] += 1

ans = 0
for k in range(1,10):
    ans += s[k]**2

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = {
    i: {j: 0 for j in range(10)} for i in range(1, 10)
}
res = 0
for i in range(n+1):
    str_i = str(i)
    t_i = int(str_i[0])
    b_i = int(str_i[-1])
    if b_i == 0:
        continue
    res += d[b_i][t_i] * 2
    d[t_i][b_i] += 1
    res += 1 if t_i == b_i else 0
print(res)

Submission status: Accepted

Solution code:
N=int(input())
P=list(range(100))
for y in range(100):
 P[y]=0
for i in range(N):
 if i+1<=9:
  c=(i+1)*11
  P[c]+=1
 else:
  z=str(i+1)
  d=len(z)
  e=int((i+1)/(10**(d-1)))
  f=(i+1)%10
  g=10*e+f
  P[g]+=1
k=0
for i in range(9):
 for j in range(9):
  s=(i+1)*10+(j+1)
  t=(j+1)*10+(i+1)
  z=P[s]*P[t]
  k+=z
print(k)

Submission status: Accepted

Solution code:
N = int(input())
digit = len(str(N))

ans = 0
for a in range(1, 10):
    for b in range(1, 10):
        x, y = 0, 0
        if a == b:
            if a <= N:
                x += 1
                y += 1

        if a*10+b <= N:
            x += 1

        if b*10+a <= N:
            y += 1

        if digit <= 2:
            ans += x*y
            continue

        d = 3
        while d < digit:
            x += 10**(d-2)
            y += 10**(d-2)
            d += 1

        for i in range(10**(digit-2)):
            if int(str(a)+str(i).zfill(digit-2)+str(b)) <= N:
                x += 1
            if int(str(b)+str(i).zfill(digit-2)+str(a)) <= N:
                y += 1
        ans += x*y

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
INF = float("inf")


def solve(N: int):

    debug = False
    SN = str(N)
    keta = int(math.log10(N)+1)
    count = 0
    for i in range(1, N+1):
        first = str(i)[0]
        last = str(i)[-1]
        if last == "0":         # 考えない
            continue
        # ひとけた
        if first == last:
            count += 1
        # ふたけた
        if int(last+first) <= N:
            count += 1
        if keta < 3:
            continue
        # 3桁以上keta桁未満
        for j in range(3, keta):
            count += 10**(j-2)
        # keta桁
        # Nを超えない数
        if last < SN[0]:
            count += 10**(keta-2)
        elif last == SN[0]:
            # 間の桁について、i桁までまで考える。
            DP = [[0]*2 for _ in range(keta-1)]
            DP[0][False] = 1
            for j in range(1, keta-1):
                DP[j][False] = DP[j-1][False]*1
                DP[j][True] = DP[j-1][True]*10 + DP[j-1][False]*int(SN[j])
            count += DP[-1][True]
            if SN[-1] >= first:
                count += 1
    print(count)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
d = {}
for i in range(1,n+1):
    t = str(i)
    key = t[0]+t[-1]
    if "0" not in key:
        d[key] = d.get(key,0) + 1

for k in d:
    k2 = k[1]+k[0]
    ans += int(d[k])*int(d.get(k2,0))

print(ans)

Submission status: Accepted

Solution code:

import functools as ft

n = int(input())


mp_ = [0]*100 

for i in range(1,n+1):

    t = i%10

    if t == 0:
        continue
    else:

        h = int(str(i)[0])
        
        mp_[h*10 + t] += 1


ans = 0
for i, v in enumerate(mp_):

    if v == 0:
        continue

    #if v != 0:
#    print(i,v)
    j = (i%10)*10 + i//10
    t = mp_[j] * v
    ans += t

print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())

if N < 10:
    print(N)
    exit()


def cnt(s, e):
    ans = 0
    for i in range(0,10):
        num = s * 10**(i+1) + e

        if num > N:
            return ans

        if i == 0:
            ans += 1
            continue

        x = 10**i - 1
        if num + x * 10 <= N:
            ans += x + 1
            continue

        for j in range(10**i-1, -1, -1):
            if num + 10 * j <= N:
                ans += j + 1
                break 
        
        return ans

    return ans

ans = 0
for s, e in permutations([i for i in range(1,10)], 2):
   ans += cnt(s, e) * cnt(e, s)

# 先頭と末尾が同じ場合
for i in range(1, 10):
    ans += (cnt(i, i) + 1) ** 2

print(ans)

Submission status: Accepted

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

def keta(n):
    return len(str(n))

def make(n, l, r):
    ans = 0
    sn = str(n)
    for i in range(5):
        if keta(n) > 2+i:
            ans += 10 ** i
        elif keta(n) == 2 + i:
            if sn[0] > str(l):
                ans += 10 ** i
            elif sn[0] == str(l):
                if sn[-1] < r:
                    ans += int(sn[1:-1])
                else:
                    if sn[1:-1] == '':
                        ans += 1
                    else:
                        ans += int(sn[1:-1]) + 1
            else:
                pass
        else:
            pass
    if l == r:
        ans += 1
    return ans

def main():
    n = int(input().strip())
    ans = 0
    for i in range(1, n+1):
        p = make(n, str(i)[-1], str(i)[0])
        if i % 10 == 0:
            continue
        # if i < 10:
        #     p += 1
        # print("i ,p: ", i, p)
        ans += p
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
keta = len(str(N))
if keta == 1:
    print(N)
    sys.exit()
ans = {}
for i in [1,2,3,4,5,6,7,8,9]:
    for j in [1,2,3,4,5,6,7,8,9]:
        ans[(i, j)] = 0
for i in range(1, N+1):
    maxdigit = int(str(i)[0])
    mindigit = int(str(i)[-1])
    if mindigit != 0:
        ans[(maxdigit, mindigit)] += 1
cnt = 0
for i in range(1, 10):
    for j in range(1, 10):
        if i == j:
            cnt += ans[(i, i)] ** 2
        else:
            cnt += ans[(i, j)] * ans[(j, i)] * 2
            ans[(i, j)] = 0
            ans[(j, i)] = 0
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
a=[[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[]]]
for i in range(n):
    p=str(i+1)
    b,c=int(p[0]),int(p[-1])
    if c>0:
        a[b-1][c-1].append(p)
q=0
for i in range(9):
    for j in range(9):
        q+=len(a[i][j])*len(a[j][i])
print(q)

Submission status: Accepted

Solution code:
N = int(input())

li = [[0 for j in range(9)] for i in range(9)]


for i in range(1,N+1):
    start = str(i)[0]
    end = str(i)[-1]
    if start != '0' and end != '0':
        li[int(start)-1][int(end)-1] += 1
ans = 0
for i in range(1,N+1):
    start = str(i)[0]
    end = str(i)[-1]
    if start != '0' and end != '0':
        ans += li[int(end)-1][int(start)-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())

ll = []
for i in range(n):
  a = list(str(i+1))
  ll.append(a[0]+a[-1])
ls = sorted(list(set(ll)))
lc = []
for s in ls:
  lc.append(ll.count(s))

r = len(ls)
ans = 0
for i in range(r):
  if ls[i][0] == ls[i][1]:
    ans += lc[i] ** 2
  for j in range(i):
    if ls[i][0] == ls[j][1] and ls[i][1] == ls[j][0]:
      ans += 2 * lc[i] * lc[j]

print(ans)

Submission status: Accepted

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

if N < 10:
	print(N)
	sys.exit()

if N < 100:
	cnt = 0
	for i in range(1,N+1):
		if i < 10:
			cnt += 1
			if (i*10)+i <= N:
				cnt += 1
		else:
			s = str(i)
			sa,sb = s[0],s[-1]
			if sb == '0':
				continue
			ss = sb+sa
			if int(ss) <= N:
				cnt += 1
			if sa == sb:
				cnt += 1
	print(cnt)
	sys.exit()



k = len(str(N))
auto = 0
for i in range(k-1):
	auto += 10**i

auto2 = (auto-10**(k-2)) + (int(str(N)[1:]) // 10)
auto3 = auto - 10**(k-2)
m = str(N)[0]
mm = str(N)[-1]
def solve(a,b):
	ans = 0
	if a == b:
		ans += 1
	if a < m:
		return ans + auto
	elif a == m:
		if b <= mm:
			return ans + auto2 + 1
		else:
			return ans + auto2
	else:
		return ans + auto3

cnt = 0
for i in range(1,N+1):
	s = str(i)
	if s[-1] == '0':
		continue
	if i < 10:
		cnt += solve(s,s)
	else:
		sa,sb = s[0],s[-1]
		cnt += solve(sb,sa)

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
x=int(input())
s=set()
d=defaultdict(int)
for i in range(1,x+1):
    p=str(i)
    k=p[0]+p[len(p)-1]
    d[k]+=1
    s.add(k)
a=0    
for i in s:
    c=i[::-1]
    a+=d[i]*d[c]
    
print(a)

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
i = 1
while i <= N:
    if i < 10:
        ans += 1
    elif i < 100:
        if i % 10 == 0:
            pass
        elif i // 10 == i % 10:
            ans += 3
        elif i // 10 > i % 10:
            ans += 2
        else:
            pass
    elif i < 1000:
        if i % 10 == 0:
            pass
        elif i // 100 == i % 10:
            ans = ans + ((i % 100)//10)*2 + 5
        elif i // 100 > i % 10:
            ans += 22
        else:
            ans += 2
    elif i < 10000:
        if i % 10 == 0:
            pass
        elif i // 1000 == i % 10:
            ans = ans + ((i % 1000)//10)*2 + 25
        elif i // 1000 > i % 10:
            ans += 222
        else:
            ans += 22
    elif i < 100000:
        if i % 10 == 0:
            pass
        elif i // 10000 == i % 10:
            ans = ans + ((i % 10000)//10)*2 + 225
        elif i // 10000 > i % 10:
            ans += 2222
        else:
            ans += 222
    else:
        if i % 10 == 0:
            pass
        elif i // 100000 == i % 10:
            ans = ans + ((i % 100000)//10)*2 + 2225
        elif i // 100000 > i % 10:
            ans += 22222
        else:
            ans += 2222
    i +=1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())

#初期値が0の辞書
from collections import defaultdict
nn = defaultdict(lambda: 0) 

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

result=0
#print(nn)
for i in range(1,10):
    for j in range(1,10):
        number=str(i)+str(j)
        numRev=str(j)+str(i)
        if i==j:
            result+=nn[number]*nn[number]
            #nn[number]=0
        else:
            result+=nn[number]*nn[numRev]
            #nn[number]=0
            #nn[numRev]=0

print(result)

Submission status: Accepted

Solution code:
from itertools import product


def get_ms_ls(i):
    temp = list(str(i))
    return int(temp[0]), int(temp[-1])


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

    dp = [[0] * 10 for _ in range(10)]
    for i in range(1, N + 1):
        msb, lsb = get_ms_ls(i)
        dp[msb][lsb] += 1

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    ans = 0
    nf = int(str(n)[0])
    ne = int(str(n)[-1])
    lenn = len(str(n))
    dp = [[0] * 10 for i in range(10)]
    for i in range(1, n + 1):
        f = int(str(i)[0])
        e = int(str(i)[-1])
        dp[f][e] += 1
    ans = 0
    for i in range(1, n + 1):
        f = int(str(i)[0])
        e = int(str(i)[-1])
        ans += dp[e][f]

    print(ans)
    return


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

Submission status: Accepted

Solution code:
n = int(input())
matrix = [[0]*10 for _ in range(10)]
for i in range(1,n+1):
    s = str(i)
    matrix[int(s[0])][int(s[-1])] += 1
total = 0
for i in range(1,10):
    for j in range(1,10):
        total += matrix[i][j]*matrix[j][i]
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  N = int(input())
  dic = {}
  for i in range(1,N+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    if (a,b) in dic:
      dic[(a,b)] += 1
    else:
      dic[(a,b)] = 1
  #print(dic)
  ans = 0
  for i in range(1,10):
    for j in range(1,10):
      if (i,j) in dic and (j,i) in dic:
        ans += dic[(i,j)]*dic[(j,i)]
  print(ans)
    

if __name__ == '__main__':
  main()

Submission status: Accepted

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

N = int(input())

ans = 0

# toriaezu a > 10
for i in range(N):
    a = i + 1

    if a < 10:
        ans += 1
        continue

    if str(a)[-1] == '0':
        continue

    # a > b
    if str(a)[0] > str(a)[-1]:
        for j in range(len(str(a)) - 1):
            ans += (10 ** j) * 2

    # a < b
    elif str(a)[0] < str(a)[-1]:
        for j in range(len(str(a)) - 2):
            ans += (10 ** j) * 2

    # a == b
    else:
        # a:2XX2 > b:2YY2
        if a >= 100:
            ans += int(str(a)[1:-1]) * 2

        # a:2XX2 == b:2XX2
        ans += 1

        # a:2XX2, b:2Y3
        for j in range(len(str(a)) - 2):
            ans += (10 ** j) * 2

        # a:2XX2, b:2
        ans += 2

print(ans)

Submission status: Accepted

Solution code:
N=int(input())

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

for i in range(N):
    temp=list(str(i+1))
    temp=list(map(int,temp))
    lis[temp[0]][temp[-1]]+=1

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:
import numpy as np
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(raw_input())
C = np.zeros(shape=(10,10))

for i in range(N+1):
    i_char = str(i)
    i_first = int(i_char[0])
    i_last = int(i_char[-1])

    C[i_first][i_last] = C[i_first][i_last] + 1

ans = 0

#print "--"                                                                                                                           
#print C                                                                                                                              
#print "--"                                                                                                                           

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

print int(ans)

Submission status: Accepted

Solution code:
N=int(input())
dp=[[0]*9 for _ in range(9)]
for i in range(N):
  a=int(str(i+1)[0])
  b=int(str(i+1)[-1])
  if 1<=a<=9 and 1<=b<=9:
    dp[a-1][b-1]+=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
m = [[0 for j in range(10)] for i in range(10)]
for i in range(1, N+1):
  m[i%10][int(str(i)[0])] += 1

m = np.array(m)
mt = m.T
mm = m * mt
print(np.sum(mm))

Submission status: Accepted

Solution code:
N = int(input())
G = []
for i in range(9):
    G.append([])
for i in G:
    for j in range(9):
        i.append([])

for i in range(1,N+1):
    if i % 10 == 0:
        continue
    x = list(str(i))
    A = int(x[0])
    B = int(x[len(x)-1])
    G[A-1][B-1].append(i)

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
keta=len(str(n))
def f(x,y):
  if keta==1:
    if x==y and x<=n:
      return 1
    else:
      return 0
  elif keta==2:
    if x==y and x*10+y<=n:
      return 2
    elif x==y:
      return 1
    elif x*10+y<=n:
      return 1
    else:
      return 0
  else:
    if x==y:
      ret=2
    else:
      ret=1
    for i in range(1,keta-2):
      ret+=10**i
    if x<int(str(n)[0]):
      ret+=10**(keta-2)
    elif x==int(str(n)[0]):
      if y<=int(str(n)[-1]):
        ret+=int(str(n)[1:keta-1])+1
      else:
        ret+=int(str(n)[1:keta-1])
    return ret

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
from heapq import heapify, heappop, heappush
from itertools import combinations, permutations
from bisect import bisect, bisect_left

def solve():
    input = sys.stdin.readline
    N = int(input())
    strN = str(N)
    maxDig = len(strN)
    count = 0
    for i in range(1, N + 1):
        a = str(i)
        low = int(a[-1])
        if low == 0: continue
        high = int(a[0])
        if low == high: count += 1 #一番上位と下位の桁の数字が同じ
        digb = 2
        while digb < maxDig:
            count += 10 ** (digb - 2)
            digb += 1
        if N >= 10:
            if low > int(strN[0]): continue
            elif low < int(strN[0]): count += 10 ** (digb - 2)
            elif N >= 10:
                DP = [[0, 0] for _ in range(maxDig)]
                DP[0][0] = 1
                for j in range(1, maxDig - 1):
                    DP[j][0] = 1
                    DP[j][1] = DP[j-1][1] * 10 + DP[j-1][0] * (int(strN[j]))

                count += DP[maxDig - 2][1] 
                #print(i, DP)
                if int(strN[maxDig - 1]) >= high: count += 1

    print(count)
        
        

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n=int(input())
check1=[[i,i,0] for i in range(1,10)]
check2=[[i,j,0] for i in range(1,10) for j in range(i+1,10)]
l=len(check2)
check3=[[check2[i][1],check2[i][0],0] for i in range(l)]

for i in range(1,n+1):
    s=str(i)
    s1=int(s[0])
    s2=int(s[-1])
    if s2!=0:
        if s1==s2:
            check1[s1-1][2]+=1
        elif s1<s2:
            x=[0,8,15,21,26,30,33,35]
            check2[x[s1-1]+(s2-s1-1)][2]+=1
        else:
            x=[0,8,15,21,26,30,33,35]
            check3[x[s2-1]+(s1-s2-1)][2]+=1
cnt=0
for i in range(9):
    cnt+=(check1[i][2])*(check1[i][2])
for i in range(l):
    cnt+=2*(check2[i][2]*check3[i][2])
print(cnt)

Submission status: Accepted

Solution code:
# = int(input())
# = map(int, input().split())
# = list(map(int, input().split()))
# = list(input())
# = [tuple(map(int, input().split())) for _ in range(n)]

n = int(input())

d = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, n+1):
    temp = list(str(i))
    h = int(temp[0])
    t = int(temp[-1])
    if t != 0:
        d[h][t] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())


def msb(x):
    while x >= 10:
        x //= 10
    return x


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

for i in range(1, N + 1):
    l = i % 10
    if l == 0:
        continue
    m = msb(i)
    n[l - 1][m - 1] += 1

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

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache

d={}

N=int(input())

for i in range(1,N+1):
    start=int(str(i)[0])
    end=int(str(i)[-1])
    tmp=d.get((start,end),0)
    d[(start,end)]=tmp+1

retval=0

for i in range(1,10):
    for j in range(1,10):
        retval+=d.get((i,j),0)*d.get((j,i),0)

print(retval)

Submission status: Accepted

Solution code:
N=int(raw_input())

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

for i in range(1,N+1):
    if i % 10 > 0:
        num[int(str(i)[0])][i % 10] += 1

#print num

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

Submission status: Accepted

Solution code:
N = int(input())
table = [[0]*9 for _ in range(9)]
for n in range(1,N+1):
    tail = str(n)[-1]
    if tail == '0':
        continue
    head = str(n)[0]
    table[int(head)-1][int(tail)-1] += 1
ans = 0
# for i in range(9):
#     print(table[i])
for i in range(9):
    for j in range(i+1):
        #print(i,j)
        if i == j:
            temp = table[i][j]*table[j][i]
        else:
            temp = 2*table[i][j] * table[j][i]
        ans += temp

# if N <= 10:
#     print(N+1)
#     exit()
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int):
    x=[[0]*10 for _ in range(10)]
    for i in range(N):
        h=int(str(i+1)[0])
        t=(i+1)%10
        x[h][t]+=1

    ans=0
    for i in range(9):
        ans+=x[i+1][i+1]**2

    for i in range(9):
        for j in range(8-i):
            ans+=x[i+1][j+i+2]*x[j+i+2][i+1]*2

    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()