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:

n = int(input())

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

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

    s=str(_)
    i=int(s[0])
    j=int(s[-1])

    number[i][j] += 1

count = 0
for i in range(10):

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

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())

count = defaultdict(int)
for x in map(str, range(1, N+1)):
    head = x[0]
    tail = x[-1]
    count[(head, tail)] += 1
    
ans = 0
items = list(count.items())
for (head, tail), c in items:
    ans += c * count[(tail, head)]
    
print(ans)

Submission status: Accepted

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

for i in range(1,n+1):
    s = str(i)
    j = int(s[0])
    k = int(s[-1])
    Number[j][k] += 1
count = 0
for i in range(10):
    for j in range(10):
        count += Number[i][j] *Number[j][i]
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(sum)

Submission status: Accepted

Solution code:
# 解説放送

def main():
    from collections import defaultdict

    n = int(input())

    d = defaultdict(int)
    for x in range(1, n + 1):
        if x % 10 == 0:
            continue

        tail = x % 10
        head = 0
        while x:
            head = x
            x //= 10

        d[head, tail] += 1

    ret = 0
    for (head, tail), cnt in tuple(d.items()):
        # RuntimeError: dictionary changed size during iteration
        # defaultdictの未定義(?)のkeyにアクセスするせいで、REになる
        # d.items()をtuple化して回避
        ret += d[head, tail] * d[tail, head]
        # (head,tail)の出現数を辞書で管理する
        # (head,tail)*(tail,head)の組み合わせがある

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
strN = str(N)
order = len(strN)
Ns = int(strN[0])
Ne = int(strN[-1])

ces = [[0  for i in range(10) ] for j in range(10)]
cse = [[0  for i in range(10) ] for j in range(10)]
for n in range(1, N + 1):
  n_last = n % 10
  n_first = n
  while n_first >= 10:
    n_first = n_first // 10
  ces[n_last][n_first] += 1
  cse[n_first][n_last] += 1

count = 0
for e in range(1, 10):
  for s in range(1, 10):
    count += ces[e][s] * cse[e][s]
    
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

from collections import OrderedDict
pairs = OrderedDict()

for k in range(1, N+1):
    first = int(str(k)[0])
    last = int(str(k)[-1])
    if (first, last) in pairs:
        pairs[(first, last)] += 1
    else:
        pairs[(first, last)] = 1

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

print(cnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict
def getLeft(num):
    while num >= 10:
        num = int(num / 10)
    return num

def getRight(num):
    return num % 10

n = int(input())
a = defaultdict(int)
for number in range(1,n+1):
    left = getLeft(number)
    right = getRight(number)
    a[(left,right)] += 1


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

Submission status: Accepted

Solution code:
n = int(input())
p = [[0]*10 for _ in range(10)]
x, y = int(str(n)[0]), int(str(n)[-1])
keta = len(str(n))
ans = 0

for a in range(1, 10):
    for b in range(1, 10):
        for k in range(1, keta+1):
            if k == 1:
                if a == b and a <= n:
                    p[a][b] += 1
            elif k == 2:
                if int(str(a)+str(b)) <= n:
                    p[a][b] += 1
            elif k >= 3 and k < keta:
                p[a][b] += 10 ** (k-2)
            else:
                if a < x:
                    p[a][b] += 10 ** (k-2)
                elif a == x and b <= y:
                    p[a][b] += int(str(n)[1:-1]) + 1
                elif a == x and b > y:
                    p[a][b] += int(str(n)[1:-1])
                    
for a in range(1, 10):
    for b in range(1, 10):
        ans += p[a][b] * p[b][a]
print(ans)

Submission status: Accepted

Solution code:
# ABC 152 D
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
snm = lambda: map(str, input().split())
nl = lambda: list(map(int, input().split()))
n=ni()
c=[[0]*10 for _ in range(10)]

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

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

Submission status: Accepted

Solution code:
import math,sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
lili=lambda:list(map(int,sys.stdin.readlines()))
li = lambda:list(map(int,input().split()))
#for deque append(),pop(),appendleft(),popleft(),count()
I=lambda:int(input())
S=lambda:input().strip()
mod = 1000000007

a=[]
n=I()
for i in range(1,10):
    k=[]
    for j in range(1,10):
        k.append(0)
    a.append(k)
        

for i in range(1,n+1):
    if(i%10==0):
        continue
    k=str(i)
    p=int(k[0])-1
    q=int(k[-1])-1
    a[p][q]+=1
ans=0

for i in range(0,9):
    for j in range(0,9):
        ans+=a[i][j]*a[j][i]
print(ans)

Submission status: Accepted

Solution code:
import sys

def preprocessing():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))

    solve(N)

def solve(N: int):
    counts = [[0] * 10 for _ in range(10)]
    for i in range(10, N + 1):
        b = i % 10 
        while i >= 10:
            i //= 10 # Truncated division (i = i // 10)
        counts[i][b] += 1
    ret = 0
    for i in range(1, min(N + 1, 10)):
        ret += 1
        if counts[i][i] > 0:
            ret += counts[i][i] * 2
    for i in range(1, 10):
        for j in range(1, 10):
            ret += counts[i][j] * counts[j][i]
    print(ret)
    return


 
if __name__ == "__main__":
    preprocessing()

Submission status: Accepted

Solution code:
N = int(input())
Len = len(str(N))

# head tail
C = [] 

for i in range(0,10):
    C.append([0,0,0,0,0,0,0,0,0,0])

for A in range(0,N):
    ac = str(A+1)
    ha = int(ac[0])
    ta = int(ac[-1])
    C[ha][ta] += 1

ans = 0
for head in range(0,10):
    for tail in range(0,10):
        ans += C[head][tail] * C[tail][head]

print(ans)

Submission status: Accepted

Solution code:
MAP_MAX = 10
# 入力値
N = int(input())

# 10*10の2次元リスト
data_list = [[0 for i in range(MAP_MAX)] for j in range(MAP_MAX)]

# 先頭の桁、末尾の値取得
def get_fN_eN(num):
    # 先頭の値取得
    tmp_N = num
    while(tmp_N >= 10):
        tmp_N = tmp_N // 10
    f_N = tmp_N
    # 末尾の値取得
    e_N = num % 10

    return [f_N, e_N]

# 1から順にチェック
for i in range(1, N+1):

    # 先頭、末尾の値取得
    get_f_e = get_fN_eN(i)
    
    # リストに格納
    data_list[get_f_e[0]][get_f_e[1]] += 1

# 個数を計算
# [[f_N][e_N] * [e_N][f_N]]の総和
sum_val = 0
for i in range(1, MAP_MAX):
    for j in range(1, MAP_MAX):
        sum_val += data_list[i][j] * data_list[j][i]

print(sum_val)

Submission status: Accepted

Solution code:
N=int(input())

D={}
for i in range(1,N+1):
    I=str(i)
    if (I[-1],I[0]) not in D:
        D[(I[-1],I[0])]=1
    else:
        D[(I[-1],I[0])]+=1

ans=0
for j in range(1,N+1):
    J=str(j)
    if (J[0],J[-1]) in D:
        ans+=D[(J[0],J[-1])]
print(ans)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)

def solve(N: int):
    counts = [[0] * 10 for _ in range(10)]
    for i in range(10, N + 1):
        b = i % 10
        while i >= 10:
            i //= 10
        counts[i][b] += 1
    ret = 0
    for i in range(1, min(N + 1, 10)):
        ret += 1
        if counts[i][i] > 0:
            ret += counts[i][i] * 2
    for i in range(1, 10):
        for j in range(1, 10):
            ret += counts[i][j] * counts[j][i]
    print(ret)
    return

def solve_(N: int):
    def is_valid(n, a, b):
        if n % 10 != b:
            return False
        while n >= 10:
            n //= 10
        if n % 10 == a:
            return True
        return False
    ret = 0
    for i in range(1, 10):
        for x in range(1, N + 1):
            if is_valid(x, i, i):
                print(x, i, i)
                ret += 1
    print(' -------- ')
    for i in range(1, 10):
        for j in range(1, 10):
            for x in range(10, N + 1):
                if is_valid(x, i, j):
                    print(x, i, j)
                    ret += 1
    print(ret)
    return

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

def f(a,b):
    ret = 0
    a,b = min(a,b),max(a,b)
    l = len(str(N))
    if a==b:
        if l==1:
            if a<=N: return 1
            else: return 0
        ret = 1
        for d in range(l-1):
            for i in range(10**d):
                A = a*(10**(d+1)+1) + i*10
                if A<=N: ret += 1
                else: break
            else:
                continue
            break
        ret **= 2
    else:
        Acnt = 0
        Bcnt = 0
        for d in range(l-1):
            for i in range(10**d):
                A = a*(10**(d+1)) + b + i*10
                B = b*(10**(d+1)) + a + i*10
                if B<=N: Bcnt += 1
                if A<=N: Acnt += 1
                else: break
            else:
                continue
            break
        ret = Acnt * Bcnt    
    return ret

ans = 0
for a in range(1,10):
    for b in range(1,10):
        ans += f(a,b)
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    c = collections.defaultdict(int)
    for i in range(1,n+1):
        s = str(i)
        c[s[0]+s[-1]] += 1

    r = 0
    for k,v in c.items():
        for l,u in c.items():
            if k > l or k[0] != l[1] or k[1] != l[0]:
                continue
            if k == l:
                r += v * u
            else:
                r += v * u * 2

    return r


print(main())

Submission status: Accepted

Solution code:
N = int(input())

D = {}
for i in range(1,N+1):
  I = str(i)
  if (I[-1],I[0]) not in D:
    D[(I[-1],I[0])] = 1
  else:
    D[(I[-1],I[0])] += 1

ans = 0
for j in range(1,N+1):
  J = str(j)
  if (J[0],J[-1]) in D:
    ans += D[(J[0], J[-1])]    
print(ans)

Submission status: Accepted

Solution code:
import math
import sys

A  = int(input())

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

def count(n):
    for x in range(1,n+1):
        xj = x % 10 
        s = x; 
        while True:
            if s < 10:
                xi = s
                C[xi][xj] = C[xi][xj] + 1
                break
            s = s// 10 


count(A)
# print(C)
ans = 0 
for i in range(1,10):
    for j in range(1,10):
        ans = ans + C[i][j] * C[j][i]

print(ans)

Submission status: Accepted

Solution code:
#abc152 d
n=int(input())

def first_num(x):
    l=x
    while int(x/10)>0:
        x=int(x/10)
        l=x
    return l

kiroku=dict()
for i in range(1,n+1):
    first=first_num(i)
    last=i%10
    #組はタプルに入ってる
    if (first,last) in kiroku:
        kiroku[first,last]+=1
    else:
        kiroku[first,last]=1

ans=0
for key in kiroku.keys():
    f,l=key
    if (l,f) in kiroku:
        kumi=kiroku[f,l]*kiroku[l,f]
        ans+=kumi
print(ans)

Submission status: Accepted

Solution code:
N=int(input()) 

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

Submission status: Accepted

Solution code:
n = int(input())

ans = 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, n+1):
    ss = str(j)
    ans += numbers[int(ss[-1])][int(ss[0])]

print(ans)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())

    numss = [[0]*(10) for _ in range(10)]
    for A in range(1, N+1):
        ss = str(A)
        numss[int(ss[-1])][int(ss[0])] += 1

    ans = 0
    for A in range(1, N+1):
        ss = str(A)
        ans += numss[int(ss[0])][int(ss[-1])]

    print(ans)

solve()

Submission status: Accepted

Solution code:
def get_lmb(N):
    while (N >= 10):
        N = int(N/10)
    return N

def get_rmb(N):
    return N%10

def find_all():
    from collections import defaultdict
    dic = defaultdict(int)
    N = int(input())
    for i in range(1, N+1):
        lmb, rmb = get_lmb(i), get_rmb(i)
        dic[(lmb, rmb)] += 1
    # print(dic)
    count = 0
    for i in range(1, 10):
        for j in range(1, 10):
            count += dic[(i, j)] * dic[(j, i)]
    # for i in range(1, 10):
    #     count += dic[(i, i)] * dic[(i, i)]
    return count

print(find_all())

Submission status: Accepted

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

for i in range(1,n+1):
    if i%10!=0:
        num=str(i)
        A[int(num[0])-1][int(num[-1])-1] +=1

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
D={}
for i in range(1,n+1):
    s=(str(i)[0],str(i)[-1])
    if s not in D:
        D[s]=1
    else:
        D[s]+=1
ans=0
for i in D:
    if (i[1],i[0]) in D:
        ans+=D[i]*D[i[1],i[0]]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
mat = list()
for i in range(10):
    mat.append([0] * 10)

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

Submission status: Accepted

Solution code:
N = int(input())
mat = list()
for i in range(10):
    mat.append([0] * 10)

for i in range(N):
    A = int(str(i + 1)[0])
    B = int(str(i + 1)[-1])
    mat[A][B] += 1
ans = 0
for i in range(N):
    A = int(str(i + 1)[0])
    B = int(str(i + 1)[-1])
    ans += mat[B][A]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
c=[[0 for z in range(10)] for z in range(10)] #c[i][j]=1以上N以下の整数で先頭i末尾jとなる数字の個数
for i in range(1,10):
  for j in range(1,10):
    for k in range(1,N+1):
      k=str(k)
      if k[0]==str(i) and k[-1]==str(j):
        c[i][j]=c[i][j]+1
S=0
for i in range(1,10):
  for j in range(1,10):
    S=S+c[i][j]*c[j][i]
print(S)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

n = i()
nx = str(n)[0]
ny = str(n)[-1]
digit = len(str(n))
is_full = [1, 11, 111, 1111, 11111]

for i in range(1, n + 1):
    x = str(i)[0]
    y = str(i)[-1]
    if y == "0":  # 0が先頭になるときを考えない
        continue

    if x == y:  # xとyが同じとき3などの一桁の値をとりうるので+1する
        ans += 1

    if y < nx:  # 注目している数字の末尾，つまり考えている数字y??xの先頭の文字yがnxより小さいとき
        if digit - 2 < 0:  # 無条件で取れる，ものが無ければcontinue
            continue
        ans += is_full[digit - 2]
    elif y == nx:
        if digit - 3 >= 0:
            ans += is_full[digit - 3]
        for num, k in enumerate(list(str(n))[1: -1], 1):  # y???xとnx???nyを比較していく
            ans += int(k) * 10 ** (digit - 2 - num)

        if digit >= 2 and x <= ny:  # 10の位をもうひとつ上げられるとき
            ans += 1
    else:  # 注目している数字の末尾，つまり考えている数字y??xの先頭の文字yがnxより大きいとき
        if digit - 3 < 0:  # このときは無条件に取れるis_fullの位が一段階下がる
            continue
        ans += is_full[digit - 3]

print(ans)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]

n = i()
nx = str(n)[0]
ny = str(n)[-1]
digit = len(str(n))
is_full = [1, 11, 111, 1111, 11111]

for i in range(1, n + 1):
    x = str(i)[0]
    y = str(i)[-1]
    if y == "0":
        continue
    #digit = len(str(i))
    if x == y:
        ans += 1
        if y < nx:
            if digit - 2 < 0:
                continue
            ans += is_full[digit - 2]
        elif y == nx:
            if digit - 3 >= 0:
                ans += is_full[digit - 3]
            for num, k in enumerate(list(str(n))[1: -1], 1):
                ans += int(k) * 10 ** (digit - 2 - num)

            if digit == 2:
                if int(x + y) <= n:
                    ans += 1
            if digit >= 3 and x <= ny:
                ans += 1

        else:
            if digit - 3 < 0:
                continue
            ans += is_full[digit - 3]
    else:
        if y < nx:
            if digit - 2 < 0:
                continue
            ans += is_full[digit - 2]
        elif y == nx:
            if digit - 3 >= 0:
                ans += is_full[digit - 3]
            for num, k in enumerate(list(str(n))[1: -1], 1):
                ans += int(k) * 10 ** (digit - 2 - num)
            if digit == 2:
                if int(x + y) <= n:
                    ans += 1
            if digit >= 3 and x <= ny:
                ans += 1
        else:
            if digit - 3 < 0:
                continue
            ans += is_full[digit - 3]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ab = [0] * 100
for i in range(1, n + 1):
    s = str(i)
    front = s[0]
    back = s[-1]
    ab[int(front + back)] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())

ans=0
Dic={}
for i in range(1,n+1):
    s=str(i)
    d=(s[0],s[-1])
    if d in Dic.keys():
        Dic[d]+=1
    else:
        Dic[d]=1

for a in Dic.keys():
    b=(a[1],a[0])
    if b in Dic.keys():
        ans+=Dic[a]*Dic[b]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

print(count)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
dic={}
for i in range(1,n+1):
    s=str(i)
    d=(s[0],s[-1])
    if d not in dic.keys():
        dic[d] = 1
    else:
        dic[d] += 1
 
for a in dic.keys():
    b = (a[1],a[0])
    if b in dic.keys():
        ans += dic[a]*dic[(a[1],a[0])]
 
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
#N,K,M= map(int,input().split())
#A =list(map(int,input().split()))
#L = [list(map(str,input().split())) for i in range(N)]
keta = len(str(N))
answer = 0

k = 1
for i in range(1,N+1):
    k = 1
    if i % 10 == 0:
        continue
    #桁が１桁
    if str(i)[0] == str(i)[-1]:
        answer += 1
    #桁が２桁
    if int(str(i)[-1]+str(i)[0]) <= N:
        answer += 1
    k =3
    #桁が３桁から与えられたNの桁以下の時
    if k <= keta:
        flag = 10
        while k < keta:
            answer += flag
            k += 1
            flag *= 10
        #k と keta が同じとき
        if str(i)[-1] < str(N)[0]:
            answer += flag
        elif str(i)[-1] == str(N)[0]:
            #print(i)
            kake = ''
            for j in range(1,keta-1):
                kake += str(N)[j]
            #print(kake)
            if str(i)[0] <= str(N)[-1]:
                answer += (int(kake)+1)
            else:
                answer += max((int(kake)),0)
print(answer)

Submission status: Accepted

Solution code:

from collections import defaultdict

N = int(input())
dic = defaultdict(int)

for i in range(1, N+1):
    num_str = str(i)
    if int(num_str[-1]) == 0:
        continue
    else:
        dic[(int(num_str[0]), int(num_str[-1]))] += 1

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
ary = np.zeros((10, 10))
for i in range(n):
  ary[int(str(i + 1)[0]) - 1][int(str(i + 1)[-1]) - 1] += 1
print(np.sum(ary * ary.T).astype(np.int32))

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

m = [[0]*10 for _ in range(0,10)]
for i in range(1,n+1):
  s = str(i)
  a = int(s[0])
  b = int(s[-1])
  m[a][b] += 1

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 19 21:23:54 2020

@author: over-
"""
def check(N):
    c=0
    for i in range(1,N+1):
        if str(i)[-1] !=0:
            for j in range(1,N+1):
                if str(i)[0]==str(j)[-1] and str(i)[-1]==str(j)[0]: c+=1
                  
    return c

N=input()
L=len(N)

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

    
    if y!=0:
        d=0
        if x==y: 
            d+=1
        if L==2:
            if int(N)>=int('{}{}'.format(y,x)):
                d+=1
        if L==3:
            d+=1
            if int(N)>=int('{}9{}'.format(y,x)):
                d+=10
            elif int(N)>=int('{}0{}'.format(y,x)):
                d+=int(N[1:-1])
                if int(N)>=int('{}{}{}'.format(y,N[1:-1],x)):
                    d+=1
        if L==4:
            d+=1+10
            if int(N)>=int('{}99{}'.format(y,x)):
                d+=100
            elif int(N)>int('{}00{}'.format(y,x)):
                d+=int(N[1:-1])
                if int(N)>=int('{}{}{}'.format(y,N[1:-1],x)):
                    d+=1
        if L==5:
            d+=1+10+100
            if int(N)>=int('{}999{}'.format(y,x)):
                d+=1000
            elif int(N)>=int('{}000{}'.format(y,x)):
                d+=int(N[1:-1])
                if int(N)>=int('{}{}{}'.format(y,N[1:-1],x)):
                    d+=1
        if L==6:
            d+=1+10+100+1000
            if int(N)>=int('{}9999{}'.format(y,x)):
                d+=10000
            elif int(N)>=int('{}0000{}'.format(y,x)):
                d+=int(N[1:-1])
                if int(N)>=int('{}{}{}'.format(y,N[1:-1],x)):
                    d+=1
        
        c+=d
        #print(i,d)

print(c)
#print(check(int(N)))

Submission status: Accepted

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

Submission status: Accepted

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

for a in dic.keys():
    b = (a[1],a[0])
    if b in dic.keys():
        ans += dic[a]*dic[(a[1],a[0])]

print(ans)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc152/submissions/9607915

N = int(input())
lis = [[0]*10 for _ in range(10)]

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

ans = 0
#A,Bになりうる数字の数をカウントして掛け算
for i in range(10):
    for j in range(10):
        ans += lis[i][j]*lis[j][i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
tmp = int("0"+str(N)[1:-1])
NR = int(str(N)[-1])
for i in range(1,N+1):
    L = int(str(i)[0])
    R = int(str(i)[-1])
    if R == 0:
        continue
    if L == R:
        ans += 1
    for j in range(1,6):
        mini = (R*10**j) +L
        maxi = ((R+1)*10**j) +L-10
        if N < mini:
            break
        if maxi <= N:
            ans += 10**(j-1)
            continue
        
        if L <= NR:
            ans += tmp+1
        else:
            ans += tmp
        break
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
check=[[0]*9 for i in range(9)]
for i in range(1,n+1):
    s=str(i)
    s1=int(s[0])-1
    s2=int(s[-1])-1
    if s2==-1:continue
    check[s1][s2]+=1
cnt=0
for i in range(9):
    for j in range(9):
        cnt+=check[i][j]*check[j][i]
print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
def comp_begin_end(a):
    if int(str(n)[0]) >= int(str(n)[-1]): return 1
    else:   return 0

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n = int(readline())
#解説AC
# calcurate Cij
c = [[0]*10 for _ in range(10)] #これだと１つ足すと全てのc[i][0]に加算される
for k in range(1,n+1):
    i = int(str(k)[0]) 
    j =  int(str(k)[-1])
    c[i][j] += 1
#print(c)
ans = 0
for i in range(10):
    for j in range(10):
        ans += c[i][j] * c[j][i]
print(ans)
"""
ans = 0
for i in range(1,n+1):
    if comp_begin_end(i): ans += 1
    if 10 <= n < 100:
        if int(str(n)[0]) == int(str(n)[-1]): ans += 1
    if 100 <= n < 1000:
        ans += 1
        if int(str(n)[0]) == int(str(n)[-1]): ans += 1
    if 1000 <= n < 100000:
        ans += 11
        if int(str(n)[0]) == int(str(n)[-1]): ans += 1
    if 10000 <= n:
        ans += 111
        if int(str(n)[0]) == int(str(n)[-1]): ans += 1
print(ans)"""