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 (Python):
n = int(input())
dat = [[0] * 10 for _ in range(10)]
for i in range(1, n+1):
    dat[int(str(i)[0])][int(str(i)[-1])] += 1
res = sum([dat[a][b] * dat[b][a] for a in range(10) for b in range(10)])
print(res)

Solution (Python):
n = int(input())

if n < 10:
    print(n)
else:
    
    x = n // 10
    
    basenum = x * x + 8
    addnum = 0
    
    
    hassame = False
    
    for i in range(x * 10 + 1, n + 1):
        for j in range(1, n + 1):
            if str(i)[0] == str(j)[-1] and str(i)[-1] == str(j)[0]:
                addnum += 1
                if i == j:
                    hassame = True
    res = basenum + addnum * 2 
    res -= 1 if hassame else 0 
    print(res)

Solution (Python):
n = int(input())
if n < 10:
    print(n)
else:
    x = n // 10
    basenum = x * x + 8
    addnum = 0
    
    hassame = False
    for i in range(x * 10 + 1, n + 1):
        
        for j in range(1, n + 1):
            
            if str(i)[0] == str(j)[-1] and str(i)[-1] == str(j)[0]:
                addnum += 1
                if i == j:
                    hassame = True
                
    res = basenum + addnum * 2
    if hassame:
        res -= 1
    
    print(res)

Solution (Python):
n = int(input())
k = [[0] * 10 for _ in range(10)]
for i in range(n + 1):
    l = int(str(i)[0])
    r = int(str(i)[-1])
    k[l][r] += 1
ans = 0

for i in range(1, 10):
    for j in range(1, 10):
        ans += k[i][j] * k[j][i]
print(ans)

Solution (Python):
n = int(input())
k = [[0] * 10 for _ in range(10)]
for i in range(n + 1):
    l = int(str(i)[0])
    r = int(str(i)[-1])
    k[l][r] += 1
ans = 0

for i in range(1, 10):
    for j in range(1, 10):
        ans += k[i][j] * k[j][i]
print(ans)

Solution (Python):
from itertools import combinations_with_replacement as combination

N=int(input())
lst=[[0]*10 for i in range(10)]
for i in range(1,N+1):
	s=str(i)
	l=int(s[0])
	r=int(s[-1])
	lst[l][r]+=1
cmb=list(combination([i for i in range(1,10)],2))

ans=0

for i in range(len(cmb)):
	if cmb[i][0]!=cmb[i][1]:
		ans+=lst[cmb[i][0]][cmb[i][1]]*lst[cmb[i][1]][cmb[i][0]]
	ans+=lst[cmb[i][0]][cmb[i][1]]*lst[cmb[i][1]][cmb[i][0]]
    
print(ans)

Solution (Python):
N = int(input())

def f(n):
    str_n = str(n)
    return (str_n[0], str_n[-1])

from collections import Counter
c = Counter()

for i in range(1, N+1):
    p = f(i)
    c[p] += 1




ans = 0
for i in range(1, N+1):
    p = f(i)
    p_ = (p[1], p[0])
    ans += c[p_]
print(ans)

Solution (Python):
import math
import decimal
import queue
import bisect
import heapq
import time
import itertools

mod = int(1e9+7)

class Library():
    @staticmethod
    def Swap(a,b):
        return [b,a]

    @staticmethod
    def myRound(a,dig=0): 
        p = 10 ** dig
        return (a * p * 2 + 1) // 2 / p

    @staticmethod
    def Roundup(a,dig=0): 
        if a < 0:
            return int(a)
        else:
            return int(a + 0.9)

    @staticmethod
    def Gcd(a,b): 
        if (a<b):
            a,b = Library.Swap(a,b)
        if (b==0):
            return a
        else:
            return Library.Gcd(b,a%b)

    @staticmethod
    def Lcm(a,b): 
        return a//Library.Gcd(a,b)*b

    @staticmethod
    def Divisors(a): 
        divisors = []
        for i in range(1,int(a**0.5)+1):
            if a%i==0:
                divisors.append(i)
                if i!=a//i:
                    divisors.append(a//i)
        divisors.sort()
        return divisors

class Prime():
    
    
    def __init__(self,num):
        self.num = num
        self.sieve = [0 for i in range(self.num + 1)]
        self.sieve[0] = -1
        self.sieve[1] = -1
        for i in range(2,self.num + 1):
            if self.sieve[i] == 0:
                self.sieve[i] = i
                for j in range(i + i,self.num + 1,i):
                    self.sieve[j] = i
    
    
    def isPrime(self,num = None):
        if num is None:
            num = self.num
        if self.sieve[num] == num:
            return True
        else:
            return False
    
    
    def PrimeNum(self,num = None):
        if num is None:
            num = self.num
        prime = []
        for i in range(2,num):
            if self.sieve[i] == i:
                prime.append(i)
        return prime
    
    
    def PrimeFact(self,num = None):
        if num is None:
            num = self.num
        fact = []
        while self.sieve[num] != num:
            fact.append(self.sieve[num])
            num //= self.sieve[num]
        fact.append(num)
        return fact

def main():
    n = int(input())
    mp = {}
    for i in range(1,n+1):
        s = i % 10
        t = i
        while t >= 10:
            t //= 10
        if t * 10 + s in mp.keys():
            mp[t * 10 + s] += 1
        else:
            mp[t * 10 + s] = 1
    ans = 0
    for i in range(1,10):
        for j in range(1,10):
            if j * 10 + i in mp.keys() and i * 10 + j in mp.keys():
                ans += mp[j * 10 + i] * mp[i * 10 + j]
    print(ans)
    return

if __name__=='__main__':
    main()

Solution (Python):
def N(x,y,n): 
    if n<100:
        r=0
        for i in range(1,n+1):
            if str(i)[0]==str(x) and str(i)[-1]==str(y):
                r+=1
        return r
    
    r=N(x,y,99)
    d=len(str(n))
    for i in range(1,d-2):
        r+=10**i
    top=int(str(n)[0])
    if top<x:
        r+=0
    elif top==x:
        r+=int(str(n)[1:-1])
        if str(n)[-1]>=str(y):
            r+=1
    else:
        r+=10**(d-2)

        
    return r


ans=0
n=int(input())
for x in range(1,10):
    for y in range(1,10):
        ans+=N(x,y,n)*N(y,x,n)
print(ans)

Solution (Python):
n = int(input())
c = [[0] * 10 for _ in range(10)]
ans = 0
for i in range(1, n + 1):
    l = int(str(i)[0])
    r = i % 10
    c[l][r] += 1
for i in range(10):
    for j in range(10):
        ans += c[i][j] * c[j][i]
print(ans)

Solution (Python):
from collections import defaultdict

def f(x):
    a = x%10
    b = 0
    while x:
        b = x
        x //= 10
    return (a,b)

N = int(input())
freq = defaultdict(int)
for i in range(N):
    p = f(i+1)
    freq[p] += 1
ans = 0
for i in range(N):
    p = f(i+1)
    q = (p[1], p[0])
    ans += freq[q]
print(ans)

Solution (Python):
n = int(input())

c=[[0]*10 for i in range(10)]
for k in range(1,n+1):
  k=str(k)
  i=int(k[0])
  j=int(k[-1])
  c[i][j]+=1
  
ans=0
for i in range(10):
  for j in range(10):
    ans+=c[i][j]*c[j][i]
print(ans)

Solution (Python):
from itertools import combinations_with_replacement as combination
n=int(input())
lst=[[0]*10 for i in range(10)]
for i in range(1,n+1):
	s=str(i)
	l=int(s[0])
	r=int(s[-1])
	lst[l][r]+=1
cmb=list(combination([i for i in range(1,10)],2))
ans=0
for i in range(len(cmb)):
	if cmb[i][0]!=cmb[i][1]:
		ans+=lst[cmb[i][0]][cmb[i][1]]*lst[cmb[i][1]][cmb[i][0]]
	ans+=lst[cmb[i][0]][cmb[i][1]]*lst[cmb[i][1]][cmb[i][0]]
print(ans)

Solution (Python):
import math
n = int(input())
freq = [[0] * 10 for _ in range(10)]

for i in range(1, n+1):
  a = int(str(i)[0])
  b = int(str(i)[-1])
  freq[a][b] += 1

ans = 0
for j in range(1, n+1):
  a = int(str(j)[0])
  b = int(str(j)[-1])
  ans += freq[b][a]

print(ans)

Solution (Python):
n = input()
digit_num = len(n)
if digit_num == 1:
    print(n)

else:
    first_digit = int(n[0])
    last_digit = int(n[-1])
    n = int(n)
    count = 0
    
    for i in range(1, 10):
        for j in range(i, 10):
            ci = 0
            cj = 0
            
            if i == j:
                if i < first_digit:
                    for k in range(digit_num - 1):
                        ci += 10 ** k
                    ci += 1
                
                elif i == first_digit:
                    for k in range(digit_num - 2):
                        ci += 10 ** k
                    ci += 1
                    ci += (n % (10 ** (digit_num-1))) // 10
                    if last_digit >= i:
                        ci += 1
                
                else:
                    for k in range(digit_num - 2):
                        ci += 10 ** k
                    ci += 1
            
                count += ci ** 2
            
            else:
                if j < first_digit:
                    for k in range(digit_num - 1):
                        ci += 10 ** k
                    cj = ci
                
                elif j == first_digit:
                    for k in range(digit_num - 1):
                        ci += 10 ** k
                    for k in range(digit_num - 2):
                        cj += 10 ** k
                    cj += (n % (10 ** (digit_num-1))) // 10
                    if last_digit >= i:
                        cj += 1
                
                elif i < first_digit and j > first_digit:
                    for k in range(digit_num - 1):
                        ci += 10 ** k
                    for k in range(digit_num - 2):
                        cj += 10 ** k
                
                elif i == first_digit:
                    for k in range(digit_num - 2):
                        ci += 10 ** k
                    ci += (n % (10 ** (digit_num-1))) // 10
                    if last_digit >= j:
                        ci += 1
                    
                    for k in range(digit_num - 2):
                        cj += 10 ** k
                    
                else:
                    for k in range(digit_num - 2):
                        ci += 10 ** k
                    cj = ci
                    
                count += ci * cj * 2

    print(count)

Solution (Python):
N = int(input())
D = {}
for i in range(1, N+1):
    str_i = str(i)
    if (str_i[-1], str_i[0]) not in D:
        D[(str_i[-1], str_i[0])] = 1
    else:
        D[(str_i[-1], str_i[0])] += 1

ans = 0
for j in range(1, N+1):
    str_j = str(j)
    if (str_j[0], str_j[-1]) in D:
        ans += D[(str_j[0], str_j[-1])]

print(ans)

Solution (Python):
N = int(input())
l = [[0] * 9 for i in range(9)]
for i in range(1, N+1):
    if int(str(i)[-1]) != 0:
        l[int(str(i)[0])-1][int(str(i)[-1])-1] += 1

ans = 0
for i in range(9):
    for j in range(9):
        ans += l[i][j]*l[j][i]
print(ans)

Solution (Python):
def count_same(first, last, digit):
    dig10 = 10**(digit-1)
    if n//dig10 > first:
        return dig10//10
    elif n//dig10 == first:
        return (n%dig10 - last) //10 + 1
    else:
        return 0

def count(first,last):
    count = 0
    if first == last and first <=n: count += 1
    if n <10:
        count += 0
    elif n < 100:
        if first*10+last <=n: count += 1
    elif n < 1000:
        count += 1 + count_same(first,last,3)
    elif n < 10000:
        count += 11 + count_same(first,last,4)
    elif n < 100000:
        count += 111 + count_same(first,last,5)
    else:
        count += 1111 + count_same(first,last,6)
    return count
    
n=int(input())
ans = 0
for i in range(1,10):
    for j in range(i,10):
        if i==j:
            ans += count(i,j)**2
        else:
            ans += count(i,j)*count(j,i) * 2
print(ans)

Solution (Python):
import numpy as np
def cmb(n, r):
    if n<r:
      return 0
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n = int(input())
field = np.array([[0 for _ in range(9)] for _ in range(9)])

for i in range(1, n+1):
    stri = str(i)
    mae = int(stri[0])
    ushiro = int(stri[-1])
    if mae != 0 and ushiro != 0:
        field[mae-1, ushiro-1] += 1

total = 0
for i in range(9):
    val = field[i, i]
    
    
    total += val
    total += 2*cmb(val, 2)

for i in range(9):
    for j in range(i, 9):
        if i==j:
            continue
        else:
            total += 2*field[i,j]*field[j,i]

print(total)

Solution (Python):
N = int(input())
count = [[0] * 10 for i in range(10)]
ans = 0

for i in range(N + 1):
    l, r = int(str(i)[0]), int(str(i)[-1])
    count[l][r] += 1

for i in range(1, 10):
    for j in range(1, 10):
        ans += count[i][j] * count[j][i]
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())

cnt = defaultdict(int)

for a in range(1, N + 1):
    cnt[(str(a)[0], str(a)[-1])] += 1

ans = 0
for head in range(1, 10):
    for tail in range(1, 10):
        ans += cnt[(str(head), str(tail))] * cnt[(str(tail), str(head))]

print(ans)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())

ans = 0
count = {}

for i in map(str, range(1, N+1)):
    first = i[0]
    last = i[-1]
    key = first + last
    if "0" not in key:
        count[key] = count.get(key, 0) + 1

for key, value in count.items():
    new_key = key[-1] + key[0]
    ans += value * count.get(new_key, 0)

print(ans)

Solution (Python):
N=int(input())
L=[[0 for x in range(10)] for y in range(10)]

o=1
for i in range(1,N+1):
  if 10*o==i:
    o*=10
  y=i%10
  if y==0:
  	continue
  x=i//o
  L[x][y]+=1

t=0
for i in range(1,10):
  for j in range(1,10):
    t+=L[i][j]*L[j][i]

print(t)

Solution (Python):
from collections import defaultdict
import sys
input = sys.stdin.readline


def f(x):
    a = str(x)[-1]
    b = str(x)[0]
    return (b, a)


def main():
    N = int(input())

    freq = defaultdict(int)
    for i in range(1, N + 1):
        p = f(i)
        freq[p] += 1

    ans = 0
    for i in range(1, N + 1):
        p = f(i)
        q = (p[1], p[0])
        ans += freq[q]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())

def prefix_postfix(n):
    sn = str(n)
    return int(sn[0]), int(sn[-1])

import numpy as np
cnt = np.zeros((10, 10), np.uint64)

for i in range(1, N+1):
    pair = prefix_postfix(i)
    cnt[pair] += 1

ans = 0

for i in range(10):
    for j in range(10):
        ans += cnt[i, j] * cnt[j, i]

print(int(ans))

Solution (Python):
N = int(input())
sheet = [[0 for i in range(10)] for i in range(10)]
cnt = 0

for i in range(1, N + 1):
  str_i = str(i)
  sheet[int(str_i[0])][int(str_i[-1])] += 1

res = 0
for i in range(10):
  for j in range(10):
    res += sheet[i][j] * sheet[j][i]

print(res)

Solution (Python):

from itertools import product, permutations, combinations
from bisect import bisect_right,bisect_left

N=int(input())

tbl = [[0] * 10 for i in range(10)]

for i in range(1, N+1):
    num = str(i)
    s = int(num[0])
    e = int(num[-1])
    tbl[s][e] += 1

cnt = 0
for i in range(10):
    for j in range(10):
        cnt += tbl[i][j] * tbl[j][i]
print(cnt)

Solution (Python):
n = int(input())

table = [[0]*10 for _ in range(10)]
for i in range(1,n+1):
  t = int(str(i)[0])
  b = i%10
  table[t][b] += 1
  
ans = 0  
for i in range(10):
  for j in range(10):
    ans += table[i][j] * table[j][i]
    
print(ans)

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
f = defaultdict(lambda : 0)
for i in range(1,n+1):
    s = str(i)
    t,u = int(s[0]), int(s[-1])
    f[(u,t)] += 1

ans = 0
for i in range(1,n+1):
    s = str(i)
    t,u = int(s[0]),int(s[-1])
    ans += f[(t,u)]
print(ans)

Solution (Python):
n = int(input())
res = [[0]*9 for _ in range(9)]
for i in range(1,n+1):
  a = i%10
  if a== 0: continue
  while i > 0:
    b = i%10
    i = i//10
  res[a-1][b-1] += 1


ans = 0

for i in range(9):
  for j in range(9):
    ans += res[i][j] * res[j][i]

print(ans)

Solution (Python):
n = int(input())
f_dict = {}
s_dict = {}
cnt_all = 0
for i in range(n):
  moji = str(i+1)
  if not moji[0]+moji[-1] in f_dict:
    f_dict[moji[0]+moji[-1]] = 1
  else:
    f_dict[moji[0]+moji[-1]] += 1 
  if not moji[-1]+moji[0] in s_dict:
    s_dict[moji[-1]+moji[0]] = 1
  else:
    s_dict[moji[-1]+moji[0]] += 1
for mykey in f_dict.keys():
  if mykey in s_dict:
      cnt_all += f_dict[mykey] * s_dict[mykey]
print(cnt_all)

Solution (Python):
N = int(input())

pool = []
for _ in range(10):
    pool.append([0] * 10)

for n in range(1, 1 + N):
    i = int(str(n)[0])
    j = n % 10
    pool[i][j] += 1

ans = 0
for i in range(10):
    for j in range(10):
        ans += pool[i][j] * pool[j][i]
print(ans)

Solution (Python):
def solve():
    N = int(input())

    d = [[0] * 10 for i in range(10)]

    for num in range(1,N+1):
        num = str(num)
        f = int(num[0])
        e = int(num[-1])
        d[f][e] += 1
        
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += d[i][j] * d[j][i]

    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
S=input()
TT=[[0]*10 for _ in range(10)]
N=int(S)

for i in range(1,N+1):
    ii=str(i)
    a=int(ii[0])
    b=int(ii[-1])
    TT[a][b]+=1
    
ans=0
for i in range(10):
    for j in range(10):
        ans+=TT[i][j]*TT[j][i]
        
print(ans)

Solution (Python):
N=int(input())

N_digit = len(str(N))
if N_digit==1:
    print(N)
    exit()

N_head = N//(10**(N_digit-1))
N_tail = N%10

count = 0
for A in range(1, N+1):
    A_digit = len(str(A))
    A_head  = A//(10**(A_digit-1))
    A_tail  = A%10

    if A_tail==0:
        continue

    if A_head==A_tail:
        count += 1

    for digit in range(2, N_digit+1):
        if digit==N_digit:
            if A_tail < N_head:
                count += 10**(digit-2)
            elif A_tail == N_head:
                if A_head <= N_tail:
                    count += N//10-N_head*(10**(N_digit-2))+1
                else:
                    count += N//10-N_head*(10**(N_digit-2))
            else:
                pass
        else:
            count += 10**(digit-2)
print(count)

Solution (Python):
N = int(input())

L = [[0]*10 for _ in range(10)]
for n in range(1, N+1):
    i = int(str(n)[0])
    j = int(str(n)[-1])
    L[i][j] += 1

ans = 0
for i in range(10):
    for j in range(10):
        ans += L[i][j]*L[j][i]

print(ans)

Solution (Python):
import math
n = int(input())

def first_last(n):return n//(10**int(math.log10(n))), n%10
data = []
for i in range(9):data.append([0, 0, 0, 0, 0, 0, 0, 0, 0])

for i in range(1, n+1):
    first, last = first_last(i)
    if(first!=0 and last!=0):data[first-1][last-1] += 1

ans = 0
for i in range(9):
    for j in range(9):
        ans += data[i][j] * data[j][i]
print(ans)

Solution (Python):
N = int(input())
d = [[0 for _ in range(10)] for _ in range(10)]

for i in range(1, N+1):
    S = str(i)
    s = int(S[0])
    t = int(S[-1])
    d[s][t] += 1

ans = 0
for i in range(10):
    for j in range(10):
        ans += d[i][j] * d[j][i]

print(ans)

Solution (Python):
N = int(input())
C = [[0] * 10 for i in range(10)]

for i in range(1, N + 1):
  p = len(str(i))
  a = i // (10 ** (p - 1))
  b = i % 10
  C[a][b] += 1
  
count = 0
for i in range(10):
  for j in range(10):
    count += C[i][j] * C[j][i]
    
      
print(count)

Solution (Python):
n = int(input())

rdict = {}
slist = []
for i in range(1, n+1):
    s = str(i)
    key = (s[0], s[-1:])
    if key in rdict:
        rdict[key] += 1
    else:
        rdict[key] = 1
ans = 0
for i in range(1, n+1):
    s = str(i)
    key = (s[-1:], s[0])
    if key in rdict:
        ans += rdict[key]

print(ans)

Solution (Python):









import math

def com(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def main():
    l = [[0]*10, [0]*10, [0]*10, [0]*10, [0]*10, [0]*10, [0]*10, [0]*10, [0]*10]
    ans = 0
    N = int(input())

    for i in range(1,N+1):
        i = str(i)
        if i[0] != 0 and i[-1] != 0:
            l[int(i[0])-1][int(i[-1])-1] += 1

    for i in range(9):
        for j in range(9):
            ans += l[i][j]*l[j][i]
    return ans
print(main())

Solution (Python):
n = int(input())

def f(p,q):
    res = 0
    if p == q and n >= p:
        res += 1
    

    for k in range(2,7):
        t = p*pow(10,k-1) + q
        if t > n:
            continue
        for w in range(10**(k-2)):
            if t+w*10 <= n:
                res += 1
            else:
                break
    return res

ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += f(i,j)*f(j,i)
print(ans)

Solution (Python):
N = int(input())

cnt = [[0] * 10 for i in range(10)]
for n in range(1, N + 1):
    cnt[int(str(n)[0])][int(str(n)[-1])] += 1

ans = 0
for i in range(10):
    for j in range(10):
        ans += cnt[i][j] * cnt[j][i]

print(ans)

Solution (Python):
S=input()
N=int(S)
n=len(S)


ans=0
p=[1,10,100,1000,10000,100000]

for i in range(N+1):
    ii=str(i)
    a=int(ii[0])
    b=int(ii[-1])
    if a*b!=0:
        if a==b:
            ans+=1
        for j in range(2,n+1):
            if j<n:
                ans+=p[j-2]
                
            elif j==n:
                floor=b*p[j-1]+a
                if j==2:
                    ceil=floor
                else:
                    ceil=floor+p[j-1]-p[1]
                
                if N>=ceil:
                    ans+=p[j-2]
                    
                elif N<floor:
                    continue
                else:
                    ans+=(N-floor)//10+1
                    
    
                    
print(ans)

Solution (Python):
n = int(input())

a = [[0 for i in range(10)]for j in range(10)]
ans = 0

for i in range(n):
    np = str(i+1)
    s = len(np)
    r1 = np[-1]
    r2 = np[-s]
    
    a[int(r1)][int(r2)] += 1
                    
for i in range(10):
    for j in range(10):
        ans += a[i][j]*a[j][i]
        
print(ans)

Solution (Python):
N = int(input())

table = [[0] * 10 for _ in range(10)]

for i in range(1, N+1):
    head = int(str(i)[0])
    tail = int(str(i)[-1])
    table[head][tail] += 1

ans = 0
for i in range(10):
    for j in range(10):
        ans += table[i][j] * table[j][i]
print(ans)

Solution (Python):
n = int(input())

c = [[0 for i in range(10)] for j in range(10)]

for i in range(n+1):
    if i % 10 == 0:
        continue
    s = str(i)
    a = int(s[0])
    b = int(s[-1])

    c[a][b] += 1

ans = 0

for i in range(10):
    for j in range(10):
        ans += c[i][j] * c[j][i]

print(ans)

Solution (Python):
n=int(input())
count=[[0 for i in range(10)] for j in range(9)]
ans=0
num=1
zero=0
for i in range(1,n+1):
    if (10**(zero+1))<=num:
        zero+=1
    a=i//10
    r=i-a*10
    l=i//(10**zero)
    count[l-1][r]+=1
    num+=1

for i in range(1,10):
    for j in range(1,10):
        if i==j:
            continue
        ans+=(count[i-1][j])*(count[j-1][i])
for i in range(1,10):
    ans+=(count[i-1][i]**2)
print(ans)

Solution (Python):
n = int(input())

l = [[0]*10 for _ in range(10)]

for i in range(1, n+1):
    l[int(str(i)[0])][(i % 10)] += 1

ans = 0
for i in range(10):
    for j in range(10):
        ans += l[i][j] * l[j][i]

print(ans)

Solution (Python):
n = int(input())
ans = 0

li = [[0]*10 for _ in range(10)]

for i in range(1,n+1):
    j = str(i)
    li[int(j[0])][int(j[-1])] += 1

for i in range(1,10):
    for j in range(1,10):
        ans += li[i][j] * li[j][i]

print(ans)

Solution (Python):
N = int(input())
ans = 0
count = []
for i in range(10):
    count.append([0] * 10)
for i in range(N+1):
    str_i = str(i)
    if str_i[-1] == "0":
        continue
    else:
        if str_i[-1] == str_i[0]:
            ans += 2*count[int(str_i[0])][int(str_i[-1])] +1
            count[int(str_i[0])][int(str_i[-1])] += 1
        else:
            ans += count[int(str_i[-1])][int(str_i[0])]*2
            count[int(str_i[0])][int(str_i[-1])] += 1
print(ans)

Solution (Python):
n = int(input())
mem = [[0]*10 for _ in range(10)]
for i in range(1,n+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    mem[a][b] +=1

ans = 0
    
for i in range(1,n+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    ans += mem[b][a]
print(ans)

Solution (Python):
from itertools import product


def solve(string):
    l = len(string)
    *ns, = map(int, string)
    n = int(string)
    c = [0] + [10**i for i in range(6)]
    t = sum(c[:l - 1])
    t = [[t] * 10 for _ in range(10)]
    if l > 1:
        for i in range(10):
            t[i][i] += 1
    for i in range(1, ns[0]):
        for j in range(1, 10):
            t[i][j] += c[l - 1]
    for i in range(ns[0] * 10**(l - 1), n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
from itertools import product


"""
def solve(string):
    n = int(string)
    t = [[0] * 10 for _ in range(10)]
    for i in range(n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    
    
"""


def solve(string):
    l = len(string)
    n = int(string)
    c = [0] + [10**i for i in range(6)]
    t = sum(c[:l - 1])
    t = [[t] * 10 for _ in range(10)]
    if l > 1:
        for i in range(10):
            t[i][i] += 1
    for i in range(10 ** (l - 1), n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))

if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
n = int(input())

count = [[0]*10 for i in range(10)]

for i in range(1,n+1):
    first = int(str(i)[0])
    last = int(str(i)[-1])

    if first == 0 or last == 0:
        continue
    else:
        count[first][last] += 1

ans = 0
for first in range(1,10):
  for last in range(1,10):
    ans += count[first][last] * count[last][first]
print(ans)

Solution (Python):
N = int(input())
n= str(N)
cnt = 0
c=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
  i = str(i)
  a = int(i[0])
  b = int(i[-1])
  c[a][b]+=1
ans = 0
for i in range(1,10):
  for j in range(1,10):
    ans +=c[i][j]*c[j][i]
print(ans)

Solution (Python):
n=int(input())
c=[[0]*10 for i in range(10)]
for i in range(1,n+1):
  c[int(str(i)[0])][int(str(i)[len(str(i))-1])]+=1
ans=0
for i in range(1,10):
  for j in range(1,10):
    ans+=c[i][j]*c[j][i]
print(ans)

Solution (Python):
n = int(input())
r = range(1, min(9, n) + 1)
a = [None] + [[0] * 10 for _ in r]
for i in r:
  a[i][i] += 1
for i in r:
  for j in r:
    k = 1
    while 10 * k * i + j <= n:
      a[i][j] += min(k, (n - j) // 10 - k * i + 1)
      k *= 10
print(sum(a[i][j] * a[j][i] for i in r for j in r))

Solution (Python):
r = range(1, 10)
n = int(input())
a = [None] + [[0] * 10 for _ in r]
for i in r:
  if i <= n:
    a[i][i] += 1
for i in r:
  for j in r:
    k = 1
    while 10 * k * i + j <= n:
      a[i][j] += min(k, (n - j) // 10 - k * i + 1)
      k *= 10
print(sum(a[i][j] * a[j][i] for i in r for j in r))

Solution (Python):
r = range(1, 10)
n = int(input())
a = [None] + [[0] * 10 for _ in r]
for i in r:
  if i <= n:
    a[i][i] += 1
for i in r:
  for j in r:
    k = 1
    while 10 * k * i + j <= n:
      a[i][j] += min(k, (n - j) // 10 - k * i + 1)
      k *= 10
print(sum(a[i][j] * a[j][i] for i in r for j in r))

Solution (Python):
n = int(input())
l = [[0] * 10 for i in range(10)]
ans = 0

for i in range(1, n+1):
  top = int(str(i)[0])
  btm = int(str(i)[-1])
  if btm == 0:
    continue
  l[top][btm] += 1
    
for i in range(1, 10):
  for j in range(1, 10):
    ans += l[i][j] * l[j][i]
    
print(ans)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())



c_list = [[0] * 10 for i in range(10)]
for n in range(1, N + 1):
    
        
            
            
    int(str(n)[0])
    c_list[int(str(n)[0])][int(str(n)[-1])] += 1
output = 0
for i in range(10):
    for j in range(10):
        output += c_list[i][j] * c_list[j][i]

print(output)

Solution (Python):
n = int(input())
r = range(1, 10)
a = [[0] * 9 for _ in r]
for i in r:
  if i <= n:
    a[i - 1][i - 1] += 1
for i in r:
  for j in r:
    k = 1
    while 10 * k * i + j <= n:
      a[i - 1][j - 1] += min(k, (n - j) // 10 - k * i + 1)
      k *= 10
print(sum(a[i][j] * a[j][i] for i in range(9) for j in range(9)))

Solution (Python):
N=int(input())

IJ = [[0] * 10 for i in range(10)]
ans = 0

for i in range(1,N+1):
    Top = str(i)[0]
    Btm = str(i)[-1]

    if Btm == '0':
        continue
    
    IJ[int(Top)][int(Btm)] += 1

for i in range(1, 10):
    for j in range(1, 10):
        ans += IJ[i][j] * IJ[j][i]
print(ans)

Solution (Python):
N = int(input())
ans = 0
list = [[ 0 for i in range(10)] for j in range(10)]

for i in range(1,N+1):
  left = int(str(i)[0])
  right = int(str(i)[-1])
  list[left][right] += 1

for i in range(10):
  for j in range(10):
    ans += list[i][j] * list[j][i]
print(ans)

Solution (Python):
from itertools import product


def solve(string):
    n = int(string)
    t = [[0] * 10 for _ in range(10)]
    for i in range(n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
from itertools import product


def solve(string):
    l = len(string)
    *s, = map(int, string)
    n = int(string)
    t = [[0] * 10 for _ in range(10)]
    for i in range(n + 1):
        i = str(i)
        t[int(i[0])][int(i[-1])] += 1
    return str(sum(t[i][j] * t[j][i] for i, j in product(range(1, 10), repeat=2)))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
n = int(input())
ans = 0
for i in range(1,n+1):
    t,b = str(i)[0],str(i)[-1]
    if b == "0":
        continue
    if t==b:
        ans += 1
    if int(b + t) <= n:
        ans += 1
    l = len(str(n))
    for j in range(l-2):
        if j == l - 3:
            if int(str(n)[0]) > int(b):
                ans += 10**(j + 1)
            elif int(str(n)[0]) == int(b):
                ans += int(str(n)[1:-1])
                if int(str(n)[-1]) >= int(t):
                    ans += 1
        else:
            ans += 10 ** (j + 1)
print(ans)

Solution (Python):


n=int(raw_input())
l=[[0 for i in range(10)] for j in range(10)]
for i in range(1,n+1):
 l[int(str(i)[0])][int(str(i)[-1])]+=1
res=0
for i in range(10):
 for j in range(10):
  res+=l[i][j]*l[j][i]
print res