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:
import sys
from itertools import permutations


def main():
    input = sys.stdin.readline
    n = int(input())
    cache = {}
    ans = 0

    for i in range(1, n+1):
        if (int(str(i)[0]), int(str(i)[-1])) in cache.keys():
            cache[(int(str(i)[0]), int(str(i)[-1]))] += 1
        else:
            cache[(int(str(i)[0]), int(str(i)[-1]))] = 1

    for k, v in cache.items():
        if k[0] == k[1] and v == 1:
            ans += v**2
        
        elif (k[1], k[0]) in cache.keys():
            ans += cache[(k[1], k[0])] * v

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=[[0]*9 for i in range(9)]
sum=0
for i in range(1,N+1):
  s=str(i)
  if(int(s[0])!=0 and int(s[-1])!=0):
    A[int(s[0])-1][int(s[-1])-1]+=1
for i in range(9):
  for j in range(9):
    sum+=A[i][j]*A[j][i]
  #print(A[i][i])
#print(A)  
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

# 先頭末尾の組み合わせを数える
for i in range(1,N+1):
    x = str(i)
    counter[int(x[0])][int(x[-1])] += 1

ans = 0

# (先頭末尾)と(末尾先頭)の組み合わせ
for i in range(10):
    for j in range(10):
        ans += counter[i][j] * counter[j][i]
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

ans = 0

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

Submission status: Accepted

Solution code:
n=int(input())
List=[]
List+=[0]*100
for i in range(1,n+1):
  List[int(str(i)[0])*10+int(str(i)[-1])]+=1
sum=0
for i in range(1,10):
  sum+=List[11*i]**2
for i in range(1,10):
  for j in range(1,10):
    if i!=j:
      sum+=List[10*i+j]*List[10*j+i]
print(sum)

Submission status: Accepted

Solution code:
def solve(N):
    ret = 0
    counter = [[0 for _ in range(10)] for _ in range(10)]
    for n in range(1, N + 1):
        i, j = map(int, [str(n)[0], str(n)[-1]])
        counter[i][j] += 1

    for i in range(10):
        for j in range(10):
            ret += counter[i][j] * counter[j][i]
    return ret


if __name__ == '__main__':
    N = int(input())
    ret = solve(N)
    print(ret)

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)
    dp[int(s[0])][int(s[-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())
A=[[0 for i in range(9)] for i in range(9)]
for i in range(1,n+1):
  q=int(str(i)[0])
  w=i%10 
  if w==0 : continue
  A[q-1][w-1]+=1
ans=0
for i in range(9):
  for j in range(9):
    ans+=A[i][j]*A[j][i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    N = int(input())
    combs = [(S[0], S[-1]) for S in map(str, range(1, N+1))]
    d = defaultdict(int)
    for pre, suf in combs:
        d[(pre, suf)] += 1
        d[(suf, pre)]

    ans = 0
    for key, val in d.items():
        pre, suf = key
        ans += val * d[(suf, pre)]
        
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    N = int(input())
    combs = [(S[0], S[-1]) for S in map(str, range(1, N+1))]
    d = defaultdict(int)
    for pre, suf in combs:
        if suf != "0":
            d[(pre, suf)] += 1
            d[(suf, pre)]

    ans = 0
    for key, val in d.items():
        pre, suf = key
        ans += val * d[(suf, pre)]
        
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
c = np.zeros((10, 10))
n = int(input())
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(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
ans_lis = [[0] * 9 for i in range(9)]
for i in range(1,N+1):
  t = str(i)
  mae = int(t[0])
  ushiro = int(t[-1])
  if ushiro == 0:
    continue
  ans_lis[mae-1][ushiro-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    ans += ans_lis[i][j] * ans_lis[j][i]
print(ans)

Submission status: Accepted

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

    freq = {}
    for i in range(1, N+1):
        pair = str(i)[0] + str(i)[-1]
        freq.setdefault(pair, 0)
        freq[pair] += 1
    
    ans = 0
    freq_keys = freq.keys()
    for i in range(1, N+1):
        pair_rev = str(i)[-1] + str(i)[0]
        if pair_rev in freq_keys:
            ans += freq[pair_rev]
    
    print(ans)    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    freq = {}
    for i in range(1, N+1):
        pair = str(i)[0] + str(i)[-1]
        freq.setdefault(pair, 0)
        freq[pair] += 1
    
    ans = 0
    for i in range(1, N+1):
        pair_rev = str(i)[-1] + str(i)[0]
        if pair_rev in freq.keys():
            ans += freq[pair_rev]
    
    print(ans)
     
      
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    freq = {}
    for i in range(1, N+1):
        pair = str(i)[0] + str(i)[-1]
        if pair in freq.keys():
            freq[pair] += 1
        else:
            freq[pair] = 1
    
    ans = 0
    for i in range(1, N+1):
        pair_rev = str(i)[-1] + str(i)[0]
        if pair_rev in freq.keys():
            ans += freq[pair_rev]
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0

for k in range(1,N+1):
    l,r = str(k)[0],str(k)[-1]
    nums[int(l)][int(r)] += 1


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

Submission status: Accepted

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

def main():
     n = int(input())
     matrix = [[0 for i in range(10)] for j in range(10)]
     ans = 0

     for i in range(1, n+1):
          s = str(i)
          first = int(s[0])
          end = int(s[-1])
          matrix[first][end] += 1
          # print(matrix)

     for i in range(1, 10):
          for j in range(1, 10):
               ans += matrix[i][j] * matrix[j][i]
               # print('{}, {}, {}'.format(i, j, ans))
     print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict as d
dic=d(int)
for i in range(1,int(input())+1):
  dic[(int(str(i)[0]),i%10)]+=1
ans=0
for i in range(10):
  for j in range(10):
    ans+=dic[(i,j)]*dic[(j,i)]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

ans = 0

for i in range(1, 10):
    for j in range(1, 10):
        ans += pat[i][j] * pat[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 k in range(1, n + 1):
    c[int(str(k)[0])][int(str(k)[-1])] += 1

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

Submission status: Accepted

Solution code:
import math

n = int(input())
mp = [[0 for _ in range(10)] for _ in range(10)]

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

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

print(sm)

# c = 0
# for i in range(1, n):
#     for j in range(1, n):
#         si, sj = str(i), str(j)
#         if si[0] == sj[-1] and si[-1] == sj[0]:
#             c += 1

Submission status: Accepted

Solution code:
import math
import collections
import sys

input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]

n=I()
info=[]
for i in range(1,n+1):
    if i%10!=0:
        info.append(str(i)[0]+str(i)[-1])
#print(info)
c=collections.Counter(info)
#print(c)
ans=0
for i in range(1,10):
    for j in range(1,10):
        if i==j:
            ans+=(c[str(i)+str(j)])**2
            #print(i,j,end=" ")
            #print((c[str(i)+str(j)])**2)
        else:
            ans+=(c[str(i)+str(j)])*(c[str(j)+str(i)])
            #print(i,j,end=" ")
            #print((c[str(i)+str(j)])*(c[str(j)+str(i)]))
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

n = n_in()
from collections import Counter
c = Counter()

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

from itertools import product

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

Submission status: Accepted

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

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

ans = 0
for p in range(9):
    for q in range(9):
        ans += cheak_box[p][q] * cheak_box[q][p]
        
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n_str=input()
n_int=int(n_str)
cnt=[[0]*9 for i in range(9)]

for i in range(1,n_int+1):
    if i%10==0:continue
    temp=str(i)
    cnt[int(temp[0])-1][int(temp[-1])-1]+=1

ans=0
for i in range(9):
    for j in range(9):
        if cnt[i][j]==0:continue
        if i==j:
            temp=cnt[i][j]
            ans+=temp**2
        else:
            ans+=cnt[i][j]*cnt[j][i]
print(ans)
#print(*cnt,sep='\n')

Submission status: Accepted

Solution code:
N = int(input())

n = 1

M = N
while M // 10 != 0:
    M //= 10
    n += 1
# nはNの桁数

c = N // (10 ** (n - 1))  # cはNの最高位
d = N % 10  # dはNの最低位


def f(a, b):  # N以下の最高位a、最低位bの正整数の個数
    if a < c:
        if a == b:
            return ((10 ** (n - 1)) // 9) + 1
        else:
            return ((10 ** (n - 1)) // 9)
    elif a > c:
        if a == b:
            if n == 1:
                return 0
            else:
                return ((10 ** (n - 2)) // 9) + 1
        else:
            if n == 1:
                return 0
            else:
                return ((10 ** (n - 2)) // 9)
    else:
        if a == b:
            if n == 1:
                return 1
            elif n == 2:
                if b <= d:
                    return 2
                else:
                    return 1
            else:
                if b <= d:
                    return 2 + (10 ** (n - 2)) // 9 + (N - c * (10 ** (n - 1))) // 10
                else:
                    return 1 + (10 ** (n - 2)) // 9 + (N - c * (10 ** (n - 1))) // 10
        else:
            if n == 1:
                return 0
            elif n == 2:
                if b <= d:
                    return 1
                else:
                    return 0
            else:
                if b <= d:
                    return 1 + (10 ** (n - 2)) // 9 + (N - c * (10 ** (n - 1))) // 10
                else:
                    return 0 + (10 ** (n - 2)) // 9 + (N - c * (10 ** (n - 1))) // 10


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
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
ans = 0
memo = {}
for i in range(1, n + 1):
    i = str(i)
    if (i[-1], i[0]) not in memo:
        memo[(i[-1], i[0])] = 1
    else:
        memo[(i[-1], i[0])] += 1
for i in range(1, n + 1):
    i = str(i)
    if (i[0], i[-1]) in memo:
        ans += memo[(i[0], i[-1])]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from itertools import product

n = int(input())

c = np.zeros((10, 10), dtype=np.int)
for i in range(1, n+1):
    first = int(str(i)[0])
    last = int(str(i)[-1])
    c[first, last] += 1

res = 0
for i, k in product(range(10), repeat=2):
    res += c[i, k] * c[k, i]

print(res)

Submission status: Accepted

Solution code:
n = int(input())

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

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
count = 0
work1 = [0,1,10,10**2,10**3,10**4,10**5]
work2 = [0,1,1,10,10**2,10**3,10**4]
for i in range(1, n+1):
    if i//10 == 0:
        count += 1
    elif int(str(i)[0]) > int(str(i)[-1]) and str(i)[-1] != '0':
        temp = len(str(i))
        count += sum(work1[:temp])*2
    elif int(str(i)[0]) < int(str(i)[-1]) and str(i)[-1] != '0':
        temp = len(str(i))-1
        count += sum(work1[:temp])*2
    elif str(i)[0] == str(i)[-1]:
        temp = len(str(i))
        count += sum(work2[:temp])*2+1
        if temp >= 3:
            count += int(str(i)[1:-1])*2
print(count)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    dic = {}
    for i in range(10):
        for j in range(10):
            dic[(i, j)] = 0
    for i in range(1, N+1):
        tmp = str(i)
        s, e = int(tmp[0]), int(tmp[-1])
        dic[(s, e)] += 1
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += dic[(i, j)] * dic[(j, i)]
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    dic = {}
    for i in range(1, N+1):
        tmp = str(i)
        s, e = int(tmp[0]), int(tmp[-1])
        if (s, e) in dic:
            dic[(s, e)] += 1
        else:
            dic[(s, e)] = 1
    
    ans = 0
    for (i, j), count in dic.items():
        tmp = 0
        if i == j:
            tmp = count**2
        else:
            if (j, i) in dic:
                tmp = count * dic[(j, i)]
        ans += tmp
        # print(i, j, count)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

forward = np.zeros(100, dtype=int)
backward = np.zeros(100, dtype=int)

for i in range(1, N+1):
    top, bottom = int(str(i)[0]), i % 10
    forward[top * 10 + bottom] += 1
    backward[bottom * 10 + top] += 1
    
res = (forward * backward).sum()
print(res)

Submission status: Accepted

Solution code:
n = int(input())
dict_par = dict()
for i in range(1,10):
    i = str(i)
    for k in range(1,10):
        k = str(k)
        dict_par[i+k] = 0
        for num in range(1, n+1):
            num = str(num)
            if (num[0] == i) & (num[-1] ==k):
                dict_par[i+k] +=1
ans = 0
for i in range(1,10):
    i = str(i)
    for k in range(1,10):
        k = str(k)
        ans+= dict_par[i+k] * dict_par[k+i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

c = [[0 for i in range(10)] for i in range(10)]
      
for i in range(1, n+1):
    
    tail = i % 10
    
    while(i // 10 != 0):
        i = i // 10
    head = i

    c[head][tail] += 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:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
length=len(str(n))

ans=0
for i in range(1,10):
    if n<i:
        break
    s=0
    for j in range(1,length+1):
        if j<=1:
            s+=1
        elif j<length:
            s+=(10**(j-2))
        else:
            if not(length==6 and i>=2):
                for m in range(n,10**(length-1),-1):
                    if str(m)[0]==str(m)[-1]==str(i):
                        s+=1
    ans+=s*s
for i1 in range(1,10):
    for i2 in range(1,10):
        if i1==i2:
            break;
        s1=0
        s2=0
        for j in range(2,length+1):
            if j<length:
                s1+=10**(j-2)
                s2+=10**(j-2)
            else:
                if not(length==6 and i1>=2 and i2>=2):
                    for m in range(n,10**(length-1),-1):
                        if str(m)[0]==str(i1) and str(m)[-1]==str(i2):
                            s1+=1
                        elif str(m)[0]==str(i2) and str(m)[-1]==str(i1):
                            s2+=1
        ans+=s1*s2*2
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):
    k = str(k)
    r = int(k[-1])
    l = int(k[0])
    C[l][r] += 1

a = 0
for l in range(10):
    for r in range(10):
        a += C[l][r] * C[r][l]

print(a)

Submission status: Accepted

Solution code:
n = int(input())
dp = [[0]*10 for _ in range(10)]
for d in range(1,n+1):
  s = str(d)
  dp[int(s[0])][int(s[-1])] += 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:
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from functools import reduce
from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm(a, b):
  return a*b//gcd(a, b)


def main():
  n = int(input())
  head_tail = [[0]*10 for _ in range(10)]
  for i in range(1, n+1):
    s = str(i)
    h = int(s[0])
    t = int(s[-1])
    head_tail[h][t] += 1
  ans = 0
  for h in range(1,10):
    for t in range(1,10):
      ans += head_tail[h][t] * head_tail[t][h]
  print(ans)
  
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import queue

N = int(input())


change=[[0] * (9) for _ in range(9)]
for i in range(1, 10):
    for j in range(1, 10):
        if  i == j and i <= N:
            cnt = 1
        else:
            cnt = 0
        q = queue.Queue()
        n = i * 10 + j
        while n <= N:
            cnt += 1
            for k in range(0, 10):
                q.put((n // 10) * 100 + k * 10 + n % 10)
            n = q.get()
        change[i-1][j-1] = cnt
#print(change)
ans = 0
for s in range(1, 10):
    for t in range(1, 10):
        ans += change[s-1][t-1] * change[t-1][s-1]
print(ans)

Submission status: Accepted

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

for n in range(1,N+1):
    i=n%10
    j=n
    while 10<=j:
        j=j//10
    c[i][j]=c[i][j]+1

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

Submission status: Accepted

Solution code:
N=int(input())

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = str(n)
l= len(s)-1
ans = 0
temp = [[0]*10 for i in range(10)] ##temp[i][j]::一桁目i,最期の桁jの数
for k in range(1,n+1):

    temp[int(str(k)[0])][int(str(k)[-1])] +=1
for i in range(10):
    for j in range(10):
        ans+=temp[i][j]*temp[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
count = [[0] * 10 for _ in range(10)] # dp[head][tail]
for i in range(1, N+1):
    s = str(i)
    head = int(s[0]); tail = int(s[-1])
    count[head][tail] += 1

answer = 0
for h in range(10):
    for t in range(10):
        answer += count[h][t] * count[t][h]
print(answer)

Submission status: Accepted

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


def f(x):
    s = str(x)
    return (s[0], s[-1])


freq = defaultdict(int)

for i in range(1, n+1):
    p = f(i)
    freq[p] += 1


ans = 0

for i in range(1, n+1):
    a, b = f(i)
    ans += freq[(b, a)]

print(ans)

Submission status: Accepted

Solution code:
N = int(input()) + 1

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

counts = {(i + 1, j + 1): [0, 0] for i in range(9) for j in range(i, 9)}
for i in range(n):
  num = str(i + 1)
  s = int(num[0])
  e = int(num[-1])
  if e == 0:
    continue
  if s <= e:
    counts[(s, e)][0] += 1
  else:
    counts[(e, s)][1] += 1

count = 0
for (a, b), (v1, v2) in counts.items():
  if a == b:
    count += v1 * v1
  else:
    count += v1 * v2 * 2

print(count)

Submission status: Accepted

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

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


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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

count_dists = {}
for s in range(1,10):
    count_dists[s] = {}
    for t in range(1,10):
        count_dists[s][t] = 0

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

    if end_num == 0:
        continue

    count_dists[lead_num][end_num] = count_dists[lead_num][end_num] + 1

answer_count = 0

for u in range(1,10):
    for v in range(1,10):
        num1 = count_dists[u][v]
        num2 = count_dists[v][u]

        if u == v:
            answer_count += num1 ** 2
        else:
            answer_count += num1 * num2

# print(count_dists)
print(answer_count)

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    p = [[0]*10 for _ in range(10)]
    res = 0
    for x in range(1, n+1):
        x = str(x)
        c, d = int(x[0]), int(x[-1])
        if c==0 or d==0:
            continue
        p[d][c] += 1
        # if c != d:
        #     p[d][c] += 1
    for x in range(1, n+1):
        x = str(x)
        c, d = int(x[0]), int(x[-1])
        if c==0 or d==0:
            continue
        res += p[c][d]
        # if c != d:
        #     res -= 1
    print(res)

    # for i in range(1,10):
    #     for j in range(1,10):
    #         for x in range(1, n+1):
    #             x = str(x)
    #             c, d = int(x[0]), int(x[-1])
    #             if i==c and j==d:
    #                 p[c][d] += 1
    # for x in range(1, n+1):
    #     x = str(x)
    #     c, d = int(x[0]), int(x[-1])
    #     res += p[c][d]
    #     if c != d:
    #         res -= 1
    # print(res)



if __name__ == '__main__':
    main()

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):
    str_i = str(i)
    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().strip())

map = {i: {j: 0 for j in range(1, 10)} for i in range(1, 10)}

div = 1
for n in range(1, N + 1):
  h = n // div
  if h >= 10:
    div *= 10
    h = n // div
  t = n % 10
  if t == 0:
    continue
  map[h][t] += 1

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

print(total)

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):
  c[int(str(i)[0])][int(str(i)[-1])] += 1

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

Submission status: Accepted

Solution code:
N = int(input())

count = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, N + 1):
    s = str(i)
    prefix = int(s[0])
    surfix = int(s[-1])
    if surfix != 0:
        count[prefix - 1][surfix - 1] += 1

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

count = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, N + 1):
    s = str(i)
    prefix = int(s[0])
    surfix = int(s[-1])
    if surfix != 0:
        count[prefix - 1][surfix - 1] += 1

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

for i in range(9):
    ans += count[i][i] ** 2

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

#索引数
d = defaultdict(int)
 
n = int(input())

#数え上げ
for i in range(1, n+1):
    start = str(i)[0]
    end = str(i)[-1]
    d[(start, end)] += 1

#リスト化 
L = list(d.items())
 
ans = 0
 
for (start, end), i in L:
    ans += i * d[(end, start)]
 
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

d = defaultdict(int)

n = int(input())

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

L = list(d.items())

ans = 0

for (start, end), i in L:
    ans += i * d[(end, start)]

print(ans)

Submission status: Accepted

Solution code:
def total_pairs(N):
    hashmap = {}
    answer = 0
    for current in range(1, N+1):
        s = str(current)
        first, second = s[0], s[-1]
        total = first+second
        if not hashmap.get(total):
            hashmap[total] = 1
        else:
            c = hashmap[total]
            hashmap[total] = c + 1
            
    #print(hashmap)
    for k, v in hashmap.items():
        #print(k, v)
        f_s, s_s = k[0], k[1]
        if f_s == s_s:
            answer += v*v 
        else:
            temp = s_s+f_s
            if hashmap.get(temp):
                value = hashmap[temp]
                answer +=(v*value)
        #print("--->", answer)
    return answer

N = int(input())
print(total_pairs(N))

Submission status: Accepted

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


def solve(N: int):
    digits = [[0 for i in range(10)] for j in range(10)]
    d = 1
    for i in range(1, N+1):
        last = i % 10
        first = i//d
        if first >= 10:
            d *= 10
            first = i//d
        digits[last-1][first-1] += 1
    sum = 0
    for i in range(10):
        for j in range(10):
            sum += digits[i][j]*digits[j][i]
    print(sum)
    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()

Submission status: Accepted

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

N=int(input())
for i in range(1,min([N+1,10])):
  dic[(i,i)]+=1
for i in range(10,min([N+1,100])):
  dic[(i//10,i%10)]+=1
for i in range(100,min([N+1,1000])):
  dic[(i//100,i%10)]+=1
for i in range(1000,min([N+1,10000])):
  dic[(i//1000,i%10)]+=1
for i in range(10000,min([N+1,100000])):
  dic[(i//10000,i%10)]+=1
for i in range(100000,N+1):
  dic[(i//100000,i%10)]+=1
  
ans=0
for i in range(1,10):
  for j in range(1,10):
    x=dic[(i,j)]
    y=dic[(j,i)]
    ans+=x*y
print(ans)

Submission status: Accepted

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

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

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

print(ans)