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



Sample input:
25


Sample output:
17

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def sigma2(n,i,j):
    if i==j and n<i:
        ncnt= 0
    elif i==j and i<=n:
        ncnt=1
    else:
        ncnt=0    

    if i*10+j<=n:
        ncnt=ncnt+1
        
    mcnt=-1
    nn=0
    for kt in range(1,6,1):
       for kt2 in range(10**kt):
           mcnt=mcnt+1
           nn=i*10**(kt+1)+kt2*10+j
           if nn>n:
               break
       if nn>n:
           break
               
    return mcnt+ncnt                


n=int(input())
ncnt=0
for k in range(1,10,1):
    for m in range(1,10,1):
        s1=sigma2(n,k,m)
        s2=sigma2(n,m,k)
        ncnt=ncnt+s1*s2
#        if s1*s2>0:
#            print(k,m,s1,s2)
print(ncnt)

Submission status: Accepted

Solution code:
N = int(input())
l = [[0] * 10 for i in range(10)]
ans = 0
for i in range(1, N+1):
  if str(i)[-1] == "0":
    continue
  else:
    l[int(str(i)[0])][int(str(i)[-1])] += 1
for i in range(10):
  for j in range(10):
    ans += l[i][j] * l[j][i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import math
INF = float("inf")


def solve(N: int):
    table = [[0]*10 for _ in range(10)]

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

    count = 0
    for i in range(10):
        for j in range(10):
            count += table[i][j]*table[j][i]
    print(count)
    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))  # type: int
    solve(N)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
r = range(1, min(9, n) + 1)
a = [None]
for i in r:
  a.append([None])
  for j in r:
    a[i].append(i == j)
    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))

Submission status: Accepted

Solution code:
n = int(input())
r = range(1, min(9, n) + 1)
a = [None] + [[None] * 10 for _ in r]
for i in r:
  for j in r:
    a[i][j] = i == j
    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))

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int)
n = int(input())

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

ans = 0
for i, j in list(d):
    c = d[(i,j)]
    f = d[(j,i)]
    ans += int(d[(i,j)]) * int(d[(j,i)])
print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

n = 100
a = 0

while N > n:
    a += n//100
    n *= 10

n //= 10

x = [[a]*9 for _ in range(9)]

b = N // n
for i in range(b-1):
    for j in range(9):
        x[i][j] += n//10

for i in range(N%n+1):
    c = i % 10
    if c != 0:
        x[b-1][c-1] += 1

for i in range(min(9, N)):
    x[i][i] += 1

cnt = 0

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

print(cnt)

Submission status: Accepted

Solution code:
import copy
from collections import defaultdict

n = int(input())

count = defaultdict(int)

# (先頭, 末尾) = (h, t) となるモノの数を数える
for i in range(1, n + 1):
    h = int(str(i)[0])
    t = int(str(i)[-1])
    count[(h, t)] += 1

copied_count = copy.deepcopy(count)
ans = 0
for (h, t), v in count.items():
    ans += v * copied_count[(t, h)]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[[0]*10 for i in range(10)]
for i in range(1,n+1):
    a[int(str(i)[0])][i%10]+=1
def N(x,y): # no of x...y < n
    return a[x][y]

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

Submission status: Accepted

Solution code:
from functools import lru_cache

@lru_cache(maxsize=10**6)
def N(x,y,n): # no of x...y < n
    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

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

Submission status: Accepted

Solution code:
def N(x,y,n): # no of x...y < n
    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

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from collections import defaultdict

def main():
  count = defaultdict(lambda :defaultdict(int))
  n = int(input())
  for i in range(1, n+1):
    count[int(str(i)[0])][int(str(i)[-1])] += 1

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

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
def main3(N):
    lst = list()
    for i in range(1, 10):
        lst.append([0]*9)

    for n in range(1, N+1):
        head = int(str(n)[0])
        tail = int(str(n)[-1])
        #末尾が0はNG
        if tail != 0:
            lst[head-1][tail-1] += 1
        
    #for i in range(0, len(lst)):
    #    print(lst[i])
    
    count = 0
    for i in range(0, len(lst)):
        for j in range(0, len(lst[i])):
            count += lst[i][j] * lst[j][i]

    print(count)

N = int(input())
main3(N)

Submission status: Accepted

Solution code:
N = int(input())

k = {i: 0 for i in range(1, 100) if str(i)[-1] != '0'}

for i in range(1, N+1):
    if i < 10:
        k[i] += 1
        zoro = int(str(i)*2)
        if zoro <= N:
            k[zoro] += 1
    else:
        _i = str(i)
        first = int(_i[0])
        last  = int(_i[-1])
        if last != 0:
            handstand = int(str(last)+str(first))
            if first == last:
                k[first] += 1
                k[int(str(first)*2)] += 1
            elif handstand <= N:
                k[handstand] += 1

ans = 0
for i in range(1, N+1):
    _i = str(i)
    first = int(_i[0])
    last  = int(_i[-1])
    if i < 10:
        ans += k[i]
    elif last != 0:
        ans += k[int(str(first)+str(last))]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

memo = [[0] * 10 for _ in range(10)]
for i in range(1, N + 1):
    S = str(i)
    a, b = int(S[0]), int(S[-1])
    memo[a][b] += 1

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

print(ans)

Submission status: Accepted

Solution code:
A = [[0 for i in range(10)] for j in range(10)]
N = int(input())

result = 0
for i in range(1, N+1):
    s = str(i)
    f = int(s[0])
    l = int(s[-1])
    A[f][l] += 1
    # print('i:{} f:{}, l:{}'.format(i, f, l))

# print(A)

result = 0
for i in range(1, 10):
    for j in range(1, 10):
        result += A[i][j] * A[j][i]
        # print('debug i:{}, j:{}'.format(i, j))
        # print(result)

print(result)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n = int(input())
# else以下は10以上の時のみ適応できるので10未満は埋め込み
if n < 10:
    print(n)
else:
    # 説明中のiを求めるため10でわる(切り捨て)
    x = n // 10
    # 公式による計算。x^2+8
    basenum = x * x + 8
    addnum = 0
    # ここ以下で(10*i から n, 1 から n)と(1 から n, 10*i から n)の数え上げを行うが、
    # 重複した数え上げがないか確認するフラグ
    hassame = False
    # 数え上げ(ここでは、(10*i から n, 1 から n)だけ数え上げ、倍にすることで(1 から n, 10*i から n)分も数え上げる)
    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 # 10の倍数の公式分と　力わざで数え上げた加算
    res -= 1 if hassame else 0 # ダブルカウントがあれば1減らす
    print(res)

Submission status: Accepted

Solution code:
n = int(input())
if n < 10:
    print(n)
else:
    x = n // 10
    basenum = x * x + 8
    addnum = 0
    # print("{0} {1}".format(x* 10 + 1, n))
    hassame = False
    for i in range(x * 10 + 1, n + 1):
        # print(i)
        for j in range(1, n + 1):
            # print(j)
            if str(i)[0] == str(j)[-1] and str(i)[-1] == str(j)[0]:
                addnum += 1
                if i == j:
                    hassame = True
                # print("{0},{1}".format(i, j))
    res = basenum + addnum * 2
    if hassame:
        res -= 1
    # print("n={0}, x={1}, basenum={2}, addnum={3} res ={4}".format(n,x,basenum,addnum,res))
    print(res)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

# for k in sorted(c.keys()):
#     print(k, c[k])

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
def N(x,y,n): # no of 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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]
    #print(val)
    #print(cmb(val, 2))
    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(field)
print(total)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]
    #if C[i][j] * C[j][i] > 0:
      #print(i, j)
print(count)

Submission status: Accepted

Solution code:
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(slist)
print(ans)

Submission status: Accepted

Solution code:
#n = int(input())
#L = list(map(int, input().split()))
#import math
#import sys
#import heapq
#import fractions
#import bisect
#import numpy
#import queue
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())

Submission status: Accepted

Solution code:
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)