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:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from sys import stdin
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)
from pprint import pprint


'''
25
'''

def solve(x):
	d = {}
	for i in range(1,10):
		for j in range(1,10):
			s = str(i) + str(j)
			d[s] = 0

	rr = {}
	for i in d:
		rr[i] = i[::-1]
		rr[i[::-1]] = i

	ans = 0
	for i in range(1,x+1):
		ii = str(i)
		if(ii[-1] != '0'):
			s = ii[0] + ii[-1]
			r = rr[s]
			d[s] += 1
	for i in d:
		ans += d[i]*d[rr[i]]
	return ans

N = int(stdin.readline())
ans = solve(N)
print(ans)

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from sys import stdin
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)
from pprint import pprint


'''

'''

def solve(x):
	d = {}
	for i in range(1,10):
		for j in range(1,10):
			s = str(i) + str(j)
			d[s] = 0
	# pprint(d)
	rr = {}
	for i in d:
		rr[i] = i[::-1]
		rr[i[::-1]] = i
	# l = [i for i in range(1,x+1)]
	ans = 0
	for i in range(1,x+1):
		ii = str(i)
		if(ii[-1] != '0'):
			s = ii[0] + ii[-1]
			r = rr[s]
			d[s] += 1
			# d[r] += 1
			# dd[s].append(i)
			# if(len(ii) > 1):
				# if(i not in dd[r]):
					# dd[r].append(i)
	# print(d)
	# pprint(dd)
	# pprint(d)
	for i in d:
		ans += d[i]*d[rr[i]]
	return ans

N = int(stdin.readline())
ans = solve(N)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

ans = 0
for x in range(1,10):
    for y in range(1,10):
        a_count = 0
        b_count = 0
        for z in range(1,7):
            if z == 1:
                if x == y and x <= N:
                    a_count += 1
                continue
            num_min = y + x * (10 ** (z-1))
            num_max = y + x * (10 ** (z-1)) + 10 * ((10 ** (z-2))-1)
            if num_max >= N:
                a_count += max(0,(N-num_min)//10+1)
                break
            a_count += 10 ** (z-2)
        for z in range(1,7):
            if z == 1:
                if x == y and x <= N:
                    b_count += 1
                continue
            num_min = x + y * (10 ** (z-1))
            num_max = x + y * (10 ** (z-1)) + 10 * ((10 ** (z-2))-1)
            if num_max >= N:
                b_count += max(0,(N-num_min)//10+1)
                break
            b_count += 10 ** (z-2)
        ans += a_count * b_count
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[]
for i in range(1,n+1):
  t=list(str(i))
  l.append((t[0],t[-1]))
ans=0
for i in range(1,10):
  for j in range(1,10):
    ans+=l.count((str(i),str(j)))*l.count((str(j),str(i)))
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(n):
    x = str(i + 1)
    c[int(x[0])][int(x[-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())
D={}
DD={}
for i in range(1,n+1):
   s=(str(i)[0],str(i)[-1])
   t=(str(i)[-1],str(i)[0])
   if s not in D:
      D[s]=1
   else:
      D[s]+=1
   if t not in DD:
      DD[t]=1
   else:
      DD[t]+=1
ans=0
for i in D:
   if i in DD:
      ans+=D[i]*DD[i]
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n = int(input())
cnt = [[0] * 9 for i in range(9)]
# print(len(cnt), len(cnt[0]))
# print(cnt)
for i in range(1, n + 1):
    prefix = int(str(i)[0])
    suffix = int(str(i)[-1])
    if suffix != 0:
        cnt[prefix - 1][suffix - 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 = {(str(i), str(j)):0 for i in range(10) for j in range(10)}
for i in range(1, N+1):
    s = str(i)
    c[(s[0], s[-1])] += 1
ans = 0
for i in range(10):
    for j in range(10):
        i, j = str(i), str(j)
        ans += c[(i, j)] * c[(j, i)]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
d = defaultdict(int)
for i in range(1, N+1):
    si = str(i)
    d[(si[0], si[-1])] += 1
ans = 0
for i in '123456789':
    for j in '123456789':
        ans += d[(i, j)] * d[(j, i)]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
memo = dict()
for i in range(1,n+1):
    string_i = str(i)
    com = (string_i[0], string_i[-1])
    if com in memo:
        memo[com] += 1
    else:
        memo[com] = 1
ans = 0
for i in range(1,n+1):
    string_i = str(i)
    com = (string_i[-1], string_i[0])
    if com in memo:
        ans += memo[com]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
digit=len(str(N))
first=int(str(N)[0])
last=N%10
mid=(N-first*10**(digit-1)-last)//10
table=[[0 for i in range(0,10)]for j in range(0,10)]

if digit==1:
    print(N)
else:
    for i in range(1,10):
        for j in range(1,10):
            if i!=j:
                table[i][j]=(10**(digit-1)-10)//90
            else:
                table[i][j]=(10**(digit-1)-10)//90+1
    for i in range(1,10):
        for j in range(1,10):
            if i<first:
                table[i][j]+=10**(digit-2)
            elif i==first:
                if last>=j:
                    table[i][j]+=mid+1
                else:
                    table[i][j]+=mid
            else:
                table[i][j]+=0

    sum=0
    for i in range(0,10):
        for j in range(0,10):
            if i!=j:
                sum+=table[i][j]*table[j][i]

    subsum=0
    for i in range(0,10):
        subsum+=table[i][i]*(table[i][i]-1)+table[i][i]
    print(sum+subsum)

Submission status: Accepted

Solution code:
import itertools
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
sys.setrecursionlimit(10**9)

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

    counter = [[0] * 10 for _ in range(10)]
    for i in range(N+1):
        i_str = str(i)
        counter[int(i_str[0])][int(i_str[-1])] += 1

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

    print(ans)

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
from fractions import gcd 
from functools import reduce
import sys
sys.setrecursionlimit(200000000)
from inspect import currentframe


# my functions here!
#標準エラー出力
def printargs2err(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' : '+repr(arg) for arg in args),file=sys.stderr)
def debug(x):
    print(x,file=sys.stderr)
def printglobals():
    for (symbol, value) in globals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
def printlocals():
    for (symbol, value) in locals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)


#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
#どっかで最悪計算量の入力データを用意する関数を作ろう？
"""
#バケット法行こうか
"""
#solution:
N,=pin()#第一引数
ans=0
htbacket=[[0]*10 for i in range(10)]
for i in range(1,N+1):
    a=int(str(i)[0])#先頭と末尾
    b=int(str(i)[-1])
    htbacket[a][b]+=1
#print(htbacket)

for i in range(10):
    for j in range(10):
        pass
        #htbacket[a],[b]とペアを取れる数を列挙したい
        if i==j:
            ans+=htbacket[i][j]**2
        else:
            ans+=htbacket[i][j]*htbacket[j][i]
print(ans)
#print([["NA","YYMM"],["MMYY","AMBIGUOUS"]][cMMYY][cYYMM])
"""
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""

Submission status: Accepted

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

dp = [[0] * 10 for _ in range(10)]
for i in range(1, n + 1):
    s = str(i)
    x = int(s[0])
    y = int(s[-1])
    dp[x][y] += 1

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

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

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

# c[i][j]: 先頭の桁の数がiで、末尾の桁の数がjとなる個数
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
# AとBは区別できる
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())
c=[]
for i in range(10):
  c.append([0]*10)
for i in range(1,n+1):
  for j in range(1,10):
    for k in range(1,10):
      if str(i)[0]==str(j) and str(i)[len(str(i))-1]==str(k):
        c[j][k]+=1
num=0
for j in range(1,10):
  for k in range(1,10):
    num+=c[j][k]*c[k][j]
print(num)

Submission status: Accepted

Solution code:
N = int(input())
table = [[0] * 10 for _ in range(10)]
for i in range(1,N+1):
    start = int(str(i)[0])
    end = int(str(i)[-1])
    table[start][end] += 1
ret = 0
for i in range(1, N+1):
    start = int(str(i)[0])
    end = int(str(i)[-1])
    ret += table[end][start]
print(ret)

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

Solution code:
n=int(input())
l=len(str(n))
top=int(str(n)[0])
bottom=int(str(n)[-1])
ans=0
for i in range(1,n+1):
  first=int(str(i)[0])
  last=int(str(i)[-1])
  if last==0:
    continue
  if first==last:
    ans+=1
  if last*10+first<=n:
    ans+=1
  for i in range(1,l-2):
    ans+=10**i
  if l>=3:
    if last>top:
      continue
    elif last<top:
      ans+=10**(l-2)
    else:
      if first<=bottom:
        ans+=int(str(n)[1:-1])+1
      else:
        ans+=int(str(n)[1:-1])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

#print(kouho)

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

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(1,10):
        ans += d[i][j] * d[j][i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
arr = [str(i) for i in range(1, N+1)]
total = 0
pair = {(str(i), str(j)): 0 for i in range(0, 10) for j in range(0, 10)}
for i in arr:
    a = i[0]
    b = i[-1]
    pair[(a, b)] += 1
for i in arr:
    a = i[-1]
    b = i[0]
    total += pair[(a, b)]
print(total)

Submission status: Accepted

Solution code:
N = int(input())

freq = [[ 0 for i in range(9)] for i in range(9)]

for i in range(1,N + 1):
  num = str(i)
  if num[-1] != "0":
    p = int(num[0])
    q = int(num[-1])
    freq[p - 1][q - 1] += 1

ans = 0

for i in range(9):
  for j in range(9):
    A = freq[i][j]
    B = freq[j][i]
    ans += A * B

print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    d = defaultdict(int)
    for i in range(1,N+1):
        s = str(i)
        h,t = s[0],s[-1]
        d[h,t]+=1
    ans  = 0  
    for i in range(1,N+1):
        s = str(i)
        h,t = s[0],s[-1]
        ans += d[t,h]
    print(ans)


if __name__ == '__main__':
    main()
    # S=ST()
    # ans = 0
    # for i in range(len(S)):
    #     for it in combinations([i for i in range(len(S))], i):
    #         before = 0
    #         for j in it:
    #             print(int(S[before:j+1]), before,j)
    #             ans += int(S[before:j+1])
    #             before = j+1
    #         try:
    #             ans += int(S[before:])
    #         except:
    #             pass
    #         print(ans)
    # print(ans)

Submission status: Accepted

Solution code:
n = int(input())

digit_dict = {}
for i in range(1,10):
    for j in range(1,10):
        now = i *10 + j
        new = {now:0}
        digit_dict.update(new)

ans = 0

for num in range(1, n+1):
    top = int(str(num)[0])
    end = int(str(num)[-1])
    if end == 0:
        continue
    if top == end:
        ans += 1

    now = top * 10 + end
    now_rev = end * 10 + top
    
    ans += digit_dict[now_rev] * 2
    digit_dict[now] += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
# (先頭A,末尾B)を表すリストを作る
L = [[0 for i in range(10)]for i in range(10)]

for i in range(1,N+1):
  #str変換で先頭と末尾を取る
  A = int(str(i)[0])
  B = int(str(i)[-1])
  #該当するリストに足す
  L[A][B] += 1
  
ans = 0

#リストの反転した形とかける
for i in range(10):
  for j in range(10):
    ans += L[i][j] * L[j][i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
# 先頭が i でお尻が j の数がN以内にいくつあるか
lst = [[0 for _ in range(10)] for _ in range(10)]

def f(a):
	now = str(i)
	top = int(now[0])
	bottom = int(now[-1])
	return top, bottom

for i in range(1, N + 1):
	top, bottom = f(i)
	lst[top][bottom] += 1

ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())
letters_mapping = {num: {num2:[] for num2 in range(10)} for num in range(10)}

for num in range(1, n + 1):
    first_letter = str(num)[0]
    last_letter = str(num)[-1]
    letters_mapping[int(first_letter)][int(last_letter)].append(num)

count = 0
for i, v in letters_mapping.items():
    for j, x in letters_mapping[i].items():
        for num in letters_mapping[i][j]:
            count += len(letters_mapping[j][i])
            
print(count)

Submission status: Accepted

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

for i in range(1, N+1):
    STR = str(i)
    a = int(STR[0])
    b = int(STR[-1])
    C[a][b] += 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())
letters_mapping = {num: {num2:[] for num2 in range(10)} for num in range(10)}

for num in range(1, n + 1):
    first_letter = str(num)[0]
    last_letter = str(num)[-1]
    letters_mapping[int(first_letter)][int(last_letter)].append(num)

count = 0
for i, v in letters_mapping.items():
    for j, x in letters_mapping[i].items():
        for num in letters_mapping[i][j]:
            count += len(letters_mapping[j][i])
            
print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input()) 
counter = defaultdict(int)

for s in map(str, range(1, n+1)):
    head = s[0]
    tail = s[-1]
    counter[(head, tail)] += 1
    
ans = 0
items = list(counter.items())
for (head, tail), cnt in items:
    ans += cnt*counter[(tail, head)]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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


print(ans)

Submission status: Accepted

Solution code:
#import math
#import numpy as np
N = int(input())
ans = 0
dp=[[0]*10 for _ in range(10)]

for a in range(1, N+1):
    a_t = int(str(a)[0])
    a_b = int(str(a)[-1])
    dp[a_t][a_b]+=1

for b in range(1,N+1):
    b_t = int(str(b)[0])
    b_b = int(str(b)[-1])
    ans+=dp[b_b][b_t]

print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(N):
	a = i + 1
	b = a % 10
	while a > 0:
		if a < 10:
			break
		a = a // 10
	res[a][b] = res[a][b] + 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


def create_tuple(x):
    a = x % 10
    b = int(str(x)[0])
    return (a, b)


N = int(input())
mp = defaultdict(int)
ans = 0

for i in range(1, N + 1):
    p = create_tuple(i)
    mp[p] += 1

for i in range(1, N + 1):
    p = create_tuple(i)
    q = (p[1], p[0])
    ans += mp[q]

print(ans)

Submission status: Accepted

Solution code:
#!python3

def resolve():
    N = tuple(map(int,input()))

    if len(N) == 1:
        print(N[0])
        return

    n = len(N)
    dp = [[0]*10 for i in range(10)]

    n0, nx = N[0], N[-1]
    sx1 = (10**(n-1) - 1) // 9
    sx2 = (10**(n-2) - 1) // 9
    sxn = 0
    for i in range(1, n-1):
        sxn = sxn * 10 + N[i]
    for ax in range(1, 10):
        for a1 in range(1, n0):
            dp[a1][ax] = sx1

        for a1 in range(n0, 10):
            dp[a1][ax] = sx2

        dp[ax][ax] += 1

        dp[n0][ax] += sxn
        if ax <= nx:
            dp[n0][ax] += 1

    #for x in dp:
    #    print(x)

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())

A=[[0 for s in range(10)] for t in range(10)]


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

x=0
for s in range(10):
	for t in range(10):
		x+=A[s][t]*A[t][s]
print(x)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

def f(x):
  a = x%10
  b = 0
  while x > 0:
    b = x
    x //= 10
  return a,b

freq = np.zeros((10,10), dtype=int)
for k in range(1,N+1):
  a,b = f(k)
  freq[a,b] += 1

print((freq*freq.T).sum())

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 i in D:
    t=(i[1],i[0])
    if t in D:
        ans+=D[t]*D[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [[0 for i in range(10)] for j in range(10)]
for x in range(1, N+1):
    s1 = str(x)[0]
    s2 = str(x)[-1]
    a[int(s1)][int(s2)] += 1

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

Submission status: Accepted

Solution code:
#!python3

def resolve():
    N = tuple(map(int,input()))

    if len(N) == 1:
        print(N[0])
        return

    n = len(N)
    dp = [[[0]*10 for j in range(2)] for i in range(2)]

    k0  = N[0]

    dp0, dp1 = dp

    dp0[0][k0] = 1
    for i in range(1, k0):
        dp0[1][i] = 1
    for i in range(1, n-1):
        for k in range(1, 10):
            dp1[1][k] = 10 * dp0[1][k] + 1

        x, y = N[i], dp0[0][k0]
        dp1[1][k0] += x * y
        dp1[0][k0] = y

        #print(dp1)
        dp0, dp1 = dp1, dp0

    dp2 = [[0]*10 for i in range(10)]
    for i in range(1, 10):
        for j in range(1, 10):
            dp2[i][j] = dp0[1][i]

        for j in range(1, N[-1]+1):
            dp2[i][j] += dp0[0][i]

        dp2[i][i] += 1

    #for x in dp2:
    #    print(x)

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
#!python3

def resolve():
    N = tuple(map(int,input()))

    if len(N) == 1:
        print(N[0])
        return

    n = len(N)
    dp = [[[0]*10 for j in range(2)] for i in range(2)]

    k0  = N[0]
    dp[0][0][k0] = 1

    dp0, dp1 = dp
    for i in range(n-1):
        xx = 1 if i else 0

        for k in range(1, 10):
            dp1[1][k] = 10 * dp0[1][k] + xx

        x, y = N[i], dp0[0][k0]
        if i == 0:
            dp1[0][k0] = y
            for k in range(1, k0):
                dp1[1][k] = y
        else:
            dp1[1][k0] += x * y
            dp1[0][k0] = y

        #print(dp1)
        dp0, dp1 = dp1, dp0

    dp2 = [[0]*10 for i in range(10)]
    for i in range(1, 10):
        for j in range(1, 10):
            dp2[i][j] = dp0[1][i]

        for j in range(1, N[-1]+1):
            dp2[i][j] += dp0[0][i]

        dp2[i][i] += 1

    #for x in dp2:
    #    print(x)

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def d10(n):
    a = []
    while n:
        n, m = divmod(n, 10)
        a.append(m)

    return a

def resolve():
    N = tuple(map(int,input()))

    if len(N) == 1:
        print(N[0])
        return

    n = len(N)
    dp = [[[0]*10 for j in range(2)] for i in range(2)]

    k0  = N[0]
    dp[0][0][k0] = 1

    dp0, dp1 = dp
    for i in range(n-1):
        xx = 1 if i else 0

        for k in range(1, 10):
            dp1[1][k] = 10 * dp0[1][k] + xx

        x, y = N[i], dp0[0][k0]
        if i == 0:
            dp1[0][k0] = y
            for k in range(1, k0):
                dp1[1][k] = y
        else:
            dp1[1][k0] += x * y
            dp1[0][k0] = y

        #print(dp1)
        dp0, dp1 = dp1, dp0

    dp2 = [[0]*10 for i in range(10)]
    for i in range(1, 10):
        for j in range(1, 10):
            dp2[i][j] = dp0[1][i]

        for j in range(1, N[-1]+1):
            dp2[i][j] += dp0[0][i]

        dp2[i][i] += 1

    #for x in dp2:
    #    print(x)

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




if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
res = 0
list_ = [[0 for j in range(10)] for i in range(10)]
for i in range(1, n + 1):
    s = list(str(i))
    initl = int(s[0])
    end = int(s[-1])
    list_[initl][end] += 1
for i in range(1, 10):
    for j in range(1, 10):
        res += list_[i][j] * list_[j][i]
print(res)

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
    
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())
p = [[0 for _ in range(10)] for _2 in range(10)]
ans = 0
for i in range(1, n+1):
  l = int(str(i)[0])
  r = int(str(i)[-1])
  ans += 2 * p[r][l]
  p[l][r] += 1
  if l == r:
    ans += 1
print(ans)

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

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

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S=input()
N=int(S)
keta=len(S)
ans=[[0]*10 for _ in range(10)]
fa=0
for A in range(1,N+1):
    ns=str(A)
    tailA=int(ns[-1])
    if tailA==0: continue
    headA=int(ns[0])
    if ans[headA][tailA]==0:
        dp=[[[0]*2 for __ in range(2)] for _ in range(keta+1)]
        dp[0][0][1]=1
        for i in range(keta):
            x=int(S[i])
            for j in range(2):
                for k in range(2):
                    for n in range(10):
                        ni=i+1
                        nj=1 if j==1 or n>0 else 0
                        nk=1 if k==1 and n==x else 0
                        if k==1 and n>x: continue
                        if j==0 and nj==1 and n!=tailA: continue
                        if ni==keta and n!=headA: continue
                        dp[ni][nj][nk]+=dp[i][j][k]
        for k in range(2):
            ans[headA][tailA]+=dp[keta][1][k]
    fa+=ans[headA][tailA]
print(fa)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
Matrix = np.zeros((9, 9), dtype=np.int64)

for n in range(N + 1):
    if not n % 10:
        continue
    string = str(n)
    first = int(string[0])
    last = int(string[-1])
    Matrix[first - 1][last - 1] += 1

print((Matrix * Matrix.T).sum())

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(10):
    for j in range(10):
        ans += cnt[i][j] * cnt[j][i]
print(ans)

Submission status: Accepted

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

for i in range(n+1):
	left=int(str(i)[0])
	right=i%10
	c[left][right]+=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 sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    d = collections.defaultdict(int)
    for i in range(1, n + 1):
        s = str(i)
        d[s[0], s[-1]] += 1
    cnt = 0
    for i in range(1, n + 1):
        s = str(i)
        cnt += d[s[-1], s[0]]
    print(cnt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
d = {}
for i in range(1, N+1):
  i = str(i)[0] + str(i)[-1]
  d[i] = d.get(i, 0) + 1
ans = 0
for i in map(str, range(1, 10)):
  for j in map(str, range(1, 10)):
    ans += d.get(i+j, 0) * d.get(j+i, 0)
print(ans)

Submission status: Accepted

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

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

ans = 0

def calc(i):
    lst = []
    while i!=0:
        lst.append(i%10)
        i = i//10
    lst.reverse()
    return lst
  
for a in range(1, N+1):
    lsta = calc(a)
    m = len(lsta)

    if lsta[0] > lsta[m-1]:
        if lsta[m-1]==0:continue
        ans += sum([2*10**(l) for l in range(m-1)])
    elif lsta[0] == lsta[-1]:
        if m <= 2:ans += 2*m-1
        else:
            ans += (a%(10**(m-1))//10)*2 +1
            for l in range(m-2):
                ans += 10**(l)*2
            ans += 2
      
    else:
        if m == 2:continue
        else:
            for l in range(m-2):
                ans += 2*10**(l)
print(ans)