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 sys
import math
import bisect
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]


# A
def A():
    n, m = LI()
    if n == m:
        print("Yes")
    else:
        print("No")

    return


# B
def B():
    a, b = LI()
    if a <= b:
        ans = str(a) * b
    else:
        ans = str(b) * a
    print(ans)
    return


# C
def C():
    n = I()
    p = LI()
    n_min = n + 1
    ans = 0
    for num in p:
        if num < n_min:
            ans += 1
            n_min = num
    print(ans)
    return


# D
def D():
    n = I()
    cnt = [[0] * 10 for i in range(10)]
    ans = 0
    for i in range(1, n + 1):
        h = str(i)[0]
        t = str(i)[-1]
        cnt[int(h)][int(t)] += 1
    for i in range(1, 10):
        for j in range(1, 10):
            ans += cnt[i][j] * cnt[j][i]
    print(ans)
    return


# E
def E():
    return


# F
def F():
    return


# Solve
if __name__ == "__main__":
    D()

Submission status: Accepted

Solution code:
import collections
import math
import operator as op
from functools import reduce
import numpy as np
import math
import bisect
import heapq


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    N = get_input(int)
    A = np.zeros((10, 10), dtype=np.int32)
    for i in range(1, N+1):
        A[int(str(i)[0]), int(str(i)[-1])] += 1

    # print(A.sum())
    # print(A)
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += A[i, j] * A[j, i]

    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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):
    p = str(i)
    s = int(p[0])
    f = int(p[-1])
    l[s][f] += 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 Counter

n = int(input().strip())

cnt = Counter()
result = 0
for i in range(1, n + 1):
    s = str(i)
    result += cnt[s[-1], s[0]] * 2
    if s[0] == s[-1]:
        result += 1
    cnt[s[0], s[-1]] += 1
print(result)

Submission status: Accepted

Solution code:
N = int(input())

AB = [[[] for _ in range(10)] for _ in range(10)]
for i in range(1, N+1):
    if int(str(i)[0]) != 0 and int(str(i)[-1]) != 0:
        AB[int(str(i)[0])][int(str(i)[-1])].append(i)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

combi=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
    num = str(i)
    sentou=num[0]
    matubi=num[-1]
    combi[int(sentou)][int(matubi)]+=1

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1, N + 1):
    if i % 10 == 0:
        continue
    s = str(i)
    x = int(s[-1])
    y = int(s[0])
    d[x-1][y-1] += 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:
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
li=[[0]*10 for i in range(10)]
for i in range(n):
    tmp=str(i+1)
    li[int(tmp[0])][int(tmp[-1])]+=1
# print(li)
ans=0
for i in range(10):
    for j in range(10):
        ans+=li[i][j]*li[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()

count = 0
d = defaultdict(int)
for i in range(1,10):
    for j in range(1,10):
        count = 0
        for k in range(1,N+1):
            if str(k)[0]==str(j) and str(k)[-1]==str(i):
                count += 1
        d[(i,j)] = count
#print(d)

ans = 0
for i in range(1,N+1):
    ans += d[(int(str(i)[0]),int(str(i)[-1]))]
    #print(i,d[(int(str(i)[0]),int(str(i)[-1]))])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys
d = defaultdict(set)
N=int(input())
for i in range(1,N+1):
    #print(i)
    j=str(i)
    d[(int(j[0]),int(j[-1]))].add(i)

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

Submission status: Accepted

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


N = int(readline())
sN = str(N)

a = {}

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

    t = (f, l)
    if not t in a:
        a[t] = 0
    
    a[t] += 1

count = 0


for k, v in a.items():
    f, l = k
    if f == l:
        count += v * v
    else:
        counter = a.get((l, f), 0)
        count += counter * v


print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
count = defaultdict(int)
for n in range(1, N+1):
    i = n
    while i >= 10:
        i //= 10
    j = n % 10
    if i != 0 and j != 0:
        count[(i, j)] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += count[(i, j)] * count[(j, i)]
print(ans)

Submission status: Accepted

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

for i in range(1, n + 1):
    strI = str(i)
    if strI[-1] == "0":
        continue
    tmp[int(strI[0]) -1 ][int(strI[-1]) - 1] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[[0]*9for i in range(9)]
for i in range(1,n+1):
    if i%10!=0:
        t=str(i)
        s[int(t[0])-1][int(t[-1])-1]+=1
an=0
for i in range(1,10):
    for j in range(1,10):
        an+=s[i-1][j-1]*s[j-1][i-1]
print(an)

Submission status: Accepted

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

ans = 0

table = [[0 for i in range(10)] for j in range(10)]
for n in range(1,N+1):
  strn = str(n)
  table[int(strn[0])][int(strn[-1])] += 1

for i in range(1,10):
  for j in range(1,10):
    ans += table[i][j] * table[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*9=81通りだけ
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:
import math
from collections import deque

s=input()
l=len(s)
n=int(s)
ans=0

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

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

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import product
from collections import defaultdict
N = int(input())
S = defaultdict(int)
for i in range(1, N+1):
    s = str(i)
    S[s[0]+s[-1]] += 1
ans = 0
for s, e in product("123456789", repeat=2):
    ans += S[s+e]*S[e+s]
print(ans)

Submission status: Accepted

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

for i in range(1,n+1):
    p=len(str(i))
    x=i//(10**(p-1))
    y=i%10
    c[x][y]+=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())
#print#(N)

D={}
for i in range(1,N+1):
    I=str(i)
    if (I[-1],I[0]) not in D:
        D[(I[-1],I[0])]=1
    else:
        D[(I[-1],I[0])]+=1
#print(D)
ans=0
for j in range(1,N+1):
    J=str(j)
    if (J[0],J[-1]) in D:
        ans+=D[(J[0],J[-1])]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

L={(i,q):0 for i in range(1,10) for q in range(1,10)}

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

ans=0
for i in range(1,9):
    for q in range(i+1,10):
        ans+=2*L[(i,q)]*L[(q,i)]

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

print(ans)

Submission status: Accepted

Solution code:
a = int(input())

biao = dict()

for i in range(1, a+1):
    i = str(i)
    if i[0] not in biao:
        biao[i[0]] = {i[-1] : 1}
    else:
        if i[-1] not in biao[i[0]]:
            biao[i[0]][i[-1]] = 1
        else:
            biao[i[0]][i[-1]] += 1

count = 0
for i in range(1, 10):
    for j in range(1, 10):
        try:
            i = str(i)
            j = str(j)
            count += biao[i][j] * biao[j][i]
        except:
            pass

print(count)

Submission status: Accepted

Solution code:
import queue
import math
import copy
"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""

N = int(input())
mp = [[0 for j in range(10)]for i in range(10)]
for i in range(1,N+1):
	j = i
	while j >= 10:
		j //= 10
	mp[j][i%10] += 1
ans = 0
for i in range(1,10):
	for j in range(1,10):
		ans += mp[i][j]*mp[j][i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())

A = [str(i) for i in range(1, N + 1)]

C = Counter()
for s in A:
    C[s[0] + s[-1]] += 1

ans = 0
for s in A:
    if s[0] != '0' and s[-1] != '0':
        ans += C[s[-1] + s[0]]

print(ans)

Submission status: Accepted

Solution code:
import collections
def main():

    N = int(input())

    d = collections.defaultdict(int)
    for i in range(1, N+1):
        s = str(i)
        d[(s[0], s[-1])] += 1

    ans = 0
    for a in d:
        b = (a[1], a[0])
        if b in d:
            ans += d[a] * d[b]
    return ans



if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    d = dict()
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            d[(i, j)] = 0
    for i in range(1, n+1):
        ii = str(i)
        start = int(ii[0])
        end = int(ii[-1])
        if start == 0:
            continue
        if end == 0:
            continue
        d[(start, end)] += 1
    for i in range(1, 10):
        for j in range(1, 10):
            if i != j:
                ans += d[(i, j)]*d[(j, i)]
    for i in range(1, 10):
        ans += d[(i, i)]*d[(i, i)]
    print(ans)


main()

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	L = [[0] * 10 for i in range(10)]
	for i in range(1, N + 1):
		A = str(i)
		x = int(A[0])
		y = int(A[-1])
		L[x][y] += 1
	
	ans = 0
	for i in range(1, 10):
		for j in range(1, 10):
			ans += L[i][j] * L[j][i]

	print(ans)


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1, N + 1):
  s = str(i)
  if s[0] == s[-1]:
    same[int(s[0])] += 1
  else:
    diff[int(s[0])][int(s[-1])] += 1

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

print(answer)

Submission status: Accepted

Solution code:
def solve(n):
    cnt_dict = dict()
    for k in range(11, 100):
        if k % 10 != 0:
            cnt_dict[str(k)] = 0
    for i in range(1, n + 1):
        if i % 10 != 0:
            cnt_dict[str(i)[0] + str(i)[-1]] += 1
    res = 0  # 0 is ignored
    for k in range(11, 100):
        if k % 10 != 0:
            res += cnt_dict[str(k)] * cnt_dict[str(k)[1] + str(k)[0]]
    # print(res)
    return res


def main():
    n = int(input())
    res = solve(n)
    print(res)


def test():
    assert solve(25) == 17
    assert solve(1) == 1
    assert solve(100) == 108
    assert solve(2020) == 40812
    assert solve(200000) == 400000008


if __name__ == "__main__":
    test()
    main()

Submission status: Accepted

Solution code:
def main():
    n = input()
    dic = {}
    for i in xrange(1, n + 1):
        s = str(i)
        key = (s[0], s[-1])
        try:
            dic[key] += 1
        except KeyError:
            dic[key] = 1
    ans = 0
    for a, b in dic:
        ans += dic[(a, b)] * dic.get((b, a), 0)
    print ans


main()

Submission status: Accepted

Solution code:
N = int(input())
A = [[0 for i in range(11)] for j in range(11)]
for i in range(1, N+1):
  A[int(str(i)[0])][int(str(i)[-1])] +=1

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

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

Submission status: Accepted

Solution code:
N = int(input())
arr = [[0] * 10 for _ in range(10)]
ans = 0
for i in range(1, N+1):
    s = str(i)
    head = int(s[0])
    tail = int(s[-1])
    if head == tail:
        ans += 1
    ans += arr[tail][head] * 2
    arr[head][tail] += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
data = [0 for i in range(100)]
ans = 0
for now in range(1,n+1):
    s = int(str(now)[0])
    t = int(str(now)[-1])
    q = 10*t + s
    p = 10*s + t
    data[p] += 1
    ans += 2*data[q]
    if s == t:ans -= 1

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

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

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

ans = 0

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

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

for i in range(9):
    for j in range(9):
        if i==j:
            continue
        ans += t[i][j]*t[j][i]

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = [[0 for j in range(10)] for i in range(10)]
for i in range(1,N+1):
    sentou = int(str(i)[0])
    matsubi = int(str(i)[-1])
    L[sentou][matsubi] += 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]*9 for i in range(9)]
for i in range(1,N+1):
    if i%10 == 0:
        continue
    X = str(i)
    A,B = int(X[0])-1,int(X[-1])-1
    ans[A][B]+=1
ret = 0
for i in range(9):
    for j in range(9):
        ret += ans[i][j]*ans[j][i]
print(ret)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()

dic = defaultdict(int)

for i in range(1, N+1):
	dic["{},{}".format(str(i)[0], str(i)[-1])] += 1

# print(dic)

ans = 0
for n in dic.copy():
	s, e = n.split(",")
	if s == e:
		ans += dic[n]**2
	else:
		s_o, e_o = e, s
		ans += dic[n] * dic["{},{}".format(s_o, e_o)]
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#n,m = [int(i) for i in readline().split()]
#d = [int(i) for i in readline().split()]

#文字p,文字q

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

n = int(input())
for i in range(1,n+1):
    q = i%10
    r = i
    while r:
        p = r%10
        r //= 10
    #print(p,q,i)        
    res[p][q] += 1        

ans = 0
for p in range(1,10):
    for q in range(1,10):
        ans += res[p][q]*res[q][p]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
cnt = [[0 for j in range(10)] for i in range(10)]
for i in range(1, N+1):
	s = str(i)
	a,b = int(s[0]), int(s[-1])
	cnt[a][b] += 1

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

Submission status: Accepted

Solution code:
#%%
n = int(input())
a = [[0] * 10 for _ in range(10)]
for i in range(1, n+1):
    tmp = str(i)
    mae = int(tmp[0])
    usiro = int(tmp[-1])

    a[mae][usiro] += 1
    #a[usiro][mae] += 1



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

Submission status: Accepted

Solution code:
n = int(input())

ac = [[0 for i in range(1,10)] for j in range(1, 10)]

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

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

Submission status: Accepted

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

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())

st = [[0]*10 for _ in range(10)]
for n in range(1,N+1):
    if n%10==0:
        continue
    u = n%10
    d = int(str(n)[0])
    st[u][d] += 1

ans = 0
for n in range(1, N+1):
    if n%10==0:
        continue
    u = n%10
    d = int(str(n)[0])
    ans += st[d][u]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

ans = 0

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

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

ans = ans

print(ans)

Submission status: Accepted

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

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def main():
    n = ri()
    dp = [[0 for _ in range(10)] for _ in range(10)]
    ans = 0
    for i in range(1, n+1):
        s = str(i)
        l = int(s[0])
        r = int(s[-1])
        dp[l][r] += 1
    for i in range(1, n+1):
        s = str(i)
        l = int(s[0])
        r = int(s[-1])
        # print(i, l, r, dp[l][r], dp[r][l])
        ans += dp[r][l]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

    N = int(input())
    dic = defaultdict(int)
    for i_ in range(1, N+1):
        i = str(i_)
        dic[(int(i[0]), int(i[-1]))] += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())

class BinomialCoefficient(object):

    def __init__(self, N=510000, MOD=10**9+7):
        self.fac = [1, 1]
        self.finv = [1, 1]
        self.inv = [0, 1]
        self.MOD = MOD
        for i in range(2, N + 1):
            self.fac.append((self.fac[-1] * i) % MOD)
            self.inv.append((-self.inv[MOD % i] * (MOD // i)) % MOD)
            self.finv.append((self.finv[-1] * self.inv[-1]) % MOD)

    def comb(self, n, r):
        if r < 0 or n < r or r < 0:
            return 0
        return self.fac[n] * self.finv[r] * self.finv[n - r] % self.MOD


bc = BinomialCoefficient(N)
d = dict()
for i in range(1, N + 1):
    key = str(i)[0] + str(i)[-1]
    if d.get(key) is None:
        d[key] = 1
    else:
        d[key] += 1

ans = 0
for k, v1 in d.items():
    v2 = d.get(k[1] + k[0])
    if v2 is not None:
        ans += v1 * v2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

# print(ht)


print(ans)

Submission status: Accepted

Solution code:

N = int(input())

memo = [0]*100

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

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

print(cnt)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
ct = 0
for i in range(1, n+1):
    head = i%10
    if head == 0:
        continue
    k = i
    tail = 0
    while k > 0:
        tail = k%10
        k //= 10
    if head == tail:
        if head <= n:
            ct += 1
    # (head)...(tail)
    d = 10
    while d <= n:
        # head*d + tail
        if n >= (head+1)*d:
            ct += d//10
        elif n < head*d:
            pass
        else:
            ct += (n-head*d-tail+10)//10
        d *= 10
print(ct)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from operator import itemgetter
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1,N+1):
    sento = int(str(i)[0])
    ushiro = int(str(i)[-1])
    D[sento][ushiro] += 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())
tb = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1,n+1):
  t = int(str(i)[0])
  b = int(str(i)[-1])
  tb[t][b] += 1
ans = 0
for l in range(10):
  for m in range(10):
    ans += tb[l][m] * tb[m][l]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a = {}
for i in range(1, n + 1):
    k = str(i)
    j = (int(k[0]), int(k[-1]))
    if j in a:
        a[j] += 1
    else:
        a[j] = 1

def g(b):
    if b in a:
        return a[b]
    return 0
ans = 0
for i in range(10):
    for j in range(10):
        ans += g((i, j)) * g((j, i))
print(ans)

Submission status: Accepted

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

for i in range(1, n+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    A[a][b] += 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:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()

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

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

ans = 0
for s in range(10):
    for e in range(10):
        ans += cnts[s][e] * cnts[e][s]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque, defaultdict
import itertools as ite
import sys
import math
from decimal import *

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
memo = [[0 for i in range(10)] for j in range(10)]
for i in range(1, N + 1):
    S1 = i % 10
    S2 = 0
    hoge = i
    while hoge > 0:
        S2 = hoge % 10
        hoge /= 10
    memo[S1][S2] += 1

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

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())

d = defaultdict(int)
for i in range(1, n+1):
    top = int(str(i)[0])
    bot = int(str(i)[-1])
    d[(top, bot)] += 1

ans = 0
for (top, bot), value in d.items():
    if (bot, top) in d:
        ans += value*d[(bot, top)]
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import defaultdict

N = int(read())

counter = defaultdict(int)

for x in map(str,range(1,N+1)):
    head = x[0]
    tail = x[-1]
    counter[(head, tail)] += 1

answer = 0
items = list(counter.items())
for (head, tail), cnt in items:
    answer += cnt * counter[(tail, head)]

print(answer)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N = int(sys.stdin.buffer.readline())

counts = defaultdict(int)

for i in range(1, N + 1):
    p = int(str(i)[0])
    s = int(str(i)[-1])
    counts[p, s] += 1
ans = 0
for (p, s), cnt in list(counts.items()):
    ans += counts[s, p] * cnt


print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    d = defaultdict(lambda : 0)
    for i in range(1,n+1):
        s = str(i)
        l = s[0]
        r = s[-1]
        d[(l,r)] += 1
    ans = 0
    for i in range(1,n+1):
        s = str(i)
        l = s[0]
        r = s[-1]
        ans += d[(r,l)]
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N = int(input())
mem = [[0]*10 for i 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)