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:
from collections import defaultdict
n = int(input())
cnt = 0
d = defaultdict(int)
for i in range(1,n+1):
  a = int(str(i)[0])
  b = int(str(i)[-1])
  if b != 0:
    d[a*10+b] += 1
for i in range(1,n+1):
  a = int(str(i)[0])
  b = int(str(i)[-1])
  if b != 0:
    cnt += d[b*10+a]
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import decimal


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True

    @staticmethod
    def fact(N):
        res = {}
        tmp = N
        for i in range(2, int(N ** 0.5 + 1) + 1):
            cnt = 0
            while tmp % i == 0:
                cnt += 1
                tmp //= i
            if cnt > 0:
                res[i] = cnt
        if tmp != 1:
            res[tmp] = 1
        if res == {}:
            res[N] = 1
        return res


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N = Scanner.int()
    memo = [[0 for _ in range(10)] for _ in range(10)]
    for i in range(1, N + 1):
        s = str(i)
        memo[int(s[0])][int(s[-1])] += 1

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


def main():
    # sys.stdin = open("sample.txt")
    # T = Scanner.int()
    # for _ in range(T):
    #     solve()
    # print('YNeos'[not solve()::2])
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
P = []
Q = []
for L in range(1, N+1):
    L = str(L)
    a = int(L[0])
    b = int(L[-1])
    if a == 0 or b == 0:
        continue
    P.append((a, b))
C = Counter(P)

ans = 0
for i in range(len(P)):
    q = (P[i][-1], P[i][0])
    ans += C[q]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=[0]*100
for i in range(n+1):
  s=str(i)
  d[int(s[0])*10+int(s[-1])]+=1

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

Submission status: Accepted

Solution code:
def MAP(): return map(int, input().split())
def LIST(): return list(MAP())
 
N = int(input())
count = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, N + 1):
    string_i = str(i)
    initial = int(string_i[0])
    end = int(string_i[-1])
    count[initial][end] += 1
ans = 0
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:
N = int(input())

# N = 25

def calculate(n):
    result = {}

    for index in range(1, n + 1):

        i = int(str(index)[0])
        j = int(str(index)[-1])
        if result.get((i, j)) == None:
            if i != 0:
                result.__setitem__((i, j), 1)
        else:
            result.__setitem__((i, j), result.get((i, j)) + 1)

    res = 0
    for i in range(10):
        for j in range(10):
            s1 = result.get((i, j))
            s2 = result.get((j, i))

            s1 = 0 if s1 == None else s1
            s2 = 0 if s2 == None else s2

            res += s1 * s2

    print(res)


calculate(N)

Submission status: Accepted

Solution code:
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
a = [[0] * 10 for i in range(10)]
for i in range(1,N+1):
    s = int(str(i)[0])
    g = int(str(i)[-1])
    a[s][g] += 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())
ans=-1
c=[[0]*10for i in range(11)]
for i in range(n+1):c[int(str(i)[0])][int(str(i)[-1])]+=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:
import numpy as np
def main():
    n = int(input())
    count = np.zeros((10, 10))
    for i in range(1, n + 1):
        i = str(i)
        s = int(i[0])
        e = int(i[-1])
        count[s, e] += 1
    print(np.sum(count * count.T, dtype = np.int))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
dp = np.zeros((10, 10)).astype(int)
dp[1, 1] = 1
ans = 1
for i in range(2, N + 1):

    i_str = str(i)
    i_start = int(i_str[0])
    i_end = int(i_str[-1])
    # print(i_start,i_end)
    if i_start == i_end:
        ans += 1
    ans += dp[i_end, i_start]*2
    dp[i_start, i_end] += 1
    # print(dp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
D = {i: 0 for i in range(100)}
for i in range(1, N+1):
    if i % 10 != 0:
        s = str(i)
        D[int(s[0])*10+int(s[-1])] += 1
S = 0
for i in range(10):
    for j in range(10):
        S += D[i*10+j] * D[j*10+i]
print(S)

Submission status: Accepted

Solution code:
N = int(input())
D = {i: 0 for i in range(100)}
for i in range(1, N+1):
  if i % 10 != 0:
    s = str(i)
    D[int(s[0])*10+int(s[-1])] += 1
S = 0
for i in range(10):
  for j in range(10):
    S += D[i*10+j] * D[j*10+i]
print(S)

Submission status: Accepted

Solution code:
N = int(input())
D = {i: [0] for i in range(100)}
for i in range(1, N+1):
  if i % 10 != 0:
    s = str(i)
    D[int(s[0])*10+int(s[-1])][0] += 1
    D[int(s[0])*10+int(s[-1])].append(i)
S = 0
for i in range(10):
  for j in range(10):
    S += D[i*10+j][0] * D[j*10+i][0]
print(S)

Submission status: Accepted

Solution code:
n=int(input())
ans=-1
c=[[0]*10 for _ in range(11)]
for i in range(n+1):
  c[int(str(i)[0])][int(str(i)[-1])]+=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:
def top(n):
    return int(str(n)[0])

def bottom(n):
    return int(str(n)[-1])

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

for i in range(1,n+1):
    c[top(i)][bottom(i)] += 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())
ct = [[0] * 10 for _ in range(10)]
# print(ct)

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

Submission status: Accepted

Solution code:
N=int(input())

D={(x//10,x%10):0 for x in range(100)}
for i in range(1,N+1):
    if i<10:
        D[(i,i)]=1
    else:
        Q=int(str(i)[0])
        R=i%10
        D[(Q,R)]+=1

S=0
for x in range(1,10):
    for y in range(1,10):
        S+=D[(x,y)]*D[(y,x)]

print(S)

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
def cmb(n, r):
    import math
    if n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


# from decorator import stop_watch
# 
# 
# @stop_watch
def solve(N):
    ABs = {}
    ABs_visited = {}
    for h in range(1, 10):
        for t in range(1, 10):
            ABs.setdefault((h, t), 0)
            ABs_visited.setdefault((h, t), False)

    for n in range(1, N + 1):
        s = str(n)
        h, t = int(s[0]), int(s[-1])
        if not (h == 0 or t == 0):
            ABs[(h, t)] += 1

    ans = 0
    for k in ABs:
        if ABs_visited[k]:
            continue
        h, t = k
        ABs_visited[(h, t)] = True
        # ABs_visited[(t, h)] = True

        # if h == t:
        #     ans += ABs[(h, t)] + cmb(ABs[(h, t)], 2)
        # else:
        #     ans += ABs[(h, t)] * ABs[(t, h)]
        ans += ABs[(h, t)] * ABs[(t, h)]
        # print(k, ABs[k], ans)
        # input()
    # print(ABs)

    print(ans)


if __name__ == '__main__':
    N = int(input())
    solve(N)

Submission status: Accepted

Solution code:
N=int(input())
ls=[[0]*9 for _ in range(9)]
for i in range(1,N+1):
  a=i
  while a//10!=0:
    a//=10
  b=i%10
  if b!=0:
    ls[a-1][b-1]+=1 
ans=0    
for i in range(1,10):
  for j in range(1,10):
    ans+=ls[i-1][j-1]*ls[j-1][i-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

div = 1
pairs = [[0] * 10 for i in range(10)]

for i in range(1,N+1):
    if i // div >= 10:
        div *= 10
        f = 1
    else:
        f = i//div
    pairs[f][i%10] += 1

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

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
table,ans = np.zeros((9,9)),0

for k in range(n):
    num = str(k+1)
    if int(num[0])!=0 and int(num[-1])!=0:
        table[int(num[0])-1][int(num[-1])-1]+=1

for i in range(9):
    for j in range(9):
        ans += table[i][j]*table[j][i]
print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
c = [[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])
    c[l][r] += 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())
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(10):
    for j in range(10):
        ans += cnt[i][j] * cnt[j][i]
print(ans)

Submission status: Accepted

Solution code:
import math
import numpy as np
N = int(input())
tb = [[0]*10]*10
tb = np.array(tb)
for i in range(1,N+1):
    s = str(i)
    tb[int(s[0])][int(s[-1])] =tb[int(s[0])][int(s[-1])] +1
 
point =0
 
for i in range(0,10):
    for j in range(0,10):
        point += tb[i][j] * tb[j][i]
 
print(point)

Submission status: Accepted

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

for i in range(1,N+1):
    I = str(i)
    fast = int(I[0])
    last = int(I[-1])
    if not last==0:
        li[fast-1][last-1] += 1

for i in range(9):
    for k in range(9):
        counter += li[i][k]*li[k][i]


print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

k = ni()

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

for i in range(k+1):
  s = [int(_) for _ in str(i)]
  p[s[0]][s[-1]]+=1

ans=0

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

print(ans)

Submission status: Accepted

Solution code:
mod = 1e9+7
def add(a, b):
    c = a + b
    if c >= mod:
        c -= mod
    return c

def main():
    m = {}
    N = int(raw_input())
    
    for i in range(1, N+1):
        s = str(i)
        first = s[0]
        last = s[-1]
        key = first + last
        m[key] = m.get(key, 0) + 1
    
    ans = 0
    
    #print(m)
    for k in m:
        if k[0] == k[1]:
            ans += m[k] * m[k]
        else:
            swap = k[1] + k[0]
            ans += m[k] * m.get(swap, 0)
    """
    for key1 in m:
        for key2 in m:
            #if key1 == key2:
            #    continue
            if key1[0] == key2[1] and key1[1] == key2[0]:
                ans += m[key1] * m[key2]
    """
    
    print(ans)
    
    
    

main()

Submission status: Accepted

Solution code:
mod = 1e9+7
def add(a, b):
    c = a + b
    if c >= mod:
        c -= mod
    return c

def main():
    m = {}
    N = int(raw_input())
    
    for i in range(1, N+1):
        s = str(i)
        first = s[0]
        last = s[-1]
        key = first + last
        m[key] = m.get(key, 0) + 1
    
    ans = 0
    
    #print(m)
    
    for key1 in m:
        for key2 in m:
            #if key1 == key2:
            #    continue
            if key1[0] == key2[1] and key1[1] == key2[0]:
                ans += m[key1] * m[key2]
    
    print(ans)
    
    
    

main()

Submission status: Accepted

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

for i in range(1,n+1):
    s=str(i)
    s1 = int(s[0]) - 1
    s2 = int(s[-1]) - 1
    if s2 == -1 : continue
    check[s1][s2] += 1

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

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1, n+1):
  aaa[int(str(i)[0])][i%10] += 1

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

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
dic = defaultdict(int)

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

point = 0

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

print(point)

Submission status: Accepted

Solution code:
n = int(input())
s0=list(str(n))
ans=0

if len(s0)==1:
    print(n)
    exit()

if len(s0)==2:
    for i in range(1,n+1):
        s1=list(str(i))
        if s1[-1]=='0':
            continue
        if s1[0]==s1[-1]:
            ans+=1
        if int(s1[-1])*10+int(s1[0])<=n:
            ans+=1
    print(ans)
    exit()

for i in range(1,n+1):
    s1=list(str(i))
    if s1[-1]=='0':
        continue
    if s1[0]==s1[-1]:
        ans+=1
    for j in range(2,len(s0)):#nより小さい桁数のものを足す
        ans+=10**(j-2)
    if int(s0[0])>int(s1[-1]):#nより入れ替えた数の最高位数が小さいとき、全て足す
        ans+=10**(len(s0)-2)
    elif s0[0]==s1[-1]:#nと入れ替えた数の最高位数が同じ時
        ans+=int(''.join(s0[1:len(s0)-1]))+1
        if int(s0[-1])<int(s1[0]):
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
dct = defaultdict(int)
for i in range(1,n+1):
    s = str(i)
    dct[(s[0],s[-1])] += 1
ans = 0
for i in range(1,n+1):
    s = str(i)
    ans += dct[(s[-1],s[0])]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
cnt=0

def check(v,n):
  if len(n) > len(v):
    return 10**(len(v)-2)
  if len(n) < len(v):
    return 0
  if n[0] > v[-1]:
    return 10**(len(v)-2)
  if n[0] == v[-1]:
    x=1 if v[0] > n[-1] else 0
    return max(0, int(n[1:-1])-x+1)
  if n[0] < v[-1]:
    return 0

for i in range(1,n+1):
  st=str(i)
  nst=str(n)
  if st[-1]=='0':
    continue
  if len(st) == 1:
    cnt+=1
    continue
  if len(st)==2:
    if int(st[-1]+st[0]) <= n:
      cnt += 1
  else:
    cnt+= check(st,nst)
    for i in range(len(st)-2):
      xst=st[-1]+'9'*i+st[0]
      cnt += check(xst,nst)*2
  if st[0]==st[-1]:
    cnt += 2

print(cnt)

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:
import sys
import  math
from heapq import *
import fractions
from collections import defaultdict
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
n_l=[[0]*10 for i in range(10)]#col matu row sentou
for i in range(1,N+1):
    if(1<=i and i<10):
        n_l[i][i]+=1
    elif(10<i and i<10**2):
        n_l[i%10][i//10]+=1
    elif(10**2<i and i<10**3):
        n_l[i%10][i//100]+=1
    elif(10**3<i and i<10**4):
        n_l[i%10][i//1000]+=1
    elif(10**4<i and i<10**5):
        n_l[i%10][i//10000]+=1
    elif(10**5<i):
        n_l[i%10][i//100000]+=1
    

ans=0
for i in range(1,10):
    for k in range(1,10):
        if(i!=k):
            ans+=(n_l[i][k])*((n_l[k][i]))

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(input())

c = [[0 for i in range(10)] for _ in range(10)]
def f(n):
    x = 0
    while n > 0:
        n //= 10
        x +=1
    return x

for i in range(1, N + 1):
    d = f(i)
    front = i // (10 ** (d - 1)) 
    back = i % 10
    c[front][back] += 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:
from collections import Counter
n = int(input())
li = [(int(str(x)[0]), int(str(x)[-1])) for x in range(1, n + 1)]

c = dict(Counter(li).items())

cnt = 0
for k, v in c.items():
    a, b = k
    if a == b:
        cnt += v**2
    elif (b, a) in c:
        v2 = c[(b, a)]
        cnt += v * v2

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())

import copy

raw=[0]*10
a=[]

for i in range(10):
  a.append(copy.deepcopy(raw))

ans=0
#print(a)

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

#print(a)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

def hs(k):
    ks = str(k)
    lk = len(ks)
    a = int(ks[0])
    b = int(ks[len(ks) - 1])
    if lk == 1:
        return 1
    if b == 0:
        return 0
    s = 0
    if (a > b) & (lk > 1):
        s += 10 ** (lk - 2) * 2
    elif a == b:
        if lk == 2:
            s += 1
        else:
            s += ((k % (a * 10**(lk - 1))) // 10 + 1) * 2 - 1
    if lk >= 2:
        for i in range(lk - 1):
            if lk - i - 3 >= 0:
                s += 10 ** (lk - 3 - i) * 2
            elif a == b:
                s += 2
    return s

ans = 0
for i in range(1, n + 1):
    ans += hs(i)
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())

    cnt = [[0] * 10 for _ in range(10)]
    for i in range(1, n + 1):
        head = int(str(i)[0])
        tale = int(str(i)[-1])
        cnt[head][tale] += 1
        
    res = 0
    for i in range(1, n + 1):
        head = int(str(i)[0])
        tale = int(str(i)[-1])
        res += cnt[tale][head]

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
class Counter(dict):
    def __missing__(self, i):
        return 0


n = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

def solve():
    input = sys.stdin.readline
    N = int(input())
    ht = defaultdict(int)

    for i in range(1, N + 1):
        k = i
        head = k % 10
        while k >= 10:
            k //= 10
        tail = k
        if head > 0: ht[(head, tail)] += 1
    ans = 0
    for i in range(1, N + 1):
        k = i
        tail = k % 10
        while k >= 10:
            k //= 10
        head = k
        if tail > 0: ans += ht[(head, tail)]
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

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:
n = int(input())
lst = [[0 for j in range(10)] for i in range(10)]

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

cnt = 0
for i in range(10):
    for j in range(i, 10):
        if i == j:
            cnt += lst[i][j] * lst[j][i]
        else:
            cnt += lst[i][j] * lst[j][i] * 2
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())


dig = [[0 for _ in range(10)] for i in range(10)]

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


ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import defaultdict


N = int(read())
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:
N = int(input())
matrix = [[0,0,0,0,0,0,0,0,0,0] for _ in range(10)]
for i in range(1,N+1):
    first = int(str(i)[0])
    last = int(str(i)[-1])
    matrix[first][last] += 1
ans = 0
for i in range(10):
    for l in range(10):
        ans += matrix[i][l]*matrix[l][i]
print(ans)

Submission status: Accepted

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

for i in range(1, N+1):
  if i%10==0: continue
  strI = str(i)
  f,l = strI[-1], strI[0]
  dp[int(f)][int(l)] += 1

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

print(res)

Submission status: Accepted

Solution code:
N = int(input())
N_ = str(N)
f_N, l_N = int(N_[0]), int(N_[-1])
digit = len(N_)
if digit > 2:
    inter_N = int(N_[1:digit-1])
cnt = 0
for num in range(1, N+1):
    num = str(num)
    f, l = int(num[-1]), int(num[0])
    if f == 0:
        continue
    for i in range(1, digit+1):
        if i == 1:
            if f == l:
                cnt += 1
            else:
                continue
        else:
            if i != digit:
                cnt += 10**(i-2)
            elif digit == 2 and i == digit:
                if f*10+l <= N:
                    cnt += 1
                else:
                    continue
            else:
                if f > f_N:
                    continue
                elif f == f_N:
                    if l > l_N:
                        cnt += inter_N
                    elif l <= l_N:
                        cnt += (inter_N+1)
                else:
                    cnt += 10**(i-2)
print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
c = [[0] * 10 for _ in [0] * 10]
for i in range(1, int(input()) + 1):
    c[int(str(i)[0])][int(str(i)[-1])] += 1

print(sum(c[i][j] * c[j][i] for i in range(10) for j in range(10)))

Submission status: Accepted

Solution code:
N = int(input())

c = [[0 for _ in range(10)] for __ in range(10)]
for x in range(1, N+1):
    check = str(x)
    it = int(check[0])
    jt = int(check[-1])
    c[it][jt] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

ans = 0
for i in range(10):
	for j in range(10):
		ans += X[i][j] * X[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):
  i=str(i)
  ii=len(i)
  A[int(i[0])][int(i[ii-1])]+=1
ans=0
for i in range(10):
  for j in range(10):
    ans+=A[i][j]*A[j][i]
print(ans)

Submission status: Accepted

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
import numpy as np
# sympy as syp(素因数分解とか)
Mod = 1000000007
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1] 
for i in range(2, 10**5 + 1):
    fact.append((fact[-1] * i) % Mod)
    inv.append((-inv[Mod % i] * (Mod // i)) % Mod)
    factinv.append((factinv[-1] * inv[-1]) % Mod)
    
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n - r] % p
    
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[y] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main():  #startline-------------------------------------------
    n = int(input())
    ls = [[0] * 10 for i in range(10)]
    for i in range(1, n + 1):
        val = list(str(i))
        ls[int(val[-1])][int(val[0])] += 1
    
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += ls[i][j] * ls[j][i]

    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Accepted

Solution code:
n=int(input())

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

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

Submission status: Accepted

Solution code:
n = int(input())

def count_ab(start, end):
    l = len(str(n))
    if start == 0:
        return 0
    if l == 1:
        if start == end:
            if n >= start:
                return 1
            else:
                return 0
        else:
            return 0
    else:
        if start == end:
            cnt = 1
        else:
            cnt = 0
        if int(str(n)[0]) > start:
            for i in range(1, l):
                cnt += 10 ** i // 10
        if int(str(n)[0]) == start:
            for i in range(1, l - 1):
                cnt += 10 ** i //10
            cnt += int(str(n)[1:]) // 10
            if int(str(n)[-1]) >= end:
                cnt += 1
        if int(str(n)[0]) < start:
            for i in range(1, l - 1):
                cnt += 10 ** i //10
        return cnt

ans = 0
for i in range(10):
    for j in range(10):
        A = count_ab(i, j)
        B = count_ab(j, i)
        ans += A * B

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 i%10!=0:
    l[i%10-1][int(str(i)[0])-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:
c=[[0 for i in range(9)] for i in range(9)]

n=int(input())
for i in range(1,n+1):
    I=str(i)
    if I[0]!="0" and I[len(I)-1]!="0":
        c[int(I[0])-1][int(I[len(I)-1])-1]+=1

s=0
for i in range(9):
    for j in range(9):
        s+=c[i][j]*c[j][i]

print(s)

Submission status: Accepted

Solution code:
n=int(input())
c = [[0]*10 for i in range(10)]
n_len = len(str(n))
for k in range(1,n+1):
  k_str = str(k)
  l,r=int(k_str[0]),int(k_str[-1])
  c[l][r]+=1

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