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: Time Limit Exceeded

Solution code:
n=int(input())
seikei = []
res = 0

seikei = [str(x+1) if (len(str(x+1)) < 3) else (str(x+1)[0] + str(x+1)[len(str(x+1)) - 1]) for x in range(n) if not (x+1)%10 == 0]

for moji in seikei:
  count = len([x for x in seikei if (moji if len(moji) == 2 else moji * 2) == (x[1] + x[0] if len(x) == 2 else x * 2)])
  res += count

print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
len_n = len(str(N))
top_n = N // (10 ** (len_n - 1))
bot_n = N % 10

count = 0

for i in range(1, N + 1):
  p = len(str(i))
  a = i // (10 ** (p - 1))
  b = i % 10
  if a == b:
    count += 1
  s = 2
  while len_n > s:
    count += 10 ** (s - 2)
    s + 1
  if i > 9:
    if top_n > b:
      count += 10 ** (s - 2)
    elif top_n == b:
      if bot_n > a:
        count += 1
      if i > 99:
        count += N // 10 - top_n * 10 ** (len_n - 1)
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

def figure(x):
    t = 1
    ans = 0
    while t <= x:
        t = t*10
        ans = ans + 1
    return ans

def top(x):
  if figure(x) != 1:
    ans = 0
    p = x
    while p >= 10**(figure(x)-1):
        p = p-10**(figure(x)-1)
        ans = ans + 1
    return ans
  else:
    return x

def bottom(x):
    return x%10

topList=[]
bottomList=[]
for i in range(1,N+1):
    topList.append(top(i))
    bottomList.append(bottom(i))
    
Flist=[]   
for i in range(1,10):
    for j in range(0,10):
        count=0
        for k in range(1,N+1):
            if top(k) == i and bottom(k) == j:
                count = count+1
        Flist.append(count)

ans = 0
for i in range(len(Flist)):
    if top(i+10) == bottom(i+10):
      ans = ans + Flist[i]**2
    else:
      if i%10 == 0:
        ans = ans
      else:
        ans = ans + Flist[i]*Flist[bottom(i+10)*10+top(i+10)-10]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
def min(a):
  return a%10
def max(a):
  while a>=10:
    a=a//10
  return a
c=0
for i in range(1,N+1):
  for j in range(1,N+1):
    if min(i)==max(j) and min(j)==max(i):
      c=c+1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import math
n = input()
m = int(n)

ans = 0
for i in range(m):
    for j in range(m):
        s = str(i + 1)
        t = str(j + 1)
        if s[0] == t[-1] and s[-1] == t[0]:
            ans += 1
            
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

count=0
for i in range(1,N+1):
    head=int(str(i)[0])
    hip=i%10
    jstart=head-hip
    if jstart<0:
        jstart+=10
    for j in range(jstart,N+1-i,10):
        if str(i+j)[0]==str(i)[-1]:
            count+=2
            if j==0:
                count-=1
            else:
            	pass
        else:
            j=(int(str(i+j)[0])+1)*10**(len(str(i+j))-1)+jstart

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

ans = 0

for a in range(1,N+1):
    for b in range(1,N+1):
        A=str(a)
        B=str(b)
        if A[0] == B[-1] and A[-1] == B[0]:
            ans += 1
            
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())

# a[0] = b[-1]
# b[0] = a[-1]
# a < n
# b < n

def checker(x, y, n):
    if str(x)[0] == str(y)[-1]:
        if str(y)[0] == str(x)[-1]:
            if x <= n:
                if y <= n:
                    return True
    else:
        return False
if n < 10:
    print(n)
    sys.exit(0)
else:
    ptns = 0
lst = [[x, y] for x in range(1, n+1) for y in range(1, n+1) if checker(x, y, n)]
print(len(lst))

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())

tl = [i for i in range(1, n+1) if i % 10 != 0]

l = list(itertools.product(tl, tl))

# print(l)

ans = []
count = 0
for pair in l:
    a = str(pair[0])
    b = str(pair[1])
    if (a[0] == b[len(b)-1]) and (a[len(a)-1] == b[0]):
        count += 1
        # ans.append(pair)

print(count)
# print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
ans = 0
for i in range(1, n+1):
  for j in range(1, n+1):
    str_i = str(i)
    str_j = str(j)
    if str_i[0] == str_j[-1] and str_i[-1] == str_j[0]:
      ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())
ans = 0

x = []
y = []

for i in range(1,n+1):
    for j in range(1,n+1):
        x = list(str(i))
        y = list(str(j))

        if x[0] == y[-1]:
            if x[-1] == y[0]:
                ans += 1
            else:
                pass
        else:
            pass

#        if x[0] == y[-1] and x[-1] == y[0]:
#            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

def num_suit(num, n):
  moji = str(num)
  if moji[-1] == "0":
    return 0
  
  counter = 0
  #1
  if moji[0] == moji[-1]:
    cand = int(moji[0])
    if cand <= n:
      counter += 1
#      print(num, cand)
  #2
  cand = int(moji[-1] + moji[0])
  if moji[-1] != "0" and cand <= n:
    counter += 1
#    print(num, cand)
  #3
  for i in range(0, 10):
    cand = int(moji[-1] + str(i) + moji[0])
    if cand <= n:
      counter += 1
    else:
      break
  #4
  for i in range(0, 100):
    num = int(moji[-1] + "{0:02d}".format(i) +moji[0])
    if cand <= n:
      counter += 1
    else:
      break
  #5
  for i in range(0, 1000):
    num = int(moji[-1] + "{0:03d}".format(i) +moji[0])
    if cand <= n:
      counter += 1
    else:
      break
  #6
  for i in range(0, 10000):
    num = int(moji[-1] + "{0:04d}".format(i) +moji[0])
    if cand <= n:
      counter += 1
    else:
      break
#  print("counter", counter)
  return counter

total = 0
for i in range(1, n + 1):
  total += num_suit(i, n)
print(total)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
cnt=0
for a in range(1,n+1):
  for b in range(1, n+1):
    if str(a)[0]==str(b)[-1] and str(a)[-1]==str(b)[0]: cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(1,n+1):
    for j in range(1,i+1):
        if str(i)[0]==str(j)[-1] and str(j)[0]==str(i)[-1]:
            if i==j:
                ans+=1
            else:
                ans+=2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
count = 0
i = 1
while(i <= n):
  j = 1
  fa = repr(i)[0]
  la = repr(i)[-1]
  while (j <= n):
    fb = repr(j)[0]
    lb = repr(j)[-1]
    if ((la == fb and fa == lb)):
      count += 1
    j += 1
  i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt =0
for i in range(1,int(s)+1):
    for j in range(1,int(s)+1):
        if str(i)[0] == str(j)[-1] and str(j)[0] == str(i)[-1] :
            cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input().rstrip()
N = int(s)

ans = 0
i = 1

while i < N + 1:
    j = int(str(i)[0])
    while j < N + 1:
        if str(i)[-1] == str(j)[0] and str(i)[0] == str(j)[-1]:
            ans = ans + 1
        j = j +  10
    i = i + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    a = 0
    for i in range(1, N+1):
        iN = str(i)
        for j in range(1, N+1):
            jN = str(j)
            if iN[-1] == jN[0] and iN[0] == jN[-1]:
                #print(iN, jN)
                a += 1
    print(a)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ans = 0
for i in range(1, N+1):
    head, tail = str(i)[0], str(i)[-1]
    if tail == "0":
        continue
    if head == tail:
        ans += 1
    if int(tail + head) <= N:
        ans += 1
    if len(str(N))-2 >= 1 or head == tail:
        for num in range(len(str(N))-2, 0, -1):
            for j in range(int("9"*num), -1, -1):
                pad = str(j).zfill(num)
                if int(tail + pad + head) <= N:
                    # print(int(tail + pad + head))
                    ans += int(j) + 1
                    if num > 1:
                        ans += int((num - 1) * "1") * 10
                    break
            else:
                continue
            break    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())

# a[0] = b[-1]
# b[0] = a[-1]
# a < n
# b < n

def checker(x, y, n):
    if str(x)[0] == str(y)[-1]:
        if str(y)[0] == str(x)[-1]:
            if x <= n:
                if y <= n:
                    return True
    else:
        return False
if n < 10:
    print(n)
    sys.exit(0)
else:
    ptns = 0
for x in range(1, n+1):
    for y in range(1, n+1):
        if checker(x, y, n):
            ptns += 1
print(ptns)

Submission status: Time Limit Exceeded

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

N = int(input())

def isHandStand(A, B):
    A_str = str(A)
    B_str = str(B)
    if A_str[-1] == B_str[0] and A_str[0] == B_str[-1]:
        return True
    return False

count = 0
for A in range(1, N+1):
    for B in range(1, N+1):
        # print(A, B)
        if isHandStand(A, B):
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
# D - Handstand 2
N = int(input())
count = 0
tmp = 0
DP = []

if N < 10:
    print(N)
    exit()
else:
    n = str(N)
    for i in range(1, N + 1):
        I = str(i)
        for j in range(1,N+1):
            J = str(j)
            if J[-1] == I[0] and J[0] == I[-1]:
                count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
 
ans=0
for i in range(1,n+1):
       if i%10==0:
              continue
       for j in range(1,n+1):
              if str(i)[0]==str(j)[-1] and str(i)[-1]==str(j)[0]:
                     ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
count = 0
for i in range(1,n+1):
        for j in range(1,n+1):
            if i % 10 == int(str(j)[0]) and j % 10 == int(str(i)[0]):
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
s = [str(s) for s in range(1,n+1)]

h=0

for num in range(n):
    #print(len(s[num]))
    
    for nf in range(n):
        #print(num, nf)
   
        if(s[num][-1]==s[nf][0] and s[num][0]==s[nf][-1]):
            #print(s[num],s[nf],       s[num][-1], s[nf][0],   s[num][0], s[nf][-1])
            h = h+1

print(h)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

count = 0
for a in range(1, N + 1):
    a_s = str(a)
    for b in range(1, N + 1):
        b_s = str(b)

        if a_s[0] == b_s[-1] and a_s[-1] == b_s[0]:
            count += 1


print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys

import itertools
N = int(input())
st = set()

r = [''] + [str(v) for v in range(0, 10)]
cnt = 0
for a in [str(v) for v in range(1, 10)]:
    it = itertools.product(r, r, r)
    for s in it:
        ss = a + ''.join(s) + a
        if int(ss) > N:
            break
        if int(ss) <= N and int(a) <= N:
            #print(ss, a)
            #print(a, ss)
            st.add((ss,a))
            st.add((a,ss))
            #cnt += 2
    if int(a) <= N:
        #print(a, a)
        st.add((a,a))
        #(cnt += 1

for i in range(10, N + 1):
    s = str(i)
    h = s[0]
    t = s[-1]
    if t == '0':
        continue
    it = itertools.product(r, r, r)
    for s in it:
        ss = t + ''.join(s) + h
        if int(ss) > N:
            break
        if int(ss) <= N:
            #print(i, ss)
            st.add((i, ss))
            #cnt += 1

print(len(st))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
memo1 = 0
memo2 = 0
ans = 0
stra = ""
strb = ""
str1 = ""
str2 = ""

for a in range(1,10):
    for b in range(1,10):
        stra = str(a)
        strb = str(b)
        for i in range(1, n+1):
            str1 = str(i)[0]
            str2 = str(i)[-1]
            if str1 == stra and str2 == strb:
                memo1 += 1
            if str1 == strb and str2 == stra:
                memo2 += 1
        ans += memo1 * memo2
        memo1 = 0
        memo2 = 0



print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
count=0
for i in range(N+1):
    a0=i%10
    ketaa=len(str(i))
    a1=int(i/(10**(ketaa-1)))
    for j in range(N+1):
        b0=j%10
        ketab=len(str(j))
        b1=int(j/(10**(ketab-1)))

        if(a0==b1)and(a1==b0):
            count+=1
print(count-1)

Submission status: Time Limit Exceeded

Solution code:
s = input().rstrip()
N = int(s)

ans = 0

for i in range(1, N + 1):
    for j in range(1, N + 1):
        if str(i)[-1] == str(j)[0] and str(i)[0] == str(j)[-1]:
            ans = ans + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

lst = list()
count = 0
for i in range(1, N+1):
    for j in range(i, N+1):
        ahead = int(str(i)[0])
        atail = i % 10
        bhead = int(str(j)[0])
        btail = j % 10
        if ahead == btail and atail == bhead:
            count += (1 if i == j else 2)
print(count)

Submission status: Time Limit Exceeded

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

    answer = 0

    for A in range(1, N+1):
        A_top = str(A)[0]
        A_least = str(A)[-1]
        for B in range(1, N+1):
            B_top = str(B)[0]
            B_least = str(B)[-1]
            if A_least == B_top and A_top == B_least:
                answer += 1

    print(answer)

 
if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
print(len([j for i in range(1,N+1) for j in range(1,N+1) if (str(i)[0] == str(j)[len(str(j))-1]) & (str(j)[0] == str(i)[len(str(i))-1])]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
d = [0] * (n+1)

#片方がその数字だと固定されたときに新しく追加される要素数を計算する関数
def count(u):
    top = int(str(u)[0])
    bottom = int(str(u)[-1])
    #先頭と最後が一緒な候補を入れていく。1けたと2けたは処理が例外的なので先に入れる
    c = []
    if bottom == 0:
        return 0
    if top == bottom:
        c.append(top)
    c.append(int(str(bottom)+str(top)))

    for i in range(len(str(u))-2):
        for k in range(10**(i+1)):
            r = str(bottom)
            r += str(k).zfill(i+1)
            r += str(top)
            c.append(int(r))
    cnt = 0
    for cc in c:
        if cc < u:
            cnt += 2
        elif cc == u:
            cnt += 1
    

    return cnt

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

print(d[-1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c = 0
a = [i for i in range(n+1) if i%10 != 0]
for i in a:
    for j in a:
        if str(i)[0] == str(j)[-1] and str(j)[0] == str(i)[-1]:
            c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
cnt = 0

for i in range(1, N+1):
    headi = i // 10**(len(str(i))-1)
    taili = i % 10
    
    for j in range(1, N+1):
        headj = j // 10**(len(str(j))-1)
        tailj = j % 10
        if headi == tailj and taili == headj:
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())


def judge(a, b):
    a_str, b_str = str(a), str(b)
    if a_str[0] == b_str[-1] and a_str[-1] == b_str[0]:
        return True
    else:
        return False


ans = 0
for i in range(1, n+1):
    for j in range(1, n+1):
        if judge(i, j):
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
nn=str(n)
# c=int(math.log10(n)//1)+1

a=list(range(1,n+1))

b=[[[] for _ in range(10)] for __ in range(10)]
for i in range(1,10):
    for j in range(1,10):
        box=[]
        for h in a:
            if str(h)[0]==str(i) and str(h)[-1]==str(j):
                box.append(h)
        b[i][j]=box

# b=[[[] for _ in range(10)] for __ in range(9)]

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
res=0
for i in range(1,N+1):
    for j in range(1,N+1):
        if "".join(reversed(str(i)))==str(j):
            res+=1
        elif set(str(i)) == set(str(j)) and len(set(str(i)))==1:
            res+=1
        elif list(str(i))[-1]==list(str(j))[0] and list(str(i))[0]==list(str(j))[-1]:
            res+=1
print(res)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())

li = list(itertools.combinations_with_replacement(range(1, N + 1), 2))

cnt = 0

for c in li:
    liA = []
    liB = []
    for i in str(c[0]):
        liA.append(int(i))
    for i in str(c[1]):
        liB.append(int(i))
    if liA[-1] == liB[0]:
        if liA[0] == liB[-1]:
            cnt += 1
            if c[0] != c[1]:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N=int(input())
P=[i for i in range(1,N+1) if i%10 != 0]

P_ite=list(itertools.combinations_with_replacement(P, 2))
P_ite_len=len(P_ite)

ans=0
for i in range(P_ite_len):
    a=str(P_ite[i][0])
    b=str(P_ite[i][1])
    if a[0] == b[-1] and a[-1] == b[0]:
        if a == b:
            ans+=1
        else:
            ans+=2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
t=0
for i in range(1,N+1):
    count = [j for j in range(1,N+1) if (str(i)[0] == str(j)[len(str(j))-1]) & (str(j)[0] == str(i)[len(str(i))-1])]
    t=t+len(count)
print(t)

Submission status: Time Limit Exceeded

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

for i in range(1, N + 1):
  str_i = str(i)
  if sheet[int(str_i[0])][int(str_i[-1])] == 0:
    for j in range(N + 1):
      str_j = str(j)
      if str_j[0] == str_i[-1] and str_j[-1] == str_i[0]:
        sheet[int(str_i[0])][int(str_i[-1])] += 1
    cnt += sheet[int(str_i[0])][int(str_i[-1])]

  else:
    cnt += sheet[int(str_i[0])][int(str_i[-1])]


print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

count = 0
for A in range(1, N+1):
    for B in range(A, N+1):
        if str(A)[-1]==str(B)[0] and str(B)[-1]==str(A)[0]:
            count +=1
            if A != B:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
count=0
A = []
B = []
for i in range(1,N+1):
  n = str(i)
  for j in range(1,N+1):
    m = str(j)
    if n[0]==m[-1] and n[-1]==m[0]:
      count +=1
#    print(n[0])
 #   print(m[-1])
  #  print(n[0])
   # print(m[-1])
#    B.pop()
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

count = 0
for A in range(1, N+1):
    for B in range(1, N+1):
        if str(A)[-1]==str(B)[0] and str(B)[-1]==str(A)[0]:
            count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import unittest


class TestD(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(25), 17)

    def test_2(self):
        self.assertEqual(think(1), 1)

    def test_3(self):
        self.assertEqual(think(100), 108)

    def test_4(self):
        self.assertEqual(think(2020), 40812)

    def test_5(self):
        self.assertEqual(think(200000), 400000008)


def solve():
    n = read()
    result = think(n)
    write(result)


def read():
    return read_int(1)[0]


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n):
    count = 0
    for i in range(1, n + 1):
        first_digit = first_digit_of(i)
        for j in range(first_digit, n + 1, 10):
            count += 1 if satisfies_condition(i, j) else 0
    return count


def write(result):
    print(result)


def satisfies_condition(a, b):
    str_a, str_b = str(a), str(b)
    return str_a[-1] == str_b[0]


def first_digit_of(a):
    return int(str(a)[0])


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

for i in range(1,10):
  for j in range(1,10):
    arr[i][j] = len([k for k in range(1,n+1) if (str(k)[0] == str(i) and str(k)[-1] == str(j))])

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

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
s = [str(s) for s in range(1,n+1)]

h=0

for num in range(len(s)):
    #print(len(s[num]))
    
    for nf in range(len(s)):
        #print(num, nf)
   
        if(s[num][-1]==s[nf][0] and s[num][0]==s[nf][-1]):
            #print(s[num],s[nf],       s[num][-1], s[nf][0],   s[num][0], s[nf][-1])
            h = h+1

print(h)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
cnt = 0
ls = []
"""
def chk(a,b):
    if a[len(a)-1] == '0' or b[len(b)-1] == '0':
        return 0
    elif a[0] == b[len(b)-1] and a[len(a)-1] == b[0] and a == b:
        return 1
    elif a[0] == b[len(b)-1] and a[len(a)-1] == b[0]:
        return 2
    else:
        return 0
"""
ls = []
dic = {}
for i in range(1,10):
    for j in range(1,10):
        ls.append([str(i),str(j)])

for i in range(81):
    dic[i] = 0

for i in range(1,n+1):
    a = str(i)
    if a[len(a)-1] != '0':
        #print([a[0],str(i)[len(a)-1]])
        if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
            #print(a,1)
            #print([str(i)[len(a)-1],a[0]])
            cnt += 1
        elif a[0] == a[len(a)-1]:
            #print(a,2)
            #print([str(i)[len(a)-1],a[0]])
            cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
        elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
            #print(a)
            cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
        dic[ls.index([a[0],str(i)[len(a)-1]])] += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input());
ans = 0;
for a in range(1,n+1) :
    for b in range(1,n+1) :
        if (str(a)[0] == str(b)[len(str(b)) - 1]):
            if(str(b)[0] == str(a)[len(str(a)) - 1]):
                ans += 1;
print(ans);

Submission status: Time Limit Exceeded

Solution code:
n=input()
n_int=int(n)
count=0

if n_int==1:
    count=1
else:
    n_list=list(range(1,n_int))
    
    for a in n_list:
        list_a=list(str(a))
        len_a=len(list_a)
        for b in n_list:        
            list_b=list(str(b))
            len_b=len(list_b)

            if (list_a[len_a-1]==list_b[0]) and (list_a[0]==list_b[len_b-1]):
                count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input());
ans = 0;
for a in range(1,n+1) :
    for b in range(1,n+1) :
        if (str(a)[0] == str(b)[len(str(b)) - 1]):
            if(str(b)[0] == str(a)[len(str(a)) - 1]):
                ans += 1;
print(ans);

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

num=0

li = [i+1 for i in range(N)]

l4 = list(map(lambda x: str(x)[0]+str(x)[-1], li))
l5 = list(map(lambda x: str(x)[-1]+str(x)[0], li))

for l in l4:
  num += l5.count(l)
print(num)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
N=[str(i) for i in range(1,n+1)]

count=0
for a in N:
  for b in N:
    if a[0]==b[-1] and a[-1]==b[0]:
      count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

ans = []

for i in range(1,N+1):
    for j in range(1, N+1):
        if str(i)[-1] == str(j)[0] and str(i)[0] == str(j)[-1]:
            ans.append((i, j))
            
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
N=[str(i) for i in range(1,n+1)]

count=0
for a in N:
  for b in N:
    if str(a)[0]==str(b)[-1] and str(a)[-1]==str(b)[0]:
      count+=1

print(count)

Submission status: Time Limit Exceeded

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

for i in range(1,n+1):
    x=str(i)
    for j in range(1,n+1):
        y=str(j)
        if x[-1]==y[0] and  x[0]==y[-1]:
            #print(i,j)
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
a = int(input())
b = list(range(1, a+1))
c = itertools.combinations(b, 2)
count = 0
for i in c:
  a1 = str(i[0])
  a2 = str(i[1])
  if a1[-1] == a2[0] and a1[0] == a2[-1]:
    count += 1
count *= 2
for i in b:
  j = str(i)
  if j[0] == j[-1]:
    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Ncount = 0

for i in range(1, N+1):
  A = str(i)
  if A[0] == A[-1]:
    Ncount += 1

for i in range(2, N + 1):
  for j in range(1, i):
    A = str(i)
    B = str(j)
    if (A[0] == B[-1]) & (A[-1] == B[0]):
      Ncount += 2

print(Ncount)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = 0
B = 0
ans = 0
for i in range(1, N+1):
  A = i
  for n in range(1, N+1):
    B = n
    AA = str(A)
    BB = str(B)
    if AA[0] == BB[-1] and AA[-1] == BB[0]:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

count = 0
l = []

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

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

        if a == y and b == x:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
N=[str(i) for i in range(1,n+1)]

count=0
for a in N:
  for b in N:
    if str(a)[0]==str(b)[-1] and str(a)[-1]==str(b)[0]:
      count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

ans = 0

a = [0] * 105
for i in range(1, 100):
    for j in range(1, N+1):
        if str(i)[0] == str(j)[-1] and str(j)[0] == str(i)[-1]:
            a[i] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

ans = 0
for i in range(n):
    i = i + 1
    for j in range(n):
        j = j + 1
        if str(i)[-1] == str(j)[0] and str(i)[0] == str(j)[-1]:
            ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

lst = list()
count = 0
for i in range(1, N+1):
    for j in range(1, N+1):
        ahead = int(str(i)[0])
        atail = i % 10
        bhead = int(str(j)[0])
        btail = j % 10
        if ahead == btail and atail == bhead:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

num=0

for a in range(1,N+1):
  for b in range(1,N+1):
    if str(a)[0] == str(b)[-1] and str(a)[-1] == str(b)[0]:
      num +=1
print(num)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	ans = 0

	if N <= 9:
		print(N)
		sys.exit()

	for i in range(1,N+1):
		tmp = str(i)
		start = tmp[0]
		end = tmp[-1]

		if "0" == start or "0" == end:
			continue

		numbers = 0
		if start == end:
			numbers = start
		else:
			numbers = end + start
		es = int(numbers)

		for j in range(es,N+1,10):
			tt = str(j)
			ss = tt[0]

			if ss == end:
				ans += 1
				#print("{} {}".format(i,j))

	print(ans)



if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
c=0
for i in range(1,n+1):
    s = str(i)
    a_s=s[0]
    a_g=s[-1]

    for j in range(1,n+1):
        ss = str(j)
        b_s=ss[0]
        b_g=ss[-1]

        if int(a_s)==int(b_g) and int(a_g)==int(b_s):
            c+=1

print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

result = 0
target_list = [i for i in range(1, n + 1) if i % 10 > 0]
for a in target_list:
    for b in target_list:
        if b > a:
            break
        else:
            head_a = str(a)[0]
            tail_a = str(a)[-1]
            head_b = str(b)[0]
            tail_b = str(b)[-1]
            if head_a == tail_b and tail_a == head_b:
                if a == b:
                    result += 1
                else:
                    result += 2

print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
cnt = 0
for i in range(1,N+1):
  for j in range(1,N+1):
    if str(i)[0:1:] == str(j)[-1::] and str(i)[-1::] == str(j)[0:1:]:
      cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
N=[i for i in range(1,n+1)]

count=0
for a in N:
  for b in N:
    if str(a)[0]==str(b)[-1] and str(a)[-1]==str(b)[0]:
      count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s_list = int(input())

f_list = []

for x in range(1, s_list+1):
	for y in range(1, s_list+1)[::-1]:
		if str(x)[0] == str(y)[-1] and str(y)[0] == str(x)[-1]:
			f_list.append((x, y))
		elif str(x)[0] == str(y)[-1] and str(y)[0] == str(x)[-1]:
			f_list.append((y, x))

print(len(f_list))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = 0
B = 0
x = 0
y = 0
ans = 0
for i in range(N):
  x += 1
  A = x
  y = 0
  for i in range(N):
    y += 1
    B = y
    AA = str(A)
    BB = str(B)
    if AA[0] == BB[-1] and AA[-1] == BB[0]:
      ans += 1
print(ans)