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: Wrong Answer)

#[allow(unused_imports)]
use std::char::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::*;
use std::str::FromStr;

#[allow(unused_macros)]
macro_rules! debug {($($a:expr),*) => {println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);}}
#[allow(unused_macros)]
macro_rules! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; let mut next = || { iter . next ( ) . unwrap ( ) } ; input_inner ! { next , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let stdin = std :: io :: stdin ( ) ; let mut bytes = std :: io :: Read :: bytes ( std :: io :: BufReader :: new ( stdin . lock ( ) ) ) ; let mut next = move || -> String { bytes . by_ref ( ) . map ( | r | r . unwrap ( ) as char ) . skip_while ( | c | c . is_whitespace ( ) ) . take_while ( | c |! c . is_whitespace ( ) ) . collect ( ) } ; input_inner ! { next , $ ( $ r ) * } } ; }
#[allow(unused_macros)]
macro_rules! input_inner { ( $ next : expr ) => { } ; ( $ next : expr , ) => { } ; ( $ next : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ next , $ t ) ; input_inner ! { $ next $ ( $ r ) * } } ; }
#[allow(unused_macros)]
macro_rules! read_value { ( $ next : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ next , $ t ) ) ,* ) } ; ( $ next : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ next , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ next : expr , chars ) => { read_value ! ( $ next , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ next : expr , usize1 ) => { read_value ! ( $ next , usize ) - 1 } ; ( $ next : expr , $ t : ty ) => { $ next ( ) . parse ::<$ t > ( ) . expect ( "Parse error" ) } ; }

#[derive(PartialEq, PartialOrd)]
pub struct Total<T>(pub T);
impl<T: PartialEq> Eq for Total<T> {}
impl<T: PartialOrd> Ord for Total<T> { fn cmp(&self, other: &Total<T>) -> std::cmp::Ordering { self.0.partial_cmp(&other.0).unwrap() }}

#[allow(dead_code)]
const MAX:usize = 100006;
#[allow(dead_code)]
const INF:i64 = std::i64::MAX;
#[allow(dead_code)]
const MOD:i64 = 1e9 as i64 + 7;


#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}


#[allow(unused_imports)]
use std::f64;

#[allow(dead_code)]
fn min<T: PartialOrd>(a:T, b:T) -> T {
    if a < b { a }
    else { b }
}
#[allow(dead_code)]
fn max<T: PartialOrd>(a:T, b:T) -> T {
    if a < b { b }
    else { a }
}

use std::hash::Hash;
#[allow(dead_code)]
fn inc<T: Hash+Ord>(map:&mut BTreeMap<T,i64>, key:T) {
    let count = map.entry(key).or_insert(0);
    *count += 1;
}

fn int(ch: char) -> i64 {
    return (ch as u8 - b'0') as i64
}

fn main() {
    input!{n:i64};
    let s = n.to_string().chars().collect::<Vec<char>>();

    let mut ans = 0i64;

    for i in 1..n+1 {
        //debug!(i);
        /*
        let mut cnt_cor = 0i64;
        for j in 1..n+1 {
            let a = i.to_string().chars().collect::<Vec<char>>();
            let b = j.to_string().chars().collect::<Vec<char>>();
            if a[0]==b[b.len()-1] && a[a.len()-1]==b[0] { cnt_cor += 1; }
        }
        */
        let mut cnt = 0i64;
        let a = i.to_string().chars().collect::<Vec<char>>();
        if a[a.len()-1] == '0' { continue; }
        //debug!(a);
        let mut b_mn = vec![a[a.len()-1],a[0]];
        let mut b_mx = vec![a[a.len()-1],a[0]];
        if a[0] == a[a.len()-1] {
            cnt += 1;
        }
        if b_mn.iter().cloned().collect::<String>().parse::<i64>().unwrap() <= n {
            cnt += 1;
        }

        for j in 0..10 {
            b_mn.insert(1,'0');
            b_mx.insert(1,'9');
            let b_mn_v = b_mn.iter().cloned().collect::<String>().parse::<i64>().unwrap();
            let b_mx_v = b_mx.iter().cloned().collect::<String>().parse::<i64>().unwrap();
            //debug!(b_mn_v,b_mx_v);
            if b_mn_v > n {
                break;
            }
            if b_mx_v <= n {
                cnt += 10i64.pow((j+1) as u32);
                //debug!(b_mn_v, b_mx_v);
                //debug!(ans);
            }
            else {
                //debug!(n,b_mn_v,b_mx_v);
                let mut v = vec![];
                for k in 1..s.len()-1 {
                    if int(s[k]) == 0 { continue; }
                    v.push(int(s[k]));
                }
                //debug!(v);

                if b_mn[b_mn.len()-1] >= s[s.len()-1] {
                    let v_len = v.len();
                    v[v_len-1] -= 1;
                    let mut cnt1 = v[0]+1;
                    for _ in 1..v.len() {
                        cnt1 *= 10;
                    }
                    //debug!(cnt1);
                    cnt += cnt1;

                    if v.len() < 2 { continue; }
                    let mut cnt2 = 1;
                    for l in 1..v.len() {
                        cnt2 *= v[l];
                    }
                    //debug!(cnt2);
                    cnt += cnt2;
                } else {
                    let mut cnt1 = v[0]+1;
                    for _ in 1..v.len() {
                        cnt1 *= 10;
                    }
                    cnt += cnt1;

                    if v.len() < 2 { continue; }
                    let mut cnt2 = 1;
                    for l in 1..v.len() {
                        cnt2 *= v[l];
                    }
                    cnt += cnt2;

                }
            }
            //debug!(cnt);
        }
        //debug!(i,cnt,cnt_cor);
        //if cnt != cnt_cor { panic!(); }
        ans += cnt;
    }

    println!("{}",ans);

}
/*

N以下の数字を全て見る。
ある数字Aについて、Aの端の数をBの端の数としたとき、
Bの組み合わせを求める。
Bは、N以下の数である必要がある。

Bの桁数を上げながら数える
端の数が同じなら１桁から、違うなら２桁から。
３桁から間の数を入れられる。

*/