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::{stdin, Read, StdinLock};
use std::str::FromStr;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let n = scan.read::<i64>();
    let mut cnt = 0i64;
    for i in 1..n+1 {
        let f = fst(i);
        let l = last(i);
        if l == 0 {continue;}
        if f == l && f <= n {
            cnt += 1;
//            dbg!((i, 1));
        }
        for k in 0.. {
            let c = count(l, f, k, n);
            if c == 0 {
                break;
            } else {
//                dbg!((i, k, c));
                cnt += c;
            }
        }
    }
    println!("{}", cnt);
}

fn count(f: i64, l: i64, k: usize, n: i64) -> i64 {
    let nf = fst(n);
    let nlen = format!("{}", n).len();
    if k + 2 > nlen || (k + 2 == nlen && f > nf) {
        0
    } else if f < nf || k + 2 < nlen {
        10f64.powi(k as i32) as i64
    } else {
        let mut ret = (last(n) >= l) as i64;
        for i in 1..k+1 {
            let p = n / 10f64.powi(i as i32) as i64 % 10;
            ret += p * 10f64.powi(i as i32 - 1) as i64;
        }
        ret
    }
}

fn fst(n: i64) -> i64 {
    format!("{}", n).chars().next().unwrap() as i64 - '0' as i64
}

fn last(n: i64) -> i64 {
    format!("{}", n).chars().last().unwrap() as i64 - '0' as i64
}

// region template
#[allow(dead_code)]
fn iu(i: i64) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> i64 { i as i64 }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion