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):
'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''
'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''
'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  
print(list(set(a) & set(b)))  
'''
'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''
'''
.append(要素)
.extend(要素)
.insert(場所,要素)
del 配列名[場所]
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
'''
copy.copy(配列名)
'''
'''
sort()
sorted(reverse=true)
'''
'''
2分探索
bisect.bisect_left(リスト, 要素)
'''
'''
リスト同士の演算
list(map(lambda x, y: x+y, el2, el3))
'''
'''
配列の初期化
配列名=[a]*n+[b]*m
'''
'''
重複
list(set())
２次元配列
list(map(list, set(map(tuple, addition))))
'''
'''
順列
for el in itertools.permutations(配列のパターン):
'''
'''
Decimal()
'''






from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
import itertools
from functools import reduce
from operator import mul
def gcd(*numbers):
    return reduce(math.gcd, numbers)


def gcd_list(numbers):
    return reduce(math.gcd, numbers)


def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)


def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]


N = int(stdin.readline().rstrip())
count = 0
add = 0
bs = []
for k in range(1, N+1):
    a = k
    bf = str(a)[len(str(a)) - 1]
    if bf == '0':
        continue
    bb = str(a)[0]
    if str(a)[0] == str(a)[len(str(a)) - 1]:
        if int(bf) > N:
            continue
        bs.append((a, int(bf)))
    if int(bf + bb) > N:
        continue
    bs.append((a, int(bf + bb)))
    if len(str(N)) >= 3:
        if bf == str(N)[0]:
            for m in range(1, len(str(N)) - 2):
                add += 10 ** m
            if int(bb) <= int(str(N)[len(str(N)) - 1]):
                add += int(str(N)[1: len(str(N)) - 1]) + 1
            if int(bb) > int(str(N)[len(str(N)) - 1]):
                add += int(str(N)[1: len(str(N)) - 1])
        if bf < str(N)[0]:
            tmp = 0
            for m in range(1, len(str(N)) - 1):
                tmp += 10 ** m
            add += tmp
        if bf > str(N)[0]:
            for m in range(1, len(str(N)) - 2):
                add += 10 ** m
print(len(bs)+add)

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

L = [0] * 100

for i in range(1,N+1):
    if i % 10 == 0:
        continue
    a = (i // (10 ** (len(str(i)) - 1))) * 10 + i % 10
    L[a] += 1
    
ans = 0
for i in range(100):
    if L[i] == 0:
        continue
    if i // 10 == i % 10:
        ans += L[i] ** 2
    else:
        a = i // 10 + (i % 10) * 10
        ans += L[i] * L[a]

print(ans)

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

Solution (Python):
n = int(input())
n_digit = len(str(n))
ans = 0
for i in range(1,n+1):
  max_a = list(str(i))[0]
  min_a = list(str(i))[-1]
  if min_a == "0":
    continue
  else:
    if max_a == min_a:
      if int(max_a)<=n:
        ans += 1
    for j in range(n_digit-1):
      max_b = int(min_a+"9"*j+max_a)
      min_b = int(min_a+"0"*j+max_a)
      if max_b <= n:
        ans += int(10**j)
      elif min_b > n:
        continue
      else:
        ans += (n-min_b)//10+1
print(ans)

Solution (Python):

import itertools

N = int(input().split()[0])
s = str(N)
l = list(range(1, 10))
p_gen = itertools.product(l, repeat=2)
p_list = list(p_gen)
K = len(str(s))


def count_kouho(x, y):
    count = 0
    for k in range(1, K + 1):
        
        if k == 1:
            if x == y:
                if x <= N:
                    count += 1
            continue
        
        if k == 2:
            if int(str(x) + str(y)) <= N:
                count += 1
            continue
        
        if k == K:
            
            
            if x < int(s[0]):
                count += 10 ** (k - 2)
            elif x > int(s[0]):
                pass
            else:
                if y <= int(s[-1]):
                    count += int(s[1:-1]) + 1
                else:
                    count += int(s[1:-1])
            continue
        count += 10 ** (k - 2)
    return count


c = 0

for p in p_list:
    head, tail = p
    kouho_1 = count_kouho(head, tail)
    if head == tail:
        kouho_2 = kouho_1
    else:
        kouho_2 = count_kouho(tail, head)
    if kouho_1 < 1 or kouho_2 < 1:
        
        continue
    new_c = kouho_1 * kouho_2
    
    c += new_c

ans = c
print(ans)

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

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

for i in range(1, n+1):
    s = str(i)
    b = int(s[0])
    e = int(s[-1])
    l[b][e] += 1

for b in range(10):
    for e in range(10):
        ans += l[b][e] * l[e][b]
        
    

print(ans)

Solution (Python):
n=int(input())
x=[0 for i in range(n+1)]

def find(a,n,x):
  k =0
  l =0
  for j in range(a,n):
    if j % 10!= 0:
      if a%10 == j//(10**(len(str(j))-1)) and j%10 == a//(10**(len(str(a))-1)):
        k+=1
        x[j] = 1
      if a%10 == j%10 and j//(10**(len(str(j))-1)) == a//(10**(len(str(a))-1)):
        l+=1
        x[j] = 1
  if a%10 == a//(10**(len(str(a))-1)):
    return k*l
  return 2*k*l

ans=0
for i in range(1,n+1):
  if x[i] == 0 and i%10!=0:
    ans += find(i,n+1,x)

print(ans)

Solution (Python):
n = int(input())
m = len(list(str(n)))
ans = 0
count = 0
c = 1
for i in range(m-1):
    if i == 0:
        count += 1
    else:
        count += c
        c *= 10

k = n//(10**(m-1))
a = n%10
ans += (count+c)*(count+c)*(k-1)

ans += (count)*(count)*(9-k)

count += (n-k*(10**(m-1)))//10+1
if a < k:
    count -= 1
ans += (count)*(count)

c = 1
count = 0
for i in range(1,m-1):
    count += c
    c *= 10

if k < 9:
    ans += count*count*(9-k)*(8-k)
if k > 1:
    ans += (count+c)*(count+c)*(k-1)*(k-2)
ans += (count+c)*(count)*(k-1)*(9-k)*2
for i in range(1,a+1):
    if i < k:
        ans += (count+c)*(count+1+(n-k*(10**(m-1)))//10)*2
    elif i > k:
        ans += count*(count+1+(n-k*(10**(m-1)))//10)*2
for i in range(a+1,10):
    if i < k:
        ans += (count+c)*(count+(n-k*(10**(m-1)))//10)*2
    elif i > k:
        ans += count*(count+(n-k*(10**(m-1)))//10)*2
print(ans)

Solution (Python):
n = int(input())
ans = 0
cmb = [[0 for _ in range(9)] for _ in range(9)]
if n <= 10:
    print(min(n,9))
    exit(0)
for i in range(1,n+1):
    if str(i)[0] != '0' and str(i)[-1] != '0':
        cmb[int(str(i)[0])-1][int(str(i)[-1])-1] += 1
for i in range(9):
    for j in range(9):
        if cmb[i][j] != 0 and cmb[j][i] != 0:
            ans += (cmb[i][j] * cmb[j][i]) 
print(ans)

Solution (Python):

N = int(input())

Nm = int(str(N)[0])
N0 = int(str(N)[-1])
ORD_N = len(str(N))
N_LEFT = int(str(N)[1:-1]) if 2 < ORD_N else -1







def get_number_less_than_N(bm, b0):
    """ 先頭bm, 後ろb0 を持ち、N より小さい値の数 """
    count = 0
    
    
    if bm == b0:
        count += 1
        
    
    if bm*10 + b0 <= N:
        count += 1
        
    
    
    for i in range(3, ORD_N):
        count += 10 ** (i-2)
        
    
    
    
    if ORD_N >= 3 and bm <= Nm:
        if bm < Nm:
            count += 10 ** (ORD_N-2)
            
        elif b0 <= N0:
            count += N_LEFT + 1
            
        else:
            count += N_LEFT
            
    

    return count
        

count = 0
for i in range(1, N+1):
    
    bm, b0 = int(str(i)[-1]), int(str(i)[0])
    if bm == 0:
        continue
    cold = count
    count += get_number_less_than_N(bm, b0)
print(count)

Solution (Python):
from collections import defaultdict
N = int(input())
S = str(N)
l = len(S)
if l == 1:
    print(N)
    exit()
s = int(S[0])
t = int(S[-1])
mn = min(s,t)

ans = 0
dp = defaultdict(int)
            
for i in range(1,10):
    for j in range(1,10):
        if i == j:
            dp[(i,j)] += 1
        if l >= 3: 
            for k in range(0,l-2):
                dp[(i,j)] += 10**k
        
        if i < s:
            dp[(i,j)] += 10**(l-2)
        elif i == s and j <= t:
            dp[(i,j)] += 1+((N-s*(10**(l-1))) // 10)
        elif i == s and j > t:
            dp[(i,j)] += (N-s*(10**(l-1))) // 10

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

'''
real = 0
for i in range(1,N+1):
    for j in range(1,N+1):
        S = str(i)
        T = str(j)
        if S[0] == T[-1] and S[-1] == T[0]:
            real += 1
print(real)
'''

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

ans = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1,n+1):
    mm = list(str(i))
    a = 0
    for j in range(len(mm)):
        a += int(mm[j])*(10**j)
    
    mm1 = int((list(str(i)))[0])
    mm2 = int((list(str(i)))[-1])
    if not(mm2!=0):
        continue
    
    ans[mm1-1][mm2-1] +=1

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


print(cnt)

Solution (Python):
n = int(input())
n_list = [[0  for s in range(10)] for i in range(10)]
for m in range(1,n+1):
  str_m = str(m)
  n_list[int(str_m[0])][int(str_m[-1])] += 1
ans = 0
for a in range(1,10):
    for b in range(1,10):
        ans += n_list[a][b] * n_list[b][a]
print(ans)

Solution (Python):
import collections

num = int(input())
count = 0
ll = []
lr = []
for nn1 in range(1, num + 1):
  left = (str(nn1).lstrip('0')[0])
  right = (str(nn1)[-1])
  ll.append(left + '+' + right)
  lr.append(right + '+' + left)

c = collections.Counter(lr)

for i in range(num):
  count += int(c[ll[i]])

print(count)

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

def count(a):
  if N < 10:
    if N < a:
      return 0
    else:
      return 1
  else:
    cnt_a = 1
    keta = 2
    while True:
      if N > int(str(a)+str(9)*(keta-2)+str(a)):
        cnt_a += 10 ** (keta-2)
        keta += 1
      elif N >= int(str(a)+str(0)*(keta-2)+str(a)):
        cnt_a += (N - a*(10**(keta-1)) - a) // 10 + 1
        break
      else:
        break
  return cnt_a
        
  
def count_(a, b):
  if N < 10:
    return 0, 0
  else:
    cnt_ = 0
    cnt__  = 0
    keta = 2
    while True:
      if N > int(str(b)+str(9)*(keta-2)+str(a)):
        cnt_ += 10 ** (keta-2)
        cnt__ += 10 ** (keta-2)
        keta += 1
      elif N >= int(str(b)+str(0)*(keta-2)+str(a)):
        cnt_ += (N - b*(10**(keta-1)) - a) // 10 + 1
        cnt__ += 10 ** (keta-2)
        break
      elif N > int(str(a)+str(9)*(keta-2)+str(b)):
        cnt_ += 10 ** (keta-2)
        break
      elif N >= int(str(a)+str(0)*(keta-2)+str(b)):
        cnt_ += (N - a*(10**(keta-1)) - b) // 10 + 1
        break
      else:
        break
  return cnt_, cnt__

ans = 0
for i in range(1, 10):
  for j in range(i, 10):
    
    if i == j:
      
      
      ans += count(i)**2
    else:
      pa, pa_ = count_(i,j)
      
      ans += pa * pa_ * 2
      
print(ans)

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10 ** 6)

    n = int(input())
    cou = 0
    for i in range(1, n + 1):
        if str(i)[-1] == '0':
            continue
        
        if str(i)[0] == str(i)[-1]:
            cou += 1
        k = 2
        while True:
            b = int(str(i)[-1] + '0' * (k - 2) + str(i)[0])
            if b <= n:
                if k > 2:
                    if n - b > 10 ** (k - 1):
                        cou += 10 ** (k - 2)
                    else:
                        cou += (n - b)//10 + 1
                else:
                    cou += 1
                
                k += 1
                continue
            
            break
    print(cou)


if __name__ == '__main__':
    main()

Solution (Python):
from math import log10, floor

def k(a):
    return len(str(a))
N = int(input())
top = str(N)[0]
btm = str(N)[-1]
mid = str(N)[1:-1]
res = 0
for i in range(1,N+1):
    a = str(i)[0]
    b = str(i)[-1]
    ia = int(a)
    ib = int(b)
    if b == "0":
        continue
    if a == b and ia <= N:
        res += 1 
    if(ib * 10 + ia <= N):
        res += 1
    for d in range(1,7):
        if ib * (10**(d+1)) + ia > N:
            continue
        ng = 10**10
        ok = 0
        while ng - ok > 1:
            mid = (ng+ok)//2
            if ib * (10**(d + k(mid))) + mid*10 + ia > N:
                ng = mid
            else:
                ok = mid
        res += ok + 1
    
    
    
    
    
    

print(res)

Solution (Python):
n = int(input())
a = [0] * 100
for i in range(1,n+1):
  msb = str(i)[0]
  lsb = str(i)[-1]
  if msb == '0' or lsb == '0':
    continue
  a[int(msb) * 10 + int(lsb)] += 1

tmp = 0
for i, ai in enumerate(a):
  if '0' not in str(i):
    if i % 11 == 0:
      tmp += ai * ai
    else:
      tmp += ai * a[int(str(i)[::-1])]
  else:
    continue
    
print(tmp)

Solution (Python):
N = int(input())
cnt = 0
lenn = len(str(N))
for i in range(1, N+1):
    num = str(i)
    s = int(num[-1])
    e = int(num[0])
    if s != 0:
        if lenn == 1:
            cnt += 1
        elif lenn == 2:
            if s == e:
                cnt += 1
            if s * 10 + e <= N:
                cnt += 1
        else:
            if s == e:
                cnt += 1
            for k in range(2, lenn + 1):
                if k == lenn:
                    if s < int(str(N)[0]):
                        cnt += 10 ** (k-2)
                    elif s == int(str(N)[0]) and e <= int(str(N)[-1]):
                        cnt += int(str(N)[1:lenn-1]) + 1
                    elif s == int(str(N)[0]) and e > int(str(N)[-1]):
                        if int(str(N)[1:lenn-1]) >= 0:
                            cnt += int(str(N)[1:lenn-1])
                else:
                    cnt += 10 ** (k-2)
print(cnt)

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

    numss = [[0]*(10) for _ in range(10)]
    for A in range(1, N+1):
        ss = str(A)
        numss[int(ss[-1])][int(ss[0])] += 1

    ans = 0
    for A in range(1, N+1):
        ss = str(A)
        ans += numss[int(ss[0])][int(ss[-1])]

    print(ans)

solve()

Solution (Python):
n=int(input())
s=[]
for i in range(9):
    s.append([0,0,0,0,0,0,0,0,0])
ans=0
for i in range(1,n+1):
    if i%10==0:
        pass
    else:
        s[(int(str(i)[0]))-1][(i%10)-1]+=1
for i in range(9):
    for k in range(9):
        ans+=s[i][k]*s[k][i]
print(ans)

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


oo = {}
for i in range(1, 2*(10**5)):
    if i > N:
        break
    a = str(i)
    if a[-1] == '0':
        continue
    k = (int(a[0]),int(a[-1]))

    if k not in oo:
        oo[k] = 0
    oo[k] += 1


d1 = {}
for k, c in oo.items():
    if k[0] == k[1]:
        d1[k] = c


d2 = {}
for k, c in list(oo.items()):
    if k[0] == k[1]:
        continue
    if (k[1], k[0]) in oo:
        d2[k] = c * oo[(k[1], k[0])]


a = list(d1.values())

a2 = list(map(lambda x:x*x,a))


b = sum(d2.values())


print(sum(a2) + b)

Solution (Python):
from collections import defaultdict

N = int(input())
res = 0
d = len(str(N))
same = defaultdict(int)
odd1 = defaultdict(int)
odd2 = defaultdict(int)

for i in range(1, N+1):
    if i % 10 == 0:
        continue

    left = str(i)[0]
    right = str(i)[-1]
    if left == right:
        same[left] += 1
    elif int(left) < int(right):
        odd1[left + right] += 1
        odd2[left + right] += 0
    else:
        odd2[right + left] += 1

for n in same.values():
    res += n**2

for x, y in zip(odd1.values(), odd2.values()):
    res += x*y*2

print(res)

Solution (Python):
n = int(input())
if n > 30:
    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)
elif n==1:
    print(1)
else:
    res = 0
    for i in range(1, n):
        for j in range(1, n):
            if str(i)[0] == str(j)[-1] and str(i)[-1] == str(j)[0]:
                res += 1
    print("{1}".format(n, res))

Solution (Python):
def p(x, y, n):
  
  if x == 0:
    
    return 0
  count = 0
  if x == y:
    if x <= n:
      count += 1

  i = 0
  flag = True
  while flag:
    for j in range(10**i):
      if (x * 10**(i+1) + y + j *10) <= n:
        
        count+=1
      else:
        flag = False
        break
    i += 1
  
  return count

n = int(input())


c = 0
for NF in range(1, 10):
  for NL in range(0, 10):
    
    
    c +=  p(NF, NL, n) * p(NL, NF, n)
print(c)

Solution (Python):

N = int(input())
mx = [[0] * 9 for i in range(9)]

for i in range(1, N+1):
    stri = str(i)
    if not stri[-1] == '0':    
        mx[int(stri[0])-1][int(stri[-1])-1] += 1


ans = 0
for j in range(1, N+1):
    strj = str(j)
    
    if not strj[-1] == '0':
        ans += mx[int(strj[-1])-1][int(strj[0])-1]


print(ans)

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

order = int(math.log10(n))
first = int(n/(10**order))


hund = [0]*99

def calc(a,b):
    global n,order,first
    num = 0

    if(a>n):
        return 0
    if(a==b and a<=n):
        num += 1
    if(order==0):
        return num
    if(a!=first):
        if(first>a):
            num += (10**(order)-1)/9
        else:
            num += (10**(order-1)-1)/9
    else:
        num += (10**(order-1)-1)/9
        if(n%10>=b):
            num += int((n - first * (10**order))/10)+1
        else:
            num += int((n - first * (10**order))/10)
    return num

def eq(a,b):
    if(a!=b):
        return 2
    return 1

for i in range(1,10):
    for j in range(1,10):
        hund[(i-1)*10+(j-1)] = calc(i,j)

count = 0

for i in range(1,10):
    for j in range(1,i+1):
        count += hund[(i-1)*10+(j-1)]*hund[(j-1)*10+(i-1)]*eq(i,j)


print(int(count))

Solution (Python):
import sys
bbn=1000000007
	


		
def main():
	n = int(input())
	nnn=[0]*10
	for i in range(10):
		nnn[i]=[0]*10
	ret=0
	for i in range(1,n+1):
		bb=str(i)
		t=int(bb[0])
		b=int(bb[-1])
		nnn[t][b]+=1
		
	ret=0
	for i in range(10):
		for j in range(10):
			ret += nnn[i][j]*nnn[j][i]
	
	print(ret)
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

def calc(a,b,n):
    ret = 0
    if a==b and a<=n:
        ret += 1
    if a*10+b <= n:
        ret += 1
    for i in range(2,7):
        if a*(10**i)+b>n:
            break
        if a*(10**i)+(10**i)-10+b<=n:
            ret += 10**(i-1)
            continue
        tmp = n
        while(tmp%10!=b):
            tmp -= 1
        tmp -= a*(10**i)
        tmp //= 10
        tmp += 1
        ret += tmp
    return ret

def main():
    N = I()
    ans = 0
    for A in range(1,10):
        for B in range(1,10):
            ans += calc(A,B,N) * calc(B,A,N)
    print(ans)

if __name__ == "__main__":
    main()

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

k = [0 for i in range(81)]

A = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
B = [0, 9, 18, 27, 36, 45, 54, 63, 72, 1, 10, 19, 28, 37, 46, 55, 64, 73, 2, 11, 20, 29, 38, 47, 56, 65, 74, 3, 12, 21, 30, 39, 48, 57, 66, 75, 4, 13, 22, 31, 40, 49, 58, 67, 76, 5, 14, 23, 32, 41, 50, 59, 68, 77, 6, 15, 24, 33, 42, 51, 60, 69, 78, 7, 16, 25, 34, 43, 52, 61, 70, 79, 8, 17, 26, 35, 44, 53, 62, 71, 80]
for i in range(1, N+1):
    h = str(i)[0]
    t = str(i)[-1]
    if t!="0":
        k[(int(h)-1)*9 + int(t) - 1] += 1

ans = sum([k[a]*k[b] for a, b in zip(A, B)])
print(ans)

Solution (Python):
n = int(input())
book = [[0] * 9 for i in range(9)]

ans = 0

for i in range(1, n+1) :
    reverse = 0
    tmp = i
    time = 0
    flag = False
    end = -1
    while(tmp!=0) :
        remind = tmp % 10
        if time == 0 :
            if remind == 0 :
                flag = True
                break
            pre = remind
        tmp = tmp // 10
        time += 1
    if flag == True :
        continue
    if remind != 0 :
        book[remind-1][pre-1] += 1

for i in range(9) :
    for j in range(i, 9) :
        if i == j :
            ans += book[i][j] * book[j][i]
            
            continue
        if book[i][j] != 0 and book[j][i] != 0 :
            ans += book[i][j] * book[j][i] * 2
            




print(ans)

Solution (Python):
n = int(input())
count = 0
for i in range(n):
    head = str(i + 1)[0]
    tail = str(i + 1)[-1]
    if tail == "0":
        continue
    if head == tail:
        count += 1
    for j in range(len(str(n)) - 2):
        count += (10 ** j)
    min_num = int(tail + "0" * (len(str(n)) - 2) + head)
    max_num = int(tail + "9" * (len(str(n)) - 2) + head)
    if min_num <= n:
        count += min((n - min_num) // 10 + 1, (max_num - min_num) // 10 + 1)
print(count)

Solution (Python):
def f(x):
    if 1<=x<=9:
        return 1
    if x%10==0:
        return 0
    x = str(x)
    a = x[0]
    b = x[-1]
    k = len(x)
    if int(a)<int(b):
        if k==2:
            return 0
        else:
            return 2*(10**(k-2)-1)//9
    elif int(a)>int(b):
        return 2*(10**(k-1)-1)//9
    else:
        if k==2:
            return 3
        else:
            return (1+(10**(k-2)-1)//9+int(x[1:k-1]))*2+1

N = int(input())
dp = [0 for _ in range(N+1)]
for i in range(min(N,9)+1):
    dp[i] = i
for i in range(10,N+1):
    dp[i] = dp[i-1]+f(i)
print(dp[N])

Solution (Python):
n = int(input())
a = [[0 for j in range(10)] for i in range(10)]
for i in range(1, n + 1):
    s = str(i)
    a[int(s[0])][int(s[-1])] += 1
sol = 0
for i in range(1, 10):
    sol += (a[i][i] * a[i][i])
for i in range(2, 10):
    for j in range(1, i):
        sol += (a[i][j] * a[j][i] * 2)
print(sol)

Solution (Python):

def my_count(a, b, n):
    ret = 0
    if a == b and a <= n:
        ret = ret + 1
    if a * 10 + b <= n:
        ret = ret + 1
    keta = len(str(n))
    if keta > 3:
        ret = ret + (int('1'*(keta-2)) - 1)
    if keta >= 3:
        candidate = a * (10 ** (keta-1)) + b
        if n - candidate >= 0:
            num = ((n - candidate) // 10) + 1
            if num >= (10 ** (keta-2)):
                ret = ret + (10 ** (keta-2))
            else:
                ret = ret + num
    return ret


n = int(input())

ret = 0

for i in range(1, 10):
    for j in range(1, 10):
        ret = ret + my_count(i, j, n) * my_count(j, i, n)

print(ret)

Solution (Python):
n=int(input())
n_ls=list(str(n))
n_top,n_bottom=int(n_ls[0]),int(n_ls[-1])
if n<100:
    n_middle=0
else:
    n_middle=int("".join(n_ls[1:-1]))
len_n=len(n_ls)
count=0
for i in range(1,n+1):
    stred_ls=list(str(i))
    top,bottom=int(stred_ls[0]),int(stred_ls[-1])
    if bottom==0:
        continue
    
    if top==bottom:
        count+=1
    
    if len_n<=2:
        if int(str(bottom)+str(top))<=n:
            count+=1
        
    else:
        for x in range(len_n-2):
            count+=10**x
        
        judged_x=x+1
        
        if n_top>bottom:
            count+=10**judged_x
        elif n_top==bottom:
            if n_bottom>=top:
                count+=n_middle+1
            else:
                count+=n_middle
        
print(count)

Solution (Python):


N = int(input())

if len(str(N))<4:
    ans = 0
    
    for i in range(1, N+1):
        for j in range(1, N+1):
            if str(i)[0]==str(j)[-1] and str(i)[-1]==str(j)[0]:
                ans += 1
    
    print(ans)
    exit()
    
L = len(str(N))
n = int(str(N)[1:])
ans = 0
F = int(str(N)[0])
L2 = int(str(N)[-1])
S = str(N)
M = int(S[1:len(S)-1])

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

    if f==0 or l==0:
        continue
    
    for k in range(1, L+1):
        
        if k==1 and f!=l:
            continue
        
        if k==1:
            ans += 1
            continue
        
        if k<L:
            ans += 10**(k-2)
        else:
            if F<l:
                continue
                
            if l<F:
                ans += 10**(k-2)
            else:
                if len(S)<=2:
                    continue
                
                ans += M
                
                if L2>=f:
                    ans += 1

print(ans)

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

count = 0
for i in range(1, N + 1):
    str_i = str(i)

    
    if int(str_i[-1]) == 0:
        continue

    
    if len(str_i) == 1:
        count += 1
        continue

    
    if len(str_i) == 2:
        if int(str_i[0]) > int(str_i[-1]):
            count += 2 
        
        if int(str_i[0]) == int(str_i[-1]):
            count += 3  
        continue

    
    
    if int(str_i[0]) > int(str_i[-1]):
        count += (10 ** (len(str_i) - 2)) * 2

    
    
    if int(str_i[0]) == int(str_i[-1]):
        count += (int(str_i[1:-1])) * 2 + 1
        count += 2  

    
    
    for j in range(len(str_i) - 2):
        count += (10 ** j) * 2

print(count)

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

num=0

li = [i+1 for i in range(N)]

l4 = list(map(lambda x: str(x)[0]+str(x)[-1], li))
ll = set(l4)
l5 = list(map(lambda x: str(x)[-1]+str(x)[0], li))
c4 = collections.Counter(l4)
c5 = collections.Counter(l5)

for l in ll:
  num += c4[l] * c5[l]
print(num)

Solution (Python):
def main():
    N = int(input())
    d = [[0] * 10 for _ in range(10)]
    for i in range(1, N + 1):
        s = str(i)
        h, t = int(s[0]), int(s[-1])
        if h * t == 0:
            continue
        d[h][t] += 1
    r = 0
    for h in range(10):
        for t in range(10):
            r += d[h][t] * d[t][h]
    print(r)


main()

Solution (Python):
N=input()
N_int=int(N)
order = len(N)
ans = 0
for i in range(1,int(N)+1):
    str_i = str(i)
    if i%10==0:
        continue
    if str_i[0]==str_i[-1]:
        ans += 1
    for j in range(1,order):
        num = int(str_i[-1])*10**j+int(str_i[0])
        if num <= N_int:
            ans += min(10**(len(str(num))-2),(N_int-num)//10+1)
print(ans)

Solution (Python):
def calc1(x,y,N): 
    d = len(str(N))
    if d == 1:
        re = 0
    elif d == 2:
        if N >= x*(10**(d-1)) + y:
            re = 1
        else:
            re = 0
    else:
        s_high = str(x) + '9'*(d-2) + str(y)
        s_low = str(x) + '0'*(d-2) + str(y)
        if N >= int(s_high):
            re = int(str('1'*(d - 1)))
        else:
            re = int('1'*(d-2))
            if N >= int(s_low):
                re += (N - int(s_low))//10 + 1
    return re

def calc2(x,y,N): 
    d = len(str(N))
    if d == 1:
        if x <= N:
            re = 1
        else:
            re = 0
    elif d == 2:
        if N >= x*(10**(d-1)) + y:
            re = 2
        else:
            re = 1
    else:
        s_high = str(x) + '9'*(d-2) + str(y)
        s_low = str(x) + '0'*(d-2) + str(y)
        if N >= int(s_high):
            re = int(str('1'*(d - 1))) + 1
        else:
            re = int('1'*(d-2)) + 1
            if N >= int(s_low):
                re += (N - int(s_low))//10 + 1
    return re
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        if i != j:
            ans += calc1(i,j,N)*calc1(j,i,N)
for i in range(1,10):
    ans +=calc2(i,i,N)**2
print(ans)

Solution (Python):
n = int(input())
nl = str(n)
ll = len(nl)
lis = []
ans = 0
if ll == 1:
  print(n)
  exit()
  
def countn(a,b):
  cur = 0
  if ll >= 3:
    cur += (10**(ll-2)-1)//9
    if a < int(nl[0]):
      cur += 10**(ll-2)
    elif a == int(nl[0]):
      nnl = nl[1:ll-1]
      if b > int(nl[-1]):
        cur += int(nnl)
      else:
        cur += (int(nnl)+1)
  elif ll == 2:
    if 10*a+b <= n:
      cur += 1
  if a == b:
    cur += 1
  return cur

for i in range(1, 10):
  for j in range(1, i+1):
    if i == j:
      ans += countn(i, j)*countn(j, i)
    else:
      ans += 2*countn(i, j)*countn(j, i)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    str_N=str(N)
    ans=0
    ans2=0
    for i in range(1,N+1):
        s=str(i)
        if s[-1]=='0':
            continue
        if s[0]==s[-1]:
            ans+=1
        if int(s[-1]+s[0])<=N:
            ans+=1
        f=True
        n=1
        while f:
            z=int(s[-1]+str(10**n-1)+s[0])
            if z<=N:
                ans+=10**n
            else:
                if int(s[-1]+str('0'*n)+s[0])>N:
                    pass
                else:
                    m=int(str_N[1:-1])
                    ans+=m
                    if int(str_N[-1])>=int(s[0]):
                        ans+=1
                f=False
                break
            n+=1
    print(ans-ans2)
                

if __name__ == '__main__':
    main()

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

d = {(i, j): 0 for i in range(1, 10) for j in range(1, 10)}

for i in range(1, N + 1):
    S = str(i)
    a, b = int(S[0]), int(S[-1])
    if a == 0 or b == 0:
        continue
    d[(a, b)] += 1

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

print(res)

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

tb = [[0] * 9 for i in range(9)]

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

  if head > 0 and tail > 0:
    tb[head-1][tail-1] += 1

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

print(ans)

Solution (Python):
N=int(input())
box={}
if N<10:
  ans=N
if N>=10:
  for i in range(1,100):
    box[i]=0
  for i in range(1,N+1):
    A_first=str(i)[0]
    if i<=9:
      A_first=0
    A_back=i%10
    index=int(A_first)*10+int(A_back)
    box[index]+=1
  ans=0
  a=0
  b=0
  for i in range(1,100):
    if len(str(i))==1:
      ans+=(box[i]+box[i*11])**2
    if i>10 and str(i)[0]!=str(i)[1] and i%10!=0:
      t=i//10+(i%10)*10
      ans+=(box[i]*box[t])
print(ans)