Problem:


Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

1 \leq N \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008



Sample input:
25


Sample output:
17

Submission status: Accepted

Solution code:
N = int(input()) # 数字を変数に格納

result = 0
list = [[0]*9 for i in range(9)]

for i in range(1,N+1):
    if i < 10:
        list[i-1][i-1] += 1
    else:
        long = len(str(i))
        first = int(i / (10**(long-1)))
        last = i % 10
        if first != 0 and last != 0:
            list[first-1][last-1] += 1
            

for i in range(9):
    for j in range(i,9):
        if i == j:
            tmp = list[i][j]
            result += tmp * tmp
        else:
            result += list[i][j] * list[j][i] * 2

print(result)

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
for i in range(10, 100): dic[str(i)]=0
for i in range(1, n+1):
    s = str(i)
    dic[s[0]+s[-1]] += 1

ans = 0
for i in range(1, n+1):
    s = str(i)
    if s[-1]=="0": continue
    ans += dic[s[-1]+s[0]]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
start, end, both = dict(), dict(), dict()
for i in range(1, N+1):
    i_s = str(i)
    l, r = int(i_s[0]), int(i_s[-1])
    both[l, r] = both.get((l, r), 0) + 1

ans = 0
for i in range(1, N + 1):
    i_s = str(i)
    l, r = int(i_s[0]), int(i_s[-1])
    ans += both.get((r, l), 0)
print(ans)

Submission status: Accepted

Solution code:
import math
import numpy as np

N = int(input())
ans = 0
num_mat = np.zeros((9, 9))
for i in range(N+1):
  head = int(list(str(i))[0])
  tail = int(list(str(i))[-1])
  rev = ''.join(list(str(i))[::-1])
  if tail==0:
    continue
  num_mat[head-1, tail-1] += 1

for i in range(9):
  for j in range(i+1):
    if i==j:
      num = num_mat[i,j]
      ans += num + num * (num-1)
    else:
      ans += num_mat[i,j] * num_mat[j,i] * 2

print(int(ans))

Submission status: Accepted

Solution code:
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
n = int(input())
ans_dict = {}
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans_dict[i*10+j]=0
for num in range(1,n+1):
    num_str =  str(num)
    i = int(num_str[len(num_str)-1])
    j = int(num_str[0])
    if (i!=0) & (j!=0):
        ans_dict[i*10+j] +=1
for key_ in ans_dict.keys():
    if str(key_)[0] == str(key_)[1]:
        ans += ans_dict[key_]**2
    else:
        key_s = key_//10+int(str(key_)[1])*10
        ans += ans_dict[key_] * ans_dict[key_s]*2
        ans_dict[key_s] = 0
print(ans)

Submission status: Accepted

Solution code:
N= int(input())
lead_N = int(str(N)[0]) 
end_N = int(str(N)[len(str(N)) - 1])
ans = 0
#print(str(100)[len(str(100)) - 1])
for i in range(1, N + 1):
  lead = int(str(i)[0])
  end = int(str(i)[len(str(i)) - 1])
  # 一桁
  if lead == end:
    ans += 1
  #2桁以上ループ lead + 中間 + end　
  for j in range((len(str(N)) - 2) + 1):
    if end == 0:
      #末尾が0
      continue
    elif end > lead_N and j == (len(str(N)) - 2) :
      #生成する整数がNに収まらない場合
      continue
    elif end == lead_N and j == (len(str(N)) - 2):
      #生成する整数の一部がNに収まらない場合
      #中間の数字
      if len(str(N)) >= 3:
        ans += int(str(N)[1:len(str(N)) - 1])

      if lead <= end_N:
        ans += 1
      #print("b")
      pass
    else:
      #何も気にしないときは組み合わせの分だけたす
      ans += 10 ** j

print(ans)

Submission status: Accepted

Solution code:
n=str(input())
H=n[0]
T=n[-1]
keta=len(n)
n=int(n)
ans=0
if keta==1:
    print(1)
    exit()
elif keta==2:
    for i in range(1,n+1):
        s=str(i)
        for j in range(1,n+1):
            S=str(j)
            if S[0]==s[-1] and S[-1]==s[0]:
                ans+=1    
else:
    for i in range(1,n+1):
        s=str(i)
        h=s[0]
        t=s[-1]
        if h!="0" and t!="0":
            ans+=(10**(keta-2)-1)//9
            if h==t:
                ans+=1
            if int(t)<int(H):
                ans+=(10**(keta-2))*(int(H)-int(t))
            elif int(t)==int(H):
                ans+=max(0,((n-10**(keta-1)*int(H)-int(h))//10)+1)
            
print(ans)

Submission status: Accepted

Solution code:
N = str(input())

resultdist = {}

ketalist = [1,11,111,1111,11111]

def chukan(N):
    if(len(N) > 2):
        return int(N[1:-1])
    else:
        return 0

def retketa1(N):
    if(len(N) > 1):
        return ketalist[len(N)-2]
    else:
        return 0

def retketa2(N):
    if(len(N) > 2):
        return ketalist[len(N)-3]
    else:
        return 0

for i in range(1,10):
    for j in range(1,10):
        count = 1 if i==j else 0
        if(i < int(N[0])):
            count += retketa1(N)
        if(i > int(N[0])):
            count += retketa2(N)
        if(i == int(N[0])):
            count += retketa2(N)
            if(j > int(N[-1])):
                count += chukan(N)
            else:
                if(len(N) > 1):
                    count += chukan(N) + 1
                else:
                    count += chukan(N)

        resultdist[str(i)+str(j)] = count

resulutcount = 0

for i in range(1, int(N)+1):
    l = str(i)[0]
    f = str(i)[-1]
    if(f != "0"):
        resulutcount += resultdist[f+l]
print(resulutcount)

Submission status: Accepted

Solution code:
from collections import defaultdict
a = [list(map(int, s.split()))[0] for s in open(0)][0]
cnt = 0 
di = defaultdict(int)
for i in range(1, a + 1):
    if len(str(i)) == 1:
        cnt += 1 
        di[str(i)] += 1
    elif str(i) != 0:
        if str(i)[0] == str(i)[-1]:
            cnt += di[str(i)[-1]] * 2 + 1
            cnt += di[str(i)[0] + str(i)[-1]]*2
            di[str(i)[0] + str(i)[-1]] += 1
        else:
            cnt += di[str(i)[-1] + str(i)[0]]*2
            di[str(i)[0] + str(i)[-1]] += 1

print(cnt)

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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

N=one_int()
N+=1

def naive_solver(N):
    output=[]
    for i in range(1,N):
        A=str(i)
        for j in range(1,N):
            B=str(j)
            if A[-1]==B[0] and B[-1]==A[0]:
#                 if A[0]=="1":
                output.append([A,B])
    return output

def naive_solver2(N):
    dicts={str(i):{str(j):0 for j in range(10)} for i in range(10) }
    for i in range(1,N):
        temp=str(i)
        dicts[temp[0]][temp[-1]] += 1
    return dicts

dicts = naive_solver2(N)

import itertools as it

from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under



dicts = naive_solver2(N)

count = 0
for si in range(1,10):
    i=str(si)
    for sj in range(si,10):
        j = str(sj) 
        
#         count += dicts[i][j] * dicts[j][i] * 2
        if j!=i:
            count+=dicts[i][j] * dicts[j][i] * 2#2*cmb(dicts[i][j]+dicts[j][i],2)    
#             print(si,sj,dicts[i][j] * dicts[j][i] * 2)#2*cmb(dicts[i][j]+dicts[j][i],2))
        else:
            count+=dicts[i][j]*dicts[j][i]#cmb(dicts[i][j]+dicts[j][i],2)
#             print(si,sj,dicts[i][j]*dicts[j][i])

print(count)

Submission status: Accepted

Solution code:
N = int(input())

m = dict()
for i in range(1, N + 1):
    n = str(i)
    s = n[0]
    t = n[-1]
    
    if t == 0:
        continue
    
    k = (s, t)
    if k in m:
        m[k] += 1
    else:
        m[k] = 1

ans = 0
for k, v in m.items():
    s, t = k
    if s == t:
        ans += v ** 2
    else:
        if (t, s) in m:
            ans += v * m[(t, s)]

print(ans)

Submission status: Accepted

Solution code:
from itertools import product
from functools import reduce
import operator
def prod(iterable):
    return reduce(operator.mul, iterable, 1)
def cnt_samelen(b,e,n_str):
  n0=int(n_str[0])
  if b>n0 or len(n_str)==1:
    return 0
  if b<n0:
    return b*int(10**(len(n_str)-2))
  
  return (int(n_str[1:-1])+1 if len(n_str)>2 else 1)-(1 if e>int(n_str[-1]) else 0)
def cnt(b,e,n_str):
  res=0
  l=len(n_str)
  if b==e and int(n_str)>=b:
    res+=1
  for i in range(l-2):
    res+=10**(i)
  res+=cnt_samelen(b,e,n_str)
  return res
def sol():
  n_str=input()
  return sum(cnt(b,e,n_str)*cnt(e,b,n_str) 
             for b,e in product(range(1,10),repeat=2))

print(sol())

Submission status: Accepted

Solution code:
N = int(input())
P = list(range(1,N+1))
ans = 0
keta = len(str(N))
for p in P:
    first = str(p)[0]
    last = str(p)[-1]
    if last == "0":
        pass
    else:
        temp = 0
        for k in range(1,keta+1):
            if k == 1:
                if first == last:
                    temp += 1
            elif k == 2:
                if int(last+first) <= N:
                    temp += 1
            elif k == keta:
                if int(last) > int(str(N)[0]):
                    pass
                elif int(last) < int(str(N)[0]):
                    temp += 10**(k-2)
                else:
                    temp += int(str(N)[1:-1])+1
                    if int(str(N)[-1]) < int(first):
                        temp -= 1
            else:
                temp += 10**(k-2)
        ans += temp
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop


#read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

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

'''
while(True):
    Nstr = str(N)
    l = len(Nstr) - 1

    if Nstr[0] == Nstr[l]:
        break

    N -= 1
'''

# print(N)


for i in range(N+1):
    s = str(i)
    l = len(s) - 1

    if s[l] == '0':
        continue
    A[int(s[0])][int(s[l])] += 1


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

# print(A)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

if N < 10:
    print(N)
else:
    count = 9
    import numpy as np
    counter = np.zeros((9,9), dtype = int)
    for i in range(11, N+1):
        if i % 10 == 0:
            pass
        else:
            s = list(str(i))
            i1 = int(s[0]) - 1
            i2 = int(s[-1]) - 1
            counter[i1][i2] += 1
    #print(counter)
    for i in range(9):
        count += counter[i][i] ** 2 + counter[i][i] * 2
    #print(count)
    for i in range(8):
        for j in range(i+1, 9):
            #print(i,j,min(counter[i][j], counter[j][i]))
            count += counter[i][j] * counter[j][i] * 2
    print(count)

Submission status: Accepted

Solution code:
import sys
from math import log10, floor, ceil
from itertools import combinations, product
n = int(input())
n_start = int(str(n)[0])
n_last = int(str(n)[-1])
n_keta = floor(log10(n)) + 1
dic = {i:0 for i in product(range(10), repeat=2)}
#memo_ls[i][j][k] : i番目まででjスタートでflgがkの時の通り数
memo_ls = [[[0 for k in range(2)] for j in range(10)] for i in range(n_keta)]
if n_keta == 1:
    print(n_start)
else:
    for i in range(n_start + 1):
        if i < n_start:
            memo_ls[0][i][1] = 1
        else:
            memo_ls[0][i][0] = 1
    for i, x in enumerate(str(n)[1:-1], 1):
        x = int(x)
        for j in range(10):
            for k in range(10):
                if j < x:
                    memo_ls[i][k][1] += memo_ls[i-1][k][0] + memo_ls[i-1][k][1]
                elif j == x:
                    memo_ls[i][k][0] = memo_ls[i-1][k][0]
                    memo_ls[i][k][1] += memo_ls[i-1][k][1]
                else:
                    memo_ls[i][k][1] += memo_ls[i-1][k][1]
            memo_ls[i][j][1] += 1
    score = 0
    for i in range(10):
        for j in range(10):
            if j <= n_last:
                score = sum(memo_ls[-2][i])
            else:
                score = memo_ls[-2][i][1]
            dic[(i, j)] = score
    for i in range(10):
        dic[(i, i)] += 1
        dic[(0, i)] = 0
    ans = 0
    for i in product(range(10), repeat=2):
        ans += dic[i] * dic[(i[1], i[0])]
    print(ans)

Submission status: Accepted

Solution code:
def add_num(n):
    str_n = str(n)
    lower = int(str_n[-1])
    upper = int(str_n[0])
    digit_num = len(str_n)
    ret_val = 0
    if lower == 0:
        return 0
    if digit_num == 1:
        return 1
    elif digit_num == 2:
        if upper < lower:
            return 0
        if upper == lower:
            ret_val += 1
        for d in range(digit_num-1):
            add_val = (10**d) * 2
            ret_val += add_val
        return ret_val

    if upper <= lower:
        for d in range(digit_num-2):
            add_val = (10**d) * 2
            ret_val += add_val
        if upper == lower:
            ret_val += 1
    else:
        for d in range(digit_num-1):
            add_val = (10**d) * 2
            ret_val += add_val
    if upper == lower:
        ret_val += 2
        # print(str_n[1:-1])
        ret_val += int(str_n[1:-1])*2
    return ret_val


def main():
    N = int(input())
    ans = 0
    for n in range(1, N+1):
        # print(n, add_num(n))
        ans += add_num(n)
    print(ans)


if __name__ == "__main__":
    main()
    add_num(11)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

r = dict()
for i in range(1, N + 1):
    ii = str(i)
    a = ii[0]
    b = ii[-1]
    if b == '0':
        continue
    a = int(a)
    b = int(b)
    if (a, b) not in r:
        r[(a, b)] = 0
    r[(a, b)] += 1

result = 0
for i in range(1, 10):
    for j in range(1, 10):
        if (i, j) not in r:
            continue
        if (j, i) not in r:
            continue
        result += r[(i, j)] * r[(j, i)]
print(result)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
Nketa = len(str(N))

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


for p in range(1,N+1):
    mae = int(str(p)[0])
    ato = int(str(p)[-1])
    range_count[mae][ato] += 1

    

count = 0
for j in range(1,N+1):
    a_mae = int(str(j)[0])
    a_ato = int(str(j)[-1])
    
    b_mae = a_ato
    b_ato = a_mae
    
    if b_mae == 0:
        continue
    else:
        count += range_count[b_mae][b_ato]
print(count)

Submission status: Accepted

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

for i in range(1,n+1):
    if str(i)[-1] != "0":
        m[((int(str(i)[0])) * 10)+(int(str(i)[-1]))] += 1

res = sum([m[i]*m[int(str(i)[::-1])] for i in range(100)])

print(res)

Submission status: Accepted

Solution code:
n = int(input())
cnt=0
for i in range(n):
  a = i+1
  k = str(a)
  l = len(k)
  k1, k2 = int(k[0]), int(k[l-1])#first and last
  flag=0
  s = 10
  if k2==k1:
    cnt+=1
  if k2!=0:
    while(flag==0):
      if k2*s+k1<=n:
        if (k2+1)*s+k1<n:
          cnt+=(s//10)
        else:
          cnt+=((n-(k2*s+k1))//10+1)
        s*=10
      else:
        flag=1
print(cnt)

Submission status: Accepted

Solution code:

class Num:
    def __init__(self, h, t, d):
        self.head = h
        self.tail = t
        self.digit = d
        self.min = self.get_min()
        self.max = self.get_max()

    def get_min(self):
        if self.digit == 1:
            if self.head == self.tail:
                return self.head
            else:
                return 0
        else:
            return self.head * (10 ** (self.digit-1)) + self.tail

    def get_max(self):
        if self.digit == 1:
            if self.head == self.tail:
                return self.head
            else:
                return 0
        if self.digit == 2:
            return self.get_min()
        else:
            return self.get_min() + (10 ** (self.digit-1) - 10)


def solve(a, b, n):
    if a.min * a.max * b.min * b.max == 0:
        return 0

    if a.min > n:
        return 0

    if b.min > n:
        return 0

    if a.max > n:
        if a.digit >= 3:
            if str(a.max)[-1] > str(n)[-1]:
                a.max = int(str(a.max)[0] + str(n-10)[1:-1] + str(a.max)[-1])
            else:
                a.max = int(str(a.max)[0] + str(n)[1:-1] + str(a.max)[-1])

    if b.max > n:
        if b.digit >= 3:
            if str(b.max)[-1] > str(n)[-1]:
                b.max = int(str(b.max)[0] + str(n-10)[1:-1] + str(b.max)[-1])
            else:
                b.max = int(str(b.max)[0] + str(n)[1:-1] + str(b.max)[-1])

    total_a = (a.max - a.min) // 10 + 1
    total_b = (b.max - b.min) // 10 + 1
    count = total_a * total_b

    # print('------')
    # print(a.min, a.max)
    # print(b.min, b.max)
    # print(count)

    return count


def main():
    n = int(input())
    digit_n = len(str(n))

    total = 0
    for a_head in range(1, 10):
        for a_tail in range(1, 10):

            b_head = a_tail
            b_tail = a_head

            for digit_a in range(1, digit_n+1):
                for digit_b in range(1, digit_n+1):
                    a = Num(a_head, a_tail, digit_a)
                    b = Num(b_head, b_tail, digit_b)
                    count = solve(a, b, n)
                    total += count

    print(total)


if __name__ == '__main__':
    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 = str(i)
    l[int(a[:1])][int(a[-1:])] += 1

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

Submission status: Accepted

Solution code:
N = input()
L = len(N)
if L == 1:
    print(int(N))
    exit()

def count(c,d):
    if c == int(N[0]):
        if L == 2:
            if d > int(N[-1]):
                n = 0
            else:
                n = 1
        else:
            n = int(N[1:-1]) + 1
            n += sum(10**i for i in range(L-2))
            if d > int(N[-1]):
                n -= 1
    elif c < int(N[0]):
        n = sum(10**i for i in range(L-1))
    else: # c > N[0]
        if L == 2:
            n = 0
        else:
            n = sum(10**i for i in range(L-2))
    if c == d:
        n += 1
    return n

ans = 0
for c in range(1,10):
    for d in range(1,10):
        nA = count(c,d)
        nB = count(d,c)
        # if nA * nB > 0:
        #     print(c,d,nA,nB)
        ans += nA * nB
print(ans)

Submission status: Accepted

Solution code:
import math
n=int(input())
nn=str(n)
# c=int(math.log10(n)//1)+1

a=list(range(1,n+1))


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


# for i in range(1,10):
#     for j in range(1,10):
#         box=0
#         for h in a:
#             if str(h)[0]==str(i) and str(h)[-1]==str(j):
# #                 box.append(h)
#                 box+=1
#         b[i][j]=box
        
        
        
for h in a:
    x=int(str(h)[0])
#     print(x)
    y=int(str(h)[-1])
    b[x][y][0]+=1



# b=[[[] for _ in range(10)] for __ in range(9)]

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

Submission status: Accepted

Solution code:
N = int(input())

cnt = 0
for i in range(1, N+1):
    first = str(i)[0]
    last = str(i)[-1]
    if first == last:
        cnt += 1
    if last != '0':
        if len(str(N)) == 2:
            if int(last+first) <= N:
                cnt += 1
        if len(str(N)) >= 3:
            for j in range(len(str(N))-2):
                cnt += 10**j
            middle = str(N)[1:-1]
            if last < str(N)[0]:
                cnt += 10**(len(str(N))-2)
            elif last == str(N)[0]:
                if first > str(N)[-1]:
                    cnt += int(middle)
                else:
                    cnt += int(middle) + 1

print(cnt)

Submission status: Accepted

Solution code:
N = input()
i_N = int(N)
s_N = str(N)

lis = [[] for i in range(9)]
for i in range(9):
  lis[i] = [[] for i in range(9)]

for i in range(1,i_N+1):
  row = int(str(i)[0])-1
  column = int(str(i)[-1])-1
  if not row == -1 and not column == -1: 
    lis[row][column].append(1)

times = 0
for i in range(9):
  for j in range(9):
    if i != j:
      times += len(lis[i][j]) * len(lis[j][i])
    else:
      times += len(lis[i][i]) **2
print(times)

Submission status: Accepted

Solution code:
n = int(input())

group = [[[] for _ in range(10)] for _ in range(10)]

for n_i in range(1, n+1):
  str_n = str(n_i)
  for i in range(1, 10):
    if int(str_n[0]) == i:
      for j in range(1, 10):
        if int(str_n[-1]) == j:
          group[i][j].append(n_i)

ans = 0
for n_i in range(1, n+1):
  str_n = str(n_i)
  f_n = int(str_n[0])
  l_n = int(str_n[-1])
  ans += len(group[l_n][f_n])
      
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

ans=0
_dict={}

for i in range(1,10):
    for g in range(1,10):
        _dict[str(i)+str(g)]=0

for i in range(1,min(10,n+1)):
    _dict[str(i)*2]+=1

for g in range(10,n+1):
    if g%10!=0:
        _dict[str(g)[0]+str(g)[-1]]+=1

for i,m in _dict.items():
    ans+=m*_dict[i[::-1]]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

X = int(str(N)[0])
Y = int(str(N)[-1])
L = len(str(N))
if L==1:
    print(N)
else:
    M = N//10 - X*(10**(L-2)) + 1
    D = [[0]*10 for i in range(10)]
    for i in range(1,10):
        for j in range(1,10):
            if i==j:
                D[i][j]+=1
            for k in range(2,L):
                D[i][j]+=10**(k-2)
            if i<X:
                D[i][j]+=10**(L-2)
            elif i==X:
                if j<=Y:
                    D[i][j]+=M
                else:
                    D[i][j]+=M-1
    ans=0
    for i in range(1,10):
        for j in range(1,10):    
            ans+=D[i][j]*D[j][i]
    #print(D[1][2])
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())

A=[0]*(100)
B = [0]*(100)
M=10

A=[[0 for j in range(M)]for i in range(M)]
B = [[0 for j in range(M)]for i in range(M)]

for n in range(1,N+1):
    if n>N:
        continue
    sn=list(map(int,list(str(n))))
    #tmp=str(sn[0])+str(sn[-1])
    if sn[0]==0 or sn[-1]==0:
        continue
    
    A[sn[0]][sn[-1]]+=1    
    #B[sn[-1]][sn[0]] += 1


"""
     tmp =10*sn[0]+sn[-1]
    A[tmp]+=1
    tmp = 10*sn[-1]+sn[0]
    B[tmp] += 1 
"""

res=0
for n in range(1,9+1):
    if n > N:
        continue

    #res+=1


res=0

for top in range(1,10):
    for tail in range(1,10):


        tmp = (A[top][tail]*A[tail][top])
        if tmp>0:
            #print(top, tail, tmp, A[top][tail])
            res+=(A[top][tail]*A[tail][top])
            #res+=(A[10*top+tail]*B[10*tail+top])


print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
dict_x = {}
for i in range(1, n + 1):
    a = i % 10
    if a == 0:
        continue
    b = (i // 10**(len(str(i))-1) ) % 10
    if str(a) + "_" + str(b) in dict_x:
        dict_x[str(a) + "_" + str(b)] += 1
    else:
        dict_x[str(a) + "_" + str(b)] = 1
count = 0
for key in dict_x:
    a, b = key.split("_")
    if str(b) + "_" + str(a) in dict_x:
        t1 = dict_x[key]
        t2 = dict_x[str(b) + "_" + str(a)]
        count += t1 * t2
print(count)

Submission status: Accepted

Solution code:
a = [[0 for i in range(9)] for j in range(9)]
for i in range(1,int(input())+1):
  i = str(i)
  if i[-1] != "0":
   	a[int(i[0])-1][int(i[-1])-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    if i != j:
      ans += a[i][j]*a[j][i]
    else:
      ans += a[i][j] ** 2
print(ans)

Submission status: Accepted

Solution code:
import math
import numpy as np


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

    M = [N // 10 ** i for i in range(5 + 1)]

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

    for i in range(1, 10):
        for j in range(1, 10):

            for k in range(6 + 1):
                if k == 0:  # 1
                    if i == j and N >= i:
                        mat[i][j] += 1

                else:  # 1x2 max=192 min=102
                    max_ = (i + 1) * 10 ** k - 10 + j
                    min_ = i * 10 ** k + j

                    if N >= max_:
                        mat[i][j] += 10 ** (k - 1)
                        continue
                    elif N < min_:
                        continue
                    else:
                        mat[i][j] += (N - min_) // 10 + 1
                        continue

    # print(np.array(mat))

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
for i in range(1,n+1):
    if str(i)[-1] == "0":
        pass
    elif i < 10:
        ans += 1
    elif i <= 100:
        if str(i)[0] == str(i)[-1]:
            ans += 3
        elif int(str(i)[0]) > int(str(i)[1]):
            ans += 2
    elif i <= 1000:
        if str(i)[0] == str(i)[-1]:
            ans += (int(str(i)[1])+3)*2-1
        elif int(str(i)[0]) > int(str(i)[-1]):
            ans += 22
        elif int(str(i)[0]) < int(str(i)[-1]):
            ans += 2
    elif i <= 10000:
        if str(i)[0] == str(i)[-1]:
            ans += (int(str(i)[1:3]) + 13)*2-1
        elif int(str(i)[0]) > int(str(i)[-1]):
            ans += 222
        elif int(str(i)[0]) < int(str(i)[-1]):
            ans += 22
    elif i <= 100000:
        if str(i)[0] == str(i)[-1]:
            ans += (int(str(i)[1:4]) + 113)*2-1
        elif int(str(i)[0]) > int(str(i)[-1]):
            ans += 2222
        elif int(str(i)[0]) < int(str(i)[-1]):
            ans += 222
    elif i <= 1000000:
        if str(i)[0] == str(i)[-1]:
            ans += (int(str(i)[1:5]) + 1113)*2-1
        elif int(str(i)[0]) > int(str(i)[-1]):
            ans += 22222
        elif int(str(i)[0]) < int(str(i)[-1]):
            ans += 2222
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
n_list =[0 for i in range(101)]
count = 0
ans = 0
for i in range(1, n+1):
    if i >=10:
        n_list[int(str(i)[0])*10+int(str(i)[len(str(i))-1])] += 1
    elif i < 10:
        n_list[int(str(i)[0])] += 1
for i in range(1,10):
    ans += n_list[i]
for i in range(1,10):
    ans += (n_list[i]*n_list[i*11])*2
for i in range(11, 100):
    if int(str(i)[1]) != 0:
        b = int(str(i)[1])*10+int(str(i)[0])
        ans += n_list[i]*n_list[b]
print(ans)

Submission status: Accepted

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

def handstand(a, b):
  cnt_ = 0
  if a == b:
    cnt_ += 1
  k = 0
  while (a+1) * (10 ** (k+1)) + b-10 <= n:
    cnt_ += (10 ** k)
    k += 1
  cnt_ += max(0, (n - (a * (10 ** (k+1)) + b) + 10) // 10)
  return cnt_  

if n < 10:
  print(n)
  exit()
for i in range(1, 10):
  for j in range(1, 10):
    cnt += handstand(i, j) * handstand(j, i)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
m = len(str(n))
ans = 0



for i in range(n + 1):
    A = str(i)
    first = A[0]
    end = A[-1]

    if end == '0':
        continue
    if first == end:
        ans += 1


    if m >= 3:
        for j in range(m - 2):
            ans += 10 ** (j)
    mini = int(end + '0' * (m - 2) + first)
    maxi = int(end + '9' * (m - 2) + first)
    # print(mini, maxi)
    if n < mini:
        continue
    elif mini <= n <= maxi:
        # print(i, mini, maxi)
        if int(str(n)[-1]) >= int(first):
            ans += int(str(n)[1:-1]) + 1
            # print('a')
        else:
            ans += int(str(n)[1:-1])
            # print('a')
    else:
        ans += (10 ** (m - 2))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
d = {}
ans = 0
for n in range(1, N+1):
  l = n%10
  u = n
  while u >= 10:
    u //= 10
  if l > 0:
    if (u,l) in d:
      d[(u,l)] += 1
    else:
      d[(u,l)] = 1
for i in range(1, 10):
  if (i, i) in d:
    a = d[(i, i)]
    ans += a*(a-1)+a
  for j in range(i+1, 10):
    a, b = (0, 0)
    if (i, j) in d:
      a = d[(i, j)]
    if (j, i) in d:
      b = d[(j, i)]
    ans += a*b*2
print(ans)

Submission status: Accepted

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

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

for i in range(1,n+1) :
	hed = i % 10;
	i_str = str(i);
	foot = int(i_str[0]);
	hedfoot[foot][hed] += 1;

ans = 0;
for i in range(1,n+1) :
	hed = i % 10;
	i_str = str(i);
	foot = int(i_str[0]);
	ans += hedfoot[hed][foot];

print(ans);

Submission status: Accepted

Solution code:
N = int(input())

if 0 <= N <= 9:
    print(N)
else:
    ans = 9
    for i in range(11, N+1):
        if i % 10 == 0:
            continue
        if i < 100:
            if int(str(i)[0]) == i % 10:
                ans += 3  # その1桁、1桁の逆パターン、そのもの
            elif (i % 10)*10+(i//10) <= N:
                ans += 1
            continue
        if int(str(i)[0]) == i % 10:
            ans += 5  # その1桁、1桁の逆パターン、その2桁、2桁の逆パターン、そのもの
            ans += ((i - (int(str(i)[0]) *
                          (10 ** (len(str(i)) - 1)))) // 10) * 2
            # print(
            #    i, ((i - (int(str(i)[0]) * (10 ** (len(str(i)) - 1)))) // 10) * 2)
            for j in range(1, len(str(i)) - 2):
                ans += (10 ** j) * 2

        elif int(str(i)[0]) > i % 10:
            for k in range(len(str(i)) - 1):
                ans += (10 ** k) * 2
        else:
            for h in range(len(str(i)) - 2):
                ans += (10**h)*2
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())

arr = [[list() for _ in range(10)] for _ in range(10)]
count = 0

for i in range(1,n+1):
  arr[int(str(i)[0])][int(str(i)[-1])].append(i)

for i in range(1,10):
  for j in range(1,10):
    arr[i][j] = len(arr[i][j])

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