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:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import copy
import decimal


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


def solve():
    N = Scanner.int()
    cnt = [[0 for _ in range(10)] for _ in range(10)]
    for i in range(1, N + 1):
        front = int(str(i)[0])
        back = int(str(i)[-1])
        cnt[front][back] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += cnt[i][j] * cnt[j][i]
    print(ans)


def main():
    # sys.setrecursionlimit(1000000)
    # sys.stdin = open("sample.txt")
    # T = Scanner.int()
    # for _ in range(T):
    #     solve()
    # print('YNeos'[not solve()::2])
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
for i in range(1,n+1):
    if str(i)[-1] == 0:
        continue
    key = str(i)[0]
    key += "+" + str(i)[-1]
    if key in dic:
        dic[key] += 1
    else:
        dic[key] = 1
res = 0
for i in range(1,n+1):
    if str(i)[-1] == 0:
        continue
    key = str(i)[-1]
    key += "+" + str(i)[0]
    if key in dic:
        res += dic[key]
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

if N<=9:print(N)
else:
  count = defaultdict(int)
  for i in range(10,N+1):
    a,b=int(str(i)[0]),int(str(i)[-1])
    if b==0:continue
    count[(a,b)] += 1

  ans=0
  for i in range(1,10):
    count[(i,i)] += 1
  count = dict(count)
  for key in count.keys():
    if key[0]<key[1] and count.get((key[1],key[0])):
      ans += count[key] * count[(key[1],key[0])] * 2
    if key[0]==key[1]:
      ans += count[key]**2
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = {(i,j):0 for i in range(1,10) for j in range(1,10)}
num = list(range(1,10))
for k in range(1,N+1):
    k = str(k)
    i = int(k[0])
    j = int(k[-1])
    if i in num and j in num:
        C[(i,j)] += 1
cnt = 0
for i in range(1,10):
    for j in range(1,10):
        cnt += C[(i,j)]*C[(j,i)]
print(cnt)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = [[0]*10 for _ in range(10)]

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

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

print(ans)

Submission status: Accepted

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

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

for n in range(1,N+1):
    s = str(n)
    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())
l = [[] for _ in range(9)]
for i in range(0,9):
    for j in range(0,9):
        l[i].append(0)
for i in range(N+1):
    if (int(str(i)[0])!=0 and int(str(i)[-1])!=0):
        l[int(str(i)[0])-1][int(str(i)[-1])-1]+=1
t=0
for i in range(9):
    for j in range(9):
        t+=l[i][j]*l[j][i]
print(t)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())

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

    res = 0
    for i in range(1, n + 1):
        i = str(i)
        head = int(i[0])
        foot = int(i[-1])
        res += cnt[foot][head]
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def resolve():
    N = int(input())
    # cnt[i][j] := 先頭がi, 末尾がjで始まるような数
    cnt = [[0] * 10 for i in range(10)]
    for i in range(1, N + 1):
        a = int(str(i)[0])  # 先頭
        b = int(str(i)[-1])  # 末尾
        cnt[a][b] += 1

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
# 解説を見た。
# https://at274.hatenablog.com/entry/2020/01/24/060000

def main():
    num = int(input())
    counter = [[0] * 10 for _ in range(10)]
    for x in range(1, num + 1):
        x = str(x)
        head = int(x[0])
        tail = int(x[-1])
        counter[head][tail] += 1
    gen = (counter[head][tail] * counter[tail][head]
           for head in range(1, 10)
           for tail in range(1, 10))
    print(sum(gen))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N = NI()
    grid = make_grid(10, 10, 0)
    ans = 0
    for i in range(N+1):
        s = str(i)
        t = int(s[0])
        b = int(s[-1])
        grid[t][b] += 1
    for i in range(1, 10):
        for j in range(1, 10):
            ans += grid[i][j] * grid[j][i]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N = NI()
    grid = make_grid(10, 10, 0)
    ans = 0
    for i in range(N+1):
        s = str(i)
        t = int(s[0])
        b = int(s[-1])
        grid[t][b] += 1
    for i in range(1, 10):
        for j in range(1, 10):
            if i != j:
                ans += grid[i][j] * grid[j][i]
            else:
                ans += grid[i][j] ** 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
x=str(n)
keta=len(str(n))

l=[[0]*10 for i in range(10)]
for i in range(n+1):
    num=str(i)
    sento=int(num[0])
    matsubi=int(num[-1])
    if matsubi==0:
        continue
    l[sento][matsubi]+=1

sm=0
for i in range(n+1):
    num=str(i)
    sento=int(num[0])
    matsubi=int(num[-1])
    if matsubi==0:
        continue
    sm+=l[matsubi][sento]

print(sm)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())

g = [[0] * (10) for _ in range(10)]
for i in range(1, N + 1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    g[a][b] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += g[i][j]*g[j][i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

n = int(input())
dic = {}
cdic = {}
d = 0
for i in range(1,n+1):
    matsubi = i%10
    sentou = i//(10**d)
    if sentou >= 10:
        d += 1
        sentou = i//(10**d)
    #print(sentou,matsubi)

    if sentou == matsubi:
        if sentou not in cdic:
            cdic[sentou] = 0
        cdic[sentou] += 1
        continue

    sw = False
    if sentou > matsubi:
        sentou,matsubi = matsubi,sentou
        sw = True

    if (sentou,matsubi) not in dic:
        dic[(sentou,matsubi)] = [0,0]

    if not sw:
        dic[(sentou,matsubi)][0] += 1
    else:
        dic[(sentou,matsubi)][1] += 1

ans = 0

for item in cdic:
    ans += cdic[item]*(cdic[item])

for item in dic:
    ans += dic[item][0]*dic[item][1]*2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1,n+1):
    check = str(i)
    head = int(check[0])
    tail = int(check[-1])
    cnt[head][tail] += 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())

dp = [[0] * 10 for _ in range(10)]
for x in range(1, N + 1):
    j = x % 10
    while x >= 10:
        x //= 10
    i = x % 10
    dp[i][j] += 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 _ in range(10)]
for i in range(1, N+1):
    i_first = int(str(i)[0])
    i_last = int(str(i)[-1])
    c[i_first][i_last] += 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())
d = [[0]*9 for _ in range(9)]
ans = 0

for num in range(1, n+1):
    s = str(num)
    x, y = int(s[0]), int(s[-1])
    if x != 0 and y != 0:
        d[x-1][y-1] += 1

for i in range(9):
    k = -1
    for j in range(9):
        if i == j:
            ans += pow(d[i][j], 2)
            k = 1
        elif k == 1:
            ans += 2*d[i][j]*d[j][i]

print(ans)

Submission status: Accepted

Solution code:
def II(): return int(input())
N=II()
def solve(n):
  tbl=[0]*100
  ans=0
  for i in range(1,n+1):
    i=str(i)
    tbl[int(i[0]+i[-1])]+=1
  for i in range(1,n+1):
    i=str(i)
    ans+=tbl[int(i[-1]+i[0])]
  return ans
a=solve(N)
print(a)

Submission status: Accepted

Solution code:
def II(): return int(input())
N=II()
tbl=[0]*101
for i in range(1,N+1):
  i=str(i)
  tbl[int(i[0]+i[-1])]+=1
ans=0
for i in range(1,N+1):
  i=str(i)
  ans+=tbl[int(i[-1]+i[0])]
print(ans)

Submission status: Accepted

Solution code:
def II(): return int(input())
N=II()
tbl=[0]*101
for i in range(1,N+1):
  i=str(i)
  tbl[int(str(i[0]+i[-1]))]+=1
ans=0
for i in range(1,N+1):
  i=str(i)
  ans+=tbl[int(str(i[-1]+i[0]))]
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
INF = 10**9
mod = 10**9+7

N = i()
table = [[0]*9 for _ in range(9)]
for i in range(N):
    s = str(i+1)
    a,b = int(s[0]),int(s[-1])
    if b == 0:
        continue
    table[a-1][b-1] += 1
ans = 0
for i in range(9):
    for j in range(9):
        ans += table[i][j]*table[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import os
import math

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 0 = 1桁目
def digit_val(number, n) -> int:
    return number // 10 ** n % 10


def max_digit_val(number) -> int:
    return number // 10 ** int(math.log10(number)) % 10


def min_digit_val(number) -> int:
    return number // 10 ** 0 % 10


def int_len(number) -> int:
    return int(math.log10(number) + 1)


# 実装を行う関数
def resolve(test_def_name=""):
    n = int(input())

    # 対象となる数字の一覧を作成(1～20万の数字かつ、0を含まない数字)
    target_list = []
    ignore_cnt = 0
    for i in range(1, n + 1):
        ignore_cnt += 1
        if ignore_cnt == 10:
            ignore_cnt = 0
            continue
        target_list.append(i)

    # 先頭X で末尾Y という数字がいくつ存在するか・・というカウンタを作成。
    cnt_dict = {}
    for i in target_list:
        if int_len(i) == 1:
            # 1桁目の場合は最初桁と最後桁が同じ
            cnt_dict[i * 10 + i] = cnt_dict.get(i * 10 + i, 0) + 1
        else:
            val = max_digit_val(i) * 10 + min_digit_val(i)
            cnt_dict[val] = cnt_dict.get(val, 0) + 1

    # 使用できるパターンを足し込みしていく。
    ans = 0
    for i in target_list:
        if int_len(i) == 1:
            ans += cnt_dict.get(i * 10 + i, 0)
        else:
            val = min_digit_val(i) * 10 + max_digit_val(i)
            ans += cnt_dict.get(val, 0)

    print(ans)


# テストクラス
class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """25"""
        output = """17"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """1"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """100"""
        output = """108"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """2020"""
        output = """40812"""
        self.assertIO(test_input, output)

    def test_input_5(self):
        test_input = """200000"""
        output = """400000008"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Accepted

Solution code:
N = int(input())
L=[[0 for i in range(10)] for j in range(10)]
for i in range(1,N+1):
    a=i%10
    if i<10:
        b=i
    c=i
    while c>=10:
        b=c//10
        c=b
    L[b][a]+=1
num=0
for i in range(10):
    for u in range(10):
        if L[i][u]!=0:
            num+=L[i][u]*L[u][i]
print(num)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

for i in range(1, n + 1):
    s = str(i)
    le = len(s)
    t1 = s[0]
    t2 = s[le - 1]
    memo[int(t1)][int(t2)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))


N = int(input())


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

for i in range(N + 1):
    a = str(i)
    h, t = map(int, [a[0], a[-1]])
    if (h,t) in ht.keys():
        ht[(h,t)] += 1

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

def f(l, r):
  cnt = 0
  if l == r and l <= N:
    cnt += 1
  if l*10 + r <= N:
    cnt += 1
  for c in range(1, 5):
    x = l*10**(c+1) + r
    i = 0
    I = int("9"*c)
    while x <= N and i <= I:
      cnt += 1
      x += 10
      i += 1
  return cnt

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

for i in range(9):
  for j in range(9):
    C[i][j] = f(i+1, j+1)

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
d = {(str(p),str(q)): 0 for p in range(1,10) for q in range(1,10)}
for i in range(1, n+1):
    c = str(i)
    if c[-1]!="0":
        d[c[0], c[-1]] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        if i==j:
            v = d[str(i), str(j)]
            ans += v * v
        else:
            v1 = d[str(i), str(j)]
            v2 = d[str(j), str(i)]
            ans += v1*v2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=[[0]*10 for i in range(10)]
for a in range(1,n+1):
  a=str(a)
  c[int(a[-1])][int(a[0])]+=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:
from itertools import product

n = int(input())

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

for i in range(n + 1):
    a, b = int(str(i)[0]), i % 10
    n_num[a][b] += 1

answer = sum(n_num[a][b] * n_num[b][a] for a, b in product(range(1, 10), repeat=2))
print(answer)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(1, N+1):
  ss = str(i)
  hd = int(ss[0])
  tl = int(ss[-1])
  numss[hd][tl] += 1
  #print(f'hd = {hd}, tl = {tl}')
  
ans = 0
for x in range(1, 10):
  for y in range(1, 10):
    ans += numss[x][y] * numss[y][x]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
hort = [[0] * 9 for i in range(9)]
#hort[i][j] := i~j
for i in range(1, n + 1):
    temp = str(i)
    if temp[0] == '0' or temp[-1] == '0':continue 
    hort[int(temp[0]) - 1][int(temp[-1]) - 1] += 1

res = 0
for i in range(9):
    for j in range(9):
        if i != j:
            temp = hort[i][j] * hort[j][i] * 2
        else:
            temp = hort[i][j] * (hort[i][j] - 1) + hort[i][j]
            #print(temp, i, j)
        hort[i][j] = 0
        res += temp

print(res)

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

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

Submission status: Accepted

Solution code:

N = int(input())
L = len(str(N))
if L == 1:
    print(N)
    quit()
    
pair = [[(10**(L-2)-1)//9]*9 for _ in range(9)]
for i in range(9):
    pair[i][i] += 1

for i in range(10**(L-1), N+1):
    pair[int(str(i)[0])-1][int(str(i)[L-1])-1] += 1 if i%10 else 0
ans = 0
for i in range(9):
    for j in range(9):
        ans += pair[i][j] * pair[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
 
N = INT()

dic = defaultdict(int)

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
tbl = [[0]*10 for _ in [0]*10]
k = 10
for i in range(1, n+1):
    if i // k: k *= 10
    l = i*10//k
    r = i%10
    tbl[l][r] += 1
    
ans = 0
for l, _t in enumerate(tbl):
    for r, t in enumerate(_t):
        if l == r:
            ans += t*t
        else:
            ans += t*tbl[r][l]
print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
ans=0
k = [[0]*10 for i in range(10)]

for i in range(1,n+1):
  j = str(i)
  l = int(j[0])
  r = int(j[-1])
  k[l][r]+=1
  
for i in range(10):
  for j in range(10):
    if i==0 or j==0:
      continue
    if i==j:
      ans += k[i][j]*k[i][j]
    else:
      ans += k[i][j]*k[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):
    s = str(i)
    d[(s[0], s[-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:
#解説pdfと考え方そのままに
#c(i,j)の部分を工夫(maspyさんに感謝)
from collections import defaultdict
n = int(input())


dic = defaultdict(int)

#for x in map(str,range(1,n+1))
for k in range(1,n+1):
    left = str(k)[0]
    right = str(k)[-1]
    dic[(left,right)] += 1
                
ans = 0
items = list(dic.items())
for (left,right),cnt in items:
    ans += cnt * dic[(right,left)]

print(ans)

Submission status: Accepted

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

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

  if s != 0 and t != 0:
    if s > t:
      s, t = s, t
    d[(s, t)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1, N+1):
  if i % 10 != 0:
    si = str(i)
    start = int(si[0])
    fin = int(si[-1])
    dp[start-2][fin-2] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

numss = [[0]*(10) for _ in range(10)]
for i in range(1, N+1):
    ss = str(i)
    hd = int(ss[0])
    tl = int(ss[-1])
    numss[hd][tl] += 1

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
cou = 0
for i in range(1,n+1):
  i = str(i)
  if i[0]!="0" and i[-1]!="0":
    if int(i[0])>int(i[-1]):
      cou +=  (10**(len(i)-1)-1)//9
    elif int(i[0])<int(i[-1]):
      cou += (10**(len(i)-2)-1)//9
  #print(i,cou)

cou *= 2
    
l = [0 for i in range(9)]
for i in range(1,n+1):
  i = str(i)
  if i[0]==i[-1]:
    l[int(i[0])-1] += 1
l1 = [i*i for i in l]
print(sum(l1)+cou)

Submission status: Accepted

Solution code:
#同値なものはリストでまとめる

n = int(input())
c = [[[] for i in range(9)] for i in range(9)]

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

ans = 0
for i in range(1,n+1)[::-1]:
	head = str(i)[0]
	tail = str(i)[-1]
	length = len(str(i))
	if tail == '0':
		continue
	if head == tail:
		if length == 1:
			ans += 1
		elif length == 2:
			ans += 3
		else:
			ans += 3
			for j in range(3,length+1):
				if length > j:
					ans += 10**(j-2) * 2
				else:
					ans += 2 * (int(str(i)[1:length-1]) + 1)
	elif head < tail:
		if length > 2:
			ans += int('1' * (length-2)) * 2
	else:
		if length > 1:
			ans += int('1' * (length-1)) * 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

table = {}

for num in range(1, n + 1):
    l = num % 10
    tmp = num
    while tmp >= 10:
        tmp //= 10
    m = tmp % 10

    if (l, m) not in table:
        table[(l, m)] = 1
    else:
        table[(l, m)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())

cnt = defaultdict(int)
for i in range(1, N + 1):
    i = str(i)
    l = i[0]
    r = i[-1]
    cnt[(l, r)] += 1
    cnt[(r, l)] += 0

ans = 0
for (l, r), c in cnt.items():
    ans += cnt[(r, l)] * c

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする
from functools import lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    #A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    count=[[0]*10 for i in range(10)]
    for i in range(1,N+1):
        si=str(i)
        head=int(si[0])
        tail=int(si[-1])
        count[head][tail]+=1
    ans=0
    for a in range(1,10):
        for b in range(1,10):
            ans+=count[a][b]*count[b][a]
    print(ans)







if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = [0]*100
ans = 0

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

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

print(ans)