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())
a = [[0] * 9 for i in range(9)]
b = [[0] * 9 for i in range(9)]
ans = 0
for A in range(1,N+1):
    list_a = str(A)
    if list_a[-1] == '0':
        continue
    a[int(list_a[0])-1][int(list_a[-1])-1] += 1
    b[int(list_a[-1])-1][int(list_a[0])-1] += 1
for i in range(9):
    for j in range(9):
        ans += a[i][j]*b[i][j]
print(ans)

Solution (Python):
N=int(input())
D={}
for i in range(1,N+1):
    i=str(i)
    D[(i[0],i[-1])]=D.get((i[0],i[-1]),0)+1
ans=0
for i in range(1,N+1):
    i=str(i)
    ans+=D.get((i[-1],i[0]),0)
print(ans)

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    N=int(readline())
    d=dict()
    for i in range(1,N+1):
        i=str(i)
        head=i[0]
        tail=i[-1]
        if tail=="0":
            continue
        if (head,tail) not in d:
            d[(head,tail)]=1
        else:
            d[(head,tail)]+=1

    ans=0
    s=set()
    for k in d.keys():
        m,l=k[0],k[1]
        if (m,l) in s:
            continue
        if (l,m) in d:
            ans+=d[(m,l)]*d[(l,m)]
            if (m,l)!=(l,m):
                ans+=d[(m,l)]*d[(l,m)]
        s.add((m,l))
        s.add((l,m))

    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
n = int(input())
numl = [[0]*9 for _ in range(9)]
for i in range(n):
  s = str(i+1)
  if s[-1]=="0":
    continue
  numl[int(s[0])-1][int(s[-1])-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    ans += numl[i][j] * numl[j][i]
print(ans)

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

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

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

Solution (Python):
def main():
    N = int(input())
    cnttable = [[0] * 9 for _ in range(9)]
    for i in range(1,N+1):
        if i%10 == 0:
            continue
        if i < 10:
            cnttable[i-1][i-1] += 1
        else:
            tmp = list(str(i))
            cnttable[int(tmp[0])-1][int(tmp[-1])-1] += 1

    ans = 0
    for i in range(9):
        for j in range(i, 9):
            if i == j:
                ans += cnttable[i][j] * cnttable[j][i]
            else:
                ans += cnttable[i][j] * cnttable[j][i] * 2

    print(ans)

if __name__ == "__main__":
    main()

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

for A in range(1,N+1):
    Astr=str(A)
    pre=int(Astr[0])
    suf=int(Astr[-1])
    c[pre][suf]+=1
cnt=0
for i in range(10):
    for j in range(10):
        cnt+=c[i][j]*c[j][i]

print(cnt)

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

for n in range(1,N+1):
  size = int (math.log10(n) + 1)
  sen= n//(10**(size-1))
  matu =n %10
  c[sen][matu] +=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())
counter=[[0 for i in range(9)] for _ in range(9)]


for i in range(1,n+1):
	tmp=str(i)
	f=int(tmp[0])
	l=int(tmp[-1])
	if l != 0:
		counter[f-1][l-1]=counter[f-1][l-1]+1


output=0
for i in range(9):
	for j in range(9):
		output+=counter[i][j]*counter[j][i]

print(output)

Solution (Python):
import itertools

N = input()
if len(N)==1:
    print(N)
    exit()


gen = (1,2,3,4,5,6,7,8,9)
A = list(itertools.product(gen,gen))
count =0

mem = [[0 for i in range(0,10,1)]for j in range(0,10,1)]

for a,b in A:
    tmp = 0
    if a < int(N[0]):
        for i in range(0,len(N)-1,1):
            tmp+= pow(10,i)
    elif a > int(N[0]):
        for i in range(0,len(N)-2,1):
            tmp+= pow(10,i)
    else:
        tmp +=1
        for i in range(1,len(N)-2,1):
            tmp+= pow(10,i)
        if len(N) > 2:
            tmp+=int(N[1:len(N)-1])+1
            if int(N[len(N)-1]) < b:
                tmp-=1
    if a==b:
        tmp+=1
    mem[a][b]=tmp

for a,b in A:
    count+=mem[a][b]*mem[b][a]

print(count)

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

counter = defaultdict(int)
for i in range(1, N+1):
    A = str(i)[0]
    B = str(i)[-1]
    counter[A+B] += 1

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

print(ans)

Solution (Python):
import itertools

N = input()
if len(N)==1:
    print(N)
    exit()


gen = (1,2,3,4,5,6,7,8,9)
A = list(itertools.product(gen,gen))
count =0

mem = [[0 for i in range(0,10,1)]for j in range(0,10,1)]

for a,b in A:
    tmp = 0
    if a < int(N[0]):
        for i in range(0,len(N)-1,1):
            tmp+= pow(10,i)
    elif a > int(N[0]):
        for i in range(0,len(N)-2,1):
            tmp+= pow(10,i)
    else:
        tmp +=1
        for i in range(1,len(N)-2,1):
            tmp+= pow(10,i)
        if len(N) > 2:
            tmp+=int(N[1:len(N)-1])+1
            if int(N[len(N)-1]) < b:
                tmp-=1
    if a==b:
        tmp+=1
    mem[a][b]=tmp

for a,b in A:
    count+=mem[a][b]*mem[b][a]

print(count)

Solution (Python):
n=int(input())
start_end_map={}
for i in range(1,n+1):
    char=str(i)
    start=char[0]
    end=char[-1]
    if end=="0":
        continue
    if (start,end) not in start_end_map:
        start_end_map[(start,end)]=0
    if (end,start) in start_end_map:
        start_end_map[(start,end)]+=1
        if start!=end and start_end_map[(end,start)]==0:
            start_end_map[(end,start)]=1
    
counter=0
for key,value in start_end_map.items():
    if value==0:
        continue
    start,end=key
    if (end,start) in start_end_map:
        counter+=start_end_map[(start,end)]*start_end_map[(end,start)]
print(counter)

Solution (Python):
inf = 10**15
mod = 10**9+7
n = int(input())
ans=0
dp = [[0 for j in range(10)]for i in range(10)]
for k in range(1,n+1):
    tmp = str(k)
    dp[int(tmp[0])][int(tmp[-1])]+=1
for i in range(1,10):
    for j in range(1,10):
        ans+=dp[i][j]*dp[j][i]
print(ans)

Solution (Python):
n=int(input())
r=0
l=[[0 for i in range(9)] for j in range(9)]
for i in range(n):
  s=str(i+1)
  a=int(s[0])
  b=int(s[-1])
  if a!=0 and b!=0:
    l[a-1][b-1]+=1
for i in range(9):
  for j in range(9):
    r+=l[i][j]*l[j][i]
print(r)

Solution (Python):
h=[[0]*10 for _ in range(10)]
n=int(input())
for i in range(1,n+1):
    k=i%10
    s=i//(10**(len(str(i))-1))
    h[s][k]+=1
ans=0
for i in range(10):
    for j in range(10):
        ans+=h[i][j]*h[j][i]
print(ans)

Solution (Python):
from collections import defaultdict

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

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

N_s = int(str(N)[0])
N_e = int(str(N)[-1])
if len(str(N)) > 2:
    N_mid = int(str(N)[1:-1])
else:
    N_mid = 0

def search(s, e):
    ans = 0
    for i in range(1, len(str(N))+1):
        if i == 1:
            if s == e and s <= N:
                ans += 1
                continue
        else:
            if i != len(str(N)):
                ans += 10**(i-2)
            else:
                if N_s > s:
                    ans += 10**(i-2)
                elif N_s == s:
                    if N_e >= e:
                        ans += N_mid + 1
                    else:
                        ans += N_mid

    return ans

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

Solution (Python):
n=int(input())
ans=0
def c(i,j):
  ret=0
  for k in range(1,n+1):
    if str(k)[0]==str(i) and str(k)[-1]==str(j):
      ret+=1
  return ret
for i in range(1,10):
  for j in range(i,10):
    if i==j:
      ans+=c(i,j)*c(j,i)
    else:
      ans+=2*c(i,j)*c(j,i)
print(ans)

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

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

freq = {}

for i in range(1, n+1):
    p = func(i)
    freq[p] = freq.get(p, 0) + 1

ans = 0

for i in range(1, n+1):
    p = func(i)
    ans += freq.get((p[1],p[0]), 0)
    
print(ans)

Solution (Python):
from sys import stdin


def main():
    n = int(stdin.readline())
    n_len = len(str(n))
    top = str(n)[0]

    ans = 0

    c = [[0 for _ in range(10)] for _ in range(10)]

    for i in range(n):
        i_str = str(i + 1)
        c[int(i_str[0])][int(i_str[-1])] += 1

    ans = 0

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

    print(ans)


if __name__ == "__main__":
    main()

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

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

for n in range(1, N+1):
    s = str(n)
    a, b = map(int, [s[0], s[-1]])
    num[a][b] += 1

ans = 0
for a in range(1, 10):
    for b in range(1, 10):
        ans += num[a][b] * num[b][a]

print(ans)

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

for i in range(1, n+1):
  m = len(str(i))
  top = int(str(i)[0])
  bottom = i % 10
  counter = 0
  if bottom*top == 0:
    continue
  if top == bottom:
    counter += 1
  for j in range(1, 7):
    left = bottom*(10**j) + top
    right = bottom*(10**j) + top + 10**j - 10
 
    if n < left:
      break
    elif left <= n and n <= right:
      counter += (n - left) // 10 + 1
      break
    elif right < n:
      counter += 10**(j-1)
  ans += counter
  
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    count=[[0]*10 for _ in range(10)]
    for i in range(1,n+1):
        s=str(i)
        count[int(s[0])][int(s[-1])]+=1

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


resolve()

Solution (Python):
def calc(left, right, N):
    def left_match(x):
        return int(str(x)[0]) == left
    def right_match(x):
        return x % 10 == right
    cnt = 0
    n = 1
    while n <= N:
        if not left_match(n):
            length = len(str(n))
            n += 10**(length - 1)
        elif not right_match(n):
            n += 1
        else:
            cnt += 1
            n += 1
    return cnt


def main():
    N = int(input())
    counter = dict()
    for left in range(1, 10):
        for right in range(1, 10):
            counter[(left, right)] = calc(left, right, N)
    answer = 0
    for left in range(1, 10):
        for right in range(1, 10):
            answer += counter[(left, right)] * counter[(right, left)]
    print(answer)


if __name__ == '__main__':
    main()

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

for i in range(1, N+1):
    lon = len(str(i)) - 1
    a = i % 10
    b = i // (10 ** lon)
    Num[a][b] += 1

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

print(ans)

Solution (Python):
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N = int(input())
    presuf = [[0] * 10 for _ in range(10)]
    for i in range(1,N + 1):
        pre = int(str(i)[0])
        suf = i%10
        presuf[pre][suf] += 1
    
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += presuf[i][j]*presuf[j][i]
    print(ans)
if __name__ == '__main__':
    main()

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

Solution (Python):
def main(n):
  def c(i,j):
    ret=0
    for k in range(1,n+1):
      if str(k)[0]==str(i) and str(k)[-1]==str(j):
        ret+=1
    return ret
  ans=0
  for a in range(1,10):
    for b in range(a,10):
      d=c(a,b)*c(b,a)
      ans+=d
      if a!=b:
        ans+=d
  print(ans)
if __name__=='__main__':
  n=int(input())
  main(n)

Solution (Python):
N = int(input())
a = [[0] * 9 for i in range(1, 10)]
b = [[0] * 9 for i in range(1, 10)]
sum_a = 0
for A in range(1,N+1):
    list_a = str(A)
    if list_a[-1] == '0':
        continue
    a[int(list_a[0])-1][int(list_a[-1])-1] += 1
    b[int(list_a[-1])-1][int(list_a[0])-1] += 1
for i in range(9):
    for j in range(9):
        sum_a += a[i][j]*b[i][j]
print(sum_a)

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

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

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

print(ans)

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

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

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

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]





N = ii()

table = dp2(0, 10, 10)

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

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

print(cnt)

Solution (Python):
"""
参考
blog名  あっとのTECH LOG   2020-01-24 
url     https://at274.hatenablog.com/entry/2020/01/24/060000
id:     AT274
"""


N = int(input())
 

import numpy as np
cnt = [[0] * 10 for i in range(10)]

for i in range(1,N+1):
    n_left =str(i)[0]   
    n_right =str(i)[-1] 
    
    cnt[int(n_left)][int(n_right)] +=1

count =np.array(cnt,dtype=np.int64)
count_T_copy =count.T.copy()
ans =np.sum(count *count_T_copy)
print(ans)

Solution (Python):
"""
参考
blog名  あっとのTECH LOG   2020-01-24 
url     https://at274.hatenablog.com/entry/2020/01/24/060000
id:     AT274
"""


N = int(input())
 

import numpy as np
count = np.zeros((10,10),dtype=np.int64) 
for i in range(1,N+1):
    n_left =str(i)[0]   
    n_right =str(i)[-1] 
    count[int(n_left) , int(n_right)] +=1

count_T_copy =count.T.copy()
ans =np.sum(count *count_T_copy)
print(ans)

Solution (Python):
N = int(input())
c = [[0]*10 for _ 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):
N = int(input())


def extract_first(i):
    return str(i)[0]

def extract_last(i):
    return str(i)[-1]

def get_key(first, last):
    return "{}-{}".format(first, last)

combi_map = {}
for i in range(0, 10):
    for j in range(0,10):
        combi_map[get_key(i, j)] = 0


for i in range(1, N+1):
    first = extract_first(i)
    last = extract_last(i)
    combi_map[get_key(first, last)] += 1

ans = 0
for i in range(1, 10):
    for j in range(i, 10):
        if i == j:
            ans += combi_map[get_key(i, j)] * combi_map[get_key(j, i)]
        else:
            ans += combi_map[get_key(i, j)] * combi_map[get_key(j, i)] * 2
print(ans)

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

digit = {}
for i in range(10):
    for j in range(10):
        digit[i,j]=0
for i in range(1,n+1):
    top = int(str(i)[0])
    last = int(str(i)[-1])
    digit[top, last]+=1
ans = 0
for i in range(10):
    for j in range(10):
        ans += digit[i,j]*digit[j,i]
print(ans)

Solution (Python):
N=int(input())
A=[[0 for n in range(9)] for n in range(9)]

for n in range(N+1):
    head=int(str(n)[0])
    tail=n%10
    if head!=0 and tail!=0:
        A[head-1][tail-1]+=1

count=0

for i in range(9):
    for j in range(9):
        count+=A[i][j]*A[j][i]

print(count)

Solution (Python):
from collections import* 
n=int(input())

d=defaultdict(int)

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

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

Solution (Python):
def input_multiple_number():
    return map(int, input().split())

N = int(input())


memo = [[-1]*10 for i in range(10)]
def getCount(head,tail):
    if memo[head][tail] != -1:
        return memo[head][tail]

    if head == 0 or tail == 0:
        memo[head][tail] = 0
        return 0

    cnt = 0

    
    if head == tail and head <= N:
        cnt += 1

    
    if head * 10 + tail <= N:
        cnt += 1 

    for i in range(1,7):
        for j in range(10**i):
            if int(str(head)+str(j).zfill(i)+str(tail))<= N:
                cnt += 1
            else:
                memo[head][tail] = cnt
                return cnt
    return cnt
def getHead(n):
    while n >= 10:
        n //= 10
    return n
def getTail(n):
    return n%10

ans = 0
for i in range(1,N+1):
    ans += getCount(getTail(i),getHead(i))
print(ans)

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

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

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())
s = [[0 for i in range(9)]for i in range(9)]
for k in range(1,N+1):
    if k % 10 == 0:
        continue
    a = list(str(k))
    s[int(a[0])-1][int(a[len(a)-1])-1] += 1
p = 0
for i in range(1,10):
    for j in range(1,10):
        p = p + s[i-1][j-1]*s[j-1][i-1]
print(p)

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

















N = int(input())
d = [[0]*9 for _ in range(9)]
for i in range(1,N+1):
    if i < 10:
        thou = i
        on = i
    elif i < 100:
        thou = i // 10
        on = i % 10
    elif i < 1000:
        thou = i // 100
        on = i % 10
    elif i < 10000:
        thou = i //1000
        on = i % 10
    elif i < 100000:
        thou = i //10000
        on = i % 10
    else:
        thou = i // 100000
        on = i % 10
    if on != 0:
        d[thou-1][on-1] += 1
s = 0
for p in range(9):
    for q in range(p,9):
        if p == q:
            s += d[p][q]*d[p][q]
        else:
            s += 2*d[p][q]*d[q][p]
print(s)

Solution (Python):


import sys



















def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    n = ii()
    d = [[0] * 10 for _ in range(10)]
    for i in range(1, n+1):
        a_str = str(i)
        d[int(a_str[0])][int(a_str[-1])] += 1
    cnt = 0
    for i in range(1, n+1):
        b_str = str(i)
        cnt += d[int(b_str[-1])][int(b_str[0])]
    print(cnt)
        


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = [0] * 100
count = 0
for i in range(1, n + 1):
    x = i%10
    y = int(str(i)[0])
    a[10*y + x] += 1
for i in range(1, n + 1):
    x = i%10
    y = int(str(i)[0])
    count += a[10*x + y]
print(count)

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

Solution (Python):

import sys


def solve(N: int):
    l = len(str(N))
    k = {}
    for i in range(1,10):
        for j in range(1,10):
            k[str(i)+str(j)] = 0
    for x in range(1, N+1):
        s = str(x)
        if s[-1] != "0":
            k[s[0]+s[-1]] += 1

    k_new = {}
    for i in range(1,10):
        k_new[str(i)+str(i)] = k[str(i)+str(i)]**2
        for j in range(i+1,10):
            n = k[str(i)+str(j)] * k[str(j)+str(i)]
            k_new[str(i)+str(j)] = n * 2

    
    print(sum([t for t in k_new.values()]))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    solve(N)

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

N = int(input())

MAP = np.zeros((9,9))
for i in range(1,N+1):
    a = i%10
    b = int(str(i)[0])
    if a != 0 and b != 0:
        MAP[a-1,b-1] += 1

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

print(int(cnt))

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





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

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

total = 0

for n in range(1, 10):
  for m in range(1, 10):
    total += table[n][m] * table[m][n]
    
print(total)

Solution (Python):
N = int(input())
num = [[0] * 9 for _ in range(9)]
for i in range(9):
    for j in range(9):
        for n in range(N + 1):
            n = str(n)
            if str(i + 1) == n[0] and str(j + 1) == n[-1]:
                num[i][j] += 1
s = 0
for i in range(9):
    for j in range(9):
        s += num[i][j] * num[j][i]
print(s)

Solution (Python):




n = int(input())

if n <= 9:
    print(n)
    exit()




cij = [[0 for _ in range(10)] for _ in range(10)]

for k in range(1, n + 1):
    k0 = int(str(k)[0])
    kn = int(str(k)[-1])
    if k0 != 0 and kn != 0:
        cij[k0][kn] += 1

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

print(ans)

Solution (Python):
N=int(input())
K=[[0 for i in range(9)] for j in range(9)]

for i in range(1,N+1):
  if int(str(i)[-1])==0:
    pass
  else:
    h=int(str(i)[0])
    t=int(str(i)[-1])
    K[h-1][t-1] += 1


ans=0
for i in range(9):
  for j in range(i+1):
    if i==j:
      ans += K[i][j]* K[j][i]
    else:
      ans += 2*K[i][j]* K[j][i]
      
      
print(ans)

Solution (Python):
from collections import deque
import numpy as np
stack = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
n = int(input())

c = np.zeros([10, 10])

for i in range(1, n+1):
    str_i = str(i)
    head = int(str_i[0])
    tail = int(str_i[-1])
    c[head, tail] += 1
count = 0
for i in range(1, 10):
    for j in range(1, 10):
        count += c[i, j]*c[j, i]
print(int(count))

Solution (Python):
N = int(input())
counts = [[0]*10 for i in range(10)]
top = 1
for i in range(1,N+1):
    if i == top*10:
        top *= 10
    a_top = i//top
    a_bottom = i%10
    
    counts[a_top][a_bottom] +=1


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

print(sums)

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

Solution (Python):
def main():
    n = int(input())
    import numpy as np
    ans = np.zeros((10,10))
    for i in range(1,n+1):
        h,t = str(i)[0],str(i)[-1]
        ans[int(h)-1][int(t)-1]+=1
    s = 0
    for i in range(10):
        for j in range(10):
            s += ans[i][j]*ans[j][i]

    print(int(s))
if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict
dd = defaultdict(int)
n=int(input())
for i in range(1, n+1):
    dd[(str(i)[0],str(i)[-1])]+=1
k=0
for i in range(1, 10):
    for j in range(1, 10):
        k+=dd[str(i), str(j)]*dd[str(j), str(i)]
print(k)

Solution (Python):

N = int(input()) 





import math
N_digits = int(math.log10(N)//1 + 1) 


safe_number = [1,2,3,4,5,6,7,8,9]
ht_comb_list = dict()

def get_OK_comb_number(head_number,tail_number):
    
    if head_number == tail_number and head_number <= N:
        ht_comb = 1
    else:
        ht_comb = 0

    
    if head_number*10 + tail_number <= N:
        ht_comb += 1

        
        base_number = 2

        while base_number+1 <= N_digits:
            max_number = head_number*10**(base_number)  + 10**(base_number) -10 + tail_number
            
            if max_number <= N:
                
                ht_comb += 10**(base_number-1)
            else:
                last_digit = N - ((N//10) * 10)
                max_number = N
                max_number -= last_digit
                max_number += tail_number
                if max_number > N:
                    max_number-= 10

                min_number = head_number*10**(base_number) + tail_number
                
                

                if max_number >= min_number:
                    ht_comb += 1+(max_number-min_number)//10
            
                break

            base_number += 1

    
    return ht_comb


for head_number in safe_number:
    for tail_number in safe_number:
        ht_comb_list[(head_number,tail_number)] = get_OK_comb_number(head_number,tail_number)       

ans = 0

for head_number in safe_number:
    for tail_number in safe_number:
        
        ans += ht_comb_list[(head_number,tail_number)] * ht_comb_list[(tail_number,head_number)]
    

print(ans)

Solution (Python):
n = int(input())
a= [[0]*9 for _ in range(9)]
ans = 0
for i in range(1,n+1):
    if (i%10==0):
        continue
    p = str(i)
    front = int(p[0])
    back = int(p[-1])
    a[front-1][back-1] += 1
for i in range(1,10):
    for j in range(1,10):
        ans += a[i-1][j-1]*a[j-1][i-1]
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):
    l = list(str(i))
    a[int(l[0])][int(l[-1])] += 1
ans = 0
for i in range(1,10):
    for j in range(i,10):
        if i == j:
            ans += a[i][j]**2
        else:
            ans += a[i][j] * a[j][i] * 2
print(ans)

Solution (Python):
import sys
import math  
import bisect  
import queue  


def input():
    return sys.stdin.readline().rstrip()


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

    C = [[0 for _ in range(0, 10)] for _ in range(0, 10)]
    for k in range(1, N + 1):
        k = str(k)
        head_k = int(k[0])
        tail_k = int(k[-1])
        C[head_k][tail_k] += 1

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

    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
N = int(s)
l = len(s)
first = int(s[0])
last = int(s[-1])
if last == 0:
    N -= 1
    s = str(N)
    l = len(s)
    first = int(s[0])
    last = int(s[-1])
if N < 10:
    print(N)
else:
    if l > 2:
        middle = int(s[1:-1])
    else:
        middle = 0
    k1 = 0
    for i in range(l - 2):
        k1 += 10 ** i
    k2 = k1
    k1 += 10 ** (l - 2)

    li = [[0 for j in range(9)] for i in range(9)]
    for i in range(9):
        for j in range(9):
            if i + 1 < first:
                li[i][j] = k1
            elif i + 1 == first:
                li[i][j] = middle + k2
                if j + 1 <= last:
                    li[i][j] += 1
            else:
                li[i][j] = k2
            if i == j:
                li[i][j] += 1
    ans = 0
    for i in range(9):
        for j in range(9):
            ans += li[i][j] * li[j][i]
    print(ans)

Solution (Python):
from collections import defaultdict
a = defaultdict(int)
n = int(input())
for i in range(1, n + 1):
    s = str(i)
    a[(s[0], s[-1])] += 1
rslt = 0
for i in range(1, 10):
    for j in range(1, 10):
        rslt += a[(str(i), str(j))] * a[(str(j), str(i))]
print(rslt)

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

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

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

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

print(ans)

Solution (Python):
import numpy as np
n = int(input())
rec = np.zeros((10, 10))

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

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

print(int(ans))

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

cnt = 0
counter1 = Counter()

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

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

print(cnt)

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

Solution (Python):
N = input()
ans = 0
def counths(n,i,j):
    s = int(n)
    bot = sum([10**x for x in range(len(n)-2)])
    if int(n[0]) > i:
        top = 10**(len(n)-2)
    elif int(n[0]) < i:
        top = 0
    else:
        a = i * 10 ** (len(n)-1) + j
        if a < s:
            top = (s-a) // 10 + 1
        elif a == s:
            top = 1
        else:
            top = 0
    if i == j:
        if len(n) > 1:
            bot += 1
        else:
            if s >= i:
                bot += 1
    return bot + top
for i in range(1,10):
    for j in range(1,10):
        ans += counths(N,i,j) * counths(N,j,i)
print(ans)

Solution (Python):
import math
N = input()
ans = 0
def counths(n,i,j):
    s = int(n)
    bot = sum([10**x for x in range(len(n)-2)])
    if int(n[0]) > i:
        top = 10**(len(n)-2)
    elif int(n[0]) < i:
        top = 0
    else:
        a = i * 10 ** (len(n)-1) + j
        if a < s:
            top = (s-a) // 10 + 1
        elif a == s:
            top = 1
        else:
            top = 0
    if i == j:
        if len(n) > 1:
            bot += 1
        else:
            if s >= i:
                bot += 1
    return bot + top
for i in range(1,10):
    for j in range(1,10):
        ans += counths(N,i,j) * counths(N,j,i)
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])][int(str(i)[-1])]+=1
c=0
for i in range(10):
  for j in range(10):
    c+=l[i][j]*l[j][i]
print(c)

Solution (Python):
N = int(input())
c = []
for i in range(10):
    c.append([0] * 10)
for i in range(N):
    s = str(i+1)
    i1 = int(s[0])
    i2 = int(s[-1])
    c[i1][i2] += 1
total = 0
for i in range(10):
    for j in range(10):
        total += c[i][j] * c[j][i]
print(total)