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 Rust (1.15.1) (judge verdict: Accepted)

use std::io::Write;

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn main() {
    let n: usize = {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let s = s.trim_right().to_owned();
        s.parse().unwrap()
    };

    let mut a = vec![vec![0; 10]; 10];
    for i in 1..10 {
        for j in 1..10 {
            a[i][j] = calc(i, j, n);
        }
    }

    // for v in a.iter() {
    //     debugln!("{:?}", v);
    // }

    let mut ans = 0;
    for i in 1..10 {
        for j in 1..10 {
            ans += a[i][j] * a[j][i];
        }
    }
    println!("{}", ans);
}

fn calc(a: usize, b: usize, n: usize) -> usize {
    let str_n = n.to_string();
    let len = str_n.len();
    let chars_n: Vec<char> = str_n.chars().collect();

    if len == 1 {
        if a == b && a <= n {
            return 1;
        } else {
            return 0;
        }
    }

    // 以後len >= 2

    let mut ret = 0;
    for i in 1..len {
        if i == 1 {
            if a == b {
                ret += 1;
            }
        } else {
            ret += 10usize.pow(i as u32 - 2);
        }
    }
    // debugln!("ret1: {}", ret);

    let d = n / 10usize.pow(len as u32 - 1);
    let rest = n - d * 10usize.pow(len as u32 - 1);
    if a < d {
        ret += 10usize.pow(len as u32 - 2);
        // debugln!("ret2: {}", ret);
    } else if a == d {
        ret += count_1_a(b, rest);
        // debugln!("ret3: {}", ret);
    }

    ret
}

// 末尾がa
fn count_1_a(a: usize, n: usize) -> usize {
    let str_n = n.to_string();
    let len = str_n.len();

    // debugln!("a: {}, n: {}", a, n);

    if len == 1 {
        if a <= n {
            return 1;
        } else {
            return 0;
        }
    }

    // 以後len >= 2

    let d = n / 10usize.pow(len as u32 - 1);
    let rest = n - d * 10usize.pow(len as u32 - 1);

    let mut ret = 0;
    ret += d * 10usize.pow(len as u32 - 2);
    ret += count_1_a(a, rest);

    ret
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_count_1_a() {
        assert_eq!(1, count_1_a(1, 10));
        assert_eq!(1, count_1_a(2, 10));
        assert_eq!(2, count_1_a(1, 20));
        assert_eq!(10, count_1_a(1, 100));
    }

    #[test]
    fn test_calc() {
        assert_eq!(1, calc(1, 1, 9));
        assert_eq!(0, calc(1, 2, 9));
        assert_eq!(2, calc(1, 1, 99));
        assert_eq!(1, calc(1, 2, 99));
        assert_eq!(2, calc(1, 1, 20));
        assert_eq!(1, calc(1, 1, 10));
        assert_eq!(1, calc(1, 2, 12));
        assert_eq!(0, calc(1, 2, 11));
        assert_eq!(0, calc(3, 1, 25));
    }
}