Problem:


Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

1 \leq N \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008



Sample input:
25


Sample output:
17

Submission status: Accepted

Solution code:
from collections import defaultdict
"""
N 以下の全ての整数に対して, (先頭, 末尾) を数える.
(先頭, 末尾), (末尾, 先頭)の組は, 独立しているので掛け算で求められる.
"""
n = int(input())
ansl = defaultdict(lambda: 0)
for i in range(1,n+1):
	sentou = int(str(i)[0])
	ushiro = int(str(i)[-1])
	ansl[(sentou, ushiro)] += 1
ans = 0
for i in range(1, 10):
	for j in range(1, 10):
		ans += ansl[(i, j)]*ansl[(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):
    cnt[int(str(i)[0])][int(str(i)[-1])] += 1

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

print(res)

Submission status: Accepted

Solution code:
n = input()
l = len(n)

table = [[0] * 9 for _ in range(9)]
for i in range(9):
    for j in range(9):
        if l == 1:
            if i + 1 <= int(n) and i == j:
                table[i][j] += 1
        elif l == 2:
            if 10 * (i + 1) + (j + 1) <= int(n):
                table[i][j] += 1
            if i == j:
                table[i][j] += 1
        else:
            if i + 1 < int(n[0]):
                for k in range(l-1):
                    table[i][j] += 10 ** k
            elif i + 1 == int(n[0]):
                if j + 1 <= int(n[-1]):
                    table[i][j] += int(n[1:l-1]) + 1
                else:
                    table[i][j] += int(n[1:l-1])
                for k in range(l-2):
                    table[i][j] += 10 ** k
            else:
                for k in range(l-2):
                    table[i][j] += 10 ** k
            if i == j:
                table[i][j] += 1

res = 0
for i in range(9):
    for j in range(9):
        res += table[i][j] * table[j][i]

print(res)

Submission status: Accepted

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

for i in range(1,N+1):
  i = str(i)
  if i[0] == '0' or i[-1] == '0':
    pass
  else:
    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:
n = int(input())

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

for i in range(n+1):
  if i % 10 != 0:
    str_i = str(i)
    mat[int(str_i[0])][int(str_i[-1])] += 1
c = 0
for y in range(1, 10):
  for x in range(y, 10):
    if y == x:
      c += mat[y][x] * mat[x][y]
    else:
      c += mat[y][x] * mat[x][y] * 2
print(c)

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)
    a = int(s[0])
    b = int(s[-1])
    c[a][b] += 1

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


print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def foo(n,x,y):
    l = len(n)
    sm = 0
    if x==y and (l>1 or n[0]>=x):
        sm = 1
    if l==1:
        return sm
    for i in range(2,l):
        sm += 10**(i-2)
    if x==n[-1]:
        sm += bar(n[:-1],y)
    elif x<n[-1]:
        sm += 10**(l-2)
    #ddprint(f"foo {n=} {x=} {y=} {sm=}")
    return sm

def bar(n,y):
    l = len(n)
    if l==1:
        return 1 if n[0]>=y else 0
    sm = (n[-1])*10**(l-2) + bar(n[:-1],y)
    return sm

nn = inn()
nnn = nn
n = []
while nn>0:
    n.append(nn%10)
    nn //= 10
sm = 0
for a in range(1,nnn+1):
    y = a%10
    if y==0:
        continue
    aa = a
    while aa>0:
        x = aa
        aa //= 10
    sm += foo(n,y,x)
print(sm)

Submission status: Accepted

Solution code:
n = int(raw_input())
f = []
for i in range(10):
	f.append([0 for _ in range(10)])
ans = 0

for a in range(1, n + 1):
	val = a
	first, last = 0, val % 10
	while val != 0:
		first = val % 10
		val = val / 10
	f[first][last] += 1

for a in range(1, n + 1):
	val = a
	first, last = 0, val % 10
	while val != 0:
		first = val % 10
		val = val / 10
	ans += f[last][first]

print ans

Submission status: Accepted

Solution code:
n=input()
dic={}
for i in range(1,int(n)+1):
  wkstr=str(i)[0]+str(i)[len(str(i))-1]
  if wkstr in dic:
    dic[wkstr]+=1
  else:
    dic[wkstr]=1
ans=0
for key in dic.keys():
  revkey=key[len(key)-1]+key[0]
  if key==revkey:
    ans+=dic.get(key)*dic.get(revkey)
  else:
    ans+=dic.get(key)*dic.get(revkey,0)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
c=[[0 for _ in range(10)] for _ in range(10)]
for h in range(1,N+1):
    num=str(h)
    c[int(num[-1])][int(num[0])]+=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:
#D
N=int(input())
ans=0
D=[[0 for i in range(10)] for j in range(10)]

for i in range(1,10):
    for j in range(1,10):
        for k in range(1,N+1):
            if str(i)==str(k)[0] and str(j)==str(k)[-1]:
                D[i][j]+=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:
import sys
N = int(sys.stdin.readline().rstrip())

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

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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
t = [[0]*9 for i in range(9)]
for j in range(1, n+1):
    if int(str(j)[-1])==0:
        continue
    t[int(str(j)[0])-1][int(str(j)[-1])-1]+=1
ans = 0
for i in range(9):
    for j in range(9):
        ans += t[i][j]*t[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):
  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:
from collections import defaultdict
def main():
    n = int(input())
    d = defaultdict(int)
    for i in range(n):
        num = str(i + 1)
        head = num[0]
        tail = num[-1]
        d[(head, tail)] += 1

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline().strip()

def C(first, last, ndigs, N):
    ans = 0
    curr = first * 10 ** (ndigs - 1) + (last if ndigs > 1 else 0)
    incs = 0
    while curr <= N and incs < 10 ** (ndigs - 1):
        ans += 1
        curr += 10
        incs += 10
    return ans

def solve(N: int):
    ndigs = len(str(N))
    ans = 0
    for ndigsA in range(1, ndigs + 1):
        for ndigsB in range(1, ndigs + 1):
            for first in range(1, 10):
                for last in range(1, 10):
                    if (ndigsA > 1 and ndigsB > 1) or first == last:
                        ans += C(first, last, ndigsA, N) * C(last, first, ndigsB, N)
    print(ans)

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
    solve(N)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
lis = [[0 for i in range(10)] for j in range(10)]
for i in range(1, n+1):
    s = str(i)
    l = int(s[0])
    r = int(s[-1])
    lis[l][r] += 1
ans = 0
for i in range(10):
    for j in range(10):
        if i == j:
            ans += lis[i][j] ** 2
        else:
            ans += lis[i][j] * lis[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys

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


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

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

    for j in range(1, 10):
        for k in range(1, 10):
            res += numbers[j][k] * numbers[k][j]

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
d = defaultdict(int)
for x in range(1, N + 1):
    s = str(x)
    l = int(s[0])
    r = int(s[-1])
    d[(l, r)] += 1

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

Submission status: Accepted

Solution code:
n=int(input())
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:
from collections import defaultdict

def abc152_d():
    N = int(input())
    freq = defaultdict(int)
    for i in range(1, N+1):
        s = str(i)
        freq[(s[0], s[-1])] += 1  # (先頭,末尾) となる個数を数える

    ans = 0
    for i in range(1, N+1):
        s = str(i)
        ans += freq[(s[-1], s[0])]  # ペアになる (末尾, 先頭) の個数を足していく
    print(ans)

abc152_d()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    # cnt[a][b] := 先頭がa,末尾がqである数の個数
    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 a in range(10):
        for b in range(10):
            ans += cnt[a][b]*cnt[b][a]
    print(ans)

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    # ある数値nについて、n[0]..n[-1]とすると
    # 1~n-1の中のある数値mに対して、n[0]==m[-1]&&n[-1]==m[0]となるようなmがいくつ存在するか調べたい
    # n:1...3 -> m:3...1かつlen(n)>len(m)を満たすもの*2
    # n:3...1 -> m:1...3かつlen(n)>=len(m)を満たすもの*2
    # n:1...1 -> m:1...1かつlen(n)>=len(m)を満たすもの*2
    # 下のパターンで、n[0]==n[-1]のときだけ(n,n)が重複するので-1する
    if N<=9:
        print(N)
    else:
        count = 9
        for n in range(10,N+1):
            n = str(n)
            nlen = len(n)
            if n[-1] == '0':
                continue
            if n[0]<n[-1]:
                tmp = int('1'*(nlen-2)) if nlen>2 else 0
                count += tmp*2
            elif n[0]>n[-1]:
                tmp = int('1'*(nlen-1)) if nlen>2 else 1
                count += tmp*2
            else:
                # 2桁以上(10~)と比較
                tmp = int('1'*(nlen-2))+int(n[1:-1])+1 if nlen>2 else 1
                count += tmp*2-1
                # 1桁(1~9)と比較
                count += 2
        print(count)

main()

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)
    d[(s[0], s[-1])] += 1


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


print(ans)

Submission status: Accepted

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

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

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

print(cnt)

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = lambda: sys.stdin.readline().rstrip()


def solve():
    N = int(input())
    d = np.zeros((9, 9), dtype='i8')

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

    ans = 0
    for i in range(9):
        for j in range(9):
            if i == j:
                ans += d[i][j] * (d[i][j] - 1) + d[i][j]
            else:
                ans += d[i][j] * d[j][i]

    # print(d)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
A= int(input())
l=[[0]*10 for _ in range(10)]
for i in range(1,A+1):
   n=str(i)
   if n[0] == "0" or n[-1] == "0":
      continue
   l[int(n[0])][int(n[-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:
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

N = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

N = int(input())
sN = str(N)
l = len(sN)

ans = 0
for n in range(1, min(10, N+1)):
    sn = str(n)
    ans += 1
    
    cnt = 1
    for i in range(2, l):
        ans += cnt
        cnt *= 10

    if l == 2:
        if sN >= sn[-1] + sn[0]:
            ans += 1
    elif l > 2 and sN[0] >= sn[-1]:
        if sN[0] == sn[-1]:
            hoge = int(sN[1:-1])
            if sN[-1] < sn[0]:
                hoge -= 1
            ans += hoge + 1
        elif sN[0] > sn[-1]:
            ans += cnt

for n in range(11, N+1):
    sn = str(n)
    if sn[-1] != "0":
        if n > 10 and sn[0] == sn[-1]:
            ans += 1


        cnt = 1
        for i in range(2, l):
            ans += cnt
            cnt *= 10

        if l == 2:
            if sN >= sn[-1] + sn[0]:
                ans += 1
        elif l > 2 and sN[0] >= sn[-1]:
            if sN[0] == sn[-1]:
                hoge = int(sN[1:-1])
                if sN[-1] < sn[0]:
                    hoge -= 1
                ans += hoge + 1
            elif sN[0] > sn[-1]:
                ans += cnt

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()

def get_head(x):
    while x:
        b = x%10
        x //= 10
    return b

dp = [[0] * 10 for i in range(10)]
for i in range(1,n+1):
    h = get_head(i)
    t = i%10
    dp[h][t] += 1

for i in range(10):
    dp[0][i] = 0
    dp[i][0] = 0

ans = 0
for i in range(1,n+1):
    h = get_head(i)
    t = i%10
    ans += dp[t][h]

print(ans)

Submission status: Accepted

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

for i in range(1, n+1):
    s = str(i)
    f = int(s[0])
    r = int(s[-1])
    cif[f][r] += 1
res = 0

for i in range(10):
    for j in range(10):
        res += cif[i][j]*cif[j][i]
print(res)

Submission status: Accepted

Solution code:
n = int(input())

num_cnt = [ [0]*9  for _ in range(9)]
for i in range(n):
    num = i+1
    num_s = str(num)
    top = int(num_s[0]) -1
    last = int(num_s[-1]) -1
    if top == -1 or last == -1:
        continue
    num_cnt[top][last] += 1

ans = 0
for x in range(0,9):
    for y in range(0,9):
        ans += num_cnt[x][y] * num_cnt[y][x]
print(ans)

Submission status: Accepted

Solution code:
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N = Z()
    maxl = len(str(N))
    output = 0
    for n in range(1, N+1):
        if n%10 == 0: continue
        s = str(n)
        if s[0] == s[-1]:
            for j in range(1, maxl+1):
                if j == 1:
                    output += 1
                    continue
                if j == 2:
                    if 10*int(s[-1]) + int(s[0]) <= N:
                        output += 1
                    continue
                if j < maxl:
                    output += 10 ** (j-2)
                    continue
                #when j == maxl
                if int(s[-1]) < int(str(N)[0]):
                    output += 10 ** max(0, j-2)
                elif s[-1] == str(N)[0]:
                    output += int(str(N)[1:maxl-1]) + 1
                    if int(s[0]) > int(str(N)[-1]): output -= 1
        else:
            for j in range(2, maxl+1):
                if j < maxl:
                    output += 10 ** (j-2)
                    continue
                # when j == maxl
                if int(s[-1]) < int(str(N)[0]):
                    output += 10 ** (j-2)
                if s[-1] == str(N)[0]:
                    if j == 2:
                        if 10*int(s[-1]) + int(s[0]) <= N:
                            output += 1
                    else:
                        output += int(str(N)[1:maxl-1]) + 1
                        if int(s[0]) > int(str(N)[-1]): output -= 1
    print(output)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())

def pair(x):
    a=x%10
    b=int(str(x)[0])
    return (a,b)
dic={}
for i in range(1,n+1):
    p=pair(i)
    if(dic.get(p)==None):dic[p]=1
    else:dic[p]+=1
ans=0
for i in range(1,n+1):
    a,b=pair(i)
    p=(b,a)
    if(not dic.get(p)==None):ans+=dic[p]
print(ans)

Submission status: Accepted

Solution code:
N = input()
l = len(N)
if l == 1:
    print(N)
    exit()
nf = N[0]
ne = N[-1]
nm = 0
base = 1
if l > 2:
    nm = int(N[1:l-1:])
for i in range(1,l-2):
    base += 10**i
ans = 0

for i in range(int(N)+1):
    i = str(i)
    f = i[0]
    e = i[-1]
    if e != "0":
        if f == e:
            ans += 1
        if e < nf:
            ans += base
            if l > 2:
                ans += 10**(l-2)
        elif e == nf:
            if f <= ne:
                ans += base
                if l>2:
                    ans += nm + 1
            else:
                ans += base
                ans += nm
        else:
            if l >2:
                ans += base
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    n = str(N)
    
    cnt = [[0] * (10) for _ in range(10)]
    
    for i in range(1, N+1):
        a = int(str(i)[0])
        b = i%10
        cnt[a][b] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += cnt[i][j]*cnt[j][i]
    print(ans)
main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())

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

for i in range(1,N+1):
    if i%10==0:
      continue
    s=str(i)
    up=int(s[0])
    down=int(s[-1])
    cnt[up][down]+=1

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from collections import deque
import math
import bisect
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
grid=[[0]*10 for i in range(10)]
for i in range(1,n+1):
    a=int(str(i)[0])
    b=int(str(i)[-1])
    grid[a][b]+=1
ans=0
for i in range(10):
    for k in range(10):
        ans+=grid[i][k]*grid[k][i]
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(raw_input())
def g( a, b , n):
	count = 0
	## CASE 1
	if a == b and a <=n : 
		count +=1
	## CASE 2
	if a *10 + b <= n:
		count +=1
	
	if len(str(n)) <=2:
		return count

	## CASE 3
	s = str(n)
	if len(s) - 3 >= 1:
		count += 10 ** (len(s) - 3)

	## CASE 4

	s = str(n)
	if a == int(s[0]):
		m = s[1:-1]

		if m != '':

			#0,m-1
			count += int(m)
			if b <= int(s[-1]):
				count +=1
	elif a < int(s[0]):
		count += 10 ** (len(s) - 2)

	return count


h = collections.Counter()
for k in range(1, n+1):
	ks = str(k)
	a,b = int(ks[0]), int(ks[-1])
	h[(a,b)] +=1
s= 0
for a in range(1,10):
	for b in range(1,10):
		s += h[(a,b)] * h[(b,a)]

print s

Submission status: Accepted

Solution code:
n=int(input())
def func(i,j,m): #m以下の整数で先頭がi末尾がjであるものの個数
  ret=0
  s=len(str(m))
  m0=int(str(m)[0])
  if s==1:
    if i==j and i<=m:
      return 1
    else:
      return 0
  if s==2:
    if 10*i+j<=m:
      return 2 if i==j else 1
    else:
      return 1 if i==j else 0
  if i<m0:
    for si in range(s-1):
      ret+=pow(10,si)
  elif i==m0:
    if j<=int(str(m)[-1]):
      ret+=int(str(m)[1:-1])+1
    else:
      ret+=int(str(m)[1:-1])
    for si in range(s-2):
      ret+=pow(10,si)
  elif i>m0:
    for si in range(s-2):
      ret+=pow(10,si)
  return ret if i!=j else ret+1
ans=0
d={}
for i in range(1,10):
  for j in range(1,10):
    a=func(i,j,n)*func(j,i,n)
    ans+=a
    #if a>0:
    #  print(i,j,func(i,j,n),func(j,i,n))

print(ans)

Submission status: Accepted

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

from collections import defaultdict

def f(x):
    return (int(str(x)[0]), int(str(x)[-1])) 
def main():
    N = int(readline())
    df = defaultdict(int)
    for i in range(1, N+1):
        df[f(i)] += 1
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans +=  df[(i, j)]*df[(j, i)]
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
p = [[0 for i in range(10)] for j  in range(10)]
for i in range(1,10):
    for j in range(1,10):
        if i == j:
            if i <= n:
                p[i][j] += 1
        now = ""
        d = deque()
        d.append("")
        while d:
            now = d.popleft()
            for k in range(10):
                d.append(now+str(k))
            if int(str(i) + now + str(j)) <= n:
                p[i][j] += 1
            else:
                break
ans = 0            
    
for i in range(1,10):
    for j in range(1,10):
        ans += p[i][j]*p[j][i]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
count=[[0]*9 for _ in range(9)]
for i in range(1,n+1):
  if i%10>0:  
    i=str(i)
    x,y=int(i[0])-1,int(i[-1])-1
    count[x][y]+=1
    
ans=0
for i in range(9):
  for j in range(9):
      ans+=count[i][j]*count[j][i]
print(ans)

Submission status: Accepted

Solution code:
def culc(x):
    a = x % 10
    b = x
    while x:
        b = x
        x //= 10
    return a, b

n = int(input())
l = [[0 for i in range(9)] for j in range(9)]
ans = 0

for i in range(1,n+1):
    a,b = culc(i)
    if a != 0 and b != 0:
        l[a-1][b-1] += 1

for i in range(1,n+1):
    a,b = culc(i)
    if a != 0 and b != 0:
        ans += l[b-1][a-1]
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
t=[[0 for i in range(10)]for j in range(10)]
for i in range(1,n+1):
  i=str(i)
  t[int(i[0])][int(i[-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:
# 数字を先頭と末尾のペアに変換
def toHeadTail(n):
    n = str(n)
    head = n[0]
    tail = n[-1]
    return (head, tail)

# 集計処理
def aggregate(N):
    pair_agg = {}
    for i in range(1, N + 1):
        pair = toHeadTail(i)
        if pair in pair_agg.keys():
            pair_agg[pair] += 1
        else:
            pair_agg[pair] = 1
    return pair_agg

N = int(input())
pair_agg = aggregate(N) 
count = 0
for i in range(1, N + 1):
    pair = toHeadTail(i)
    pair_inverse = (pair[1], pair[0])
    if pair_inverse in pair_agg.keys():
        count += pair_agg[(pair[1], pair[0])]
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [[0]*10 for _ in range(10)]
for i in range(1,N+1):
  a = i%10 #1の位
  b = i // (10**(len(str(i))-1))
  if a!= 0:
    A[a][b] += 1
t = 0
for i in range(1,10):
  s = A[i][i]
  if s != 0:
    t += int(s + (s*(s-1)))
for i in range(1,9):
  for j in range(i+1,10):
    t += A[i][j] * A[j][i]*2
print(t)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    N = int(input())
    d = len(str(N))
    ansL = [[0 for _ in range(9)] for _ in range(9)]
    for i in range(1,10):
        for j in range(1,10):
            tmp = 0
            # A(1桁)
            if i == j and i <= N:
                tmp += 1
            # AB(2桁)
            if i*10+j <= N:
                tmp += 1
            # A*B(3桁以上d桁未満)
            if d > 2:
                for k in range(3,d):
                    tmp += 10**(k-2)

                # d桁はN以下
                if int(str(N)[0]) > i:
                    tmp += 10**(d-2)
                elif int(str(N)[0]) == i:
                    tmp += int(str(N)[1:-1])
                    if int(str(N)[-1]) >= j:
                        tmp += 1
            ansL[i-1][j-1] = tmp
    
    ans = 0
    for i in range(9):
        for j in range(9):
            ans += ansL[i][j] * ansL[j][i]
    print(ans)
                

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
lis = [[0 for i in range(10)] for i in range(10)]
for i in range(1,N+1):
    fir = i//(10**(len(str(i))-1))
    las = i%10
    lis[fir][las]+=1
sum=0
for i in range(0,10):
    for j in range(i,10):
        if i==j:
            sum+=lis[i][j]*(lis[i][j]-1)+lis[i][j]
        else:
            sum+=lis[i][j]*lis[j][i]*2

#print(*lis, sep="\n")
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
# [1桁目][1の位]のリストを作る
ho = [[0 for nesya in range(9)] for motsu in range(9)]
for i in range(1,n+1):
  if i%10 != 0:
    a = str(i)
    #数字ごとにhoリストに足していく
    (ho[int(a[0])-1])[int(a[-1])-1] += 1
ans = 0
#1桁目と1の位が同じものに注意して組み合わせの数を計算する
for j in range(9):
  for k in range(j+1):
    if k == j:
      ans += (ho[k][k])**2
    else:
      ans += (ho[j][k])*(ho[k][j])*2
print(ans)

Submission status: Accepted

Solution code:
#import numpy as np

N = int(input())

l = [ [ 0 for i in range(9) ] for j in range(9) ]
#l = np.zeros((9,9), dtype=np.int)
# l[i][j] : i = 頭文字　j = 最後の文字

for k in range(N):
    x = str(k+1)
    i = int(x[0])
    j = int(x[-1])

    #print(i,j)

    if i == 0 or j == 0:
        continue
    l[i-1][j-1] += 1

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

l = np.zeros((9,9), dtype=np.int)
# l[i][j] : i = 頭文字　j = 最後の文字

for k in range(N):
    x = str(k+1)
    i = int(x[0])
    j = int(x[-1])

    #print(i,j)

    if i == 0 or j == 0:
        continue
    l[i-1][j-1] += 1

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

print(ans)

Submission status: Accepted

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

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

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

print(ans)