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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
class ProgramD
    {
        static void Main(string[] args)
        {
            
            //入力
            int n = int.Parse(Console.ReadLine());
            

            int count = 1;
            
            for(int i = 2;i <= n;i++)
            {
                if(i % 10 == 0)
                    continue;

                if(i < 10)
                {
                    count++;
                }
                else if(11 <= i && i <= 99)
                {
                    int a1 = i /10;
                    int a2 = i - a1*10;

                    if(a1 == a2)
                        count += 3;
                    else if(a1 > a2)
                        count += 2;
                }
                else if(101 <= i && i <= 999)
                {
                    int b1 = i / 100;
                    int b2 = (i - b1*100)/10;
                    int b3 = i - b2*10 - b1*100;

                    if(b1 == b3)
                        count += 5 + b2*2;
                    else if(b1 > b3)
                        count += 22;
                    else if(b1 < b3)
                        count += 2;
                    
                }
                else if(1001 <= i && i <= 9999)
                {
                    int c1 = i / 1000;
                    int c2 = (i - c1*1000)/100;
                    int c3 = (i - c2*100 - c1*1000)/10;
                    int c4 = i - c3*10 - c2*100 - c1*1000;

                    if(c1 == c4)
                        count += 25 + c2*20 + c3*2;
                    else if(c1 > c4)
                        count += 222;
                    else if(c1 < c4)
                        count += 22;
                    
                }
                else if(10001 <= i && i <= 99999)
                {
                    int d1 = i / 10000;
                    int d2 = (i - d1*10000)/1000;
                    int d3 = (i - d2*1000 - d1*10000)/100;
                    int d4 = (i - d3*100 - d2*1000 - d1*10000)/10;
                    int d5 = i - d4*10 - d3*100 - d2*1000 - d1*10000;

                    if(d1 == d5)
                        count += 225 + d2*200 + d3*20 + d4*2;
                    else if(d1 > d5)
                        count += 2222;
                    else if(d1 < d5)
                        count += 222;
                    
                }
                else if(100001 <= i)
                {
                    int e1 = i / 100000;
                    int e2 = (i - e1*100000)/10000;
                    int e3 = (i - e2*10000 - e1*100000)/1000;
                    int e4 = (i - e3*1000 - e2*10000 - e1*100000)/100;
                    int e5 = (i - e4*100 - e3*1000 - e2*10000 - e1*100000)/10;
                    int e6 = i - e5*10 - e4*100 - e3*1000 - e2*10000 - e1*100000;

                    if(e1 == e6)
                        count += 2225 + e2*2000 + e3*200 + e4*20 + e5*2;
                    else if(e1 > e6)
                        count += 22222;
                    else if(e1 < e6)
                        count += 2222;
                    
                }
                
            }

            Console.WriteLine(count);
          
        } 
    }