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:
import sys
input=sys.stdin.readline
n=int(input())
ns=str(n)
l=len(ns)
ans=0
for i in range(1,n+1):
    s=str(i)
    x=s[0]
    y=s[-1]
    if y=='0':
        continue
    if x==y:
        ans+=1
    if l==1:
        continue
    for j in range(l-2):
        ans+=10**j
    if int(ns[0])>int(y):
        ans+=10**(l-2)
    elif int(ns[0])<int(y):
        pass
    else:
        z=n-int(ns[0])*10**(l-1)
        if z>=int(x):
            w=z-int(x)
            ans+=w//10+1
print(ans)

Submission status: Accepted

Solution code:
def check(a, b):
    k = 1
    res = 0
    if a == b and (a <= N): res += 1
    while True:
        if (a * (10 ** k) + b) > N:
            break
        else:
            if len(str(N)) > (k+1) or str(N)[0] > str(a):
                res += 10 ** (k - 1)
                k += 1
                continue

            else :
                Q = N - (a * (10 ** k) + b)
                p = 1
                if len(str(Q)) > 1 :
                    p += (int(str(Q)[:-1]))
                res += p
                k += 1
    return res


def main():
    global N
    N = int(input())
    ans = 0
    for i in range(1, 10, 1):
        for j in range(1, 10, 1):
            a = check(i, j)
            b = check(j, i)
            ans += a * b
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
cnt = 0
l = len(str(n))
heads_tails = [[0 for i in range(9)] for j in range(9)]

for i in range(1, n+1):
    if i % 10 == 0:
        continue
    head = int(str(i)[0]) - 1
    tail = int(str(i)[-1]) - 1
    heads_tails[head][tail] += 1

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

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
# a,b = map(int, input().split())
# l = list(map(int, input().split()))

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

for i in range(1,n+1):
    i_str = str(i)
    mae = int(i_str[0])
    usi = int(i_str[-1])
    count[mae][usi] += 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())

def pair(n):
    l = n % 10
    while n != 0:
        f = n
        n = n // 10
    return (f, l)

d = defaultdict(int)

for i in range(1, N + 1):
    d[pair(i)] += 1

ans = 0
for key, value in d.items():
    tmp1 = value
    l, f = key
    if (f, l) in d.keys():
        tmp2 = d[(f, l)]   
        ans += tmp1 * tmp2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
c = 0
cnt = [ [0]*10 for _ in range(10) ]
for i in range(1,N+1):
    a = str(i)
    top = int(a[0])
    end = int(a[-1])
    cnt[top][end] += 1

for i in range(10):
    for j in range(10):
        for k in range(10):
            for l in range(10):
                if i == l and j == k:
                    c += cnt[i][j] * cnt[k][l]
print(c)

Submission status: Accepted

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

from collections import defaultdict

def main():
    n = int(input())
    cnt = defaultdict(int)
    for i in range(1, n + 1):
        si = str(i)
        x = int(si[0])
        y = int(si[-1])
        cnt[(x, y)] += 1
    res = 0
    for x in range(1, 10):
        for y in range(1, 10):
            num_a = cnt[(x, y)]
            num_b = cnt[(y, x)]
            res += num_a * num_b
    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

from collections import defaultdict
counter = defaultdict(int)

for k in range(1,N+1):
    temp = list(str(k))
    counter[(int(temp[0]),int(temp[-1]))] += 1

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

Submission status: Accepted

Solution code:
N=int(input())
def answer(i,j):
    i=str(i)
    j=str(j)
    kotae=0
    for k in range(1,N+1):
        if str(k)[0]==i and str(k)[-1]==j:
            kotae+=1
    return kotae
answers=0
for i in range(1,10):
    for j in range(1,10):
        answers+=answer(i,j)*answer(j,i)
print(answers)

Submission status: Accepted

Solution code:
N=int(input())
dp=[0]*100
for i in range(N):
    s=str(i+1)
    dp[int(s[0])*10+int(s[-1])]+=1
ans=sum(dp[i*10+j]*dp[j*10+i] for i in range(1,10) for j in range(1,10))
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):
  s=str(i)
  cnt[int(s[0])][int(s[-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:
N = int(input())

s = [[0 for i in range(10)] for i in range(10)]
#print(s)

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

print(ans)

Submission status: Accepted

Solution code:
N = input()
l = len(N)
ans = 0
C = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1,int(N)+1):
    s = str(i)
    top = int(s[0])
    bot = int(s[-1])
    C[top][bot] += 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:
N=int(input())

M=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
  S=str(i)
  f=int(S[0])
  b=int(S[-1])
  M[f][b]+=1
  
ans=0
for i in range(10):
  for j in range(10):
    ans+=M[i][j]*M[j][i]
    
print(ans)

Submission status: Accepted

Solution code:
n = int (input ())
cnt = [[0 for i in range (10)] for i in range (10)]
for i in range (1, n + 1) :
    p = int (str (i)[0])
    q = i % 10
    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())

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

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

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

print(ans)

Submission status: Accepted

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

c = Counter()
d = defaultdict(bool)

for i in range(1,N+1):
    s = str(i)
    c[(s[0],s[-1])] += 1
    d[(s[0],s[-1])] = True
    
k = list(c.keys())

ans = 0
for i,j in k:
    if d[(j,i)]:
        ans += (c[(i,j)]*c[(j,i)])
        d[(j,i)] = False
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

Solution code:
import math
import time


t = time.time()
def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

def fkc(keta):
    return 10**keta

def pat_b(a, n):
    tail = a % 10

    if tail == 0:
        return 0

    head = int(str(a)[0])

    ret = 0
    if tail == head: #１桁解
        ret += 1

    if n >= tail*10+head: #２桁解は１通りあるかないか
        ret += 1

    for free_keta in range(1, 6): # 3桁以上解
        mincase = 10**(free_keta+1) * tail + head

        if n < mincase:
            break
        if n >= 10**(free_keta+1) + mincase + head - 10:
            ret += fkc(free_keta)
            continue
        else:
            ret += int(int(n-mincase)/10) + 1
    return ret

def main():
    n = iip()
    result = 0
    for i in range(n+1):
        result += pat_b(i, n)
    print(result)


main()

Submission status: Accepted

Solution code:
N = int(input())

C = [[0]*10 for _ in range(10)]
for n in range(1, N+1):
    s = str(n)
    C[int(s[0])][int(s[-1])] += 1

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

Submission status: Accepted

Solution code:
n = int(input())
a = [[0]*9 for x in range(9)]
for i in range(1,n+1):
    s = str(i)
    if s[-1]=="0":
        continue
    a[int(s[0])-1][int(s[-1])-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:
def main():
    N = int(input())
    cnt = [[0] * 10 for _ in range(10)]
    for i in range(1,N+1):
        tmp = str(i)
        top = int(tmp[0])
        end = int(tmp[-1])
        cnt[top][end] += 1
    ans = 0
    for a in range(10):
        for b in range(10):
            for c in range(10):
                for d in range(10):
                    if a == d and b == c:
                        ans += cnt[a][b] * cnt[c][d]

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#152_D
n = int(input())
dp = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, n+1):
    s = str(i)
    dp[int(s[0])][int(s[-1])] += 1
    
ans = 0
for i in range(1, 10):
    for j in range(i, 10):
        if i == j:
            ans += dp[i][j] * dp[j][i]
        else:
            ans += 2 * dp[i][j] * dp[j][i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

num = [[0] * 10 for _ in range(10)]
for k in range(1, n + 1):
    s = str(k)
    num[int(s[0])][int(s[-1])] += 1

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1, n + 1):
    mln = i % 10   # Most Left Num.
    mrn = int(str(i)[0])  # Most Right Num
    a[mln][mrn] += 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 = 0
for i in range(1,10) :
  for j in range(i,10) :
    c_a = 0
    c_b = 0
    for k in range(1,n+1) :
      kl = len(str(k))
      if k//(10**(kl-1)) == i and k%10 == j :
        c_a += 1
      if k//(10**(kl-1)) == j and k%10 == i :
        c_b += 1
    if i == j :
      ans += c_a*c_b
    else :
      ans += c_a*c_b*2
      
print(ans)

Submission status: Accepted

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

for i in range (1, n + 1) :
    q = i % 10
    p = int (str (i)[0])
    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 ())
cnt = [[0 for i in range (10)] for i in range (10)]

for i in range (1, n + 1) :
    q = i % 10
    p = int (str (i)[0])
    cnt[p][q] += 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 head(x):
    r, q = divmod(x, 10)
    if r == 0:
        return q
    return head(r)
    

def tail(x):
    return x % 10


def submit():
    n = int(input())
    head_tail_count = {(i, j):0 for i in range(10) for j in range(10)}

    for x in range(n + 1):
        h = head(x)
        t = tail(x)
        head_tail_count[(h, t)] += 1

    ans = 0
    for x in range(1, 10):
        for y in range(1, 10):
            ans += head_tail_count[(x, y)] * head_tail_count[(y, x)]
    print(ans)


if __name__ == "__main__":
    submit()

Submission status: Accepted

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

Submission status: Accepted

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

def fl(x):
    f = 0
    l = x % 10
    while x != 0:
        f = x
        x = x // 10
    return (f, l)

d = defaultdict(int)

for i in range(1, N+1):
    d[fl(i)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1,N+1):
    i = str(i)
    l = int(i[0])
    r = int(i[-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 _ in range(10)]
for i in range(1, N+1):
  head = int(str(i)[0])
  tail = int(str(i)[-1])
  cnt[head][tail] += 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())
k = len(list(str(n)))


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


for k in range(1, n + 1):
    arr = list(map(int,list(str(k))))
    c[arr[0]][arr[-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:
ls = [[0 for _ in range(10)] for _ in range(10)]
N = int(input())
for i in range(1,N+1):
    s = str(i)
    ls[int(s[-1])][int(s[0])] += 1
ans = 0
for i in range(10):
    for j in range(10):
        ans += ls[i][j]*ls[j][i]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

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


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


ans=0

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

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline

# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np


def main():
	li=[[0]*10 for i in range(10)]
	n=int(input())
	for i in range(1,n+1):
		i=str(i)
		li[int(i[0])][int(i[-1])]+=1

	ans=0

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

	print(ans)
















if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
length = len(str(n))
c = [[0] * 10 for _ in range(10)]
for k in range(1, n + 1):
    s = str(k)
    first = int(s[0])
    last = int(s[-1])
    c[first][last] += 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:
# maspyさんの解答（見た目上の部分一部改変）

import sys
from collections import defaultdict
read = sys.stdin.buffer.read

# 入力値取得
N = int(read())

# Nまでに xで始まってyで終わる数字が何回でるか数え上げる。
counter = defaultdict(int)
for x in map(str, range(1, N + 1)):
    head = x[0]
    tail = x[-1]
    counter[(head, tail)] += 1

answer = 0

# forで回せるようにdefaultdictをlistに変換
items = list(counter.items())

# (X,Y)とその出現回数cntを取りながらループ
for (head, tail), cnt in items:
    # head,tailの出現回数 × headとtailを逆にした出現回数
    answer += cnt * counter[(tail, head)]

print(answer)

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])][i % 10] += 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())

L = [[0]*10 for j in range(10)]

for i in range(1, n+1):
    p = int(str(i)[0])
    s = int(str(i)[-1])
    L[p][s] += 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:
# https://atcoder.jp/contests/abc152/tasks/abc152_d
# 解説かんにんぐ

n=int(input())
freq=dict()
ans=0

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

for i in range(1,n+1):
    s=str(i)
    q=(s[-1],s[0])
    ans+=freq.get(q,0)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
def main():
    input = sys.stdin.readline
    N = int(input())
    d = defaultdict(int)

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

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

if __name__ == '__main__':
    main()

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 STR(): return input()
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 = sys.maxsize
mod = 10 ** 9 + 7

n = INT()
l = len(str(n))
arr = []
for i in range(1, 10):
    for j in range(1, 10):
        arr.append([i, j])
arr3 = [0 for _ in range(len(arr))]
for i in range(1, n + 1):
    head = int(str(i)[0])
    foot = int(str(i)[-1])
    if head != 0 and foot != 0:
        idx = arr.index([head, foot])
        arr3[idx] += 1
ans = 0
for i in range(len(arr3)):
    idx = arr.index(list(reversed(arr[i])))
    ans += arr3[i] * arr3[idx]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
matrix = [[0 for _ in range(9)] for _ in range(9)]
# print(matrix)
for i in range(1, N + 1):
    s = str(i)
    row = int(s[0])
    column = int(s[-1])
    if row == 0 or column == 0:
        continue
    matrix[row - 1][column - 1] += 1
# pprint(matrix)
ans = 0
for i in range(9):
    for j in range(9):
        # if i == j:
        #     ans += matrix[i][j]
        # else:
        #     ans += matrix[i][j] * matrix[j][i]
        ans += matrix[i][j] * matrix[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys

def solve():
    N = int(input())
    D = dict()
    for i in range(1, N + 1):
        front = i % 10
        back = i
        while back >= 10: back //= 10
        if front > 0:
            if (front, back) in D: D[(front, back)] += 1
            else: D[(front, back)] = 1
    Ans = 0
    for i in range(1, N + 1):
        back = i % 10
        if back > 0:
            front = i
            while front >= 10: front //= 10
            if (front, back) in D: Ans += D[(front, back)]
    print(Ans)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    c = [[0]*10 for _ in range(10)]
    for n in range(1, N+1):
        s = str(n)
        c[int(s[0])][int(s[-1])] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            # if c[i][j]*c[j][i]:
            #     print(i, j, c[i][j]*c[j][i])
            ans += c[i][j] * c[j][i]
    print(ans)
    # print(*c, sep="\n")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

c = [[0]*10 for _ in range(10)]
for n in range(1,N+1):
    s = str(n)
    c[int(s[0])][int(s[-1])] += 1

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

Submission status: Accepted

Solution code:
c=[10*[0]for _ in range(10)]
n=int(input())
for i in range(1,n+1):
  a=int(str(i)[0])
  b=int(str(i)[-1])
  c[a][b]+=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:
#!/usr/bin/env python3
import sys
from itertools import product

def solve(N: int):
    def f(n):
        h = n
        while h // 10 > 0:
            h //= 10
        return h, n%10
    table = [[0] * 10 for _ in range(10)]
    for h, t in map(f, range(1, N+1)):
        table[h][t] += 1
    return sum(table[i][j] * table[j][i] for i, j in product(range(1, 10), repeat=2))

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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
    print(solve(N))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left #bisect_left(list, value)
#from fractions import gcd
enu = enumerate
def input():
    return sys.stdin.readline()[:-1]
def pri(x):
    if type(x) is list:
        print('\n'.join(map(str, x)))
    else:
        print(x)

sys.setrecursionlimit(10**7)
N = int(input())

nums = defaultdict(int)

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

cnt = 0
for st in range(1, 10):
    for en in range(1, 10):
        cnt += nums[(st, en)] * nums[(en, st)]

pri(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

def start(n):
    return int(str(n)[0])
def end(n):
    return int(str(n)[-1])

for i in range(1,N+1):
    qq[start(i)][end(i)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
# import bisect
# # import heapq
# # from copy import deepcopy
# # from collections import deque
# # from collections import Counter
# # from itertools import accumulate
# # from itertools import permutations
# import numpy as np
# # import math
# a,b,c = map(int, input().split())

# p = list(map(int, input().split()))
n = int(input())
count = 0
a = [[0] * 10 for i in range(10)]
# print(a)
# print(b)
for i in range(n +1):
    head = int(str(i)[0])
    tail = int(str(i)[-1])
    # if i == 18:
    #     print(head,tail)
    a[head][tail] += 1


for i in range(n+1):
    if i % 10 == 0:
        continue

    head = int(str(i)[0])
    tail = int(str(i)[-1])
    count += a[tail][head]

print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=[[0]*10 for i in range(10)]
for i in range(1,n+1):
  keta=str(i)[0]
  makkou=str(i)[-1]
  a[int(makkou)-1][int(keta)-1]+=1
ans=0
for j in range(10):
  for i in range(10):
    ans+=a[j][i]*a[i][j]
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()  # ignore trailing spaces

n=ni()
d= [10*[0]for _ in range(10)]
ans = 0
for i in range(1,n+1):
  s = str(i)
  if s[0] == "0" or s[-1] == "0": continue
  d[int(s[0])][int(s[-1])] += 1
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:
from collections import defaultdict
def main():
    def pair(n):
        a = n % 10
        b = str(n)[0]
        return '{},{}'.format(a, b)

    N = int(input())
    freq = defaultdict(int)
    for i in range(1, N+1):
        p = pair(i)
        freq[p] += 1
    
    res = 0
    for i in range(1, N+1):
        p = pair(i)
        q = ','.join(p.split(',')[::-1])
        res += freq[q]
    print(res)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())

dp = [[-1] * 10 for _ in range(10)]
temp = 0
total = 0

mae_usiro = [[-1] * 2 for _ in range(N+1)]
for i in range(1, N+1):
    mae = int(i / 10 ** int(math.log10(i)))
    usiro = i % 10
    mae_usiro[i] = [mae, usiro]

for i in range(1, N+1):
    mae, usiro = mae_usiro[i]
    if i > 100:
        total += dp[mae][usiro]
    elif dp[mae][usiro] != -1:
        total += dp[mae][usiro]
    else:
        count = 0
        for j in range(1, N+1):
            mae2, usiro2 = mae_usiro[j]
            if mae == usiro2 and usiro == mae2:
                count += 1
        dp[mae][usiro] = count
        total += count
    
print(total)

Submission status: Accepted

Solution code:
N = int(input())
d = dict()
ans = 0
for i in range(1, N + 1):
    if not (str(i)[0], str(i)[-1]) in d:
        d[(str(i)[0], str(i)[-1])] = 1
    else:
        d[(str(i)[0], str(i)[-1])] += 1
for j in d:
    if (j[-1], j[0]) in d:
        ans += d[j] * d[(j[-1], j[0])]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
d = {}
for i in range(1, n+1):
    s = str(i)
    t = (int(s[0]), int(s[-1]))
    if t in d:
        d[t] += 1
    else:
        d[t] = 1

ans = 0
for i, c in d.items():
    r = (i[1], i[0])
    if r in d:
        ans += d[r] * c

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

print(ans)

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:
N=int(input())
d=dict()
ans=0
for i in range(1,N+1):
  if not (str(i)[0],str(i)[-1]) in d:    
    d[(str(i)[0],str(i)[-1])]=1
  else:
    d[(str(i)[0],str(i)[-1])]+=1
for j in d:
  if (j[-1],j[0]) in d:
    ans+=d[j]*d[(j[-1],j[0])]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(n):
    num = str(i+1)
    st = int(num[0])
    en = int(num[-1])
    #print(st,en)
    c[st][en] += 1
#print(*c,sep = "\n")

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


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
count = [10*[0]for _ in range(10)]
ans = 0
for i in range(1,N+1):
  s = str(i)
  if s[0] == '0' or s[-1] == '0':
    pass
  else:
    count[int(s[0])][int(s[-1])] += 1
for j in range(1,10):
  for k in range(1,10):
    ans += count[j][k]*count[k][j]
print(ans)

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
dp = [0]*max(101,(n+1))
for i in range(1,n+1):
    s = str(i)
    if s[0] == s[-1]:
        dp[int(s[0])] += 1
    elif s[-1] == "0":
        continue
    else:
        ima = int(s[0])*10 + int(s[-1])
        dp[ima] += 1
ans = 0
for i in range(1,min(100,n+1)):
    s = str(i)
    if s[0] == s[-1]:
        ans += dp[int(s[0])]**2
        dp[int(s[0])] = 0
    elif s[-1] == "0":
        continue
    else:
        ima = int(s[0])*10 + int(s[-1])
        ima2 = int(s[-1])*10 + int(s[0])
        ans += dp[ima] * dp[ima2] *2
        dp[ima] = 0
        dp[ima2] = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, n + 1):
    l = list(str(i))
    a[int(l[0])][int(l[len(l) - 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:
n=int(input())
A=[0]*105
for i in range(1,n+1):
    x=str(i)
    A[int(x[0]+x[len(x)-1])]+=1
ans=0
for i in range(11,100):
    y=str(i)
    if '0' in y:
        continue
    ans+=A[i]*A[int(y[1]+y[0])]
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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


def read_list():
    return list(read_values())


def gcd(a, b):
    if a < b:
        d, (p, q) = gcd(b, a)
        return d, (q, p)
    if b == 0:
        return a, (1, 0)
    d, (q, p) = gcd(b, a % b)
    q -= a // b * p
    return d, (p, q)


def ch(x, y):
    a1, m1 = x
    a2, m2 = y
    d, (p, q) = gcd(m1, m2)
    if (a2 - a1) % d != 0:
        return (0, -1)

    m = m1 * m2 // d
    r = (a1 + m1 * (a2 - a1) // d * p) % m
    return (r, m)


def main():
    N = int(input())
    D = len(str(N))

    res = 0
    for n in range(1, N + 1):
        p, q = int(str(n)[0]), int(str(n)[-1])
        if q == 0:
            continue
        for k in range(1, D + 1):
            if k == 1:
                res += 1 if p == q else 0
                continue

            if k == D:
                t = int(str(N)[0])
                if t < q:
                    continue
                elif t == q:
                    res += ((N - t * 10 ** (D - 1)) - p) // 10 + 1
                    continue

            res += 10 ** (k - 2)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
c = [[0] * 10 for _ in range(10)]
for k in range(1, n+1):
    i = int(str(k)[0])
    j = k % 10
    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)