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

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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


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)

Solution (Python):

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


if __name__ == "__main__":
    solve()

Solution (Python):
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)

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