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:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n = int(input())

keta = int(math.log10(n)) + 1

memo = [ [None] * 10 for _ in range(10)]

ans = 0
for a in range(1, n+1):
    x = int(str(a)[0])
    y = a % 10

    if x == 0 or y == 0:
        continue

    if memo[x][y] is not None:
        ans += memo[x][y]
        # print(a, ans)
        continue
    
    patterns = 0
    for b in range(x, n+1, 10):
        x2 = int(str(b)[0])
        y2 = b % 10
        if x == y2 and y == x2:
            patterns += 1
    memo[x][y] = patterns
    ans += patterns
    # print(a, ans)

    # patterns = 0
    # # 1桁
    # if x == y and n <= x:
    #     patterns += 1
    
    # for k in range(keta-1):

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = len(str(n))
ans = 0
cnt = 0
for a in range(1, n + 1):
    s = str(a)
    if s[-1] != '0':
        if d >= 3:
            l, r = 0, n + 1
            while r - l > 1:
                m = (l + r) // 2
                m -= 1
                st = str(m).zfill(d - 2)
                b = int(s[-1] + st + s[0])
                if b <= n:
                    l = m + 1
                else:
                    r = m + 1
            ans += l
            t = 10
            for i in range(d - 3):
                ans += t
                t *= 10
        if int(s[-1] + s[0]) <= n:
            ans += 1
        if s[0] == s[-1] and int(s[0]) <= n:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())

count = 0
d = 1
mat = [[0] * 10 for _ in range(10)]
for a in range(1, n+1):
    # a = x***y
    if a >= 10 ** d: d += 1
    x = a // (10 ** (d - 1))
    y = a % 10
    mat[x][y] += 1

for i in range(10):
    # 対角要素
    count += mat[i][i] ** 2

    # 非対角要素
    for j in range(i+1, 10):
        count += mat[i][j] * mat[j][i] * 2

print(count)

Submission status: Accepted

Solution code:
N=int(input())
Atamasiri = [[0 for i in range(10)] for j in range(10)]#Atamasiri[atama][siri]
cursaijou = 1
for n in range(1,N+1):
    if n>=cursaijou*10:
        cursaijou *= 10
    atama = n//cursaijou
    siri = n%10
    Atamasiri[atama][siri] += 1
ans = 0
for head in range(10):
    for ketsu in range(10):
        ans += Atamasiri[head][ketsu]*Atamasiri[ketsu][head]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

print(ans)

Submission status: Accepted

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


m = defaultdict(int)

for i in range(1, N+1):
    s = str(i)
    if s[-1] != "0":
        m[s[0] + s[-1]] += 1

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


print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    ls = [[0 for _ in range(10)] for _ in range(10)]
    ans = 0
    for i in range(1,N+1):
        b,a = int(str(i)[0]),int(str(i)[-1])

        if a==b:
            ans += 2*ls[a][b]+1
            
        else:
            ans += 2*ls[a][b]

        ls[b][a] += 1

    print(ans)

        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
C = [[0 for i in range(10)] for j in range(10)]
for i in range(1,N+1):
    s = str(i)
    i1,i2 = int(s[0]),int(s[-1])
    C[i1][i2] += 1
ans = 0
for i in range(10):
    for j in range(10):
        if i == 0 or j == 0:
            continue
        ans += C[i][j]*C[j][i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    n = II()
    sento = [[0 for i in range(10)] for i in range(10)]

    vs = [1, 10, 100, 1000, 10000, 100000]
    for i in range(1, n+1):
        m = i % 10
        for v in vs:

            if i // v < 10:

                s = i // v
                break
        # print(i, m, s)
        sento[m][s] += 1

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







if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
L = len(str(N))
A,B=int(str(N)[L-1]),int(str(N)[0])
c = 0
for i in range(1,N+1):
    I = str(i)
    l = len(I)
    a,b = int(I[0]),int(I[l-1])
    if a == 0 or b == 0:
        continue
    else:
        if a == b:
            c += 1
        for m in range(max(L-2,0)):
            c += 10**m
        if L == 1:
            continue
        if b > B:
            continue
        elif b < B:
            c += 10**(L-2)
        elif L > 2 and a <= A:
            c += int(str(N)[1:L-1])+1
        elif L > 2:
            c += int(str(N)[1:L-1])
        elif L == 2:
            c += 1
print(c)

Submission status: Accepted

Solution code:
N = int(input())

d = {}
d2 = {}
for i in range(1, N + 1):
    s = str(i)
    key = (s[0], s[-1])
    key2 = (s[-1], s[0])
    if key not in d:
        d[key] = 1
    else:
        d[key] += 1

    if key2 not in d2:
        d2[key2] = 1
    else:
        d2[key2] += 1


kosuu = 0
for key in d:
    val = d[key]
    val2 = d.get((key[1], key[0]))
    if val2 is not None:
        kosuu += val * val2

print(kosuu)

Submission status: Accepted

Solution code:
n = int(input())
grid = [[0 for i in range(9)] for j in range(9)]
ans = 0

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

for i in range(9):
    for j in range(i, 9):
        if i == j:
            ans += grid[i][j] * grid[j][i]
        else:
            ans += (grid[i][j] * grid[j][i])*2

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

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

Submission status: Accepted

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


def solve(N: int):
    x = [0] * 100

    for i in range(1, N+1):
        a = i%10
        b = int(str(i)[0])
        x[a + b*10] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1,10):
            if i == j:
                ans += x[i*11] ** 2
                continue
            ans += x[i*10 +j] * x[j*10 + i]
    print(ans)
    return



# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
cnts = [[0] * 10 for _ in range(10)]
ans = 0
for i in range(N + 1):
    if i % 10 == 0:
        continue
    s = str(i)
    cnts[int(s[0])][int(s[-1])] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1, N + 1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    lst[a][b] += 1

ans = 0
for a in range(1, 10):
    for b in range(1, 10):
        ans += lst[a][b] * lst[b][a]

print (ans)

Submission status: Accepted

Solution code:
N = int(input())

FN = [0] * 100

# 最初の数字*10 + 最後の数字のLUTを作成
for i in range(1, N+1):
  first_num = int(str(i)[0])
  last_num = i % 10
  FN[first_num*10 + last_num] += 1

#print(FN)
cnt = 0
for i, fn in enumerate(FN):
  if i < 11:
    continue
  rev = (i // 10) + (i % 10) * 10
  cnt += FN[rev] * fn
  #print(i, fn, FN[rev], cnt)

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

def calc(n):
	counter = [[0 for _ in range(10)] for _ in range(10)]

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

	cnt = 0
	for i in range(1, 10):
		for j in range(1, 10):
			if i == j:
				if counter[i][j] >= 1:
					cnt += counter[i][j] ** 2
			else:
				cnt += counter[i][j] * counter[j][i]
	return cnt

print(calc(n))

Submission status: Accepted

Solution code:
N = int(input())
lst = [[0 for i in range(10)]for j in range(10)]
ans = 0
for i in range(1,N+1):
    tmp = list(str(i))
    lst[int(tmp[0])][int(tmp[-1])]+=1
for i in range(10):
    for j in range(10):
        if i == 0 or j == 0:
            continue
        if i == j:
            ans+=lst[i][j]**2
        else:
            ans+=lst[i][j]*lst[j][i]
print(ans)

Submission status: Accepted

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


table = collections.defaultdict(list)

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


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


for i in range(1, N+1):
    k = get_initial(i)
    table[k][i % 10] += 1
ans = 0
for i in range(1, 10):
    if i not in table:
        continue
    for j in range(1, 10):
        ans += table[i][j] * table[j][i]

print(ans)

Submission status: Accepted

Solution code:
l = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]]
n = int(input())
c = 0
for i in range(1,n+1):
    li = len(str(i))
    a = i%10
    b = i//(10**(li-1))
    l[a][b] = l[a][b]+1

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

Submission status: Accepted

Solution code:
# coding: utf-8
N=int(input())
L=[[0 for i in range(10)] for j in range(10)]

for i in range(N):
    num=i+1
    s_num=str(num)
    t=int(s_num[0])
    b=int(s_num[-1])
    L[t][b]+=1

ans=0
for i in range(N):
    num=i+1
    s_num=str(num)
    t=int(s_num[0])
    b=int(s_num[-1])
    ans+=L[b][t]
    # print(num,ans,L[t][b],L[b][t])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
digit = len(str(n))

score = 0

for i in range(1,n+1):
    if str(i)[-1] != '0':
        first = str(i)[-1]
        end = str(i)[0]
        if first == end:
            count = 1
        else:
            count = 2
        while count < digit:
            if count <= 2:
                score += 1
            else:
                score += 10**(count - 2)
            count += 1
        if first < str(n)[0]:
            score += 10**(count - 2)
        elif first == str(n)[0]:
            if len(str(n)) == 1:
                score += 1
            elif end > str(n)[-1]:
                score += int(str(n)[1:])//10
            else:
                score += int(str(n)[1:])//10 + 1
print(score)

Submission status: Accepted

Solution code:
import math

n = int(input())
top_bottom_num = {str(i): {str(j): 0 for j in range(10)} for i in range(10)}
for i in range(1, n + 1):
    str_num = str(i)
    top_bottom_num[str_num[0]][str_num[-1]] += 1
ans = 0
for i in range(10):
    for j in range(10):
        i_j = top_bottom_num[str(i)][str(j)]
        j_i = top_bottom_num[str(j)][str(i)]
        ans += i_j * j_i
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
al=[[0 for j in range(9)] for i in range(9)]

ans=0
for i in range(1,n+1):
    l=list(str(i))
    if l[-1]!='0':
        r=l[::-1]
        if l==r:
            ans-=0
        al[int(l[0])-1][int(l[-1])-1]+=1
for i in range(9):
    for j in range(9):
        k=al[i][j]
        if i==j:
            ans+=k*k
        else:
            ans+=al[i][j]*al[j][i]
print(ans)

Submission status: Accepted

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

N = int(input())

ret = 0
for i in range(1, N + 1) :
  l = str(i)[0]
  r = str(i)[-1]
  
  if r == '0' :
    continue
    
  if l == r and int(l) <= N :
    ret += 1
    
  for j in range(0, len(str(N)) - 2) :
    ret += pow(10, j)
  
  if str(N)[0] == r :
    if len(str(N)) >= 2 :
      ret += int(str(N)[1:]) // 10
      if int(str(N)[-1]) >= int(l) :
        ret += 1
  elif str(N)[0] > r :
    ret += pow(10, len(str(N)) - 2)
    
print(ret)

Submission status: Accepted

Solution code:
N=int(input())

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

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

ANS=0
for i in range(10):
    for j in range(10):
        ANS+=BL[i][j]*BL[j][i]

print(ANS)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    cnt=[[0]*10 for _ in range(10)]
    for x in range(1,n+1):
        l=int(str(x)[0])
        r=x%10
        cnt[l][r]+=1
    ans=0
    for l in range(1,10):
        for r in range(1,10):
            ans+=cnt[l][r]*cnt[r][l]
    print(ans)

main()

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
for i in range(1, N+1):
    s = str(i)
    L = len(s)-2
    l = int(s[0])
    r = int(s[-1])
    if r == 0:
        continue
    if l > r:
        ans+=(10**(L+1)-1)//9*2
    elif l<r:
        ans+=(10**L-1)//9*2
    else:
        if L<0:
            ans+=1
        else:
            ans+=(10**L-1)//9*2+2
            ans+=((i-int(s[0]+'0'*L+s[-1]))//10+1)*2-1
print(ans)

Submission status: Accepted

Solution code:
def g(n):
    D = [[0]*10 for i in range(10)]
    for i in range(1,n+1):
        si = str(i)
        D[int(si[0])][int(si[-1])] += 1

    res = 0
    for i in range(10):
        for j in range(10):
            res += D[i][j]*D[j][i]
    return res
print(g(int(input())))

Submission status: Accepted

Solution code:
n=int(input())

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

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

    m[s][e]+=1

ans=0

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

print(ans-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

list_n = [str(i) for i in range(n+1)] 
list_f = [ int(t[0]) for t in list_n]
list_e = [ int(t[-1]) for t in list_n]

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

for i in range(n):
    a[list_f[i+1]][list_e[i+1]] += 1
    b[list_e[i+1]][list_f[i+1]] += 1

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

N = int(input())

def brute(N):
    ans = []
    for a in range(1, N + 1):
        for b in range(1, N + 1):
            a2 = str(a)
            b2 = str(b)
            if a2[0] == b2[-1] and a2[-1] == b2[0]:
                ans.append((a, b))

    return ans

def faster(N):
    arr = defaultdict(list)
    c = Counter()
    for n in range(1, N + 1):
        s = str(n)
        first = s[0]
        last = s[-1]
        arr[first + last].append(n)
        c[first + last] += 1

    ans = 0
    for key in c:
        key2 = key[-1] + key[0]
        ans += c[key] * c[key2]

    for n in range(11, 100, 11):
        s = str(n)
        ans -= len(arr[n]) * len(arr[n])

    return ans

print(faster(N))

# for n in range(1, 1000):
#     b = len(brute(n))
#     f = faster(n)
#     if b != f:
#         print("error!")
#         print(b, f)
#         sys.exit(0)

Submission status: Accepted

Solution code:
N = int(input())

def nasu(N):
  D = []
  t = N
  while t:
    D = [t % 10] + D
    t //= 10
  return len(D), D[0], D[-1]

L, M, P = nasu(N)
Q = (N - (M * (10 ** (L - 1))) - P) // 10
ans = 0
for i in range(1, N + 1):
  if i % 10 == 0: continue
  l, x, y = nasu(i)
  if x == y and x <= N:
    ans += 1
  if y < M:
    for j in range(L - 1):
      ans += 10 ** j
  elif y > M:
    for j in range(L - 2):
      ans += 10 ** j
  else:
    for j in range(L - 2):
      ans += 10 ** j
    t = y * 10 + x
    if t <= N:
      ans += 1
    if L >= 2:
      if x > P:
        ans += Q - 1
      else:
        ans += Q

print(ans)

Submission status: Accepted

Solution code:
def get_val(x):
    left = x
    while True:
        tmp = left // 10
        if tmp == 0:
            break
        left = tmp
    return left, x % 10


N = int(input())

counter = dict()
for x in range(1, N+1):
    key = get_val(x)
    if key not in counter:
        counter[key] = 0
    counter[key] += 1

res = 0
for i in range(1, N+1):
    left, right = get_val(i)
    if right == 0:
        continue
    if (right, left) in counter:
        res += counter.get((right, left))

print(res)

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
for i in range(1,N+1):
    n = str(i)
    if n[-1]=="0":continue
    if n[0]==n[-1]:
        ans+=1
    for j in range(5):
        a = int(n[-1]+"0"*j+n[0])
        b = int(n[-1]+"9"*j+n[0])
        if b<=N:
            ans+=10**j
        elif a<=N and N<b:
            ans+=(N-a)//10+1
        else:break
print(ans)

Submission status: Accepted

Solution code:


s=input()
n=int(s)

d={}

for i in range(1,n+1):
	ts=str(i)
	hs=ts[0]+" "+ts[len(ts)-1]

	if(hs in d):
		d[hs]+=1
	else:
		d[hs]=1



ans=0

for i in range(1,10):
	for j in range(1,10):
		if(((str(i)+" "+str(j)) in d) and ((str(j)+" "+str(i)) in d)):
			ans+=d[(str(i)+" "+str(j))]*d[(str(j)+" "+str(i))]

print(ans)

Submission status: Accepted

Solution code:
def f(n) : 
    while n >= 10:  
        n /= 10 
    return int(n)
n = int(input())
l = list(list(0 for i in range(10)) for j in range(10))
for i in range(1, n+1):
	last = i%10
	if last:
		l[f(i)][last] += 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())

A=[[0 for i in range(9)] for j in range(9)]
keta=1
for i in range(N+1):
  a=i%10
  if i>=keta*10:
    keta=keta*10
  b=i//(keta)
  #print(a,b)
  if a!=0 and b!=0:
    A[a-1][b-1]+=1

x=0
for i in range(9):
  for j in range(9):
    k=A[i][j]*A[j][i]
    #print(i,j,A[i][j],A[j][i])
    x+=k
print(x)

Submission status: Accepted

Solution code:
n = int(input())
R = len(str(n))
def topend(i):
  return [int(str(i)[0]),int(str(i)[len(str(i))-1])]
cnt = [[0]*10 for i in range(10)]

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

Submission status: Accepted

Solution code:
n=int(input())

ans = [[0 for i in range(14)]for j in range(15)]
for i in range(1,10):
    for j in range(1,10):
        for keta in range(1,8):
            if keta == 1:
                if i == j and i <= n:
                    ans[i][j] += 1
            elif keta == 2:
                if i*10 + j <= n:
                    ans[i][j] += 1
            else:
                mid = keta - 2
                nummin = i*10**(keta-1) + j
                if nummin > n:
                    continue
                nummax = nummin + 10**(keta-1) - 10
                if nummax <= n:
                    ans[i][j] += 10**mid
                else:
                    tmp = n - nummin
                    ans[i][j] += tmp//10 + 1

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

Submission status: Accepted

Solution code:
import collections
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
dic = collections.defaultdict(int)
for i in range(1, n + 1):
    s = str(i)
    a = s[0]
    b = s[-1]
    if b != '0':
        dic[(a, b)] += 1
res = 0
for i in range(1, 10):
    for j in range(1, 10):
        i = str(i)
        j = str(j)
        res += dic[(i, j)] * dic[(j, i)]
print(res)

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
for i in range(1,10):
    for j in range(1,10):
        x = str(i) + str(j)
        dic[x] = 0
#print(dic)
for i in range(1,N+1):
    a = i % 10
    for j in range(7):
        b  = i // 10 ** (6 - j)
        if b != 0 :
            break
    z = str(b) + str(a)
    if a != 0 and b != 0:
        dic[z] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        x = str(i) + str(j)
        y = str(j) + str(i)
        ans += dic[x] * dic[y]
print(ans)

Submission status: Accepted

Solution code:
def read():
    N = int(input().strip())
    return N,

def solve(N):
    table = [[0 for i in range(0, 10)] for j in range(0, 10)]
    for i in range(1, N+1):
        strn = str(i)
        head, tail = int(strn[0]), int(strn[-1])
        table[head][tail] += 1
    count = 0
    for i in range(1, N+1):
        strn = str(i)
        head, tail = int(strn[0]), int(strn[-1])
        count += table[tail][head]
    return count


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

Solution code:
n=int(input())
ans=0
for i in range(n):
  I=str(i+1)
  s=I[0];o=I[-1]
  if o=="0":continue
  if o==s:
    ans+=1
  a=(n-int(s))//10
  
  for j in range(6):
    chk=int(o)*(10**j)
    if chk<=a:
      if chk+10**j<=a:ans+=10**j
      else:
        ans+=a-chk+1
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict as dd
input = sys.stdin.readline
N = int(input())

d = dd(int)
res = 0
for i in range(1, N + 1):
  x = int(str(i)[0])
  y = int(str(i)[-1])
  d[x * (10 ** 6) + y] += 1
for i in range(1, N + 1):
  x = int(str(i)[0])
  y = int(str(i)[-1])
  res += d[y * (10 ** 6) + x]
#print(d)
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, N + 1):
    bottom = i % 10
    if bottom == 0:
        continue
    while i >= 10:
        i //= 10
    top = i
    top_bottom_array[top][bottom] += 1

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())

a_list= [[0 for i in range(10)] for j in range(10)]
b_list= [[0 for i in range(10)] for j in range(10)]
for i in range(N+1):
    a=int(str(i)[0])
    b=int(str(i)[-1])
    a_list[a][b]+=1
    b_list[b][a]+=1

count=0
for i in range(1,10):
    for j in range(1,10):
        count+=a_list[i][j]*b_list[i][j]

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
#import numpy as np

mod=int(10**9+7)

#main
N=int(input())
A=[[0 for a in range(11)] for b in range(11)]
for a in range(1,N+1):
    sa=str(a)
    A[int(sa[0])][int(sa[-1])]+=1
ans=0
for b in range(1,10):
    for e in range(1,10):
        ans+=A[b][e]*A[e][b]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))


def cmd(n,r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
 
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
 
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
 
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
 
    return result

n = inp()
d = defaultdict(int)
for i in range(1,n+1):
    if i%10==0:
        continue
    if i<10:
        d[i*10+i] += 1
        continue
    tmp = str(i)
    d[int(tmp[0]+tmp[-1])] += 1
# print(d)
res = 0
fin = set()
for key in list(d):
    # print(key)
    if key%11==0:
        res += d[key]**2
    else:
        tmp = int(str(key)[-1] + str(key)[0])
        if tmp in fin:
            continue
        fin.add(key); fin.add(tmp)
        res += d[key] * d[tmp] * 2
print(res)

Submission status: Accepted

Solution code:
N = int(input())
T= [[0]*9 for i in range(9)]
K= [[0]*9 for i in range(9)]

for i in range(1,N+1):
    S = str(i)
    A = int(S[0])
    B = int(S[-1])
    if A>0 and B>0:
        T[A-1][B-1]+=1

for i in range(9):
    for j in range(9):
        K[i][j] = T[i][j]*T[j][i]

output = 0
for i in range(9):
    output += sum(K[i])
print(output)

Submission status: Accepted

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

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):
    x=int(str(i)[0])
    y=int(str(i)[-1])
    L[x][y]+=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:
from collections import defaultdict

N = int(input())

top_end = defaultdict(int)

ans = 0
for i in range(1, N + 1):
    stri = str(i)
    ans += top_end[(stri[-1], stri[0])] * 2
    top_end[(stri[0], stri[-1])] += 1
    if stri[0] == stri[-1]:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=len(str(n))
dp=[[0]*9 for i in range(9)]
count=0
for i in range(1,n+1):
    x,y=int(str(i)[0]),int(str(i)[-1])
    if x!=0 and y!=0:
        dp[x-1][y-1]+=1
for i in range(9):
    for j in range(9):
        count+=dp[i][j]*dp[j][i]
print(count)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

for i in range(1,N+1):
    k = list(str(i))
    st = int(k[0])
    ls = int(k[-1])
    MAP[st][ls] += 1
ANS = 0
for i in range(1,10):
    k = MAP[i]
    for j in range(1,10):
        m = k[j]
        ANS += m*MAP[j][i]
print(ANS)

Submission status: Accepted

Solution code:
n = int(input())
d = 0
a = [[0 for i in range(10)] for j in range(10)]
for i in range(1,n+1):
	f = i//10**d
	l = i%10
	a[f][l] += 1
	if(i == 10**(d+1)-1):
		d += 1
ans = 0
for i in range(1,10):
	for j in range(1,10):
		ans += a[i][j]*a[j][i]
print(ans)

Submission status: Accepted

Solution code:
import math


def main():
    N = int(input())
    cnt = [[0] * 10 for _ in range(10)]
    for i in range(1, N+1):
        d = int(math.log(i, 10))
        cnt[i//(10**d)][i % 10] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += cnt[i][j] * cnt[j][i]
    print(ans)


if __name__ == "__main__":
    main()