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

# import re
# import math
# import collections
# import decimal
# import bisect
# import itertools
# import fractions
# import functools
# import copy
# import heapq
# import decimal
# import statistics
# import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 10
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    mat = [[0 for _ in range(10)] for _ in range(10)]

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

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

    print(ans)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

# 入力を整数に変換して受け取る
def input_int():
    return int(input())


# マイナス1した値を返却
def int1(x):
    return int(x) - 1


# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
    return map(int, input().split())


# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
    return tuple(map(int, input().split()))


# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    n = input_int()


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

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

    return ret


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

Submission status: Accepted

Solution code:
import sys


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


def main():
    N = int(input())
    C = [[0] * 10 for _ in range(10)]
    for i in range(10):
        for j in range(10):
            for n in range(1, N + 1):
                L = len(str(n)) - 1
                a = n // (10 ** L)
                b = n % 10
                if a == i and b == j:
                    C[i][j] += 1
    answer = 0
    for i in range(10):
        for j in range(10):
            answer += C[i][j] * C[j][i]
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

if n <= 10:
    print(n)
else:
    for i in range(1, n+1):
        I = str(i)
        if I[-1] != '0':
            check[int(I[0]) - 1][int(I[-1]) - 1] += 1
    ans = 0
    for i in range(9):
        for j in range(9):
            ans += check[i][j] * check[j][i]
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

for i in range(1, N+1):
    s = str(i)
    head, tail = int(s[0]), int(s[-1])
    if head > 0 and tail > 0:
        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)

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    n = II()
    D = [[0]*10 for _ in range(10)]

    for i in range(1, n+1):
        s = list(str(i))
        l = int(s[0])
        r = int(s[-1])
        # print(s, l, r)
        D[l][r] += 1
    # print(D)

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

import numpy as np

def solve():
    n = II()
    D = np.zeros((10, 10))

    for i in range(1, n+1):
        s = list(str(i))
        l = int(s[0])
        r = int(s[-1])
        # print(s, l, r)
        D[l][r] += 1
    # print(D)

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

for i in range(1, N+1):
    end = i%10
    head = int(str(i)[0])
    c[head][end] += 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:




from collections import defaultdict

N = int(input())


"""
愚直解だと、Aを1~Nまで動かした時、各Aについて何個Bを取れるかを足し合わせると答えが出る。が、これはN<=2*10**5なのでO(N^2)は間に合わない。

事前に先頭がxで末尾がyの個数を、1~Nまで数え上げておくと、各Aについて先頭と末尾を抜き出して、それに対応するBの個数を足し合わせるとO(N)で求められる
"""
d = defaultdict(int)



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


ans = 0
for a in range(1,N+1):
    x = int(str(a)[0])
    y = int(str(a)[-1])
    ans += d[(y,x)]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d={}
for i in range(1,n+1):
  f=i//(10**(len(str(i))-1))
  l=i-(i//10)*10
  if l!=0: 
    c=f*10+l
    if c in d:
      d[c]+=1
    else:
      d[c]=1
ans=0
for i in d:
  if int(str(i)[::-1]) in d:
    ans+=d[i]*d[int(str(i)[::-1])]
print(ans)

Submission status: Accepted

Solution code:
def main():
    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[i*10+j] *d[j*10+i]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    d = {}
    for i in range(0,10):
        for j in range(0,10):
            d[i*10+j] = 0
    
    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[i*10+j] *d[j*10+i]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

t = [0]*(100)
ans = 0
for k in range(1,N+1):
    if k%10 != 0:
        t[10*int(str(k)[0])+int(str(k)[-1])] += 1
for k in range(11,100):
    if k%10 != 0:
        ans += t[k]*t[10*(k%10)+k//10]
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
currentpower=0
for i in range(1,n+1):
  if i>=10**(currentpower+1):
    currentpower+=1
  b=i%10
  a=(i-i%(10**currentpower))//(10**currentpower)
  if b==0:
    continue
  if a==b:
    ans+=1
    if currentpower>0:
      ans+=2
    r=max(0,((i-(a*(10**currentpower))-b)//10))
    ans+=2*r
    for j in range(0,currentpower-1):
      ans+=2*(10**j)
  elif a<b:
    for j in range(0,currentpower-1):
      ans+=2*(10**j)
  else:
    for j in range(0,currentpower):
      ans+=2*(10**j)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

all_count = 0
options = [str(i) for i in range(10)]

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

for i in range(n+1):
    checking = str(i)
    maeato[int(checking[0])][int(checking[-1])]+=1
all_count = 0
for i in range(1,10):
    for j in range(1,10):
        for k in range(1,10):
            for p in range(1,10):
                if i == p and j == k:
                    all_count+=maeato[i][j]*maeato[k][p]
print(all_count)

Submission status: Accepted

Solution code:
from sys import exit

n = int(input())
cs = [[0]*10 for i in range(10)]

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

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

Submission status: Accepted

Solution code:
N=int(input())
L=[[0]*10 for i in range(10)]
for i in range(1,N+1):
    n=str(i)
    first=int(n[0])
    last=int(n[-1])
    L[first-1][last-1]+=1
ans=0
for i in range(1,N+1):
    n=str(i)
    first=int(n[0])
    last=int(n[-1])
    ans+=L[last-1][first-1]

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    dictType = dict()
    for i in range(1, 10):
        for j in range(1, 10): dictType[(i, j)] = 0
    for i in range(1, N + 1):
        bottom = i % 10
        if bottom == 0: continue
        top = i
        while top >= 10: top //= 10
        dictType[(top, bottom)] += 1

    ans = 0
    for i in range(1, N + 1):
        bottom = i % 10
        if bottom == 0: continue
        top = i
        while top >= 10: top //= 10
        ans += dictType[(bottom, top)]
    print(ans)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

for i in range(1, N+1):
    f = int(str(i)[0])
    l = int(str(i)[-1])
    cnt[f][l] += 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())
num = [[0 for _ in range(10)] for j in range(10)]
for i in range(1,N+1):
    s = str(i)
    f = int(s[0])
    l = int(s[-1])
    if l == 0:
        continue
    num[f][l] += 1

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

print(ans//2)

Submission status: Accepted

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

N = int(input())

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

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

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

print (ans)
# for tmp in lst:
#     print (tmp)

Submission status: Accepted

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

n = int(input())

from collections import defaultdict

d = defaultdict(int) # d[(a, b)] : 先頭a, 末尾b

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

ans = 0

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

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#D
import math
import numpy as np
n = int(input())
num = np.zeros((10, 10))
for i in range(1, n + 1):
    a = i // pow(10, (int(math.log10(i))))
    b = i % 10
    num[b][a] += 1
ans=0
for i in range(10):
    for j in range(10):
        ans += num[i][j] * num[j][i]
print(int(ans))

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    default = 0

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

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

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

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
import math
import numpy as np
n = int(input())
ans = 0
num = np.zeros((10,10))
for i in range(1,n+1):
    ma = i // pow(10,int(math.log10(i)))
    mi = i % 10
    num[mi][ma] += 1
for i in range(10):
    for j in range(10):
        ans += num[i][j] * num[j][i]
print(int(ans))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    lis=[[0]*9 for _ in range(9)]
    for i in range(1,n+1):
        si=str(i)
        if int(si[-1])>0:
            lis[int(si[0])-1][int(si[-1])-1]+=1
    cunt=0
    for i in range(9):
        for j in range(9):
            cunt+=lis[i][j]*lis[j][i]
    print(cunt)
    
    

if __name__=='__main__':
    main()

Submission status: Accepted

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

for i in range(1, N+1):
  s = int(str(i)[0])
  t = int(str(i)[-1])
  if s != 0 and t != 0:
    c[(s, t)] += 1

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:
import itertools

n = int(input())

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

for i in range(1, n + 1):
    I = str(i)
    first_d = int(I[0])
    last_d = int(I[-1])
    if first_d == 0 or last_d == 0:
        continue
    counter[first_d][last_d] += 1

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

Submission status: Accepted

Solution code:
N = int(input())
a = [[0] * 9 for i in range(9)]
b = [[0] * 9 for i in range(9)]
ans = 0
for A in range(1,N+1):
    list_a = str(A)
    if list_a[-1] == '0':
        continue
    a[int(list_a[0])-1][int(list_a[-1])-1] += 1
    b[int(list_a[-1])-1][int(list_a[0])-1] += 1
for i in range(9):
    for j in range(9):
        ans += a[i][j]*b[i][j]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
D={}
for i in range(1,N+1):
    i=str(i)
    D[(i[0],i[-1])]=D.get((i[0],i[-1]),0)+1
ans=0
for i in range(1,N+1):
    i=str(i)
    ans+=D.get((i[-1],i[0]),0)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    d=dict()
    for i in range(1,N+1):
        i=str(i)
        head=i[0]
        tail=i[-1]
        if tail=="0":
            continue
        if (head,tail) not in d:
            d[(head,tail)]=1
        else:
            d[(head,tail)]+=1

    ans=0
    s=set()
    for k in d.keys():
        m,l=k[0],k[1]
        if (m,l) in s:
            continue
        if (l,m) in d:
            ans+=d[(m,l)]*d[(l,m)]
            if (m,l)!=(l,m):
                ans+=d[(m,l)]*d[(l,m)]
        s.add((m,l))
        s.add((l,m))

    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
numl = [[0]*9 for _ in range(9)]
for i in range(n):
  s = str(i+1)
  if s[-1]=="0":
    continue
  numl[int(s[0])-1][int(s[-1])-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    ans += numl[i][j] * numl[j][i]
print(ans)

Submission status: Accepted

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

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

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:
def main():
    N = int(input())
    cnttable = [[0] * 9 for _ in range(9)]
    for i in range(1,N+1):
        if i%10 == 0:
            continue
        if i < 10:
            cnttable[i-1][i-1] += 1
        else:
            tmp = list(str(i))
            cnttable[int(tmp[0])-1][int(tmp[-1])-1] += 1

    ans = 0
    for i in range(9):
        for j in range(i, 9):
            if i == j:
                ans += cnttable[i][j] * cnttable[j][i]
            else:
                ans += cnttable[i][j] * cnttable[j][i] * 2

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

for A in range(1,N+1):
    Astr=str(A)
    pre=int(Astr[0])
    suf=int(Astr[-1])
    c[pre][suf]+=1
cnt=0
for i in range(10):
    for j in range(10):
        cnt+=c[i][j]*c[j][i]

print(cnt)

Submission status: Accepted

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

for n in range(1,N+1):
  size = int (math.log10(n) + 1)
  sen= n//(10**(size-1))
  matu =n %10
  c[sen][matu] +=1

count=0

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

Submission status: Accepted

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


for i in range(1,n+1):
	tmp=str(i)
	f=int(tmp[0])
	l=int(tmp[-1])
	if l != 0:
		counter[f-1][l-1]=counter[f-1][l-1]+1


output=0
for i in range(9):
	for j in range(9):
		output+=counter[i][j]*counter[j][i]

print(output)

Submission status: Accepted

Solution code:
import itertools

N = input()
if len(N)==1:
    print(N)
    exit()


gen = (1,2,3,4,5,6,7,8,9)
A = list(itertools.product(gen,gen))
count =0

mem = [[0 for i in range(0,10,1)]for j in range(0,10,1)]

for a,b in A:
    tmp = 0
    if a < int(N[0]):
        for i in range(0,len(N)-1,1):
            tmp+= pow(10,i)
    elif a > int(N[0]):
        for i in range(0,len(N)-2,1):
            tmp+= pow(10,i)
    else:
        tmp +=1
        for i in range(1,len(N)-2,1):
            tmp+= pow(10,i)
        if len(N) > 2:
            tmp+=int(N[1:len(N)-1])+1
            if int(N[len(N)-1]) < b:
                tmp-=1
    if a==b:
        tmp+=1
    mem[a][b]=tmp

for a,b in A:
    count+=mem[a][b]*mem[b][a]

print(count)

Submission status: Accepted

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

counter = defaultdict(int)
for i in range(1, N+1):
    A = str(i)[0]
    B = str(i)[-1]
    counter[A+B] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
start_end_map={}
for i in range(1,n+1):
    char=str(i)
    start=char[0]
    end=char[-1]
    if end=="0":
        continue
    if (start,end) not in start_end_map:
        start_end_map[(start,end)]=0
    if (end,start) in start_end_map:
        start_end_map[(start,end)]+=1
        if start!=end and start_end_map[(end,start)]==0:
            start_end_map[(end,start)]=1
    
counter=0
for key,value in start_end_map.items():
    if value==0:
        continue
    start,end=key
    if (end,start) in start_end_map:
        counter+=start_end_map[(start,end)]*start_end_map[(end,start)]
print(counter)

Submission status: Accepted

Solution code:
inf = 10**15
mod = 10**9+7
n = int(input())
ans=0
dp = [[0 for j in range(10)]for i in range(10)]
for k in range(1,n+1):
    tmp = str(k)
    dp[int(tmp[0])][int(tmp[-1])]+=1
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:
n=int(input())
r=0
l=[[0 for i in range(9)] for j in range(9)]
for i in range(n):
  s=str(i+1)
  a=int(s[0])
  b=int(s[-1])
  if a!=0 and b!=0:
    l[a-1][b-1]+=1
for i in range(9):
  for j in range(9):
    r+=l[i][j]*l[j][i]
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

Submission status: Accepted

Solution code:
N = int(input())

N_s = int(str(N)[0])
N_e = int(str(N)[-1])
if len(str(N)) > 2:
    N_mid = int(str(N)[1:-1])
else:
    N_mid = 0

def search(s, e):
    ans = 0
    for i in range(1, len(str(N))+1):
        if i == 1:
            if s == e and s <= N:
                ans += 1
                continue
        else:
            if i != len(str(N)):
                ans += 10**(i-2)
            else:
                if N_s > s:
                    ans += 10**(i-2)
                elif N_s == s:
                    if N_e >= e:
                        ans += N_mid + 1
                    else:
                        ans += N_mid

    return ans

ret = 0
for i in range(1, 10):
    for j in range(1, 10):
        ret += search(i, j) *  search(j, i)
print(ret)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
def c(i,j):
  ret=0
  for k in range(1,n+1):
    if str(k)[0]==str(i) and str(k)[-1]==str(j):
      ret+=1
  return ret
for i in range(1,10):
  for j in range(i,10):
    if i==j:
      ans+=c(i,j)*c(j,i)
    else:
      ans+=2*c(i,j)*c(j,i)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

freq = {}

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

ans = 0

for i in range(1, n+1):
    p = func(i)
    ans += freq.get((p[1],p[0]), 0)
    
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    n = int(stdin.readline())
    n_len = len(str(n))
    top = str(n)[0]

    ans = 0

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

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

    ans = 0

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Accepted

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

for i in range(1, n+1):
  m = len(str(i))
  top = int(str(i)[0])
  bottom = i % 10
  counter = 0
  if bottom*top == 0:
    continue
  if top == bottom:
    counter += 1
  for j in range(1, 7):
    left = bottom*(10**j) + top
    right = bottom*(10**j) + top + 10**j - 10
 
    if n < left:
      break
    elif left <= n and n <= right:
      counter += (n - left) // 10 + 1
      break
    elif right < n:
      counter += 10**(j-1)
  ans += counter
  
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    count=[[0]*10 for _ in range(10)]
    for i in range(1,n+1):
        s=str(i)
        count[int(s[0])][int(s[-1])]+=1

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


resolve()

Submission status: Accepted

Solution code:
def calc(left, right, N):
    def left_match(x):
        return int(str(x)[0]) == left
    def right_match(x):
        return x % 10 == right
    cnt = 0
    n = 1
    while n <= N:
        if not left_match(n):
            length = len(str(n))
            n += 10**(length - 1)
        elif not right_match(n):
            n += 1
        else:
            cnt += 1
            n += 1
    return cnt


def main():
    N = int(input())
    counter = dict()
    for left in range(1, 10):
        for right in range(1, 10):
            counter[(left, right)] = calc(left, right, N)
    answer = 0
    for left in range(1, 10):
        for right in range(1, 10):
            answer += counter[(left, right)] * counter[(right, left)]
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(1, N+1):
    lon = len(str(i)) - 1
    a = i % 10
    b = i // (10 ** lon)
    Num[a][b] += 1

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

print(ans)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N = int(input())
    presuf = [[0] * 10 for _ in range(10)]
    for i in range(1,N + 1):
        pre = int(str(i)[0])
        suf = i%10
        presuf[pre][suf] += 1
    
    ans = 0
    for i in range(10):
        for j in range(10):
            ans += presuf[i][j]*presuf[j][i]
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
nums = [0 for i in range(100)]
for i in range(1, N + 1):
    i = str(i)
    nums[int(i[0] + i[-1])] += 1
ans = 0
for i in range(1, 10):
    for j in range(i, 10):
        i = str(i)
        j = str(j)
        if i != j:
            ans += nums[int(i + j)] * nums[int(j + i)] * 2
        else:
            n = nums[int(i + j)]
            ans += n * (n - 1)
            ans += n
print(ans)

Submission status: Accepted

Solution code:
def main(n):
  def c(i,j):
    ret=0
    for k in range(1,n+1):
      if str(k)[0]==str(i) and str(k)[-1]==str(j):
        ret+=1
    return ret
  ans=0
  for a in range(1,10):
    for b in range(a,10):
      d=c(a,b)*c(b,a)
      ans+=d
      if a!=b:
        ans+=d
  print(ans)
if __name__=='__main__':
  n=int(input())
  main(n)

Submission status: Accepted

Solution code:
N = int(input())
a = [[0] * 9 for i in range(1, 10)]
b = [[0] * 9 for i in range(1, 10)]
sum_a = 0
for A in range(1,N+1):
    list_a = str(A)
    if list_a[-1] == '0':
        continue
    a[int(list_a[0])-1][int(list_a[-1])-1] += 1
    b[int(list_a[-1])-1][int(list_a[0])-1] += 1
for i in range(9):
    for j in range(9):
        sum_a += a[i][j]*b[i][j]
print(sum_a)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N = ii()

table = dp2(0, 10, 10)

for i in range(1, N+1):
    head = int(str(i)[0])
    tail = int(str(i)[-1])
    table[head][tail] += 1
#print(table)
cnt = 0
for i in range(10):
    for j in range(10):
        cnt += table[i][j] * table[j][i]
        #if i == j:
            #cnt += table[i][j]

print(cnt)

Submission status: Accepted

Solution code:
"""
参考
blog名  あっとのTECH LOG   2020-01-24 
url     https://at274.hatenablog.com/entry/2020/01/24/060000
id:     AT274
"""

#初期入力
N = int(input())
 
#
import numpy as np
cnt = [[0] * 10 for i in range(10)]
#count = np.zeros((10,10),dtype=np.int64) 
for i in range(1,N+1):
    n_left =str(i)[0]   #左端の数字（N)
    n_right =str(i)[-1] #右端の数字（N)
    #cnt[int(n_left) , int(n_right)] +=1
    cnt[int(n_left)][int(n_right)] +=1

count =np.array(cnt,dtype=np.int64)
count_T_copy =count.T.copy()
ans =np.sum(count *count_T_copy)
print(ans)

Submission status: Accepted

Solution code:
"""
参考
blog名  あっとのTECH LOG   2020-01-24 
url     https://at274.hatenablog.com/entry/2020/01/24/060000
id:     AT274
"""

#初期入力
N = int(input())
 
#
import numpy as np
count = np.zeros((10,10),dtype=np.int64) 
for i in range(1,N+1):
    n_left =str(i)[0]   #左端の数字（N)
    n_right =str(i)[-1] #右端の数字（N)
    count[int(n_left) , int(n_right)] +=1

count_T_copy =count.T.copy()
ans =np.sum(count *count_T_copy)
print(ans)

Submission status: Accepted

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

print(ans)