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 in Python (3.4.3) (judge verdict: Wrong Answer)

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

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

def one_str():
    return input()

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

N=one_int()

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

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

dicts = naive_solver2(N)

import itertools as it

from operator import mul
from functools import reduce

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



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

if N<10:
    print(N)
else:
    print(count)