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


def extract_first(i):
    return str(i)[0]

def extract_last(i):
    return str(i)[-1]

def get_key(first, last):
    return "{}-{}".format(first, last)

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


for i in range(1, N+1):
    first = extract_first(i)
    last = extract_last(i)
    combi_map[get_key(first, last)] += 1

ans = 0
for i in range(1, 10):
    for j in range(i, 10):
        if i == j:
            ans += combi_map[get_key(i, j)] * combi_map[get_key(j, i)]
        else:
            ans += combi_map[get_key(i, j)] * combi_map[get_key(j, i)] * 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

digit = {}
for i in range(10):
    for j in range(10):
        digit[i,j]=0
for i in range(1,n+1):
    top = int(str(i)[0])
    last = int(str(i)[-1])
    digit[top, last]+=1
ans = 0
for i in range(10):
    for j in range(10):
        ans += digit[i,j]*digit[j,i]
print(ans)

Submission status: Accepted

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

for n in range(N+1):
    head=int(str(n)[0])
    tail=n%10
    if head!=0 and tail!=0:
        A[head-1][tail-1]+=1

count=0

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

print(count)

Submission status: Accepted

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

d=defaultdict(int)

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

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

Submission status: Accepted

Solution code:
def input_multiple_number():
    return map(int, input().split())

N = int(input())


memo = [[-1]*10 for i in range(10)]
def getCount(head,tail):
    if memo[head][tail] != -1:
        return memo[head][tail]

    if head == 0 or tail == 0:
        memo[head][tail] = 0
        return 0

    cnt = 0

    #digit = 1
    if head == tail and head <= N:
        cnt += 1

    #digit = 2
    if head * 10 + tail <= N:
        cnt += 1 

    for i in range(1,7):
        for j in range(10**i):
            if int(str(head)+str(j).zfill(i)+str(tail))<= N:
                cnt += 1
            else:
                memo[head][tail] = cnt
                return cnt
    return cnt
def getHead(n):
    while n >= 10:
        n //= 10
    return n
def getTail(n):
    return n%10

ans = 0
for i in range(1,N+1):
    ans += getCount(getTail(i),getHead(i))
print(ans)

Submission status: Accepted

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

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

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())
s = [[0 for i in range(9)]for i in range(9)]
for k in range(1,N+1):
    if k % 10 == 0:
        continue
    a = list(str(k))
    s[int(a[0])-1][int(a[len(a)-1])-1] += 1
p = 0
for i in range(1,10):
    for j in range(1,10):
        p = p + s[i-1][j-1]*s[j-1][i-1]
print(p)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
#import math
#import collections
#from collections import deque
#from bisect import bisect_left
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
#import itertools
N = int(input())
d = [[0]*9 for _ in range(9)]
for i in range(1,N+1):
    if i < 10:
        thou = i
        on = i
    elif i < 100:
        thou = i // 10
        on = i % 10
    elif i < 1000:
        thou = i // 100
        on = i % 10
    elif i < 10000:
        thou = i //1000
        on = i % 10
    elif i < 100000:
        thou = i //10000
        on = i % 10
    else:
        thou = i // 100000
        on = i % 10
    if on != 0:
        d[thou-1][on-1] += 1
s = 0
for p in range(9):
    for q in range(p,9):
        if p == q:
            s += d[p][q]*d[p][q]
        else:
            s += 2*d[p][q]*d[q][p]
print(s)

Submission status: Accepted

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

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    n = ii()
    d = [[0] * 10 for _ in range(10)]
    for i in range(1, n+1):
        a_str = str(i)
        d[int(a_str[0])][int(a_str[-1])] += 1
    cnt = 0
    for i in range(1, n+1):
        b_str = str(i)
        cnt += d[int(b_str[-1])][int(b_str[0])]
    print(cnt)
        


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [0] * 100
count = 0
for i in range(1, n + 1):
    x = i%10
    y = int(str(i)[0])
    a[10*y + x] += 1
for i in range(1, n + 1):
    x = i%10
    y = int(str(i)[0])
    count += a[10*x + y]
print(count)

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int):
    l = len(str(N))
    k = {}
    for i in range(1,10):
        for j in range(1,10):
            k[str(i)+str(j)] = 0
    for x in range(1, N+1):
        s = str(x)
        if s[-1] != "0":
            k[s[0]+s[-1]] += 1

    k_new = {}
    for i in range(1,10):
        k_new[str(i)+str(i)] = k[str(i)+str(i)]**2
        for j in range(i+1,10):
            n = k[str(i)+str(j)] * k[str(j)+str(i)]
            k_new[str(i)+str(j)] = n * 2

    # print(k_new)
    print(sum([t for t in k_new.values()]))

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

N = int(input())

MAP = np.zeros((9,9))
for i in range(1,N+1):
    a = i%10
    b = int(str(i)[0])
    if a != 0 and b != 0:
        MAP[a-1,b-1] += 1

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

print(int(cnt))

Submission status: Accepted

Solution code:
N = int(input())

# 1. n で始まり m で終わるN(2 * 10^5)以下の数をカウントしてテーブルを作る
# 2. table[n][m] * table[m][n] が条件を満たす組み合わせ
# 3. これを 1 <= n <= 9, 1 <= m <= 9 でループして足し合わせる

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

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

total = 0

for n in range(1, 10):
  for m in range(1, 10):
    total += table[n][m] * table[m][n]
    
print(total)

Submission status: Accepted

Solution code:
N = int(input())
num = [[0] * 9 for _ in range(9)]
for i in range(9):
    for j in range(9):
        for n in range(N + 1):
            n = str(n)
            if str(i + 1) == n[0] and str(j + 1) == n[-1]:
                num[i][j] += 1
s = 0
for i in range(9):
    for j in range(9):
        s += num[i][j] * num[j][i]
print(s)

Submission status: Accepted

Solution code:
# 掛け算で求める，までは正しかった
# 式がよく分からない
# https://scrapbox.io/pocala-kyopro/D_-_Handstand_2 この考え方もやった

n = int(input())

if n <= 9:
    print(n)
    exit()

# cij = #{1 <= k <= N | kの先頭の桁の数はi，末尾の桁の数はjに等しい}
# ans = \sum_{i=0}^9 \sum_{j=0}^9 cij * cji

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

for k in range(1, n + 1):
    k0 = int(str(k)[0])
    kn = int(str(k)[-1])
    if k0 != 0 and kn != 0:
        cij[k0][kn] += 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:
N=int(input())
K=[[0 for i in range(9)] for j in range(9)]

for i in range(1,N+1):
  if int(str(i)[-1])==0:
    pass
  else:
    h=int(str(i)[0])
    t=int(str(i)[-1])
    K[h-1][t-1] += 1


ans=0
for i in range(9):
  for j in range(i+1):
    if i==j:
      ans += K[i][j]* K[j][i]
    else:
      ans += 2*K[i][j]* K[j][i]
      #print(i,j,K[i][j],K[j][i],K[i][j]*K[j][i])
      
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import numpy as np
stack = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
n = int(input())

c = np.zeros([10, 10])
# 先頭i末尾jの値
for i in range(1, n+1):
    str_i = str(i)
    head = int(str_i[0])
    tail = int(str_i[-1])
    c[head, tail] += 1
count = 0
for i in range(1, 10):
    for j in range(1, 10):
        count += c[i, j]*c[j, i]
print(int(count))

Submission status: Accepted

Solution code:
N = int(input())
counts = [[0]*10 for i in range(10)]
top = 1
for i in range(1,N+1):
    if i == top*10:
        top *= 10
    a_top = i//top
    a_bottom = i%10
    
    counts[a_top][a_bottom] +=1


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

print(sums)

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:
def main():
    n = int(input())
    import numpy as np
    ans = np.zeros((10,10))
    for i in range(1,n+1):
        h,t = str(i)[0],str(i)[-1]
        ans[int(h)-1][int(t)-1]+=1
    s = 0
    for i in range(10):
        for j in range(10):
            s += ans[i][j]*ans[j][i]

    print(int(s))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
dd = defaultdict(int)
n=int(input())
for i in range(1, n+1):
    dd[(str(i)[0],str(i)[-1])]+=1
k=0
for i in range(1, 10):
    for j in range(1, 10):
        k+=dd[str(i), str(j)]*dd[str(j), str(i)]
print(k)

Submission status: Accepted

Solution code:
#152d
N = int(input()) #2*10**5

#N 以下の正の整数の組 (A,B)であって、次の条件を満たすものの個数を求めてください。
#A,Bを先頭に 0 のつかない 10 進数表記で表したときに、 A の末尾の桁が B の先頭の桁に等しく、 A の先頭の桁が B の末尾の桁に等しい

#桁数算出
import math
N_digits = int(math.log10(N)//1 + 1) 

#末尾9通り、先頭9通り……先頭に0はつかないので
safe_number = [1,2,3,4,5,6,7,8,9]
ht_comb_list = dict()

def get_OK_comb_number(head_number,tail_number):
    #1桁のパターンを計算。
    if head_number == tail_number and head_number <= N:
        ht_comb = 1
    else:
        ht_comb = 0

    #2桁のパターンを計算。
    if head_number*10 + tail_number <= N:
        ht_comb += 1

        #3桁以降のパターンを計算
        base_number = 2

        while base_number+1 <= N_digits:
            max_number = head_number*10**(base_number)  + 10**(base_number) -10 + tail_number
            #print(base_number+1,"桁")
            if max_number <= N:
                #print(10**(base_number-1))
                ht_comb += 10**(base_number-1)
            else:
                last_digit = N - ((N//10) * 10)
                max_number = N
                max_number -= last_digit
                max_number += tail_number
                if max_number > N:
                    max_number-= 10

                min_number = head_number*10**(base_number) + tail_number
                #print(max_number,min_number)
                #print(1+(max_number-min_number)//10)

                if max_number >= min_number:
                    ht_comb += 1+(max_number-min_number)//10
            
                break

            base_number += 1

    #print("(",head_number,",",tail_number,"):",ht_comb)
    return ht_comb


for head_number in safe_number:
    for tail_number in safe_number:
        ht_comb_list[(head_number,tail_number)] = get_OK_comb_number(head_number,tail_number)       

ans = 0

for head_number in safe_number:
    for tail_number in safe_number:
        #print((head_number,tail_number),ht_comb_list[(head_number,tail_number)],end="")
        ans += ht_comb_list[(head_number,tail_number)] * ht_comb_list[(tail_number,head_number)]
    #print("")

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a= [[0]*9 for _ in range(9)]
ans = 0
for i in range(1,n+1):
    if (i%10==0):
        continue
    p = str(i)
    front = int(p[0])
    back = int(p[-1])
    a[front-1][back-1] += 1
for i in range(1,10):
    for j in range(1,10):
        ans += a[i-1][j-1]*a[j-1][i-1]
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):
    l = list(str(i))
    a[int(l[0])][int(l[-1])] += 1
ans = 0
for i in range(1,10):
    for j in range(i,10):
        if i == j:
            ans += a[i][j]**2
        else:
            ans += a[i][j] * a[j][i] * 2
print(ans)

Submission status: Accepted

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


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

    C = [[0 for _ in range(0, 10)] for _ in range(0, 10)]
    for k in range(1, N + 1):
        k = str(k)
        head_k = int(k[0])
        tail_k = int(k[-1])
        C[head_k][tail_k] += 1

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

    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
N = int(s)
l = len(s)
first = int(s[0])
last = int(s[-1])
if last == 0:
    N -= 1
    s = str(N)
    l = len(s)
    first = int(s[0])
    last = int(s[-1])
if N < 10:
    print(N)
else:
    if l > 2:
        middle = int(s[1:-1])
    else:
        middle = 0
    k1 = 0
    for i in range(l - 2):
        k1 += 10 ** i
    k2 = k1
    k1 += 10 ** (l - 2)

    li = [[0 for j in range(9)] for i in range(9)]
    for i in range(9):
        for j in range(9):
            if i + 1 < first:
                li[i][j] = k1
            elif i + 1 == first:
                li[i][j] = middle + k2
                if j + 1 <= last:
                    li[i][j] += 1
            else:
                li[i][j] = k2
            if i == j:
                li[i][j] += 1
    ans = 0
    for i in range(9):
        for j in range(9):
            ans += li[i][j] * li[j][i]
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
a = defaultdict(int)
n = int(input())
for i in range(1, n + 1):
    s = str(i)
    a[(s[0], s[-1])] += 1
rslt = 0
for i in range(1, 10):
    for j in range(1, 10):
        rslt += a[(str(i), str(j))] * a[(str(j), str(i))]
print(rslt)

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1, N+1):
    A[int(str(i)[0])][int(str(i)[-1])] += 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:
import numpy as np
n = int(input())
rec = np.zeros((10, 10))

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

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

print(int(ans))

Submission status: Accepted

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

cnt = 0
counter1 = Counter()
#counter2 = Counter()
for i in range(1,N+1):
    left = str(i)[0]
    right = str(i)[-1]
    counter1[(left, right)] += 1
    #counter2[(right, left)] += 1

for i in range(1, N+1):
    left = str(i)[0]
    right = str(i)[-1]
    cnt += counter1[(right, left)]

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
ans = 0
def counths(n,i,j):
    s = int(n)
    bot = sum([10**x for x in range(len(n)-2)])
    if int(n[0]) > i:
        top = 10**(len(n)-2)
    elif int(n[0]) < i:
        top = 0
    else:
        a = i * 10 ** (len(n)-1) + j
        if a < s:
            top = (s-a) // 10 + 1
        elif a == s:
            top = 1
        else:
            top = 0
    if i == j:
        if len(n) > 1:
            bot += 1
        else:
            if s >= i:
                bot += 1
    return bot + top
for i in range(1,10):
    for j in range(1,10):
        ans += counths(N,i,j) * counths(N,j,i)
print(ans)

Submission status: Accepted

Solution code:
import math
N = input()
ans = 0
def counths(n,i,j):
    s = int(n)
    bot = sum([10**x for x in range(len(n)-2)])
    if int(n[0]) > i:
        top = 10**(len(n)-2)
    elif int(n[0]) < i:
        top = 0
    else:
        a = i * 10 ** (len(n)-1) + j
        if a < s:
            top = (s-a) // 10 + 1
        elif a == s:
            top = 1
        else:
            top = 0
    if i == j:
        if len(n) > 1:
            bot += 1
        else:
            if s >= i:
                bot += 1
    return bot + top
for i in range(1,10):
    for j in range(1,10):
        ans += counths(N,i,j) * counths(N,j,i)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n = input()
first_n = n[0]
finish_n = n[-1]
digit = len(n)
cnt = defaultdict(lambda :0)

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

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

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()

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

Submission status: Accepted

Solution code:
n = int(input())

table = [[0]*10 for _ in range(10)]
for i in range(1, n+1):
    a = i
    a_head = int(str(a)[0])
    a_tail = a % 10
    table[a_head][a_tail] += 1

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

print(ans)

Submission status: Accepted

Solution code:
#cocoinit23

n = int(input())

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

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

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

print(ans)

Submission status: Accepted

Solution code:
'''
ika tako
事前計算で1以上N以下の数字を走査し、
「先頭桁がiで末尾桁がjの数字の個数Ci,j 」をあらかじめ求めておく。
再度、1以上N以下の数字を走査し、
その数字が先頭桁がjで末尾桁がiなら答にCi,jを加算すればよい。
'''
def solve(n):
    tbl = [0] * 100
    for i in range(1, n + 1):
        st = str(i)
        tbl[int(st[0] + st[-1])] += 1
 
    ans = 0
    for i in range(1, n + 1):
        st = str(i)
        ans += tbl[int(st[-1] + st[0])]
    return ans
 
 
n = int(input())
print(solve(n))

Submission status: Accepted

Solution code:
N = int(input())
count = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, N + 1):
    st, ed = int(str(i)[0]), int(str(i)[-1])
    count[st][ed] += 1

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

Submission status: Accepted

Solution code:
n = int(input())

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

base = 1
for i in range(1, n + 1):
    if i >= base * 10:
        base *= 10
    count[i // base][i % 10] += 1

total = 0
base = 1
for i in range(1, n + 1):
    if i >= base * 10:
        base *= 10
    total += count[i % 10][i // base]

print(total)

Submission status: Accepted

Solution code:
N = int(input())
cij = [10*[0] for i in range(0, 10)]

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

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

Submission status: Accepted

Solution code:
def judge(n):
    global count,num_list
    if n[-1]==0:
        judge(int(n)-1)
    else:
        for i in range(1,int(n)+1):
            i=str(i)
            if int(i[-1])-1!=-1:
                num_list[int(i[0])-1][int(i[-1])-1]+=1

        for i in range(1,int(n)+1):
            i=str(i)
            if int(i[-1])-1!=-1:
                count=count+num_list[int(i[-1])-1][int(i[0])-1]

N=int(input())
count=0
num_list=[[0]*9 for i in range(9)]
judge(str(N))
print(count)

Submission status: Accepted

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

# l = [[0]  * 10 for _ in range(10)]
d=defaultdict(lambda:defaultdict(int))

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

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

print(r)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

print(r)

Submission status: Accepted

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

for i in range(1, N+1):
    x = i % 10
    y = i
    while y // 10:
        y //= 10
    if x == 0 or y == 0:
        continue
    t[x][y] += 1

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

Submission status: Accepted

Solution code:
def main(n):
    c=[[0]*10 for _ in range(10)]
    for k in range(1,n+1):
        n=str(k)
        i = int(n[0])
        j = int(n[-1])
        c[i][j] += 1
    ans =0
    for i in range(10):
        for j in range(10):
            ans+=c[i][j]*c[j][i]
    print(ans)
             
if __name__=='__main__':
    main(int(input()))

Submission status: Accepted

Solution code:
N=int(input())

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

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

Submission status: Accepted

Solution code:
S = input()
n = int(S)

d = {}
for i in range(1, n+1):
    if (int(str(i)[0]), int(str(i)[-1])) in d:
        d[(int(str(i)[0]), int(str(i)[-1]))] += 1
    else:
        d[(int(str(i)[0]), int(str(i)[-1]))] = 1

ans = 0
for (a,b), v in d.items():
    if (b, a) in d:
        ans += d[(a, b)] * d[(b, a)]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
X = ['']
for i in range(1, 6):
    for j in range(10**i):
        X.append(str(j).zfill(i))
cnt = 0
for a in range(1, n+1):
    x = str(a)[0]
    y = str(a)[-1]
    if x == y:
        cnt += 1
    if y == '0':
        continue
    l, r = -1, len(X)-1
    while r-l > 1:
        c = (l+r)//2
        if int(y+X[c]+x) <= n:
            l = c
        else:
            r = c
    cnt += r
print(cnt)

Submission status: Accepted

Solution code:
from math import log
N = int(input())
li = [[0]* 10 for i in range(10)]
for i in range(1,N+1):
    u = i//10**int(log(i,10))
    l = i%10
    if l != 0:
        li[u][l] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += li[i][j] * li[j][i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1,n+1):
	j = i%10
	while i>9:
		i//=10
	a[i][j]+=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:
#定数
#1≦n≦2*10^5
n = int(input())

#先頭と末尾の数の組み合わせの個数を格納するリストを作成
HT = [[0 for i in range(10)] for j in range(10)] #Head Tail list
for i in range(1, n+1):  #O(n)
    d = len(str(i)) #digit
    hnum = i // (10**(d-1))  #head_num
    tnum = i % 10   #tail_num
    HT[hnum][tnum] += 1

#答えの計算
ans = 0
#headとtailが等しい場合
#9通り
for i in range(1, 10):
    ans += HT[i][i] ** 2
#headとtailが等しくない場合
#36通り
for i in range(1,10):
    for j in range(i+1, 10):
        ans += (HT[i][j] * HT[j][i]) * 2

#答えを出力
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

ans = sum([l[i][j]*l[j][i] for i in range(10) for j in range(10)])

print(ans)

Submission status: Accepted

Solution code:

N = int(input())


def calc(num):
    s = list(str(num))[::-1]
    s2 = int(s[-1])
    s1 = int(s[0])

    keta = 0
    while True:
        num = num // 10
        keta += 1
        if num == 0:
            break

    if s1 == s2:
        if keta == 1:
            return 1
        elif keta == 2:
            return 1 + 2
        else:
            ans = 0
            for i in range(keta - 3):
                ans += (10 ** (i+1)) * 2
            ans += (int(''.join(s[1:-1][::-1]))+1) * 2 + 3
            return ans

    elif s1 > s2:
        if keta == 2:
            return 0
        else:
            ans = 0
            for i in range(keta - 2):
                ans += 10 ** i
            return ans * 2
    else:
        if s1 == 0:
            return 0
        else:
            ans = 0
            for i in range(keta - 1):
                ans += 10 ** i
            return ans * 2


# print(10, calc(10))

dp = [0] * (N+1)

for i in range(1, N+1):
    dp[i] = dp[i-1] + calc(i)

# print(dp)
print(dp[N])

Submission status: Accepted

Solution code:
n = int(input())

count_list = []
for i in range(10):
    count_list.append([0] * 10)

for i in range(1, n + 1):
    i_str = str(i)
    j = int(i_str[0])
    k = int(i_str[-1])
    count_list[j][k] += 1

ans = sum([count_list[i][j] * count_list[j][i] for i in range(1, 10) for j in range(1, 10)])
print(ans)

Submission status: Accepted

Solution code:
# 最初と最後を保存すれば10*10ぶんもてばよくて単純に探索するというだけのことなのに1時間以上椅子を温めただけだった
n = int(input())
table = [[0 for _ in range(10)] for _ in range(10)]
for i in range(n + 1):
    f, l = i // (1 * 10**(len(str(i)) - 1)), i % 10
    table[f][l] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += table[i][j] * table[j][i]
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
# 0以外は全部同じ 0だけ先頭につけることができない
N=int(input())
# 90通りある組み合わせを選ぶのだ
combination = [[0 for j in range(10)] for i in range(10)]
for i in range(1,N+1):
    s = str(i)
    start = int(s[0])
    end=int(s[-1])
    combination[start][end] += 1
count=0
for row in range(10):
    for column in range(10):
        count+=combination[row][column]*combination[column][row]

print(count)

Submission status: Accepted

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

for i in range(1,n+1):
    bot=i%10
    if bot==0:
        continue
    top=i
    while top>=10:
        top=top//10
    arr[top][bot]+=1

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

Submission status: Accepted

Solution code:
# Problem D - Hand

# input
N = int(input())

# initialization
nums = [[0]*10 for i in range(10)]
ans = 0

# count check
for n in range(1, N+1):
    tmp = list(str(n))
    tmp = list(map(int, tmp))
    sento = tmp[0]
    ichi = tmp[-1]
    nums[sento][ichi] += 1

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

# output
print(ans)