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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
#define PI 3.141592653589793

int main(){
  int N; cin >> N; 
  ll dp[202020] = {};
  rep(i, 10){
    dp[i] = i;
  }
  for(ll i=10; i<=N; i++){
    vector<ll>keta;
    ll k = i;
    while(k>0){
      ll a = k%10; keta.push_back(a); k/=10;
    }
    if(keta[0]==0){
      dp[i]=dp[i-1];  continue;
    }
    int S=keta.size()-1;
    if(keta[S]==keta[0]){
      if(S==1)dp[i]=dp[i-1]+3;
      else{
        ll t = 1;
        rep(j, S-1){
          t+=(ll)pow(9, j);
        }
        ll p = 1;
        for(int j=1; j<=S-1; j++){
          p*=keta[j];
        }
        t+=p;
        t*=2;
        dp[i]=dp[i-1]+t;
      }
    }
      else if(keta[S]>keta[0]){
        ll t = 0;
        rep(j, S){
          t+=pow(9, j);
        }
        t*=2;
        dp[i]=dp[i-1]+t;
      }
      else if(keta[S]<keta[0]){
        ll t = 0;
        rep(j, S-1){
          t+=pow(9, j);
        }
        t*=2;
        dp[i]=dp[i-1]+t;
      }
    }
    cout << dp[N] << endl;      
    
  }