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

a = input()

n = int(a)

s = [[0] * 9 for _ in range(9)]

for i in range(1, n+1):
    j = str(i)
    k = int(j[0])
    l = int(j[-1])
    if l != 0:
        s[k-1][l-1] += 1

ans = 0

#print(s)

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    comb_count = [[0 for i in range(10)] for j in range(10)]
    res = 0

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

    for l in range(1, 10):
        for r in range(1, 10):
            res += comb_count[l][r] * comb_count[r][l]
        
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
memo = [[0] * 10 for i in range(10)]

def kazu(start_num,end_num):
    global memo
    if memo[start_num][end_num] > 0:
        #print('memo',start_num,end_num)
        return memo[start_num][end_num]
    else:
        ans = 0
        #1桁
        if start_num == end_num:
            if start_num <= n:
                #print(start_num)
                ans += 1
        #2桁
        if start_num * 10 + end_num <= n:
            #print(start_num * 10 + end_num)
            ans += 1
        #3桁
        for i in range(10):
            if start_num * 100 + i * 10 + end_num <= n:
                #print(start_num * 100 + end_num)
                ans += 1
            else:
                break
        #4桁
        for i in range(100):
            if start_num * 1000 + i * 10 + end_num <= n:
                #print(start_num * 1000 + end_num)
                ans += 1
            else:
                break
        #5桁
        for i in range(1000):
            if start_num * 10000 + i * 10 + end_num <= n:
                #print(start_num * 10000 + end_num)
                ans += 1
            else:
                break
        #6桁
        for i in range(10000):
            if start_num * 100000 + i * 10 + end_num <= n:
                #print(start_num * 100000 + end_num)
                ans += 1
            else:
                break
        memo[start_num][end_num] = ans
        #print(start_num,end_num,'---',ans)
        return ans

n = int(input())
str_n = str(n)
kotae = 0
#print(memo)
for a in range(1,n+1):
    if a%10!=0:
        str_a = str(a)
        #print('-----',a,'-----',int(str_a[-1]),int(str_a[0]))
        kotae += kazu(int(str_a[-1]),int(str_a[0]))
        #print(memo)
print(kotae)

Submission status: Accepted

Solution code:
memo = [[0] * 10 for i in range(10)]

def kazu(start_num,end_num):
    global memo
    if memo[start_num][end_num] > 0:
        #print('memo',start_num,end_num)
        return memo[start_num][end_num]
    else:
        ans = 0
        #1桁
        if start_num == end_num:
            if start_num <= n:
                #print(start_num)
                ans += 1
        #2桁
        if start_num * 10 + end_num <= n:
            #print(start_num * 10 + end_num)
            ans += 1
        #3桁
        for i in range(10):
            if start_num * 100 + i * 10 + end_num <= n:
                #print(start_num * 100 + end_num)
                ans += 1
            else:
                break
        #4桁
        for i in range(100):
            if start_num * 1000 + i * 10 + end_num <= n:
                #print(start_num * 1000 + end_num)
                ans += 1
            else:
                break
        #5桁
        for i in range(1000):
            if start_num * 10000 + i * 10 + end_num <= n:
                #print(start_num * 10000 + end_num)
                ans += 1
            else:
                break
        #6桁
        for i in range(10000):
            if start_num * 100000 + i * 10 + end_num <= n:
                #print(start_num * 10000 + end_num)
                ans += 1
            else:
                break
        memo[start_num][end_num] = ans
        #print(start_num,end_num,'---',ans)
        return ans

n = int(input())
str_n = str(n)
kotae = 0
#print(memo)
for a in range(1,n+1):
    if a%10!=0:
        str_a = str(a)
        #print('-----',a,'-----',int(str_a[-1]),int(str_a[0]))
        kotae += kazu(int(str_a[-1]),int(str_a[0]))
        #print(memo)
print(kotae)

Submission status: Accepted

Solution code:
N = int(input())


def digit(i, j):
    """
    1 <= k <= N となる k で、先頭の桁が i , 末尾の桁が j となる物の数を返す
    """
    count = 0
    for k in range(1, N+1):
        if str(k)[0] == str(i) and k % 10 == j:
            count += 1
    return count


seen = {}
for i in range(1, 10):
    for j in range(1, 10):
        seen[(i, j)] = digit(i, j)

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


print(res)

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
res = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, N + 1):
    if i % 10:  res[int(str(i)[0]) - 1][i % 10 - 1] += 1
ans = 0
for i in range(1, N + 1):
    if i % 10:  ans += res[i % 10 - 1][int(str(i)[0]) - 1]
print(ans)

Submission status: Accepted

Solution code:

N = int(input())

dp = [[0] * 10 for _ in range(10)]
for n in range(1, N + 1):
    # last
    i = n % 10

    # first
    while n >= 10:
        n //= 10
    j = n % 10

    dp[i][j] += 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 sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)


N = int(input())

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

for i in range(1,N+1):
    n = str(i)
    a[int(n[0])][int(n[-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())

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

for i in range(n+1):
  i=str(i)
  l[int(i[0])][int(i[-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:
def solve():
    N = int(input())
    cnt = [[0]*10 for i in range(10)]
    for i in range(1, N+1):
        target_str = str(i)
        cnt[int(target_str[0])][int(target_str[-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)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
import collections
import bisect


def main():
    n = int(input())
    ac, bc = collections.Counter([str(i + 1)[0] + str(i + 1)[-1] for i in range(
        n)]), collections.Counter([str(i + 1)[-1] + str(i + 1)[0] for i in range(n)])
    print(sum(ac[i] * bc[i] for i in ac.keys()))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import collections
import bisect


def main():
    n = int(input())
    a = [str(i + 1)[0] + str(i + 1)[-1] for i in range(n)]
    b = [str(i + 1)[-1] + str(i + 1)[0] for i in range(n)]
    ac, bc = collections.Counter(a), collections.Counter(b)
    print(sum(ac[i] * bc[i] for i in ac.keys()))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import collections
import bisect


def main():
    n = int(input())
    a = [str(i + 1)[0] + str(i + 1)[-1] for i in range(n)]
    b = [str(i + 1)[-1] + str(i + 1)[0] for i in range(n)]
    ac, bc = collections.Counter(a), collections.Counter(b)
    acKeys = ac.keys()
    bcKeys = bc.keys()
    cnt = 0
    for i in acKeys:
        cnt += ac[i] * bc[i]
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(1, n+1):
    if i >= 10**5:
        tmp1 = i//10**5
    elif i >= 10**4:
        tmp1 = i//10**4
    elif i >= 10**3:
        tmp1 = i//10**3
    elif i >= 10**2:
        tmp1 = i//10**2
    elif i >= 10:
        tmp1 = i//10
    else :
        tmp1 = i
    tmp2 = i % 10
    a[tmp1][tmp2] += 1

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

Submission status: Accepted

Solution code:
def main():

    N = int(input())

    d = dict()
    for i in range(1, N+1):
        u = int(str(i)[0])
        v = int(str(i)[-1])
        d[(u, v)] = d.get((u, v), 0) + 1

    ans = 0
    for u, v in d:
        if (v, u) in d:
            ans += d[(u, v)] * d[(v, u)]
    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
##以下ググった（https://at274.hatenablog.com/entry/2020/01/24/060000）
n = int( input() )

cnt = [ [ 0 for i in range( 10 ) ] for j in range( 10 ) ]
# cnt[ i ][ j ] = 先頭がi，末尾がjである，n以下の整数の個数

for k in range( n + 1 ):
    head = int( str( k )[ 0 ] )
    foot = int( str( k )[ -1 ] )
    cnt[ head ][ foot ] += 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:
# 問題:文字の先頭が'i'で末尾が'j', 且つ文字の先頭が'j'で末尾が'i'のペアはいくつ？
# 'n'以下の全ての数について 先頭を'i', 末尾が'j'としたDBを作成

n = int(input())
# cnt[i][j] := 先頭が'i', 末尾が'j'の数
cnt = [[0] * 10 for _ in range(10)]

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

# 先頭が'i'で末尾が'j'の総数 [掛ける] 先頭が'j'で末尾が'i'の総数の総和が'ans'
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 main():
    import sys
    from collections import defaultdict

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

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



    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

if n < 10:
    print(n)
    quit()

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

d = 1
for i in range(10, n + 1):
    if i % (d * 10)== 0:
        d *= 10
    h = i // d
    f = i % 10
    count[h][f] += 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:
N = int(input())
counter = [[0 for _ in range(10)] for _ in range(10)]
for k in range(1, N+1):
  counter[k%10][int(str(k)[0])] += 1
ans = 0
for k in range(1, 10):
  for j in range(1, 10):
    ans += counter[k][j]*counter[j][k]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

for n in range(1,N+1):
    n=str(n)
    n_s=int(n[0])
    n_e=int(n[-1])
    
    C[n_s][n_e]+=1
    
ans=0
for c_1 in range(10):
    for c_2 in range(10):
        ans += C[c_1][c_2]*C[c_2][c_1]
        
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(1,10):
  for j in range(1,10):
    ans+=cnt[i][j]*cnt[j][i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c = [[0] * 10 for _ 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:
# !/usr/bin/python3

"""
https://atcoder.jp/contests/abc152/tasks/abc152_d
Low Elements.
"""

def solve(n):
    res = 0

    for num in range(n+1):
        start = int(str(num)[0])
        end = int(str(num)[-1])

        for i in range(1, 10):
            for j in range(1, 10):
                if i == start and j == end: dp[i][j] += 1

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

    return res


if __name__ == "__main__":
    n = int(input())
    dp = [[0 for _ in range(10)] for _ in range(10)]

    print(solve(n))

Submission status: Accepted

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

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

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

#print(dp)
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:
# !/usr/bin/python3

"""
https://atcoder.jp/contests/abc152/tasks/abc152_d
Low Elements.
"""

def solve(n):
    res = 0

    for num in range(n+1):
        start = int(str(num)[0])
        end = int(str(num)[-1])

        for i in range(10):
            for j in range(10):
                if i == start and j == end: dp[i][j] += 1

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

    return res


if __name__ == "__main__":
    n = int(input())
    dp = [[0 for _ in range(10)] for _ in range(10)]

    print(solve(n))

Submission status: Accepted

Solution code:
from math import factorial
n=int(input())
d=[[0]*10 for _ in range(10)]
for i in range(1,n+1):
  j=i
  while True:
    if j//10:j//=10
    else:break
  d[j][i%10]+=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:
x=int(input())

dp=[[0 for n in range(10)] for n in range(10)]
dp1=dp.copy()

for n in range(x+1):
    v=str(n)
    p=int(v[0])
    q=int(v[-1])
    for a in range(10):
        for b in range(10):
            if a==p and b==q:
                dp[a][b]+=1

res=-1

for n in range(10):
    for k in range(10):
        res+=dp[n][k]*dp[k][n]

print(res)

Submission status: Accepted

Solution code:


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    C = [[0]*10 for i in range(10)]
    N = read_int()
    for n in range(1, N+1):
        first = last = n%10
        while n != 0:
            first = n%10
            n //= 10
        C[first][last] += 1
    answer = 0
    for i in range(0, 10):
        for j in range(0, 10):
            answer += C[i][j]*C[j][i]
    return answer


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

N = int(readline())

from collections import defaultdict
dic = defaultdict(int)

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

Submission status: Accepted

Solution code:
n = int(input()) 

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

for i in range(n+1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    if a == 0 or b == 0:
        continue
    cnt[a][b] += 1

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

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(1, n + 1):
    now = str(i)
    pre, pos = int(now[0]), int(now[-1])
    if pos == 0: continue
    count[pre][pos] += 1

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

print(res)

Submission status: Accepted

Solution code:
n = int(input())

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())

cnts = defaultdict(int)
for i in range(1, N + 1):
    if i % 10 == 0:
        continue
    digits = []
    while i > 0:
        i, r = divmod(i, 10)
        digits.append(r)
    digits = digits[::-1]
    cnts[(digits[0], digits[-1])] += 1

ans = 0
for (head, tail), c1 in cnts.items():
    c2 = cnts.get((tail, head), 0)
    ans += c1 * c2
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
dp=[[0]*10 for _ in range(10)]
for i in range(1,N+1):
  a=str(i)
  dp[int(a[0])][int(a[-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:
from collections import defaultdict
N = int(input())
dic = defaultdict(int)
for n in range(1,N+1):
    s = str(n)
    dic[(int(s[0]), int(s[-1]))] += 1

res = 0
for key, value in dic.items():
    a, b = key
    cnt = dic.get((b,a),0)
    res += value * cnt

print(res)

Submission status: Accepted

Solution code:
n = int(input())
num = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1, n + 1):
    num[int(str(i)[0])][int(str(i)[-1])] += 1
ans = 0
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:
N = int(input())
A = [[0 for _ in range(9)] for _ 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:
from sys import stdin
input = stdin.readline


def check(min_, max_, x):
  ret = sum([10**gap for gap in range(len(str(x))-2)])
  if min_ <= x:
    ret += int((min(max_, x) - min_)/10) + 1
  return ret


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

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline


def check(min_, max_, x):
  if x < min_:
    return 0
  else:
    return int((min(max_, x) - min_)/10) + 1


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

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      # max_ = int(str(l) + '9'*(len(str(N))-3) + str(r))
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      # print(l, r, sum_ - temp)

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


def check(min_, max_, x):
  if max_ <= x:
    # return 10**max(0, len(str(min_))-2)
    return int((max_ - min_)/10) + 1
  elif min_ <= x:
    # ret = max(1, int(str(x)[1:-1]) if len(str(x)) > 2 else 1)
    # if int(str(min_)[-1]) <= int(str(x)[-1]):
    #   ret += 1
    # return ret
    return int((x - min_)/10) + 1
  else:
    return 0


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

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      # max_ = int(str(l) + '9'*(len(str(N))-3) + str(r))
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      # print(l, r, sum_ - temp)

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


def check(min_, max_, x):
  if max_ <= x:
    return 10**max(0, len(str(min_))-2)
  elif min_ <= x:
    ret = max(1, int(str(x)[1:-1]) if len(str(x)) > 2 else 1)
    if int(str(min_)[-1]) <= int(str(x)[-1]):
      ret += 1
    return ret
  else:
    return 0


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

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      # max_ = int(str(l) + '9'*(len(str(N))-3) + str(r))
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      # print(l, r, sum_ - temp)

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

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

def main():
    n = int(input())
    d = defaultdict(int)
    for i in range(1, n+1):
        s = str(i)
        a = s[0]
        b = s[-1]
        d[a, b] += 1
    ans = 0
    d_items = list(d.items())
    for (a, b), v in d_items:
        ans += v * d[b,a]
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

for n in range(1,N+1):
  y = str(n)
  A[int(y[0])][int(y[-1])] += 1
  #print(y[0], y[-1])

#print(A)
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())
# initialize
count = []
for i in range(0, 10):
  count.append([])
  for j in range(0, 10):
    count[i].append(0)

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

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

m = {}
for i in range(1, N + 1):
  x = str(i)
  if '0' in [x[0], x[-1]]:
    continue
  key = str([x[0], x[-1]])
  add = 1
  
  if key not in m:
    m[key] = add
  else:
    m[key] += add

ans = 0
for i in range(1, 10):
  for j in range(1, 10):
    key = str([str(i), str(j)])
    if key in m:
      d = m[key]
      if i == j:
        ans += d ** 2
      elif str([str(j),str(i)]) in m:
        ans += d * m[str([str(j),str(i)])]
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys

sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
from copy import deepcopy
import queue
from collections import deque





def main():
    num = int(input())

    data = [[0 for i in range(10)] for j in range(10)]
    for i in range(1, num + 1):
        now_str = str(i)
        data[int(now_str[0])][int(now_str[-1])] += 1

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

    # print(data)
    print(ans)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

# head and tailのつもり
h_and_t = [[0 for _ in range(10)] for _ in range(10)]

for i in range(1, n + 1):
    s = str(i)
    # 一番左がpで一番右がq、のように1からNまでの数を分類
    # 10と100は同じ、2と22は同じ、など
    # 0から始まる数字はないので、h_and_t[0]はNの値に関わらず[0,0,...0]となる
    h_and_t[int(s[0])][int(s[-1])] += 1

#print(h_and_t)

ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        # i=2,j=3なら、(23,32),(23,302)...などが拾える
        # i=3,j=2なら、(32,23),(32,203)...

        # Aの場合の数*Bの場合の数
        ans += h_and_t[i][j] * h_and_t[j][i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
for i in range(1,n+1):
    i2 = str(i)
    if int(i2[-1])==0:
        pass
    elif int(i2[0]) > int(i2[-1]):
        for j in range(len(i2)-1):
            ans+= (10**j)*2
    elif int(i2[0]) < int(i2[-1]):
        for j in range(len(i2)-2):
            ans+= (10**j)*2
    elif len(i2)>2:
        for j in range(len(i2)-2):
            ans+= (10**j)*2
        ans+=3
        ans+=int(i2[1:-1])*2
    else:
        ans+=len(i2)*2-1
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())

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, 10):
    for j in range(1, 10):
        ans += memo[i][j] * memo[j][i]
print(ans)

Submission status: Accepted

Solution code:
import math
from collections import Counter

N = int(input())

c = Counter()

ans = 0
for n in range(1, N + 1):
    left = n // (10 ** int(math.log10(n)))
    right = n % 10
    c[(left, right)] += 1

for left, right in c.keys():
    ans += c[(left, right)] * c[(right, left)]

print(ans)

Submission status: Accepted

Solution code:
n=int(input(""))
le=len(str(n))
ten=10**(le-1)
one=int(str(n)[-1])

b=[]
for k in range(9):
    a=[]
    for i in range(9):
        a+=[0]
    b+=[a]
    
if(le==1):
    print(1)
else:
    
    for i in range(9):
            s=0
            if(ten*(i+2)<=n):
                s=10**(le-2)
                for k in range(9):
                    b[i][k]+=s
            elif(ten*(i+1)<n):
                s=int((n-ten*(i+1))/10)+1
                if(one==0):
                    s-=1
                for k in range(9):
                    
                    b[i][k]+=s
                    if (k+1==one):
                        s-=1
    
    for i in range(9):
        b[i][i]=1+b[i][i]
        
    s=0    
    for i in range(2,le):
            s+=10**(i-2)
    
    c=0
    for i in range(9):
        for k in range(9):
            c+=(b[i][k]+s)*(b[k][i]+s)
    
            
        
        
    print(c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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