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())
tbl=[[0 for j in range(10)] for i in range(10)]
for i in range(1,n+1):
  s=str(i)
  h=int(s[0])
  b=int(s[-1])
  tbl[h][b]+=1
ans=0
for i in range(10):
  for j in range(10):
    ans+=tbl[i][j]*tbl[j][i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1, n+1):
  i_ = str(i)
  x = int(i_[0])
  y = int(i_[-1])
  d[x][y] += 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:
def main():
    N = int(input())
    cnt = 0
    '''
    i => Aの末尾の桁が Bの先頭の桁
    j => Bの末尾の桁が Aの先頭の桁
    '''
    cnt = [[0] * 10 for _ in range(10)]
    for i in range(N+1):
        tmp = str(i)
        first = int(tmp[0])
        last = int(tmp[-1])
        cnt[first][last] += 1
    ans = 0
    for i in range(1,10):
        for j in range(1,10):
            ans += cnt[i][j] * cnt[j][i]

    print(ans)

        







if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

def getFirstLast(n):
    digs = []
    while n:
        digs.append(n % 10)
        n //= 10
    return digs[-1], digs[0]

cnt = [[0 for _ in range(10)] for _ in range(10)]
for val in range(1, n + 1):
    first, last = getFirstLast(val)
    cnt[first][last] += 1
ret = 0
for first in range(1, 10):
    for last in range(1, 10):
        ret += cnt[first][last] * cnt[last][first]
print(ret)

Submission status: Accepted

Solution code:
n=int(input())
cnt=[[0]*10 for i in range(10)] #(10,10)サイズの配列
ret = 0

def hoge(m):
    #return m[-1],m[0]
    return (m%10, int(str(m)[0]))

for k in range(1,n+1):
    a,b = hoge(k)
    cnt[a][b] += 1    

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

print(ret)

Submission status: Accepted

Solution code:
n=int(input())
def kazu(a):
    return (a%10, int(str(a)[0]))
cnt=[[0]*10 for i in range(10)]
for i in range(1,n+1):
    p,q=kazu(i)
    cnt[p][q]+=1
ans=0
for i in range(1,10):
    for j in range(1,10):
        ans+=cnt[i][j]*cnt[j][i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
X=[[0]*10 for i in range(10)]
for i in range(1,n+1):
    t=(int)(str(i)[0])
    b=(int)(str(i)[-1])
    X[t][b]+=1

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

print(ans)

Submission status: Accepted

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

n = int(input())
c = [ [0]*10 for _ in range(10) ]

for x in range(1, n+1):
    beg = int(str(x)[0])
    end = x%10
    c[end][beg] += 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:
n = int(input())

freq = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, n + 1):
    l = int(str(i)[0])
    r = int(str(i)[-1])
    freq[l][r] += 1

cnt = 0
for i in range(1, n + 1):
    r = int(str(i)[0])
    l = int(str(i)[-1])
    cnt += freq[l][r]
print(cnt)

Submission status: Accepted

Solution code:

N = int(input())

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

for i in range(N+1):

    s = str(i)

    lis[int(s[0])][int(s[-1])] += 1

ans = 0

for i in range(9):

    i += 1

    for j in range(9):

        j += 1

        ans += lis[i][j] * lis[j][i]

print (ans)

Submission status: Accepted

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

for i in range(1, N+1):
  istr=str(i)
  x=int(istr[0])
  y=int(istr[-1])
  if y!=0:
    c[x-1][y-1]+=1
    
out=0
for i in range(9):
  for j in range(9):
    out+=c[i][j]*c[j][i]
print(out)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N):
    ans = 0

    d = Counter()
    for i in range(N+1):
        l = len(str(i))
        a = i // 10 ** (l-1)
        b = i % 10
        if a != 0 and b != 0:
            d[(a, b)] += 1

    ans = 0
    for a, b in product(range(1, 10), repeat=2):
        ans += d[(a, b)] * d[(b, a)]
    return ans


def main():
    N = read_int()
    print(slv(N))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
d=[[0]*10 for i in range(10)]
ans=0
for i in range(1,n+1):
    a=i%10
    if a!=0:
        t=i
        while t>9:
            t//=10
        b=t
        d[b][a]+=1
for i in range(10):
    for j in range(10):
        ans+=d[i][j]*d[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

from collections import defaultdict

n = ni()

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

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = [list(0 for i in range(9)) for _ in range(9)]

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

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

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
d = {}
for i in range(1, n+1):
    c = str(i)
    cr = c[0]
    cl = c[-1]
    key = cr + cl
    if key in d:
        d[key] += 1
    else:
        d[key] = 1
ans = 0
for key in d:
    key_r = key[-1] + key[0]
    if key_r in d:
        ans += d[key] * d[key_r]
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

from collections import defaultdict


N = int(read().rstrip())

counter = defaultdict(int)  # 関数でdefault値を設定する

for x in range(1, N+1):
    first = str(x)[0]
    last = str(x)[-1]
    counter[(first, last)] += 1

ans = 0
items = list(counter.items())  # ループ内で存在しない可能性のある値にアクセスするので、list化する 
for (first, last), cnt in items:
    ans += cnt * counter[(last, first)]

print(ans)

Submission status: Accepted

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

for i in range(N):
    si = str(i+1)
    f,l = int(si[0]),int(si[len(si)-1])
    C[f][l] += 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:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int):
    def combination(n, r, mod=10**9+7):
        n1, r = n+1, min(r, n-r)
        numer = denom = 1
        for i in range(1, r+1):
            numer = numer * (n1-i) % mod
            denom = denom * i % mod
        return numer * pow(denom, mod-2, mod) % mod
    
    
    from collections import defaultdict
    d = defaultdict(int)
    for i in range(1,N+1):
        s = str(i)
        d[int(s[0]+s[-1])] += 1

    ans = 0


    # for k, v in d.items():
    #     ans +=  combination(v, 2)
    for i in range(10):
        for j in range(10):
            ans += d[int(str(i)+str(j))]*d[int(str(j)+str(i))]

    print(ans)

    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:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int):
    def combination(n, r, mod=10**9+7):
        n1, r = n+1, min(r, n-r)
        numer = denom = 1
        for i in range(1, r+1):
            numer = numer * (n1-i) % mod
            denom = denom * i % mod
        return numer * pow(denom, mod-2, mod) % mod
    
    
    from collections import defaultdict
    d = defaultdict(int)
    for i in range(N+1):
        s = str(i)
        d[int(s[0]+s[-1])] += 1

    ans = 0


    # for k, v in d.items():
    #     ans +=  combination(v, 2)
    for i in range(10):
        for j in range(10):
            ans += d[int(str(i)+str(j))]*d[int(str(j)+str(i))]

    print(ans-1)

    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())
d = 1
c = 0

for x in range(1, n + 1):
  l = x % 10
  
  if l == 0:
    continue
    
  r = x // d
  k = 10
    
  if l == r:
    c += 1
  
  while k <= n:
    y = l * k + r
    z = n - y
    
    if k <= z:
      c += k // 10
    elif 0 <= z:
      a  = z % k // 10 + 1
      c += a if a != 0 else 1
      
    k *= 10
  
  if       9 == x: d =     10
  elif    99 == x: d =    100
  elif   999 == x: d =   1000
  elif  9999 == x: d =  10000
  elif 99999 == x: d = 100000

print(c)

Submission status: Accepted

Solution code:
n=int(input())
count=[[0 for i in range(10)]for i in range (10)]
ans=0
for i in range(1,(n+1)):
    s=str(i)
    h=int(s[0])
    t=int(s[len(s)-1])
    count[h][t]+=1
for i in range(1,(n+1)):
    s=str(i)
    h=int(s[0])
    t=int(s[len(s)-1])
    ans+=count[t][h]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
count=[[0 for i in range(10)]for i in range (10)]
ans=0
for i in range(1,(n+1)):
    s=str(i)
    h=int(s[0])
    t=int(s[len(s)-1])
    count[h][t]+=1
for i in range(len(count)):
    for j in range(len(count)):
        ans+=count[i][j]*count[j][i]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

A = np.zeros((10,10))
 
for i in range(1, N+1):
    
    A[int(str(i)[0])][int(str(i)[-1])] += 1 
    
count = 0

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

print(int(count))

Submission status: Accepted

Solution code:
N = int(input())

freq = {}

for i in range(10):
    for j in range(10):
        freq[str(i)+str(j)] = 0
 
for i in range(1, N+1):
    i_str = str(i)

    head_str = i_str[0]
    tail_str = i_str[-1]
    freq[head_str+tail_str] += 1

ans = 0

for i in range(1, N+1):
    i_str = str(i)

    head_str = i_str[0]
    tail_str = i_str[-1]
    ans += freq[tail_str+head_str] 

print(ans)

Submission status: Accepted

Solution code:
def mainFunc():
    N = int(input())
    matrix = [[0 for i in range(10)] for i in range(10)]
    for i in range(1, N + 1):
        start = int(str(i)[0])
        end = int(str(i)[-1])
        matrix[start - 1][end - 1] += 1
    
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += matrix[i][j] * matrix[j][i]

    print(ans)


mainFunc()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
c = np.array([[0 for x in range(10)] for y in range(10)])
buf_1 = []
buf_2 = 0

def func(A) :
    A = str(A)
    return [int(A[0]),int(A[len(A)-1])]

for i in range(1,N+1) :
    buf_1 = func(i)
    #print(buf_1)
    c[buf_1[0],buf_1[1]] += 1

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

print(buf_2)
#print(c)

Submission status: Accepted

Solution code:
N = input()
m = len(N)

a0 = int(N[0])
a1 = int(N[-1])

ans = 0
if m == 1:
  ans = int(N)
else:
  mid = (int(N[1:]) - a1)//10
  t = 10**(m-2)
  ans = 9
  ans += ((a0-1)*t) * 2
  ans += (mid + (a1>=a0)) * 2
  ans += ((a0-1)*t)**2
  ans += ((mid*(a0-1) + min(a0-1, a1))*t) * 2
  ans += (mid + (a1>=a0))**2
  if m >= 3:
    ans += (t - 1) * 2
    ans += ((a0-1)*(t - 1)*t) * 2
    ans += (mid * (t - 1)) * 2
    ans += (a1 * (t - 1)//9) * 2
    ans += (t - 1)**2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
import itertools
for i in range(1,n+1):
    p,q=str(i)[0],str(i)[-1]
    if int(p)*int(q)==0:
        continue
    if len(str(i))>=3:
        if p<q:
            ans+=int("1"*(len(str(i))-2))*2
        elif p>q:
            ans+=int("1"*(len(str(i))-1))*2
        else:
            ans+=int("1"*(len(str(i))-2))*2
            ans+=(int(str(i)[1:-1])+1)*2
            ans+=1
    elif len(str(i))==1:
        ans+=1
    else:
        if p>q:
            ans+=2
        elif p==q:
            ans+=3
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
cnt = [[0] * 10 for _ in range(10)]
for i in range(1, N+1):
    d = int(math.log(i, 10))
    cnt[i//(10**d)][i % 10] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += cnt[i][j] * cnt[j][i]

print(ans)

Submission status: Accepted

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

def get_ab(n):
    str_n = str(n)
    return map(int, [str_n[0], str_n[-1]])

for i in range(1, N + 1):
    a, b = get_ab(i)
    C[a][b] += 1

# print(C)
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:
N = int(input())
mem=[[0] * 10 for _ in range(10)]

for i in range(1, N+1):
    s = str(i)
    mem[int(s[0])][int(s[-1])] += 1 
    
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += mem[i][j] * mem[j][i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
from collections import defaultdict
d=defaultdict(int)
for i in range(1,N+1):
    a,b=str(i)[0],str(i)[-1]
    d[(int(a),int(b))]+=1
r = 0
for i in range(1,10):
    for j in range(1,10):
        r += d[(i,j)] * d[(j,i)]
print(r)

Submission status: Accepted

Solution code:
N = int(input())

from collections import defaultdict

counter = defaultdict(int)

for x in map(str, range(1, N+1)):
    head = x[0]
    tail = x[-1]
    counter[(head, tail)] += 1

ans = 0
items = list(counter.items())
for (head, tail), cnt in items:
    ans += cnt * counter[(tail, head)]

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
tb = np.zeros((10, 10), dtype=int)

for n in range(1, N+1):
    n = str(n)
    lhs, rhs = int(n[0]), int(n[-1])
    tb[lhs][rhs] += 1


ret = 0
for lhs in range(1, 10):
    for rhs in range(1, 10):
       ret += tb[lhs][rhs] * tb[rhs][lhs]

print(ret)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    count = 0
    m = dict()
    for i in range(1, n + 1):
        tmp = str(i)
        t = (tmp[0], tmp[-1])
        if t in m:
            m[t] += 1
        else:
            m[t] = 1
    for i in range(1, n + 1):
        tmp = str(i)
        t = (tmp[-1], tmp[0])
        if t in m:
            count += m[t]
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
counter = defaultdict(int)
for x in map(str, range(1, n+1)):
  head = x[0]
  tail = x[-1]
  counter[(head, tail)] += 1
ans = 0
items = list(counter.items())
for (head, tail), cnt in items:
  ans += cnt * counter[(tail, head)]
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()

# 二次元配列，他の要素まで書き変わらないように静的確保，意識
# c = [[0] * 10] * 10
c = [[0 for i in range(10)] for j in range(10)]


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

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


# exit()

# dic = defaultdict(int)
# for i in range(1, n+1):
#     dic["{},{}".format(str(i)[0], str(i)[-1])] += 1

# # print(dic)

# ans = 0
# for n in dic.copy():
#     s, e = n.split(",")
#     # print(s, e)
#     # if s == e:
#     #     ans += dic[n] * dic[n]
#     # else:
#     ans += dic[n] * dic["{},{}".format(e, s)]
# print(ans)

Submission status: Accepted

Solution code:
n = int(input())

l = [0 for _ in range(81)]
for k in range(1, n + 1):
    i = int(str(k)[0])
    j = int(str(k)[-1])
    if i != 0 and j != 0:
        idx = 9 * (i - 1) + (j - 1)
        l[idx] += 1
    # print("k = " + str(k))
    # print(i, j, idx)
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        idx = 9 * (i - 1) + (j - 1)
        idx2 = 9 * (j - 1) + (i - 1)
        ans += l[idx] * l[idx2]
        # print("(" + str(i) + str(j) + ") * (" + str(j) + str(i) + ") = " + str(l[idx]) + "*" +  str(l[idx2]) + " = " +  str(l[idx] * l[idx2]))
# print(l)
print(ans)

Submission status: Accepted

Solution code:
def get_pair(x):
    s = int(str(x)[0])
    e = x % 10
    return s, e


N = int(input())
freq = {}
for i in range(1, N+1):
    s, e = get_pair(i)
    freq[(s, e)] = freq.get((s, e), 0) + 1
ans = 0
for s, e in freq:
    ans += freq.get((s, e), 0) * freq.get((e, s), 0)
print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    
    d = defaultdict(int)
    for i in range(1,N+1):
        bot,top = str(i)[0],str(i)[-1]
        d[bot,top] += 1
    
    ans = 0
    for i in range(1,N+1):
        top,bot = str(i)[0],str(i)[-1]
        ans += d[bot,top]    
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

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

print(count)

Submission status: Accepted

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

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

c[0][0]
for x in range(1,N+1):
    X=str(x)
    c[int(X[0])][int(X[-1])]+=1

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

Submission status: Accepted

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

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

    for n in range(1, N + 1):
        str_n = str(n)
        top = int(str_n[0])
        bottom = int(str_n[-1])

        c[top][bottom] += 1

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

    print(ans)


main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['No', 'Yes']
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


show_flg = True
show_flg = False


def cmb(n, r):
    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


def main():
    N = I()
    NS = str(N)
    ans = 0

    dic = {}

    for j in range(1, 10):
        for k in range(1, 10):
            if j != k:
                dic[10 * j + k] = 0
            else:
                dic[j] = 0

    for i in range(1, N+1):
        if i < 10:
            dic[i] += 1
        else:
            b = int(str(i)[0])
            e = int(str(i)[-1])
            if b == e:
                dic[b] += 1
            elif e != 0:
                dic[10 * b + e] += 1

    # print(dic)
    for j in range(0, 10):
        for k in range(0, 10):
            if 10 * j + k in dic:
                if 10 * j + k < 10:
                    ans += dic[10 * j + k] * dic[10 * j + k]
                else:
                    ans += dic[10 * j + k] * dic[10 * k + j]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
l=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
  s = str(i)
  l[int(s[0])][int(s[-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:
# 参考文献:tonnnura172
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()

dic = defaultdict(int)
for i in range(1, n+1):
    dic["{},{}".format(str(i)[0], str(i)[-1])] += 1

# print(dic)

ans = 0
for n in dic.copy():
    s, e = n.split(",")
    # print(s, e)
    # if s == e:
    #     ans += dic[n] * dic[n]
    # else:
    ans += dic[n] * dic["{},{}".format(e, s)]
print(ans)        

exit()
c = [[0] * 10] * 10

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

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

Submission status: Accepted

Solution code:
def bunkai(n:int):
    ls=list(str(n))
    top,bottom=int(ls[0]),int(ls[-1])
    if n<=99:
        middle=0
    else:
        middle=int("".join(ls[1:-1]))
    return top, middle, bottom

n=int(input())
Nt,Nm,Nb = bunkai(n)

count=0
for i in range(1,n+1):
    t,m,b = bunkai(i)
    if b==0:continue
    if t==b:count+=1
    if n<=99:
        if int(str(b)+str(t))<=n:count+=1
    else:
        keta = len(str(n))
        for x in range(keta-2):
            count+=10**x
        x+=1
        if Nt>b:
            count+=10**x
        elif Nt==b:
            if Nb>=t:
                count+=Nm+1
            else:
                count+=Nm
print(count)

Submission status: Accepted

Solution code:
N = int(input())
c = [[0] * 10 for _ in range(10)]
for i in range(1, N+1):
	c[int(str(i)[0])][int(str(i)[-1])] += 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:
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 c in range(1,10):
    for d in range(1,10):
        nA = count[c][d]
        nB = count[d][c]
        ans += nA * nB
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
C=[[0]*9  for i in range(9)]
count=0

for n in range(1,N+1):
   if str(n)[-1]!="0":
      left=int(str(n)[0])-1
      right=int(str(n)[-1])-1
      C[left][right]+=1
      
for i in range(1,10):
   for j in range(1,10):
      count+=C[i-1][j-1]*C[j-1][i-1]

print(count)

Submission status: Accepted

Solution code:
N = int(input())

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

pc = dict()
for i in range(N):
    p = td(i + 1)
    if p in pc:
        pc[p] += 1
    else:
        pc[p] = 1
ans = 0
for i in range(N):
    p = td(i + 1)
    q = (p[1], p[0])
    if q in pc:
        ans += pc[q]
print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())

# A/=Bの場合
# A>Bの数を数える

# 与えられたAについて，(A,B)が問題の条件を満たすB<Aの数を返す
def calc(A):
    A_str = str(A)
    # 桁数
    m = len(A_str)    
    # 先頭
    a = int(A_str[0])
    # 末尾
    b = int(A_str[-1])

    # # a<bなら存在しない
    # if a < b:
    #     return 0
    if b == 0:
        # 末尾が0なら存在しない
        return 0
    elif m==1:
        # 1桁ならなし
        return 0
    elif m==2:
        # 2桁なら
        if a < b:
            return 0
        else:
            return 1
    else:        
        # 3桁以上なら，
        if a < b:
            # i=0..m-3桁について，10**i個を足す
            X = 0
            for i in range(0,m-2):
                X += 10**i

            return X
            
        elif a==b:
            # 全体がm桁になるBの数は，間のm-2桁を00..0~(Bの間の数-1)で埋めた数
            X = int(A_str[1:-1])
            # i=0..m-3桁について，10**i個を足す
            for i in range(0,m-2):
                X += 10**i

            # 1桁の数字aも
            X = X + 1
            return X
        else:
            # 全体がm桁になるBの数は，間のm-2桁を00..0~99..9で埋めた数 = sum_i=0^{m-2} 10**i
            X = 0
            for i in range(0,m-1):
                X += 10**i
                
            return X

# 与えられたAについて，(A,B)が問題の条件を満たすB=Aの数を返す
def calc2(A):
    A_str = str(A)
    # 桁数
    m = len(A_str)    
    # 先頭
    a = int(A_str[0])
    # 末尾
    b = int(A_str[-1])

    if m==1:
        return 1
    else:
        if a==b:
            return 1
        else:
            return 0

num1 = 0
num2 = 0
for A in range(1,N+1):
    num1 += calc(A)
    num2 += calc2(A)


print(num1*2 + num2)

Submission status: Accepted

Solution code:
import numpy as np
import math

N = int(input())

C = np.zeros([10,10], dtype = int)
D = np.zeros([10,10], dtype = int)

for n in range(1,N+1):
    k = math.floor(math.log10(n)) + 1
    i = n//10**(k-1)
    j = n%10
    C[i][j] += 1
    D[j][i] += 1

ans = np.sum(C*D)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

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

def f(x):
    a1 = int(str(x)[0])
    a2 = x%10
    return (a1, a2)

count = defaultdict(int)
for x in range(1, n+1):
    key = f(x)
    count[key] += 1

ans = 0
for x in range(1, n+1):
    a0, a1 = f(x)
    if a1 != 0:        # !
        key = (a1, a0) # swap
        ans += count[key]

print(ans)