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())
A=[[0 for i in range(10)] for j in range(10)]

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

ans = 0

for _ in range(n):
    i = _ + 1
    if i < 10:
        num[i][i] += 1
    elif i % 10 != 0:
        moji = str(i)
        num[int(moji[0])][int(moji[len(moji)-1])] += 1
    else:
        pass

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

print(ans)

Submission status: Accepted

Solution code:
def ser_c(N):
    rec = [[0]*10 for i in range(10)]
    for l in range(1, N+1):
        k = str(l)
        i = int(k[0])
        j = int(k[-1])
        rec[i][j] += 1 
    return rec
N = int(input())
cnt = 0
rec = ser_c(N)
for i in range(10):
    for j in range(10):
        cnt += rec[i][j]*rec[j][i]
print(cnt)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
N = int(sys.stdin.readline())
cnt = defaultdict(int)
for i in range(1, N+1):
  str_i = str(i)
  cnt[(str_i[0], str_i[-1])] += 1
 
ans = 0
for d1 in range(1, 10):
  for d2 in range(1, 10):
    d1, d2 = str(d1), str(d2)
    ans += cnt[(d1, d2)] * cnt[(d2, d1)]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
list = [[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9,[0]*9]
for i in range(N):
    s = i + 1
    if s%10 != 0:
        S = str(s)
        list[int(S[0])-1][int(S[-1])-1] = list[int(S[0])-1][int(S[-1])-1] + 1
a = 0
for i in range(9):
    for j in range(9):
        a = a + list[i][j]*list[j][i]


print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
C = {}

def firstd(num):
    if num < 10:
        return num
    prev = 0
    while num > 0:
        num = num // 10
        if num == 0:
            return prev
        prev = num
    return prev

assert firstd(123) == 1
assert firstd(321) == 3

for x in range(1, N + 1):
    first = firstd(x)
    last = x % 10
    C[(first,last)] = C.get((first,last), 0) + 1

ans = 0
for x in range(1, N + 1):
    first = firstd(x)
    last = x % 10

    ans += C.get((last,first), 0)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
count = np.zeros((10, 10), dtype=np.int)
for i in range(1, n+1):
  str_i = str(i)
  head = int(str_i[0])
  tail = int(str_i[-1])
  count[head, tail] += 1
print(np.sum(count.T * count))

Submission status: Accepted

Solution code:
import math
from collections import defaultdict

N = int(input())

rr = defaultdict(lambda: 0)
for i in range(1, N + 1):
    x = int(i / (10 ** int(math.log10(i))))
    y = i % 10
    rr[(x, y)] += 1

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r += rr[(u, l)] * rr[(l, u)]
print(r)

Submission status: Accepted

Solution code:
N = int(input())

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

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

for n in range(1, N + 1):
  _last = int(str(n)[0])
  _first = int(str(n)[-1])
  answer += nmap[_first][_last]
  
print(answer)

Submission status: Accepted

Solution code:
from collections import defaultdict

def getHeadTail(n):
    tail = n%10
    while n//10 > 0:
        n //= 10
    return n, tail

def main():
    N = int( input())
    d = defaultdict( int)
    for i in range(1, N+1):
        a, b = getHeadTail(i)
        if a == 0 or b == 0:
            continue
        d[(a,b)] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += d[(i,j)]*d[(j,i)]

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

Submission status: Accepted

Solution code:
strN = input()
N = int(strN)
p = {i: {j: 0 for j in range(1, 10)} for i in range(1, 10)}
for k in range(1, N+1):
    strk = str(k)
    i = int(strk[0])
    j = int(strk[-1])
    if i == 0 or j == 0:
        continue
    p[i][j] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N, = list(map(int,input().split()))
c = [[0 for e in range(10)] for s in range(10)]
for i in range(1, N+1):
    c[int(str(i)[0])][int(str(i)[-1])] += 1
r = 0
for s in range(1, 10):
    for e in range(1, 10):
        r += c[s][e] * c[e][s]
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#input
N = int(input())

#output
from collections import Counter

M1 = []
M2 = []
for i in range(1, N+1):
    m = str(i)
    m1 = m[0] + m[-1]
    m1 = int(m1)
    m2 = m[-1] + m[0]
    m2 = int(m2)
    M1.append(m1)
    M2.append(m2)

M1 = Counter(M1)
M2 = Counter(M2)
M3 = M1 & M2
answer = 0
for m in M3.keys():
    answer += M1[m]*M2[m]

print(answer)

Submission status: Accepted

Solution code:
import math
a=int(input())
ans=0
c=[[0 for i in range(10)] for j in range(10)]
for i in range(1,a+1):
  aa=str(i)
  c[int(aa[0])][int(aa[-1])]+=1
for i in range(10):
  for j in range(10):
    ans+=c[i][j]*c[j][i]
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
v=[0]*100
ans=0
for i in range(1,n+1):
    x=int(str(i)[0])
    y=int(str(i)[-1])
    v[x*10+y]+=1

for i in range(len(v)):
    x=int(str(i)[0])
    y=int(str(i)[-1])
    ans+=v[i]*v[10*y+x]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[[0]*9 for _ in range(9)]
for i in range(1,n+1):
  s=str(i)
  l=int(s[0])
  r=int(s[len(s)-1])
  if l*r!=0:
    a[l-1][r-1]+=1
x=0
for i in range(9):
  for j in range(9):
    x+=a[i][j]*a[j][i]
print(x)

Submission status: Accepted

Solution code:
n = int(input())

dic = {}
for i in range(1, n+1):
    s = str(i)
    i0 = int(s[0])
    i1 = int(s[-1])
    k = (i0, i1)
    if k in dic:
        dic[k] += 1
    else:
        dic[k] = 1

m = 0
for k, n in dic.items():
    i0, i1 = k
    kk = (i1, i0)
    if kk in dic:
        m += n * dic[kk]

print(m)

Submission status: Accepted

Solution code:
N = int(input())
memo = {}

def func(i, j):
    if (i, j) in memo:
        return memo[(i, j)]

    count = 0
    n = 1
    while n <= N:
        str_n = str(n)

        if n >= 10 and str_n[0] != str(j):
            n += 10 ** (len(str_n) - 1)
            continue

        if str_n[-1] == str(i) and str_n[0] == str(j):
            count += 1
            n += 10
            continue

        n += 1
    
    memo[(i,j)] = count
    return count

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

print(ret)

Submission status: Accepted

Solution code:
n = int(input())
l = [[0 for j in range(9)]for i in range(9)]
for i in range(1,n+1):
  if i % 10 != 0:
    s_i = str(i)
    l[int(s_i[-1])-1][int(s_i[0])-1] += 1

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
import numpy as np
l=np.zeros(81).reshape(9,9)
for i in range(1,1+n):
    first=int(str(i)[0])-1
    last=int(str(i)[-1])-1
    if last>=0:
        l[first][last]+=1
ans=0
for i in range(9):
    for j in range(i,9):
        ans+=l[i][j]*l[j][i]
        if i!=j:
            ans+=l[i][j]*l[j][i]
print(int(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
n=int(input())
dp=[[0]*10 for x in [0]*10]
ans=0
for i in range(1,n+1):
    start,end=int(str(i)[0]),int(str(i)[-1])
    dp[start][end]+=1
for i in range(10):
    for j in range(10):
        ans+=dp[i][j]*dp[j][i]
        # if dp[i][j]*dp[j][i] > 0:
            # print(i,j,dp[i][j]*dp[j][i])
print(ans)

Submission status: Accepted

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

def pair(N):
    b=int(N)%10
    a=int(N)
    k=a//10
    while k!=0:
        a=k%10
        k=k//10
    return a,b
N=int(input())
P=np.zeros((10,10),dtype=np.int32)
ans=0
for i in range(1,N+1,1):
    a,b=pair(i)
    P[a][b]+=1
for i in range(1,10,1):
    for j in range(1,10,1):
        ans+=P[i][j]*P[j][i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

num = [[0 for j in range(10)]for i in range(10)]
for i in range(1,N+1):
    NN = i
    kouho = []
    while NN>=1:
        kouho.append(int(NN%10))
        NN = NN/10
    if len(kouho) == 0:
        continue
    
    low_bit = int(kouho[0])
    high_bit = int(kouho[-1])
    
    if (not low_bit or not high_bit):
        continue
    num[low_bit][high_bit] += 1
ans = 0


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

Submission status: Accepted

Solution code:
n=int(input())
dp=[[0 for i in range(10)] for i in range(10)]
def iandj(n):
  j=n%10
  while n>=10:
    n//=10
  i=n
  return i,j
for k in range(1,n+1):
  i,j=iandj(k)
  dp[i][j]+=1
ans=0
for i in range(1,10):
  for j in range(1,10):
  	ans += dp[i][j]*dp[j][i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
count = 0
baf = 0

if N <= 10:
    for i in range(1, N+1):
        for j in range(1, N+1):
            stri = str(i)
            strj = str(j)
            if stri[0] == strj[-1] and strj[0] == stri[-1]:
                #print(i, j)
                count += 1
    print(count)
else:
    num = (N // 10) ** 2
    if N % 10 == 0:

        print(num + 8)
    else:
        #print(num + 8)
        #print(num)
        for i in range(N // 10 * 10 ,N+1):
            for j in range(1, N+1):
                stri = str(i)
                strj = str(j)
                if stri[0] == strj[-1] and strj[0] == stri[-1]:
                    #print(i, j)
                    count += 1
                    #print(count)
        count *= 2
        for i in range(N // 10 * 10 + 1, N+1):
            for j in range(N // 10 * 10 + 1, N+1):
                stri = str(i)
                strj = str(j)
                if stri[0] == strj[-1] and strj[0] == stri[-1]:
                    #print(i, j)
                    count -= 1
                    #print(count)
        print(num+count + 8)

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
 
cnt = 0
for _ in range(1,N+1):
  a = int(str(_)[0])
  b = int(str(_)[-1])
  cnt += A[b][a]
  
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

memory = dict()
result = 0

for i in range(10):
    for j in range(10):
        memory[(i,j,)] = 0

for i in range(1, N + 1):
    if i >= 100000:
        memory[(i//100000, i%10,)] += 1
    elif i >= 10000:
        memory[(i//10000, i%10,)] += 1
    elif i >= 1000:
        memory[(i//1000, i%10,)] += 1
    elif i >= 100:
        memory[(i//100, i%10,)] += 1
    elif i >= 10:
        memory[(i//10, i%10,)] += 1
    else:
        memory[(i, i,)] += 1

for i in range(10):
    for j in range(10):
        result += memory[(i,j,)] * memory[(j,i,)]

print(result)

Submission status: Accepted

Solution code:
N = int(input())

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

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

# print(pairs)
print(count)

Submission status: Accepted

Solution code:

n = int(input())

match = [[0 for i in range(10)] for i in range(10)]

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#グローバル変数
ab = []
def solve0(n):
    ok = 0
    for a in range(1,n+1):
        for b in range(1,n+1):
            if str(a)[0] == str(b)[-1] and str(a)[-1] == str(b)[0]:
                ok += 1
                ab.append([a,b])
    return ok

def solve2(n,ab):
    ok = 0
    ok_a = 0
    ok_b = 0
    for a,b in ab:
        ok_a = count(n,a)
        ok_b = count(n,b)
        ok = ok + ok_a * ok_b
    return ok

def count(n,z):
    strN = str(n)
    strZ = str(z)
    if z > n:       # z の方が大きい場合は 0
        return 0
    if len(strN) == 1: #　n　の桁が１桁の場合
        return 1
    if len(strZ) == 1: #　z　の桁が１桁の場合
        return 1

    #　n と z　の桁が2桁以上の場合
    ok = 0
    mlen = 0
    while mlen+2 < len(strN): # z の桁が n より小さい
        ok = ok + 10**mlen
        mlen = mlen + 1
    # n と z　の桁が同じ
    if int(strZ[0]+"9"*mlen+strZ[-1]) <= n:
        ok = ok + 10**mlen
    elif int(strZ[0]+strN[1:len(strN)-1]+strZ[-1]) <= n:
        ok = ok + int(strN[1:len(strN)-1])+1
    else:
        nMid = str(strN[1:len(strN)-1])
        if int(nMid) > 0:
            nMid = str(int(nMid)-1).zfill(len(strN)-2)
            if int(strZ[0]+nMid+strZ[-1]) <= n:
                ok = ok + (int(nMid))+1
    return ok

# 配列 ab の準備
solve0(99)

# 整数の入力
n = int(input())
print(solve2(n,ab))

Submission status: Accepted

Solution code:
#グローバル変数
ab = []
nTop = 0
nBtm = 0
nMid = 0
mid = []

def solve0(n):
    ok = 0
    for a in range(1,n+1):
        for b in range(1,n+1):
            if str(a)[0] == str(b)[-1] and str(a)[-1] == str(b)[0]:
                ok += 1
                ab.append([a,b])
    return ok

def solve1(n):
    ok = 0
    for a in range(1,n+1):
        for b in range(1,n+1):
            if str(a)[0] == str(b)[-1] and str(a)[-1] == str(b)[0]:
                ok += 1
    return ok

def solve2(n,ab):
    ok = 0
    ok_a = 0
    ok_b = 0
    for a,b in ab:
        ok_a = count(n,a)
        ok_b = count(n,b)
        ok = ok + ok_a * ok_b
    return ok

def count(n,z):
    strN = str(n)
    strZ = str(z)
    if z > n:       # z の方が大きい場合は 0
        return 0
    if len(strN) == 1: #　n　の桁が１桁の場合
        return 1
    if len(strZ) == 1: #　z　の桁が１桁の場合
        return 1

    #　n と z　の桁が2桁以上の場合
    ok = 0
    mlen = 0
    while mlen+2 < len(strN): # z の桁が n より小さい
        ok = ok + 10**mlen
        mlen = mlen + 1
    # n と z　の桁が同じ
    if int(strZ[0]+"9"*mlen+strZ[-1]) <= n:
        ok = ok + 10**mlen
#       print("routeA",n,int(strZ[0]+"9"*mlen+strZ[-1]))
    elif int(strZ[0]+strN[1:len(strN)-1]+strZ[-1]) <= n:
        ok = ok + int(strN[1:len(strN)-1])+1
#        print("routeB",n,int(strZ[0]+strN[1:len(strN)-1]+strZ[-1]))
    else:
        nMid = str(strN[1:len(strN)-1])
        if int(nMid) > 0:
            nMid = str(int(nMid)-1).zfill(len(strN)-2)
            if int(strZ[0]+nMid+strZ[-1]) <= n:
                ok = ok + (int(nMid))+1
#            print("routeC",n,int(strZ[0]+str(nMid-1)+strZ[-1]))
    return ok

# 配列 ab の準備
solve0(99)

# 整数の入力
n = int(input())
print(solve2(n,ab))

Submission status: Accepted

Solution code:
n=int(input())

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())

base_N = 0
if(N > 10):
	base_N  = int(str(N)[0])*(10**(len(str(N))-1))+int(str(N)[-1])

dig_N = len(str(N))

ans = 0
for x in range(1,10):
	for y in range(1,10):
		for dig_A in range(dig_N):
			for dig_B in range(dig_N):
				check = True
				ways_A, ways_B = 0, 0
				deduct = 0
				if(dig_A == 0 and x<=N):
					if(x==y): 
						ways_A = 1
					else: check = False
				elif(dig_A == 1 and x*10+y<=N):
					ways_A = 1
				elif((x+1)*10**dig_A <= N):
					ways_A = 10**(dig_A-1)
				elif(x*(10**dig_A) <= N):
					if(x*(10**dig_A)+y <= N):
						if(y > N%10): ways_A = (N - base_N)//10
						else : ways_A = (N - base_N)//10 + 1
					else : check = False
				else:
					ways_A = 0
					check = False

				if(dig_B == 0 and y<=N):
					if(x==y): ways_B = 1
					else: check = False
				elif(dig_B == 1 and y*10+x<=N):
					ways_B = 1
					if(dig_A!=dig_B) : ways_B
				elif((y+1)*10**dig_B <= N):
					ways_B = 10**(dig_B-1)
				elif(y*(10**dig_B) <= N):
					if(y*(10**dig_B)+x <= N):
						if(x > N%10): ways_B = (N - base_N)//10
						else: ways_B = (N - base_N)//10 + 1
					else : check = False
				else:
					ways_B = 0
					check = False
				tmp = ans
				ans += ways_B * ways_A - deduct

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
import itertools
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())

m = {
    0: "",
    1: "0",
    2: "00",
    3: "000",
    4: "0000",
}
m2 = {
    0: "",
    1: "9",
    2: "99",
    3: "999",
    4: "9999",
}
def f(t):
    t = str(t)
    c = 0
    if len(t) > 1:
        if t[0] == t[-1]:
            c += 1

    if len(t) == 1:
        t = t * 2
        c += 1

    for i in range(0, 5):
        p = t[-1] + m[i] + t[0]
        p2 = t[-1] + m2[i] + t[0]

        if str(int(p)) == p and int(p) <= N:
            c += 1
            if len(p) > 2:
                n = 10 ** (len(p) - 2) - 1
                if N < int(p2):
                    n = (N - int(p)) // 10
                c += n

    return c

ans = 0
for i in range(1, N+1):
    ans += f(i)

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()

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

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

    l[int(a)][int(b)]+=1

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

  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
n = int(input())

array = [0] * 100

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


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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
cnt=0
N=int(input())
S=np.zeros((10,10),dtype=int)
#print(S)
for i in range(1,N+1):
  T=str(i)
  A=int(T[0])
  B=int(T[-1])
  S[A][B]+=1
#print(S)
for i in range(1,10):
  for j in range(1,10):
    cnt=cnt+S[i][j]*S[j][i]
print(cnt)

Submission status: Accepted

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

def solve(N: int):
    c = collections.defaultdict(int)
    if N == 1:
        print(1)
        return
    for i in range(1, N+1):
        target = str(i)
        c[target[0], target[-1]] += 1
    result = 0
    for i in range(0, 10):
        for j in range(0, 10):
            i = str(i)
            j = str(j)
            result += c[i, j] * c[j, i]
    print(result)
    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:
# -*- coding: utf-8 -*-

N = int(input())

c = {}

for n in range(1, N+1):
    n_list = list(str(n))
    first = n_list[0]
    end = n_list[-1]
    if first + end in c:
        c[first + end] += 1
    else:
        c[first + end] = 1

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok
#

def main():
    N=ii()
    d = defaultdict(int)
    for i in map(str, range(1, N+1)):
        i = str(i)
        a, b = i[0], i[-1]
        d[(a, b)] += 1
    print(sum(d[(a, b)] * d[(b, a)] for a in map(str, range(1,9+1)) for b in map(str, range(1,9+1))))

main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok
#

def main():
    N=ii()
    d = defaultdict(int)
    for i in range(1, N+1):
        i = str(i)
        a, b = i[0], i[-1]
        if a != 0 != b:
            d[(a, b)] += 1
    ans = 0
    for i in range(1, N+1):
        i = str(i)
        a, b = i[0], i[-1]
        ans += d[(b, a)]
    print(ans)


main()

Submission status: Accepted

Solution code:
N=int(input())
d=[[0]*10 for i in range(10)]
#10x10のグリッドを作る

#9x9パターンに分類されるため、各数字をグリッドに振り分ける
for i in range(1,N+1):
  n=str(i)
  d[int(n[0])][int(n[-1])]+=1

#組み合わせの数は[Aの数]x[Bの数] 先頭か語尾に0が付くものはカウントしない
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:
import numpy as np
N = int(input())
a=0
list1 = np.array([0] * N)
list2 = np.array([0] * N)
ans = np.array([[0] * 10 for i in range(10)])
for i in range(N):
    s=str(i+1)
    list1[i]=int(s[-len(s)])
    list2[i]=(i+1)%10

for i in range(N):
    ans[list1[i]][list2[i]]+=1

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

print(a)

Submission status: Accepted

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

Submission status: Accepted

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


def main():
    n = int(input())
    d = dict()
    pairs = list()

    # KeyInsight:
    # 1〜Nまでの整数について、先頭と末尾の数字のペアをカウントする
    # ex: (1, 9), (9, 1)
    # (x, y)に対応する(y, x)の個数を加算する
    for i in range(1, n + 1):
        s = str(i)
        first = s[0]
        second = s[-1]
        pairs.append((first, second))

        if (first, second) not in d.keys():
            d[(first, second)] = 1
        else:
            d[(first, second)] += 1

    ans = 0

    for first, second in pairs:
        if (second, first) in d.keys():
            ans += d[(second, first)]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
D = [[0 for x in range(10)] for y in range(10)]

for x in range(1,N + 1):
    s = int(str(x)[0])
    t = int(str(x)[-1])
    D[s][t] += 1

ans = 0
for x in range(10):
    for y in range(10):
        ans += D[x][y] * D[y][x]
print(ans)

Submission status: Accepted

Solution code:
l=[0]*100;r=range(10)
for i in range(int(input())):
    s=str(i+1);l[int(s[0]+s[-1])]+=1
print(sum([l[i*10+j]*l[j*10+i]for i in r for j in r]))

Submission status: Accepted

Solution code:
l=[0 for _ in range(100)];r=range(10)
for i in range(1,int(input())+1):
    s=str(i);l[int(s[0]+s[-1])]+=1
print(sum([l[i*10+j]*l[j*10+i]for i in r for j in r]))

Submission status: Accepted

Solution code:
n = int(input());l=[0 for _ in range(100)]
for i in range(1,n+1):
    l[int(str(i)[0]+str(i)[-1])]+=1
print(sum([l[i*10+j]*l[j*10+i]for i in range(10) for j in range(10)]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
hash_lst = [0 for _ in range(100)]

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())

    table = [0] * 100
    for i in range(n+1):
        i_str = str(i)
        table[int(i_str[0]) * 10 + int(i_str[-1])] += 1

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

    print(num)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
sample = [0] * 100

for i in range(1,N+1):

    num = str(i)
    first = num[0]
    final = num[-1]
    
    sample[int(first+final)] += 1

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys

sys.setrecursionlimit(10**9)
mod = 998244353
inf = 10**20

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()



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

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


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

Submission status: Accepted

Solution code:
n = int(input())
l = len(str(n))
ns = n//(10**(l-1))
nt = n-ns*10**(l-1)
ans = 0
if l>1:
  ans += (ns-1)*(ns-1)*10**(2*l-4)
  for i in range(2, l):
    ans += 2*(ns-1)*9*10**(l+i-4)
  ans += 2*(ns-1)*10**(l-2)
  if ns<=nt:
    ans += ((nt-ns)//10+1)*((nt-ns)//10+1)
    for i in range(1, ns):
      ans += 2*((nt-i)//10+1)*10**(l-2)
    for i in range(2, l):
      for j in range(1, min(nt+1, 10)):
        ans += 2*((nt-j)//10+1)*10**(i-2)
    ans += 2*((nt-ns)//10+1)
  else:
    ans += nt
    for i in range(2, l):
      ans += 2*nt*10**(i-2)
  for i in range(2, l):
    for j in range(2, l):
      ans += 9*9*10**(i-2+j-2)
    ans += 2*9*10**(i-2)
  ans += 9
else:
  ans += n
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

# 10*10の二次元配列を初期化する
d = [10*[0] for _ in range(10)]
#print(d)

# 1~nまでの数字に対して調べる
for i in range(1, N+1):
    # 数字を文字に変換
    s = str(i)
    # 先頭から末尾に0があったら次へ
    if s[0]=="0" or s[-1]=="0":continue
    # 先頭の数字と末尾の数字の出現回数を1増やす
    d[int(s[0])][int(s[-1])]+=1

ans =0
for i in range(10):
    for j in range(10):
        # 入れ替えた数字の出現回数を掛け合わせると答えになる
        ans += d[i][j]*d[j][i]

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [[0]*10 for _ in range(10)]
ans = 0
for i in range(1, N+1):
    s = str(i)
    A[int(s[0])][int(s[-1])] += 1
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:
n = int(input())
d = {}

for i in range(1, n+1):
  start, end = str(i)[0], str(i)[-1]
  d[(start, end)] = d.get((start, end), 0) + 1
count = 0
for key, n_key in d.items():
  count += n_key * d.get(tuple(reversed(key)), 0)
print(count)