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:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from copy import deepcopy
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


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


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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1,n+1):
    if(i % 10 == 0):
        continue
    s = str(i)
    key = (int(s[0]), int(s[-1]))
    pair_count[key] += 1

ans = 0
for k, v in pair_count.items():
    k_pair = (k[1], k[0])
    ans += v * pair_count[k_pair]

print(sum([pair_count[k] * pair_count[(k[1], k[0])] for k in pair_count]))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline().rstrip())

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

for i in range(1, n+1):
    first = int(str(i)[0])
    end = i % 10
    s[first][end] += 1

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

print(r)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

def solve():
    arr = [0] * 100
    ans = 0
    
    for i in range(1, N + 1):
        strNum  = str(i)
        arr[int(strNum[0] + strNum[-1])] += 1
        
    for i in range(1, N + 1):
        strNum = str(i)
        ans += arr[int(strNum[-1] + strNum[0])]
    
    return ans

print(solve())

Submission status: Accepted

Solution code:
import numpy as np
import math

def solve(num):
  table = np.zeros((9,9))

  for n in range(1, num+1):
    order = len(str(n))
    upper = n // 10**(order-1)
    lower = int(str(n)[-1])
    if lower == 0:
      continue
    table[lower-1][upper-1] += 1

  ans = 0
  for n in range(1, num+1):
    order = len(str(n))
    upper = n // 10**(order-1)
    lower = int(str(n)[-1])
    if lower == 0:
      continue
    ans += table[upper-1][lower-1]
  return int(ans)

if __name__ == "__main__":
  _n = int(input())
  ans = solve(_n)
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())

ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        c_ij = 0
        c_ji = 0
        for k in range(1, n + 1):
            temp = str(k)
            if temp[0] == str(i) and temp[-1] == str(j):
                c_ij += 1
            if temp[0] == str(j) and temp[-1] == str(i):
                c_ji += 1
        ans += c_ij * c_ji
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

ans = 0

for j in range(10):
    for k in range(10):
        ans += lst[j][k] * lst[k][j]

print(ans)

Submission status: Accepted

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

def get_first(num):
    log = 1
    k=num
    while True:
        if k < 10:
            break
        k = k//10
        log+=1
    #print(k,log)
    return (k,log)

for i in range(1,n+1):
    f,_ = get_first(i)
    l   = i%10
    if l==0:
        continue
    b[f-1][l-1] +=1
#print(b)

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


print(count)

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

Submission status: Accepted

Solution code:
def Int ():
    return int(input())

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=[[0]*10 for _ in range(10)]
    for i in range(1,n+1):
        i=str(i)
        s=int(i[0]); t=int(i[-1])
        A[s][t]+=1

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

    print(ans)
resolve()

Submission status: Accepted

Solution code:

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

    if N <= 10:
        return N

    # 10の位 = 上位、1の位 = 下位とする
    numberCount = [0] * 100
    for i in range(10):
        numberCount[i] = 1
        numberCount[i*10+i] = 1

    ans = 9
    for i in range(11, N+1):

        # 下位が 0 の場合は無視
        if str(i)[-1] == "0":
            continue

        # 11等の上位桁と下位桁が同じ場合をカウント
        if str(i)[0] == str(i)[-1]:
            ans += 1

        # 対応するパターン数を加算
        ans += numberCount[int(str(i)[-1])*10 + int(str(i)[0])] * 2

        # 登録
        numberCount[int(str(i)[0])*10 + int(str(i)[-1])] += 1

    return ans


print(solve())

Submission status: Accepted

Solution code:
n=input()
intn=int(n)
ln=len(n)

if intn<10:
  print(n)
  quit()

def count(i,j):
  res=0
  if i==j:
    res+=1
  if ln==2:
    if 10*i+j<=intn:
      res+=1
  else:
    res+=(10**(ln-2)-1)//9
    if i<int(n[0]):
      res+=10**(ln-2)
    elif i==int(n[0]):
      tn=int(n[1:-1])
      if j<=intn%10:
        res+=tn+1
      else:
        res+=tn
  return res

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

Submission status: Accepted

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

for i in range(1,N+1):
  istr = str(i)
  a = int(istr[0])
  b = int(istr[-1])
  if (a != 0) and (b != 0): nn[a-1, b-1] += 1
    
ans = 0
for a in range(9):
  for b in range(9):
    ans += nn[a, b] * nn[b, a]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=0
l = {}
for i in range(1,10):
  for j in range(1,10):
    l[(i,j)] = 0

for i in range(1,n+1):
  s = str(i)
  if s[-1] != '0':
    a += 2*l[(int(s[-1]),int(s[0]))]
    l[(int(s[0]),int(s[-1]))] += 1
    if s[-1] == s[0]:
      a += 1

print(a)

Submission status: Accepted

Solution code:
# 解説を読んでから

from itertools import product
from math import floor, ceil
import numpy as np

N = int(input())
d = floor(np.log10(N)) + 1  # Nの桁数
N_sento = N // (10 ** (d - 1))  # Nの先頭の数字

n = np.zeros((9, 9), dtype='int')
# n[i-1,j-1] は、先頭がi, 末尾がjであるN以下の整数の個数とする

for i, j in product(range(9), range(9)):
    # n[i, j]を求める。
    # i.e. 先頭がi+1, 末尾がj+1である整数の数

    # 1桁の数字
    if i == j and N >= (i + 1):
        n[i, j] += 1

    # 2桁以上 d桁未満 の数字
    for k in range(2, d):
        n[i, j] += 10 ** (k - 2)
    
    # d桁の数字 (d >= 2 の場合)
    if d < 2:
        pass
    elif N_sento > (i + 1):
        # 先頭の桁がNのそれより小さければ、中央d-2桁は好きに作れる
        n[i, j] += 10 ** (d - 2)
    elif N_sento == (i + 1):
        # 先頭の桁がNのそれと同じだと、Nより大きくならないように中央d-2桁を決める
        n[i, j] += (N - N_sento * 10**(d-1)) // 10 + 1
        if (j + 1) > N % 10:
            n[i, j] -= 1

print(np.einsum('ij,ji', n, n))

Submission status: Accepted

Solution code:
n = int(input())
d = {}

for a in range(1, n+1):
    a = str(a)
    f, l = a[0], a[-1]
    if '0' in [f, l]:
        continue
    if (f, l) in d:
        d[(f, l)] += 1
    else:
        d[(f, l)] = 1

ans = 0
for b in range(1, n+1):
    b = str(b)
    f, l = b[-1], b[0]
    if (f, l) in d:
        ans += d[(f, l)]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import sys
# sys.setrecursionlimit(10**4)

def _li(): return list(map(int, stdin.readline().split()))
def _li_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def _lf(): return list(map(float, stdin.readline().split()))
def _ls(): return stdin.readline().split()
def _i(): return int(stdin.readline())
def _f(): return float(stdin.readline())
def _s(): return stdin.readline()[:-1]


N = _i()
table = {}
for i in range(1, 10):
    for j in range(1, 10):
        count = 0
        if i == j:
            count += 1
        a, b = str(j), str(i)
        for order in range(len(str(N)) - 1):
            min_x = int(a + "0" * order + b)
            if min_x > N:
                break
            max_x = int(a + "9" * order + b)
            if max_x <= N:
                count += 10 ** order
            else:
                assert str(N)[0] == a
                offset = 1 if str(N)[-1] < b else 0
                count += int(str(N)[1:-1]) - offset + 1
        table[(i, j)] = count

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import product


def solve(string):
    d = len(string)
    if d == 1:
        return string
    s0, ss, sn = int(string[0]), int(string[1:-1] or 0), int(string[-1])
    t = [[0] * 10] + [[int("1" * (d - 2) or 0)] * 10 for _ in range(9)]
    for i in range(1, 10):
        t[i][i] += 1
    for i, j in product(range(1, s0), range(10)):
        t[i][j] += 10**(d - 2)
    for j in range(10):
        t[s0][j] += ss + 1 if j <= sn else ss
    return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
from itertools import product


def solve(string):
    d = len(string)
    if d == 1:
        return string
    s0, ss, sn = int(string[0]), int(string[1:-1] or 0), int(string[-1])
    t = [[0] * 10] + [[int("1" * (d - 2) or 0)] * 10 for _ in range(9)]
    for i in range(1, 10):
        t[i][i] += 1
    for i, j in product(range(1, s0), range(10)):
        t[i][j] += 10**(d - 2)
    for j in range(sn + 1):
        t[s0][j] += ss + 1
    for j in range(sn + 1, 10):
        t[s0][j] += ss
    return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
n=int(input())
a=0
l = {}
for i in range(1,10):
  for j in range(1,10):
    l[(i,j)] = 0

if n > 10:
  for i in range(1,10):
    l[(i,i)] = 1
  a = 9
  for i in range(10,n+1):
    s = str(i)
    if s[-1] != '0':
      a += 2*l[(int(s[-1]),int(s[0]))]
      l[(int(s[0]),int(s[-1]))] += 1
      if s[-1] == s[0]:
        a += 1
else:
  a = n

print(a)

Submission status: Accepted

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

def CheckDigit():
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += digit[i][j] * digit[j][i]
    return ans

def ArrayRotation(n):
    for i in range(1, n+1):
        tmp = str(i)
        digit[int(tmp[0])][int(tmp[-1])] += 1

if N <= 11:
    print(N)
else:
    ArrayRotation(N)
    print(CheckDigit())

Submission status: Accepted

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

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

for i in range(1, n + 1):
    i = list(str(i))
    head = int(i[0])
    tail = int(i[-1])
    num_table[head][tail] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
di = [[0]*9 for _ in range(9)]
for i in range(1, n+1):
    p = list(str(i))
    if p[-1] == '0':
        continue
    di[int(p[0])-1][int(p[-1])-1]+=1
result = 0
for i in range(9):
    for j in range(9):
        c = di[i][j]
        d = di[j][i]
        result+=c*d
print(result)

Submission status: Accepted

Solution code:
N = int(input())

pairs={}

for n in range(1, N+1):
  str_n = str(n)
  pair=str_n[0]+str_n[-1]
  if pair in pairs:
    pairs[pair]+=1
  else:
    pairs[pair]=1
ans = 0

for i in pairs.keys():
  if i[::-1] in pairs:
    ans+=pairs[i]*pairs[i[::-1]]

print(ans)

Submission status: Accepted

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

# input
N = int(input())

# create and init dictionary
heads_and_tails = {}
for head in range(1, 10):
    for tail in range(1, 10):
        heads_and_tails[(head, tail)] = 0

# create dictionary contents
for num in range(1, N+1):
    if num%10 == 0:
        continue
    head = int(str(num)[0])
    tail = int(str(num)[-1])
    heads_and_tails[(head, tail)] +=1

# count
count = 0
for head in range(1, 10):
    for tail in range(1, 10):
        count += heads_and_tails[(head, tail)] * heads_and_tails[(tail, head)]

print(count)

Submission status: Accepted

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


def solve(N: int):
    count = {}
    for i in range(1, N+1):
        p = get_pair(i)
        key = p[0] + '_' + p[1]
        if not count.get(key):
            count[key] = 0
        count[key] += 1
    ans = 0
    for i in range(1, N+1):
        p = get_pair(i)
        key = p[1] + '_' + p[0]
        if count.get(key):
            ans += count[key]
    # print(ans if ans > 0 else 1)
    print(ans)
    return


def get_pair(n: int):
    nf = str(n)[0]
    nl = str(n)[len(str(n)) - 1]
    return [nf, nl]


# 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:
N = int(input())
arr = [0]*100
for num in range(1,N+1):
  arr[int(str(num)[0]+str(num)[-1])] += 1
print(sum([arr[i*10+j]*arr[j*10+i]  for i in range(10) for j in range(10)]))

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1,N+1):
    a = i % 10
    b = 0
    while i > 0:
        b = i
        i = i // 10
    c[a][b] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
#N=100
nlist=list(range(1,N+1))
import math
def getmax(n):
    logscale=int(math.log10(n))
    kurai=int(n/(10**logscale))
    return kurai
def getmin(n):
    return int(n%10)

profile=[]
for n in nlist:
    profile.append([getmin(n),getmax(n)])
#print(profile)
counter=0
for i in range(1,10):
    for j in range(1,10):
        ij=profile.count([i,j])
        ji=profile.count([j,i])
        counter+=ij*ji

print(counter)

Submission status: Accepted

Solution code:
n = int(input())

c = [[0] * 10 for _ in range(10)] * n
for i in range(1, n + 1):
    i1 = i // 10 ** (len(str(i)) - 1)
    i2 = i % 10
    c[i1][i2] += 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:
from sys import stdin
def solve():
    n=int(stdin.readline())
    '''ln_n=len(str(n))
    cnt=9
    for i in range(11,n+1):
        s=str(i)
        if s[-1]=='0':
            continue
        ln=len(s)
        if ln==ln_n:
            if ln_n!=2:
                cnt+=int(s[1:-1])
            else:
                if int(s[::-1])<=n:
                    cnt+=1
        else:
            cnt+=10**(ln-2)
    print(cnt)'''
    d={}
    for i in range(1,n+1):
        s=str(i)
        if s[-1]!='0':
            k=(s[0],s[-1])
            d.setdefault(k,0)
            d[k]+=1
    cnt=0
    for i in d:
        for j in d:
            if i[0]==j[1] and i[1]==j[0]:
                cnt+=(d[i]*d[j])
    #print(d)
    print(cnt)
                
solve()

Submission status: Accepted

Solution code:
n=int(input())

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

for i in range(1,n+1):
    t=str(i)
    c[int(t[0])][int(t[-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:
# coding: utf-8
N = int(input())
l = [[0 for _ in range(10)] for _ in range(10)]
for v in range(1, N+1):
    a, b = int(str(v)[0]), int(str(v)[-1])
    l[a][b] += 1
ans = 0
for i in range(10):
    for j in range(10):
        ans += l[i][j] * l[j][i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def ht(n):
    str_n = str(n)
    head = int(str_n[0])
    tail = int(str_n[-1])
    return (head, tail)

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

for i in range(1, N+1):
    h,t = ht(i)
    htmap[h][t] += 1

ans = 0
for i in range(1, N+1):
    h,t = ht(i)
    ans += htmap[t][h]

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)
    k = (s[0],s[-1])
    d[k] += 1

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

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

mat = np.zeros((10,10))

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

total = (mat*(mat.T)).sum()
print(int(total))

Submission status: Accepted

Solution code:

class Solution:

    def solve(self, N: int) -> int:

        c = []
        for i in range(10):
            c.append([])
            for j in range(10):
                c[i].append(0)

        base = 1
        for n in range(1, N+1):
            if n % (10 * base) == 0:
                base *= 10

            first = n // base
            last = n % 10

            c[first][last] += 1

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

        return answer


if __name__ == '__main__':

    # standard input
    N = int(input())

    # solve
    solution = Solution()
    print(solution.solve(N))

Submission status: Accepted

Solution code:
import collections
N = int(input())
li = [i for i in range(1, N+1)]
l1 = list(map(lambda x:str(x)[0]+str(x)[-1], li))
l2 = list(map(lambda x:str(x)[-1]+str(x)[0], li))
c1 = collections.Counter(l1)
c2 = collections.Counter(l2)

num = 0
for l in set(l1):
    num += c1[l] * c2[l]
print(num)

Submission status: Accepted

Solution code:
n = int(input())

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

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

    c[start][stop] += 1

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

#iとjがどちらも0の場合は問題設定と合致しないので引く
print(r)

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        c1 = 0
        c2 = 0
        d = 1
        for x in range(1,N+1):
            if x % (d *10) == 0:
                d = d * 10
        
            if x % 10 == i and x // d == j:
                c1 += 1
            if x % 10 == j and x // d == i:
                c2 += 1
        ans += c1 * c2

print (ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1, n+1):
    if i % 10 == 0:
        continue
    i = str(i)
    d[i[0]+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:
import os, sys

def msd(n):
  d = 0
  while n > 0:
    d = n % 10
    n //= 10
  return d

def main():
  N = int(input())
  c = [[0] * 10 for _ in range(10)]
  for n in range(1, N + 1):
    l = msd(n)
    r = n % 10
    c[l][r] += 1
  #print(c)
  ans = 0
  for i in range(1, 10):
    for j in range(1, 10):
      ans += c[i][j]*c[j][i]
  print(ans)


if __name__ == '__main__':
  if sys.platform == 'ios':
    sys.stdin = open(os.path.splitext(__file__)[0] + '.txt')
  sys.setrecursionlimit(10**6)
  main()

Submission status: Accepted

Solution code:
import math

n = int(input())

def N(n, i, j):
    num = 0
    for x in range(1,n+1):
        low = int(x%10)
        high = int(x/(pow(10, int(math.log10(x)))))
        if((high == i)and(low == j)):
            num = num + 1
    return num

s = 0
for i in range(1,10):
    s = s + pow(N(n, i, i), 2)

for i in range(2,10):
    for j in range(1,i):
        s = s + 2*N(n, i, j)*N(n, j, i)

print(s)

Submission status: Accepted

Solution code:
N = int(input())

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

cnt = 0

for i in range(1, N+1):
    head = int(str(i)[0])
    tail = int(str(i)[len(str(i)) - 1])
    list[head][tail] += 1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	poole = {}

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

		if (start,end) in poole:
			poole[(start,end)] += 1
		else:
			poole[(start,end)] = 1

	ans = 0
	for i in range(1,N+1):
		ii = str(i)
		start = ii[0]
		end = ii[-1]

		if (end,start) in poole:
			tmp = poole[(end,start)]
			ans += tmp

	print(ans)







if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
ans = 0
d = 0
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

for i in range(10):
    for j in range(10):
        if i == j:
            d += M[i][j] * M[j][i]
        else:
            ans += M[i][j] * M[j][i]

print(ans + d)

Submission status: Accepted

Solution code:
N=int(input())
count=[[0]*9 for i in range(9)]
for i in range(N+1):
  j=str(i)
  if j[0] != '0' and j[-1] != '0':
    count[int(j[0])-1][int(j[-1])-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:
"""
108345237845804
498073591
are an example match, because (1,4) and (4,1)
each integer can be encoded as this kind of pair
if there is also
4768281
then we have 2 matches so far, because the
first matches with the second and third
considering 1 and 11 and 111 and 1111 and 11111
each are encoded as (1,1), so we get 9 matches from just these 5...
solution :
just count how many integers get encoded as a certain pair, then calculate
the number of pairs that would be made from them
eg if (1,4) = 10 and (4,1) = 5 then there are 5*10 = 50 pairs
How many encodings? no leading zero, so also ending zero wont get a match,
so 1 to 9 and 1 to 9, is 9*9=81 encodings
"""


def handstand(N):
    # first count how many integers have a given encoding
    count_encoding = {(x, y): 0 for x in range(1, 10) for y in range(1, 10)}
    for i in range(1, N+1):
        string_convert = str(i)
        start_val = int(string_convert[0])
        end_val = int(string_convert[-1])
        if end_val == 0: continue
        count_encoding[start_val, end_val] += 1
    # now count the encoding matches
    matches_count = 0
    for x in range(1, 10):
        for y in range(1, 10):
            matches_count += count_encoding[x, y]*count_encoding[y, x]
    return matches_count


if __name__ == "__main__":
    test1 = "25"
    test2 = "1"
    test3 = "100"
    test4 = "2020"
    test5 = "200000"
    test6 = "16"
    test = test4
    # in_val = int(test)
    in_val = int(input())
    print(handstand(in_val))

Submission status: Accepted

Solution code:
N = int(input())


def head_tail(num):
    if len(str(num)) == 1:
        return (num, num)
    top = int(str(num)[0])
    tail = int(str(num)[-1])
    return (top, tail)


pairs = {}
for i in range(1, N+1):
    p = head_tail(i)
    if p not in pairs:
        pairs[p] = 1
    else:
        pairs[p] += 1

ans = 0
for i in range(1, N+1):
    pair = head_tail(i)
    re = (pair[1], pair[0])
    ans += pairs.get(re, 0)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    l = [[0] * 10 for _ in range(10)]
    for i in range(1,N + 1):
        i = str(i)
        t = i[0]
        b = i[-1]
        if t == 0 or b == 0:
            continue
        l[int(t)][int(b)] += 1
    r = 0
    for i in range(1, 10):
        for j in range(1, 10):
            r += l[i][j] * l[j][i]
    return r

print(main())

Submission status: Accepted

Solution code:
n = int(input())
sn = str(n)

def gutyoku(head, tail):
    result = 0
    for i in range(tail, n + 1, 10):
        if int(str(i)[0]) == head:
            result += 1
    return result

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

Submission status: Accepted

Solution code:
import itertools
import numpy as np
N = int(input())
log_n = int(np.log10(N))

vals = []

for a,b in itertools.product(range(1,10), range(1,10)):
    res = 0
    if a == b and a <= N:
        res += 1
    for i in range(1, log_n + 1):
        mini = a * (10 ** i) + b
        if N - mini < 0:
            break
        if 10 * (10 ** (i - 1) - 1) <= N - mini:
            res += 1 + 10 * (10 ** (i - 1) - 1) // 10
        else:
            res += 1 + np.floor((N - mini) / 10)
    vals.append(((a, b), int(res)))
table = {k:v for k,v in vals}
res = 0
for x in list(itertools.combinations(range(1,10),2)):
    res += 2 * (table[x] * table[(x[1],x[0])])
for x in range(1,10):
    res += table[(x,x)] ** 2
print(res)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    flg = 0
    val=0
    ans=0
    ns = str(n)
    nl = len(ns)
    tmp = [[0]*10 for i in range(11)]
    for i in range(1,n+1):
        s = str(i)
        top = int(s[0])
        btm = int(s[-1])
        tmp[top][btm] += 1

    for i,a in enumerate(tmp):
        for j,b in enumerate(a):
            if b != 0:
                ans += b*tmp[j][i]
    print(ans)





if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
lists = [str(i) for i in range(1,N+1)]
count = 0
for i in range(1,10):
    for j in range(i,10):
        s_cnt, t_cnt = 0, 0
        si, sj = str(i), str(j)
        for k in lists:
            if k[0] == si and k[-1] == sj:
                s_cnt += 1
            elif k[0] == sj and k[-1] == si:
                t_cnt += 1
        if i != j:
            count += s_cnt * t_cnt * 2
        elif i == j:
            count += s_cnt ** 2
print(count)

Submission status: Accepted

Solution code:
n = int(input())

pattern = {}

for nn in range(1,n + 1):
    f = int(nn / (10 ** (len(str(nn) ) -1)))
    l = nn % 10  
    p = f * 10 + l
    if f == l:
        p = f
    
    if  p in pattern:
       pattern[p] += 1
    else:
        pattern[p] = 1


s = 0
for p, v in pattern.items():

    f = int(p / (10 ** (len(str(p) ) -1)))
    l = p % 10
    p2 = l * 10 + f
    if l == 0:
        continue      
    elif p < 10: 
        s +=  v * v
    elif p2 in pattern:
        s += v * pattern[p2]
print(s)