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 Scala (2.13.1) (judge verdict: Accepted)

import java.util.Scanner;

object Main extends App {

  def sub(a: Int, b: Int, mx: Int): Int = {
    (1 to 6).map { n =>
      sub2(a, b, mx, n);
    }.sum;
  }

  def sub2(a: Int, b: Int, mx: Int, n: Int): Int = {
    if (n == 1) {
      if (a == b) {
        if (a <= mx) {
          1;
        } else {
          0;
        }
      } else {
        0;
      }
    } else if (n == 2) {
      if (a * 10 + b <= mx) {
        1;
      } else {
        0;
      }
    } else {
      val t1 = (1 until (n-1)).foldLeft(1)((s1, s2) => s1 * 10);
      if (t1 * 10 * (a + 1) <= mx) {
        t1;
      } else {
        val c = (mx / t1 - a * 10) min 9;
        (0 to c).map { i =>
          sub2(a * 10 + i, b, mx, n - 1);
        }.sum;
      }
    }
  }

  val sc = new Scanner(System.in);
  val mx = sc.nextInt();

  val count1 = (1 to 9).map { i =>
    ((i+1) to 9).map { j =>
      sub(i, j, mx) * sub(j, i, mx);
    }.sum;
  }.sum;
  val count2 = (1 to 9).map { i =>
    val s = sub(i, i, mx);
    s * s;
  }.sum;
  val answer = count1 * 2 + count2;
  println(answer);
}