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())
K = str(N)
l = len(str(N))

ans = 0

def tp(i,j,p):
    tmp = 0
    if p >= 2:
        if l == p:
            if i == int(K[0]):
                if j > int(K[l-1]):
                    tmp = (N-int(K[0])*10**(l-1)-int(K[l-1]))//10
                else:
                    tmp = (N-int(K[0])*10**(l-1)-int(K[l-1]))//10+1
            elif i >= int(K[0]):
                tmp = 0
            else:
                tmp = 10**(p-2)
        elif l > p:
            tmp = 10**(p-2)
        return tmp
    else:
        if i == j and i <= N:
            return 1
        else:
            return 0


for i in range(1,10):
    for j in range(1,10):
        for p in range(1,l+1):
            for q in range(1,l+1):
                ans += tp(i,j,p)*tp(j,i,q)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
if n<=9:
    print(n)
    exit()
dp=[[0]*10 for _ in range(10)]
for k in range(1,n+1):
    s=str(k)
    dp[int(s[0])][int(s[-1])]+=1
ans=0
for i in range(10):
    for j in range(10):
        ans+=dp[i][j]*dp[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N = int(readline())
    dp = [[0] * 10 for _ in range(10)]

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

        ans += 2 * dp[second][first]

        if first == second:
            ans += 1

        dp[first][second] += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

print(count)

Submission status: Accepted

Solution code:
import sys
# import math
# import bisect
# import numpy as np
# from decimal import Decimal
# from numba import njit, i8, u1, b1 #JIT compiler
# from itertools import combinations, product
# from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_ints2(x):   return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)

def f(num):
    front = num
    back = num
    while front >= 10:
        front //= 10
    while back >= 10:
        back %= 10
    
    return front, back

def Main():
    n = read_int()
    cnt = [[0] * 10 for _ in range(10)]
    for x in range(1, n + 1):
        front, back = f(x)
        cnt[front][back] += 1
    
    ans = 0
    for x in range(10):
        for y in range(10):
            ans += cnt[x][y] * cnt[y][x]
    print(ans)

if __name__ == '__main__':
    Main()

Submission status: Accepted

Solution code:
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
Comlist = []
Ans = dict()
Answer = 0
for i in range(1,10):
    for j in range(1,10):
        M = i*10 + j
        Comlist.append(str(M))
        Ans[str(M)] = 0
for k in range(1,N+1):
    strk = str(k)
    if k < 10:
        Tar = strk + strk
        Ans[Tar] += 1
    else:
        Tar = strk[0] + strk[-1]
        if Tar in Ans:
            Ans[Tar] += 1
Answer = 0
for q in range(len(Comlist)):
    p = Comlist[q]
    pin = p[1] + p[0]
    Answer += Ans[p] * Ans[pin]
print(Answer)

Submission status: Accepted

Solution code:
N = int(input())

l = [[0]*10 for i in range(10)]
for i in range(1, N+1):
  x = int(str(i)[0])
  y = int(str(i)[-1])
  l[x][y] += 1
  
ans = 0
for i in range(1, 10):
  for j in range(i, 10):
    if i == j:
      ans += l[i][j] * l[j][i]
    else:
      ans += 2 * l[i][j] * l[j][i]
print(ans)

Submission status: Accepted

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

for n in range(1,N+1):
  i=int(str(n)[0])
  j=int(str(n)[-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:
n = int(input())
a = [[0 for i in range(10)] for j in range(10)]
for i in range(1, n+1):
    s = str(i)
    if s[-1] != '0':
        a[int(s[0])][int(s[-1])] += 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:
N = int(input())
A = [[0 for i in range(9)] for j in range(9)]
i = 92838
for i in range(1, N+1):
  if i % 10 != 0:
    A[int(str(i)[0])-1][i % 10 - 1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    ans += A[i][j] * A[j][i]
print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())
N_log = math.floor(math.log(N, 10))
N_top = N // pow(10, N_log)
N_bottom = N % 10
N_rest = N % pow(10, N_log)


def n_b(b0, b1):
    if b0 == 0:
        return 0
    ret = 0
    for keta in range(N_log + 1):
        ret += n_b_keta(b0, b1, keta)
    return ret


def n_b_keta(b0, b1, keta):
    if keta == 0:
        return b0==b1
    elif keta != N_log:
        return pow(10, keta - 1)
    else:
        if b0 > N_top:
            return 0
        elif b0 < N_top:
            return pow(10, keta - 1)
        else:
            return 1 + (N_rest//10) - (b1>N_bottom)

ans = 0
pre = 0
for i in range(1, N + 1):
    ans += n_b(int(str(i)[-1]), int(str(i)[0]))
    # print(i, ans-pre)
    pre = ans
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1, n + 1):
    if i % 10 != 0:
        i = str(i)
        sentou = int(i[0])
        matubi = int(i[-1])
        graph[sentou - 1][matubi - 1] += 1

import numpy as np

graph = np.array(graph)

print(np.trace(np.dot(graph, graph)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections as cl


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 main():
    N = II()
    items = [[0 for i in range(9)]for j in range(9)]

    for i in range(N+1):
        start = int(str(i)[0])
        end = int(str(i)[-1])
        if end == 0:
            continue
        items[start - 1][end - 1] += 1

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

    print(ans)


main()

Submission status: Accepted

Solution code:
L = [[0]*10 for _ in range(10)]
S = 0
for i in range(1, int(input())+1):
  L[int(str(i)[0])][int(str(i)[-1])] += 1
for j in range(9):
  for i in range(j+1, 10):
    S += 2*L[j][i]*L[i][j]
for i in range(10):
  S += L[i][i]**2
print(S)

Submission status: Accepted

Solution code:
import math
N = input()
d = list(map(int, list(N)))
N = int(N)

comb = [0] * 10 * 10
for i in range(1,N + 1):
  top = str(i)[0]
  bot = str(i)[-1]
  pair = 0
  pair = int(str(i)[0]) * 10 + int(str(i)[-1])
  comb[pair] += 1

ans = 0

for i, var in enumerate(comb):
  top = int(str(i)[0])
  bot = int(str(i)[-1])
  if top == bot:
    ans += var ** 2
  else:
    ans += comb[top * 10 + bot] * comb[top + bot * 10]
    

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

#int(str(n)[i]) nのi桁目
for n in range(1, n+1):
  cnt[int(str(n)[0])][int(str(n)[-1])] += 1

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def dfs(d, tight, str_N, pre):
    if d >= len(str_N) - 1:
        if int(pre) <= int(str_N[-1]):
            return 1
        else:
            return int(not tight)

    ans = 0
    if tight:
        for i in range(0, int(str_N[d]) + 1):
            ans += dfs(d + 1, str(i) == str_N[d], str_N, pre)
    else:
        for i in range(0, 10):
            ans += dfs(d + 1, False, str_N, pre)

    return ans


def solve():
    N = int(input())
    str_N = str(N)
    D = len(str_N)

    memo = [-1] * 10
    ans = 0
    for A in range(1, N + 1):
        a = str(A)
        pre = a[0]
        post = a[-1]

        if pre == "0" or post == "0":
            continue

        for d in range(1, D + 1):
            if d == 1:
                if pre == post:
                    ans += 1
            elif d != D:
                ans += 10 ** (d - 2)
            else:
                # B = post, xxx, pre
                if post == str(N)[0]:
                    if memo[int(pre)] == -1:
                        memo[int(pre)] = dfs(1, True, str_N, pre)
                    ans += memo[int(pre)]
                elif int(post) < int(str_N[0]):
                    ans += 10 ** (d - 2)

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

N = int(input())


cnt = {}
for i in range(1,N+1):
    N_str = str(i)

    num = N_str[0]+N_str[-1]
    if N_str[-1] == '0':
        pass
    else:
        if num in cnt.keys():
            cnt[num] +=1
        else:
            cnt[num] = 1

ans = 0
for i in cnt.keys():
    try:
        cnt_inv = cnt[i[::-1]]
        ans += cnt[i] * cnt_inv
    except:
        pass
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[[0 for _ in range(9)] for _ in range(9)]

for i in range(1,n+1):
  x=int(str(i)[0])
  y=int(str(i)[-1])
  if x!=0 and y!=0:
    a[x-1][y-1]+=1
  
ans=0
  
for i in range(9):
  for j in range(9):
    if i!=j:
      
      ans+=a[i][j]*a[j][i]
      
    else:
      ans+=(a[i][i]*a[i][i])
      
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

N = input()
digit = len(N)
f_digit = int(N[0])
l_digit = int(N[-1])
N = int(N)
cnt = 0

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

for i in range(1, 10):
    for j in range(1, 10):
        g_cnt[i][j] = 0
        if i == j:
            if i <= N:
                g_cnt[i][j] += 1
            if i*10+i <= N:
                g_cnt[i][j] += 1
        else:
            if i*10+j <= N:
                g_cnt[i][j] += 1

        if digit >= 3:
            num = deque([str(k) for k in range(10)])
            while num:
                q = num.popleft()
                for k in range(10):
                    if int(str(i)+q+str(k)+str(j)) <= N:
                        num.append(q+str(k))
                if int(str(i)+q+str(j)) <= N:
                    g_cnt[i][j] += 1

for i in range(1, 10):
    for j in range(i, 10):
        if i == j:
            cnt += g_cnt[i][j] ** 2
        else:
            cnt += g_cnt[i][j] * g_cnt[j][i] * 2

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
from itertools import combinations
n = int(input().rstrip())

c = Counter()

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
d={}
for i in range(1,10):
  for j in range(1,10):
    d[i*10+j]=0
for i in range(1,min(10,N+1)):
  d[i*10+i]=1
for i in range(11,N+1):
  i=str(i)
  h,t=int(i[0])*10,int(i[-1])
  if t!=0:
    d[h+t]+=1
ans=0
for i in range(1,9):
  for j in range(i+1,10):
    ans+=(d[i*10+j]*d[j*10+i])*2
for i in range(1,10):
  ans+=d[i*10+i]**2
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
d={}
for i in range(1,10):
  for j in range(1,10):
    d[i*10+j]=0
for i in range(1,min(10,N+1)):
  d[i*10+i]=1
p,h,t,c=10,10,1,1
for i in range(11,N+1):
  if t==10:
    t=1
  else:
    d[h+t]+=1
    t+=1
  if p==c:
    h+=10
    c=0
  if h==100:
    h=10
    p*=10
  c+=1
ans=0
for i in range(1,9):
  for j in range(i+1,10):
    ans+=(d[i*10+j]*d[j*10+i])*2
for i in range(1,10):
  ans+=d[i*10+i]**2
print(ans)

Submission status: Accepted

Solution code:
n = input()

if int(n)<10:
    print(n)
    exit()

matubi=int(n[-1])
sentou=int(n[0])
keta=len(n)

if n[1:keta-1]=="":
    aida=0
else:
    aida=int(n[1:keta-1])

n=int(n)
def cnt(saisyo,saigo):
    kotae=(10**(keta-2)-1)/9
    if saisyo<sentou:
        kotae+=10**(keta-2)
    elif saisyo==sentou:
        tmp=saisyo*10**(keta-1)+saigo
        while True:
            if str(tmp)[0]==str(saisyo) and tmp<=n:
                kotae+=1
            else:
                break
            tmp+=10
    if saisyo==saigo:
        kotae+=1
    return kotae

ans=0

import itertools
for seq in itertools.product(range(1,10),range(1,10)):
    ans+=cnt(seq[0],seq[1])*cnt(seq[1],seq[0])

print(int(ans))

Submission status: Accepted

Solution code:
n=int(input())
s=[]
t=[]
for i in range(9):
    s.append([])
for i in range(100):
    t.append([[],[]])
for i in range(1,n+1):
    a=str(i)
    b=max(int(a[0]),int(a[len(a)-1]))
    c=min(int(a[0]),int(a[len(a)-1]))
    if b!=0 and c!=0:
        if b==c:
            s[b-1].append(i)
        elif int(a[0])<int(a[len(a)-1]):
            t[b*9+c][0].append(i)
        else:
            t[b*9+c][1].append(i)
ans=0
for i in range(9):
    x=len(s[i])
    ans+=x**2
for i in range(100):
    x=len(t[i][0])
    y=len(t[i][1])
    ans+=2*x*y
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

from collections import defaultdict
dic = defaultdict(int)

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

ans = 0
for key in dic.keys():
  a,b = key
  if (b,a) in dic:
    ans += dic[(a,b)] * dic[(b,a)]
  
print(ans)

Submission status: Accepted

Solution code:
import sys

def f(fl, i, j):
    return sum(1 for v in fl if v[0] == i and v[1] == j)


def first_last(N):
    for n in range(1, N + 1):
        last = n % 10

        while n >= 10:
            n, _ = divmod(n, 10)
        first = n
        yield first, last


def resolve(in_):
    N = int(next(in_).strip())
    fl = tuple(first_last(N))
    
    ans = sum(f(fl, i, j) * f(fl, j, i) for i in range(10) for j in range(10))
    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans = 0
for k, v in c.items():
  ans += v*c[(k[1], k[0])]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A=[0]*100
for i in range(1,N+1):
  first=int(str(i)[0])
  last=int(str(i)[-1])
  A[first*10+last]+=1
score=0
for i in range(10):
  for j in range(10):
    score+=A[i*10+j]*A[j*10+i]
print(score)

Submission status: Accepted

Solution code:
N = int(input())
dp = [[0]*10 for _ in range(10)]
for a in range(N+1):
    A = str(a)
    dp[int(A[0])][int(A[-1])] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += dp[i][j]*dp[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
ans = 0
n_ketasuu = len(str(n))
sta_max = int(str(n)[0])
end_max = int(str(n)[n_ketasuu-1])
for i in range(1,n+1):
    sta = int(str(i)[len(str(i))-1])
    end = int(str(i)[0])
    if sta == 0:
        continue
    #1桁
    if sta == end:
        ans +=1
    #2桁
    if n_ketasuu >= 2 and sta*10 + end <= n:
        ans += 1
    #3桁
    if n_ketasuu > 3 or (n_ketasuu == 3 and sta < sta_max):
        ans += 10
    elif n_ketasuu == 3 and sta == sta_max:
        ans += int(str(n)[1:n_ketasuu-1])
        if end <= end_max:
            ans += 1
    #4桁
    if n_ketasuu > 4 or (n_ketasuu == 4 and sta < sta_max):
        ans += 100
    elif n_ketasuu == 4 and sta == sta_max:
        ans += int(str(n)[1:n_ketasuu-1])
        if end <= end_max:
            ans += 1
    #5桁
    if n_ketasuu > 5 or (n_ketasuu == 5 and sta < sta_max):
        ans += 1000
    elif n_ketasuu == 5 and sta == sta_max:
        ans += int(str(n)[1:n_ketasuu-1])
        if end <= end_max:
            ans += 1
    #6桁
    if n_ketasuu > 6 or (n_ketasuu == 6 and sta < sta_max):
        ans += 10000
    elif n_ketasuu == 6 and sta == sta_max:
        ans += int(str(n)[1:n_ketasuu-1])
        if end <= end_max:
            ans += 1

print(ans)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
count = [0,1]
if N == 1:
  print(1)
  exit()
for i in range(2,N+1):
  digit = len(str(i))
  head = int(str(i)[0])
  tail = int(str(i)[-1])
  plus = 0
  if digit == 1:
    plus=1
  elif digit == 2:
    if head == tail:
      plus = 3
    elif head>tail and tail>0:
      plus = 2
  elif tail>0:
    for j in range(digit-2):
      plus += 10**j*2
    if head == tail:
      mid = str(i)[1:-1]
      while len(mid)>1 and mid[0]=='0':
        mid = mid[1:]
      mid = int(mid)
      plus += 2*mid+1
      plus += 2
    elif head > tail:
      plus += 10**(digit-2)*2
  count.append(count[-1]+plus)
print(count[N])

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
import math

# a != b のとき


def count(a, b, N):

    keta = len(str(N))

    cnt = 0

    # k == 1 なら a == b
    if a == b:
        cnt += 1 if a <= N else 0

    if keta == 1:
        return cnt

    # k == 2 なら ba のみ
    if keta == 2:
        cnt += 1 if b * 10 + a <= N else 0
        return cnt

    # k 桁より少ない数の場合 : 好きな数を間に挟む
    for k in range(2, keta):
        cnt += 10**(k - 2)

    # k 桁の数字の場合
    s = N // (10**(keta - 1))
    t = N % 10
    if b > s:
        return cnt
    elif b < s:
        cnt += 10**(keta - 2)
    else:
        tempN = (N - s * (10**(keta - 1)) - t) // 10
        # print(f"tempN {tempN}")
        cnt += (tempN + 1)  # tempN が 1 なら 0,1 を選べる
        if a > t:
            cnt -= 1

    return cnt


def count_aa(a, N):

    keta = len(str(N))

    cnt = 0

    # keta == 1
    if keta == 1:
        return 1 if a <= N[-1] else 0

    cnt += 1  # a

    # k == 2 なら aa のみ
    if keta == 2:
        return cnt + 1 if a <= N[-1] else 0

    # 3桁以上
    # k 桁より少ない数の場合
    for k in range(2, keta):
        cnt += 10**(k - 2)

    # k 桁の数字の場合
    s = N // (10**(keta - 1))
    t = N % 10
    if b > s:
        return cnt
    elif b < s:
        cnt += 10**(keta - 2)
    else:
        tempN = (N - s * (10**(keta - 1)) - t) // 10
        # print(f"tempN {tempN}")
        cnt += (tempN + 1)  # tempN が 1 なら 0,1 を選べる
        if a > t:
            cnt -= 1

    return cnt


N = int(input())

c = 0
for i in range(1,N+1):
    a = int(str(i)[0])
    b = i % 10
    # print(a,b)
    if b == 0:
        continue
    c += count(a,b,N)

print(c)

Submission status: Accepted

Solution code:
N = int(input())
results = {(str(i), str(j)): 0 for i in range(1, 10) for j in range(1, 10)}


for i in range(N+1):
    s = str(i)
    if (s[0], s[-1]) in results.keys():
        results[(s[0], s[-1])] += 1

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

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
d = [[0] * 10 for _ in range(10)]
for i in range(1, n + 1):
    d[int(str(i)[0])][i % 10] += 1
ret = 0
for s in range(10):
    for e in range(10):
        ret += d[s][e] * d[e][s]
print(ret)

Submission status: Accepted

Solution code:
n = int(input())
d = dict()
for i in range(1, n+1):
    t = i
    tail = t % 10
    while t >= 10:  # head, tail nomi miru.
        t //= 10
    head = t
    d[(head, tail)] = d.get((head, tail), 0) + 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        temp = d.get((i,j), 0) * d.get((j,i), 0)
        # print(i, j, temp, d.get((i, j)), d.get((j, i)))
        ans += temp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
M = [0] * 100
for i in range(1, N+1):
    i = str(i)
    l, r = int(i[0]), int(i[-1])
    M[l*10+r] += 1
ans = 0
for i, cnt in enumerate(M):
    l, r = divmod(i, 10)
    ans += cnt * M[r*10+l]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
t = [0] * 100
for i in range(11, 100):
  msb = i // 10
  lsb = i % 10
  if lsb == 0:
    continue
  for j in range(1, n+1):
    if j % 10 == lsb and int(str(j)[0]) == msb:
      t[i] += 1

ans = 0
for i in range(11, 100):
  msb = i // 10
  lsb = i % 10
  if lsb == 0:
    continue
  if msb == lsb:
    ans += t[i] ** 2
  else:
    ans += t[i] * t[lsb * 10 + msb]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
d = defaultdict(int)
for i in range(1, n+1):
    if str(i)[-1] == '0':
        continue
    d[int(str(i)[0] + str(i)[-1])] += 1
ans = 0
for i in range(11, 100):
    ans += d[i] * d[int(str(i)[1] + str(i)[0])]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
d = defaultdict(int)
for i in range(1, N+1):
    s = str(i)
    top = int(s[0])
    end = int(s[-1])
    d[(top, end)] += 1
counter = 0
for i in range(1, N+1):
    s = str(i)
    top = int(s[0])
    end = int(s[-1])
    if (end, top) in d:
        counter += d[(end, top)]
print(counter)

Submission status: Accepted

Solution code:
n = int(input())
start_end = [[0] * 10 for _ in range(10)]
for i in range(1,n+1):
    start = int(str(i)[0])
    end = int(str(i)[-1])
    start_end[start][end] += 1
ans = 0
for start in range(10):
    for end in range(start,10):
        if start == end:
            ans += start_end[start][end]**2
        else:
            ans += start_end[start][end] * start_end[end][start] * 2
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy


def soinsu(n):
    ret = defaultdict(int)
    for i in range(2, int(math.sqrt(n) + 2)):
        if n % i == 0:
            while True:
                if n % i == 0:
                    ret[i] += 1
                    n //= i
                else:
                    break

    if not ret:
        return {n: 1}
    return ret


def solve():
    n = getN()

    sen = [defaultdict(int) for i in range(10)]
    ref = []
    for i in range(1, n+1):
        c = str(i)
        a, b = int(c[0]), int(c[-1])
        sen[a][b] += 1
        ref.append((a,b))

    # print(sen)
    ans = 0
    for a, b in ref:
        if a != b:
            if a in sen[b]:
                ans += sen[b][a]
        else:
            ans += sen[b][a]
        # print(a, b, ans)

    print(ans)







def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

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)
    head = int(s[0])
    tail = int(s[-1])
    count[head][tail] += 1

ans = 0
for a in range(1, N + 1):
    s = str(a)
    head = int(s[0])
    tail = int(s[-1])
    ans += count[tail][head]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
# cnt[i][j] := 先頭がi, 末尾がjで始まるような数
cnt = [[0] * 10 for i in range(10)]
for n in range(1, N + 1):
    cnt[int(str(n)[0])][int(str(n)[-1])] += 1

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

print(ans)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    C = [[0 for _ in range(10)] for __ in range(10)]
    for i in range(1, N+1):
        c = str(i)
        C[int(c[0])][int(c[-1])] += 1
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += C[i][j] * C[j][i]
    print(ans)

if '__main__' == __name__:
    resolve()

Submission status: Accepted

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