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:
S=input()
N=int(S)
n=len(S)


ans=0
p=[1,10,100,1000,10000,100000]

for i in range(N+1):#数iに対する相手を探す
    ii=str(i)
    a=int(ii[0])
    b=int(ii[-1])
    if a*b!=0:
        if a==b:#a**aの形なら1桁の答えaが存在
            ans+=1
        for j in range(2,n+1):#桁数
            if j<n:
                ans+=p[j-2]
                #print(i,j,n,ans)
            elif j==n:
                floor=b*p[j-1]+a
                if j==2:
                    ceil=floor
                else:
                    ceil=floor+p[j-1]-p[1]
                #print(i,j,floor,ceil)
                if N>=ceil:
                    ans+=p[j-2]
                    #print(i,j,n,ans)
                elif N<floor:
                    continue
                else:#floorとceilの間
                    ans+=(N-floor)//10+1
                    #print(i,j,n,ans)
    #print("i,ans:{},{}".format(i,ans))
                    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(n):
    np = str(i+1)
    s = len(np)
    r1 = np[-1]
    r2 = np[-s]
    
    a[int(r1)][int(r2)] += 1
                    
for i in range(10):
    for j in range(10):
        ans += a[i][j]*a[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):
    head = int(str(i)[0])
    tail = int(str(i)[-1])
    table[head][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:
n = int(input())

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

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

    c[a][b] += 1

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
count=[[0 for i in range(10)] for j in range(9)]
ans=0
num=1
zero=0
for i in range(1,n+1):
    if (10**(zero+1))<=num:
        zero+=1
    a=i//10
    r=i-a*10
    l=i//(10**zero)
    count[l-1][r]+=1
    num+=1
#print(count)
for i in range(1,10):
    for j in range(1,10):
        if i==j:
            continue
        ans+=(count[i-1][j])*(count[j-1][i])
for i in range(1,10):
    ans+=(count[i-1][i]**2)
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])][(i % 10)] += 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())
ans = 0

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

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

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())
ans = 0
count = []
for i in range(10):
    count.append([0] * 10)
for i in range(N+1):
    str_i = str(i)
    if str_i[-1] == "0":
        continue
    else:
        if str_i[-1] == str_i[0]:
            ans += 2*count[int(str_i[0])][int(str_i[-1])] +1
            count[int(str_i[0])][int(str_i[-1])] += 1
        else:
            ans += count[int(str_i[-1])][int(str_i[0])]*2
            count[int(str_i[0])][int(str_i[-1])] += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import product


def solve(string):
    l = len(string)
    *ns, = map(int, string)
    n = int(string)
    c = [0] + [10**i for i in range(6)]
    t = sum(c[:l - 1])
    t = [[t] * 10 for _ in range(10)]
    if l > 1:
        for i in range(10):
            t[i][i] += 1
    for i in range(1, ns[0]):
        for j in range(1, 10):
            t[i][j] += c[l - 1]
    for i in range(ns[0] * 10**(l - 1), n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    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):
    n = int(string)
    t = [[0] * 10 for _ in range(10)]
    for i in range(n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    # print(t)
    # return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))
"""


def solve(string):
    l = len(string)
    n = int(string)
    c = [0] + [10**i for i in range(6)]
    t = sum(c[:l - 1])
    t = [[t] * 10 for _ in range(10)]
    if l > 1:
        for i in range(10):
            t[i][i] += 1
    for i in range(10 ** (l - 1), n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    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())

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

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

    if first == 0 or last == 0:
        continue
    else:
        count[first][last] += 1

ans = 0
for first in range(1,10):
  for last in range(1,10):
    ans += count[first][last] * count[last][first]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
n= str(N)
cnt = 0
c=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
  i = str(i)
  a = int(i[0])
  b = int(i[-1])
  c[a][b]+=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())
c=[[0]*10 for i in range(10)]
for i in range(1,n+1):
  c[int(str(i)[0])][int(str(i)[len(str(i))-1])]+=1
ans=0
for i in range(1,10):
  for j in range(1,10):
    ans+=c[i][j]*c[j][i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
r = range(1, min(9, n) + 1)
a = [None] + [[0] * 10 for _ in r]
for i in r:
  a[i][i] += 1
for i in r:
  for j in r:
    k = 1
    while 10 * k * i + j <= n:
      a[i][j] += min(k, (n - j) // 10 - k * i + 1)
      k *= 10
print(sum(a[i][j] * a[j][i] for i in r for j in r))

Submission status: Accepted

Solution code:
r = range(1, 10)
n = int(input())
a = [None] + [[0] * 10 for _ in r]
for i in r:
  if i <= n:
    a[i][i] += 1
for i in r:
  for j in r:
    k = 1
    while 10 * k * i + j <= n:
      a[i][j] += min(k, (n - j) // 10 - k * i + 1)
      k *= 10
print(sum(a[i][j] * a[j][i] for i in r for j in r))

Submission status: Accepted

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

for i in range(1, n+1):
  top = int(str(i)[0])
  btm = int(str(i)[-1])
  if btm == 0:
    continue
  l[top][btm] += 1
    
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:
from sys import stdin
N = int(stdin.readline().rstrip())
# c_ij:1<=k<=Nで、kの先頭がiに等しく、末尾がjに等しい数を考える
# 0<=i,j<=9
# sum(0<=i<=9)sum(0<j<=9)c_ij * c_jiで答えが出るみたい
c_list = [[0] * 10 for i in range(10)]
for n in range(1, N + 1):
    # for i in range(10):
        # for j in range(10):
            # if str(n)[0] == str(i) and str(n)[-1] == str(j):
            #     c_list[i][j] += 1
    int(str(n)[0])
    c_list[int(str(n)[0])][int(str(n)[-1])] += 1
output = 0
for i in range(10):
    for j in range(10):
        output += c_list[i][j] * c_list[j][i]
# print(c_list)
print(output)

Submission status: Accepted

Solution code:
n = int(input())
r = range(1, 10)
a = [[0] * 9 for _ in r]
for i in r:
  if i <= n:
    a[i - 1][i - 1] += 1
for i in r:
  for j in r:
    k = 1
    while 10 * k * i + j <= n:
      a[i - 1][j - 1] += min(k, (n - j) // 10 - k * i + 1)
      k *= 10
print(sum(a[i][j] * a[j][i] for i in range(9) for j in range(9)))

Submission status: Accepted

Solution code:
N=int(input())

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

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

    if Btm == '0':
        continue
    
    IJ[int(Top)][int(Btm)] += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from itertools import product


def solve(string):
    n = int(string)
    t = [[0] * 10 for _ in range(10)]
    for i in range(n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    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):
    l = len(string)
    *s, = map(int, string)
    n = int(string)
    t = [[0] * 10 for _ in range(10)]
    for i in range(n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    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())
ans = 0
for i in range(1,n+1):
    t,b = str(i)[0],str(i)[-1]
    if b == "0":
        continue
    if t==b:
        ans += 1
    if int(b + t) <= n:
        ans += 1
    l = len(str(n))
    for j in range(l-2):
        if j == l - 3:
            if int(str(n)[0]) > int(b):
                ans += 10**(j + 1)
            elif int(str(n)[0]) == int(b):
                ans += int(str(n)[1:-1])
                if int(str(n)[-1]) >= int(t):
                    ans += 1
        else:
            ans += 10 ** (j + 1)
print(ans)

Submission status: Accepted

Solution code:
#abc152d

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

Submission status: Accepted

Solution code:
#python3
from collections import defaultdict
from collections import namedtuple

P = namedtuple('P', ['front', 'end'])

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

def main():

    n = int(input())
    freq = defaultdict(int)

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


    ans = 0
    for i in range(1, n + 1):
        p = f(i)
        q = (p.end, p.front)
        ans += freq[q]

    print(ans)

main()

Submission status: Accepted

Solution code:
n = int(input())
li_a = []
for i in range(9):
  li_a.append([0,0,0,0,0,0,0,0,0])
for i in range(1, n+1):
  k = str(i)
  k0 = int(k[0])
  k1 = int(k[-1])
  if k0*k1 != 0:
    li_a[k0-1][k1-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    ans += li_a[i][j]*li_a[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

N = ir()
# Aの先頭の桁と末尾の桁の組み合わせは9*10=90通りだけ
table = [[0] * 10 for i in range(10)]
for x in range(1, N+1):
    x = str(x)
    i = int(x[0])
    j = int(x[-1])
    table[i][j] += 1

table = np.array(table)
answer = (table * table.T).sum()
print(answer)

Submission status: Accepted

Solution code:
#python3
from collections import defaultdict

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

def main():

    n = int(input())
    freq = defaultdict(int)

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


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

    print(ans)

main()

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

N = ir()
# Aの先頭の桁と末尾の桁の組み合わせは9*10=90通りだけ
table = np.zeros((10, 10)).astype(int)
for x in range(1, N+1):
    x = str(x)
    i = int(x[0])
    j = int(x[-1])
    table[i][j] += 1

table = np.array(table)
answer = (table * table.T).sum()
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
li = [0] * 100 # li[top][bottom]
for i in range(1,n+1):
  top = str(i)[0]
  bottom = str(i)[-1]
  if top != 0 and bottom != 0:
    li[int(top+bottom)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
cnt = [[0 for _ in range(11)] for _ in range(11)]
for i in range(1, n + 1):
    x = i
    while x >= 10:
        x //= 10
    cnt[x][i % 10] += 1
ans = 0
for i in range(1, n + 1):
    x = i
    while x >= 10:
        x //= 10
    ans += cnt[i % 10][x]
print(ans)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

for num in range(1, n+1):
    first = int(str(num)[0])
    last = int(str(num)[-1])

    if first == 0 or last == 0:
        continue
    else:
        count[first][last] += 1

ans = 0
for first in range(1, 10):
    for last in range(1, 10):
        pattern = count[first][last] * count[last][first]
        ans += pattern

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
d = defaultdict(int)

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

num = '123456789'
res = 0
for i in num:
    for j in num:
        res += d[(i, j)] * d[(j, i)]

print(res)

Submission status: Accepted

Solution code:
#!python3

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

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

    return a

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

    if N < 10:
        print(N)
        return

    k = N // 10
    A = d10(N)

    a = A[-1]
    b = A[0]

    ans = k ** 2 + 8
    if b:
        c = len(A)
        d = d0 = 0
        for i in range(c-1):
            d, d0 = 10 ** i + d, d

        if a > b:
            ans += 2 * d * b
        else:
            e = k - a*10**(c-2)
            ans += 2 * d * (a-1)
            ans += 2 * (d0 + e + 1) + 1
            ans += 2 * d0 * (b - a)
    print(ans)



if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
#!python3

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

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

    return a

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

    if N == 1:
        print(1)
        return

    k = N // 10
    A = d10(N)

    a = A[-1]
    b = A[0]

    ans = k ** 2 + 8
    if b:
        c = len(A)
        d = d0 = 0
        for i in range(c-1):
            d, d0 = 10 ** i + d, d

        if a > b:
            ans += 2 * d * b
        else:
            e = k - a*10**(c-2)
            ans += 2 * d * (a-1)
            ans += 2 * (d0 + e + 1) + 1
            ans += 2 * d0 * (b - a)
    print(ans)



if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N=int(input())
A=[[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9]

for n in range(N+1):
    temp=str(n)
    r=int(temp[-1])
    l=int(temp[0])
    A[r-1][l-1]+=1

ans=0
for i in range(9):
    for j in range(9):
        ans+=A[i][j]*A[j][i]
if N<=9:
    print(N)
else:
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c=[[0]*10 for _ 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())

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

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

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

Submission status: Accepted

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

ans = 0
for i in range(1,n+1):
    s=str(i)
    if s[0]=="0" or s[-1]=="0":
        continue
    else:
        d[int(s[0])][int(s[-1])] += 1

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

print(ans)

Submission status: Accepted

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

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

s = 0
for t in range(10):
  for b in range(10):
    s += c[t][b] * c[b][t]

print(s)

Submission status: Accepted

Solution code:
def d():
    N = int(input())
    a = {}
    count = 0

    for i in range(1, N+1):
        l, r = str(i)[0], str(i)[-1]
        a[l, r] = a.get((l, r), 0) + 1

    # print(a)

    for i in range(1, N+1):
        l, r = str(i)[0], str(i)[-1]
        # print('a.get(({0}, {1}), 0) * a.get(({1}, {0}), 0) = {2}'.format(l, r, a.get((l, r), 0) * a.get((r, l), 0)))
        count += a.get((r, l), 0)

    print(count)


##########
d()

Submission status: Accepted

Solution code:
n = int(input())
d = [[0]* 9 for _ in range(9)]
for num in range(1, n+1):
    str_num = str(num)
    if str_num[-1] != "0":
        start = int(str_num[0]) - 1
        end = int(str_num[-1]) - 1
        d[start][end] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())


def func(x):
    while x >= 10:
        x //= 10
    return x


cnt = [[0] * 10 for _ in range(10)]
for i in range(1, n + 1):
    digit_end = i % 10
    digit_start = func(i)
    cnt[digit_start][digit_end] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

result = 0
for j in range(10):
    for k in range(10):
        result += cnt[j][k] * cnt[k][j]

print(result)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
data=[0]*100
for ni in range(1,n+1):
    a=str(ni)[0]; b=str(ni)[-1]
    a=int(a); b=int(b)
    data[a*10+b]+=1
for i in range(1,10):
    for j in range(1,10):
        ans+=data[i*10+j]*data[j*10+i]
# print(data)
print(ans)

Submission status: Accepted

Solution code:
def make_key(a, b):
    return str(a) + '-' + str(b)


N = int(input())
nums = {}
for i in range(1, 10):
    for j in range(1, 10):
        nums[make_key(i, j)] = 0

ans = 0
for n in range(1, N + 1):
    strn = str(n)
    if strn[-1] != '0':
        key = make_key(strn[0], strn[-1])
        nums[key] += 1


for key, value in nums.items():
    a, b = map(int, key.split('-'))
    if a == b:
        ans += value ** 2
    else:
        key2 = make_key(b, a)
        ans += value * nums[key2]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
l = map(str, range(1, N+1))
summary = [[0 for _ in range(9)] for _ in range(9)]

for i in l:
    if i[-1] != "0":
        summary[int(i[0])-1][int(i[-1])-1] += 1

c = 0
for i in range(9):
    for j in range(9):
        c += summary[i][j] * summary[j][i]

print(c)

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
x = list(d.keys())
for i in x:
    ans+=d[i[::-1]]*d[i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
numMap = {}
answer = 0
def StartEnd(x):
    start = str(x)[0]
    end = str(x)[-1]
    return start, end

for i in range(1, N+1):
    # print('numMap:', numMap)
    pair = StartEnd(i)
    if pair in numMap:
        numMap[pair] += 1
    else:
        numMap[pair] = 1

for i in range(1, N+1):
    key = StartEnd(i)
    rev = key[::-1]
    # print('rev:', rev)
    if rev in numMap:
        answer += numMap[rev]
    
print(answer)

Submission status: Accepted

Solution code:
def left(text, n):
  return text[:n]

def right(text, n):
  return text[-n:]

def mid(text, n, m):
  return text[n-1:n+m-1]
  
n = int(input())

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

# [1,9][9,1]など先頭＋最終の組み合わせを設定
for i in range(1,n+1):
	first = int(left(str(i),1))
	end = int(right(str(i),1))
	rangeSet[first][end] += 1

kumiawase = 0
for k in range(1,10):
	for l in range(1,10):
		tmp = rangeSet[k][l] * rangeSet[l][k]
		kumiawase += tmp	
print(kumiawase)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
 
def solve(x):
    df = str(x)[0]
    dl = str(x)[-1]
    return df, dl
 
numMap = {}
 
for i in range(1, n+1):
    # print(i)
    # print(solve(i))
    pair = solve(i)
    if pair in numMap:
        numMap[pair] += 1
    else:
        numMap[pair] = 1
 
for i in range(1, n+1):
    key = solve(i)
    rev = key[::-1]
    # print('key:', key)
    # print('rev:', rev)
    if rev in numMap:
        ans += numMap[rev]
 
# print(numMap)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0

def solve(x):
    df = str(x)[0]
    dl = str(x)[-1]
    return df, dl

numMap = {}

for i in range(1, n+1):
    # print(i)
    # print(solve(i))
    pair = solve(i)
    if pair in numMap:
        numMap[pair] += 1
    else:
        numMap[pair] = 1

for i in range(1, n+1):
    key = solve(i)
    rev = key[::-1]
    # print('key:', key)
    # print('rev:', rev)
    if rev in numMap:
        ans += numMap[rev]

# print(numMap)
print(ans)

Submission status: Accepted

Solution code:
import sys

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

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

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

    print(ans)

main()

Submission status: Accepted

Solution code:
n=int(input())

r=[[0 for _ in range(10)] for __ in range(10)]

for i in range(1,n+1):
    q=i%10
    p = i
    while p>=10:
        p //=10
    r[p][q] +=1
res=0
for i in range(1,10):
    for j in range(1,10):
        res += r[i][j]*r[j][i]
print(res)

Submission status: Accepted

Solution code:
import collections
n = int(input())
d = collections.Counter(map(lambda x: str(x)[0]+" "+str(x)[-1], list(range(1,n+1))))
ans = 0
for k, v in d.items():
	a,b = map(int, k.split())
	if a==b:
		ans += v*v
	elif a<b:
		s = str(b) + " " + str(a)
		if s in d:
			u = d[s]
			ans += 2*v*u
print(ans)

Submission status: Accepted

Solution code:
N = input()
l = len(N)


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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
n = int(input())

table = [[0 for i in range(10)] for j in range(10)]
for i in range(1, n+1):
    i = str(i)
    a, b = int(i[0]), int(i[-1])
    table[a][b] += 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())

count = [0]*100
for i in range(1, n+1):
    s = str(i)
    x = int(s[0] + s[len(s)-1])
    count[x] += 1

ans = 0
for i in range(1, n+1):
    s = str(i)
    x = int(s[len(s)-1] + s[0])
    ans += count[x]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

for first_num in range(1,10):
  for last_num in range(1,10):
    ans += C[first_num][last_num] * C[last_num][first_num]

print(ans)

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

C = list2d(10, 10, 0)
for a in range(1, N+1):
    s = int(str(a)[0])
    t = int(str(a)[-1])
    C[s][t] += 1

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

Submission status: Accepted

Solution code:
n = int(input().strip())

arr = ["1",
       "2",
       "3",
       "4",
       "5",
       "6",
       "7",
       "8",
       "9"
       ]
pairs = {}

for i in arr:
    for j in arr:
        pairs[i + "_" + j] = 0
for i in range(1, (n + 1), 1):
    si = str(i)
    if si[0] != "0" and si[-1] != "0":
        pairs[si[0] + "_" + si[-1]] += 1
ans = 0
for i in range(1, (n + 1), 1):
    si = str(i)
    if si[0] != "0" and si[-1] != "0":
        ans += pairs[si[-1] + "_" + si[0]]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for x in range(1,n+1):
    if x%10!=0:
        str_x = str(x)
        l,r = int(str_x[0]), int(str_x[-1])
        table[l][r] += 1

ans = 0
for x in range(1, n+1):
    if x%10 != 0:
        str_x = str(x)
        l,r = int(str_x[0]), int(str_x[-1])
        if l==r:
            ans += table[l][r]
        else:
            ans += table[r][l]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
t = [[0 for _ in range(9)] for l in range(9)]
ans = 0
for i in range(1, N+1):
    if len(str(i)) == 1:
        t[i-1][i-1] += 1
    elif str(i)[-1] != "0":
        t[int(str(i)[0])-1][int(str(i)[-1])-1] += 1
for i in range(1, N+1):
    a = str(i)[0]
    b = str(i)[-1]
    if b == "0":
        continue
    elif a == b:
        ans += t[(int(a)-1)][int(a)-1]
    else:
        ans += t[(int(b)-1)][int(a)-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
C = [[0 for _ in range(10)] for _ in range(10)]
for n in range(1, N+1):
    s, e = int(str(n)[0]), int(str(n)[-1])
    C[s][e] += 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())
 
li = [[0] * 9 for _ in range(9)]
 
for i in range(1, n + 1):
    str_i = str(i)
    top = int(str_i[0])
    bottom = int(str_i[-1])
    if bottom == 0:
        continue
    li[top-1][bottom-1] += 1
 
cnt = 0
for i in range(1, n + 1):
    str_i = str(i)
    top = int(str_i[0])
    bottom = int(str_i[-1])
    if bottom == 0:
        continue
    cnt += li[bottom-1][top-1]
 
print(cnt)

Submission status: Accepted

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