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:
from functools import lru_cache
import sys
sys.setrecursionlimit(10**9)
@lru_cache(maxsize=None)
def f(n):
    if 0 < n and n  < 10:
        return n
    else:
        n = str(n)
        if int(n[-1]) == 0:
            return f(int(n)-1)
        if int(n[0]) < int(n[-1]):
            k = len(n) - 2
            n = int(n)
            if k > 0:
                return f(n-1) + int('1'*k)*2
            else:
                return f(n-1)
        elif int(n[0]) > int(n[-1]):
            k = len(n) - 1
            n = int(n)
            s = int('1'*k)*2
            return  f(n-1) + s
        else:
            k = len(n) - 2
            if k > 0:
                N = int(n)
                return f(N-1) + int('1'*k)*2  + int(n[1:-1])*2 + 3
            else:
                N = int(n)
                return f(N-1) + 3
                
p = int(input())
print(f(p))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
length=len(str(n))

ans=0
for i in range(1,10):
    if n<i:
        break
    s=0
    for j in range(1,length+1):
        if j<=1:
            s+=1
        elif j<length:
            s+=(10**(j-2))
        else:
            for m in range(n,10**(length-1),-1):
                if str(m)[0]==str(m)[-1]==str(i):
                    s+=1
    ans+=s*s
for i1 in range(1,10):
    for i2 in range(1,10):
        if i1==i2:
            break;
        s1=0
        s2=0
        for j in range(2,length+1):
            if j<length:
                s1+=10**(j-2)
                s2+=10**(j-2)
            else:
                for m in range(n,10**(length-1),-1):
                    if str(m)[0]==str(i1) and str(m)[-1]==str(i2):
                        s1+=1
                    elif str(m)[0]==str(i2) and str(m)[-1]==str(i1):
                        s2+=1
        ans+=s1*s2*2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def i__j(i, j, N):
    ans = 0
    for k in range(1, N+1):
        str_k = str(k)
        if str_k[0] == str(i) and str_k[-1] == str(j):
            ans += 1

    return ans

N = int(input())

c = [[0 for i in range(9)] for j in range(9)]
for i in range(9):
    for j in range(9):
        c[i][j] = i__j(i+1, j+1, N)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
cnt=0
for i in range(1,10):
    for k in range(1,10):
        a=0
        b=0
        for n in range(1,N+1):
            r=n%10
            l=n//(10**(len(str(n))-1))
            if k==r and i==l:
                a+=1
            if i==r and k==l:
                b+=1
        cnt+=a*b
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = []
b = []
num  = 0
for i in range(1,N+1):
    a.append(i)
    b.append(i)
for i in a:
    for j in b:
        if str(i)[0] == str(j)[-1] and str(i)[-1] == str(j)[0]:
            num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ans = 0
tbl1 = [[0]*10 for i in range(10)]
tbl2 = [[0]*10 for i in range(10)]
# for j in range(10):
#     for k in range(10):
#         cnt1 = 0
#         cnt2 = 0
#         for i in range(1,n+1):
#             s = str(i)
#             if s[0] == str(k)and s[-1] == str(j):
#                 cnt1 += 1
#             if s[0] == str(j) and s[-1] == str(k):
#                 cnt2 += 1
#         ans += cnt1*cnt2
for i in  range(1,n+1):
    s = str(i)
    for j in range(10):
        for k in range(10):
            if s[0] == str(k)and s[-1] == str(j):
                tbl1[k][j] += 1
            if s[0] == str(j) and s[-1] == str(k):
                tbl2[j][k] += 1
for j in range(10):
        for k in range(10):
            ans += tbl1[k][j] * tbl2[j][k]
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def count(i,j):
    cnt = 0
    for k in range(1,N+1):
        temp = list(str(k))
        if temp[0]==str(i) and temp[-1]==str(j):
            cnt += 1
    return cnt

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
def answer(i,j):
    kotae=0
    for k in range(1,N+1):
        if int(str(k)[0])==i and int(str(k)[-1])==j:
            kotae+=1
    return kotae
answers=0
for i in range(10):
    for j in range(10):
        answers+=answer(i,j)*answer(j,i)
print(answers)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

ans = 0
for i in range(1,10) :
  for j in range(i,10) :
    c_a = 0
    c_b = 0
    for k in range(1,n+1) :
      if int(str(k)[0]) == i and int(str(k)[-1]) == j :
        c_a += 1
      if int(str(k)[0]) == j and int(str(k)[-1]) == i :
        c_b += 1
    if i == j :
      ans += c_a*c_b
    else :
      ans += c_a*c_b*2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

lst = []

for A in range(1,N+1):
    for B in range(1,N+1):
        lst.append([str(A),str(B)])

count = 0

for i in lst:
    if i[0][-1] == i[1][0] and i[0][0] == i[1][-1]:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

dp = dict()
total = 0
for i in range(1, N+1):
    mae = str(i)[0]
    usiro = str(i)[-1]
    key = mae + usiro
    if key in dp.keys():
        total += dp[key]
    else:
        count = 0
        for j in range(1, N+1):
            mae = str(j)[0]
            usiro = str(j)[-1]
            if key == usiro + mae:
                count += 1
        dp[key] = count
        total += count
    
print(total)

Submission status: Time Limit Exceeded

Solution code:
def handstand(n):
    count = 0
    for i in range(1, n + 1):
        A_str = str(i)
        A_first_int = A_str[0]
        A_last_int = A_str[len(A_str) - 1]
        for j in range(1, n + 1):
            B_str = str(j)
            B_first_int = B_str[0]
            B_last_int = B_str[len(B_str) - 1]
            if A_last_int == B_first_int and A_first_int == B_last_int:
                count += 1
    return count

if __name__ == "__main__":
    n = int(input())
    print(handstand(n))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def c_i_j(n, i, j):
    counter = 0
    for num in range(1, n + 1):
        if int(str(num)[0]) == i and int(str(num)[-1]) == j:
            counter += 1
    return counter

for i in range(1, 10):
    for j in range(1, 10):
        ans += c_i_j(N, i, j) * c_i_j(N, j, i)

print(ans)

Submission status: Time Limit Exceeded

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

if N == 1:
    count += 1    
else:    
    for A in range(1,N + 1):
        AA = list(str(A))
        if AA[0] == AA[len(AA) - 1]:
            count += 1
        for B in range(A + 1,N + 1):
            BB = list(str(B))
            if AA[0] == BB[len(BB) - 1] and AA[len(AA) - 1] == BB[0]:
                count += 2
print(count)

Submission status: Time Limit Exceeded

Solution code:
def handstand2(n):
    c = 0
    for i in range(10):
        for j in range(10):
            c_i_j = 0
            c_j_i = 0
            for k in range(n):
                if str(k+1)[0] == str(i) and str(k+1)[-1] == str(j):
                    c_i_j += 1
                if str(k+1)[0] == str(j) and str(k+1)[-1] == str(i):
                    c_j_i += 1
            c += c_i_j * c_j_i
    return c

n = int(input())

print(handstand2(n))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
ans=0
for i in range(10):
    for j in range(10):
        a = 0
        b = 0
        #print(i,j)
        for k in range(1,n+1):
            if str(k)[0] == str(i) and str(k)[-1] == str(j):
                #sys.stdout.write(str(k)+",")
                a+=1
            if str(k)[0] == str(j) and str(k)[-1] == str(i):
                b+=1
                #print(k)
        ans+=a*b
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 (str(i)[-1] == str(j)[0]) and (str(i)[0] == str(j)[-1]):
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [i+1 for i in range(n)]
count = 0
from itertools import product
res = product(a,repeat = 2)
for c in res:
    if (str(c[0])[-1:] == str(c[1])[0]) and (str(c[0])[0] == str(c[1])[-1:]):
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [i+1 for i in range(n)]
count = 0
from itertools import product
for c in product(a,repeat = 2):
    if (str(c[0])[-1:] == str(c[1])[0]) and (str(c[0])[0] == str(c[1])[-1:]):
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

count = 0
for i in range(1,N+1):
    li = i//10**(len(str(i))-1) 
    ri = i%10
    for j in range(i,N+1):
        lj = j//10**(len(str(j))-1)
        rj = j%10
        if li == rj and lj == ri:
            if i == j:
                count += 1
            else:
                count += 2

print(count)

Submission status: Time Limit Exceeded

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

#############
# Libraries #
#############

import math
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

#############
# Main Code #
#############

N = int(input())

@lru_cache(maxsize=None)
def count(n,a,b):
  counter = 0
  for i in range(1,n+1):
    if int(str(i)[0])==a and int(str(i)[-1])==b:
      counter += 1
  return counter

ans = 0
for i in range(10):
  for j in range(10):
    ans += count(N,i,j)*count(N,j,i)

print(ans)

Submission status: Time Limit Exceeded

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

#############
# Libraries #
#############

import math
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

#############
# Main Code #
#############

N = int(input())

def count(n,a,b):
  counter = 0
  for i in range(1,n+1):
    if int(str(i)[0])==a and int(str(i)[-1])==b:
      counter += 1
  return counter

ans = 0
for i in range(10):
  for j in range(10):
    ans += count(N,i,j)*count(N,j,i)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
def c(i:int,j:int,n:int): #kの先頭の桁の数はiに等しく、末尾の桁の数はjに等しいn以下の数
  k = 0
  for x in range(1,n+1):
    L_x = list(str(x))
    if int(L_x[0]) == i and int(L_x[-1]) == j:
      k += 1
  return k
K = 0
for i in range(10):
  for j in range(10):
    K += c(i,j,N)*c(j,i,N)
print(K)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ans = 0
for A in range(1, N+1):
    for B in range(A, N+1):
        str_A = str(A)
        str_B = str(B)
        if str_A[-1] == str_B[0] and str_A[0] == str_B[-1]:
            # print(A, B)
            ans += 1
            if A != B:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

C = 0
for i in range(1,10):
  for j in range(1,10):
    cij = 0
    cji = 0
    for k in range(1,N+1):
      s = str(k)
      if int(s[0]) == i and int(s[-1]) == j:
        cij += 1
      if int(s[0]) == j and int(s[-1]) == i:
        cji += 1
    C += cij*cji
print(C)

Submission status: Time Limit Exceeded

Solution code:
from numpy import *

N = int(input())

A = zeros(N, dtype=int)
B = zeros(N, dtype=int)

al = []
bl = []

count = 0
for i in range(N):
    A[i] = i + 1
    al.append( str(A[i]) )

    a_s = (al[i])[0]
    a_e = (al[i])[len(al[i]) - 1]

    for j in range(N):
        B[j] = j + 1
        bl.append( str(B[j]) )

        b_s = (bl[j])[0]
        b_e = (bl[j])[len(bl[j]) - 1]

        if a_e == b_s and a_s == b_e:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.buffer.readline
N=int(readline().strip())
n=[str(i) for i in range(1,N+1)]
cnt=0
for i in n:
  for j in n:
    if i[0]==j[-1] and i[-1]==j[0]:
      cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    M = [str(i) for i in range(1,N+1)]
    ans = 0

    for i in M:
        ans += len(list([s for s in M if s.startswith(i[-1]) and s.endswith(i[0])]))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def f(n):
  res = 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]:
        #print(i, j)
        res += 1
  return res

print(f(int(input())))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

Submission status: Time Limit Exceeded

Solution code:

N=int(input())

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
contain=[]
for i in range(N+1):
    if str(i)[-1]!='0' or str(i)[-1]!='0':
        contain.append([str(i)[0],str(i)[-1]])

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

def func(x,y,n=N):
    R=0
    for i in range(1,n+1):
        Len=len(str(i))
        up=int(str(i)[0])
        down=int(str(i)[Len-1])
        if up==x and down==y:
            R+=1
    return R

ans=0

for j in range(10):
    for k in range(10):
        ans+=func(j,k)*func(k,j)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

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


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

Submission status: Time Limit Exceeded

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

counter = 0
rnum = 0
cnum = 0
l = len(lst)

for i in range(l):
    rnum = lst[-1][-1] + lst[-1][0]
    cnum = lst.count(rnum)
    if rnum[0] == rnum[1]:
        if cnum == 1:
            counter += 1
        else:
            counter += (2 * lst.count(rnum)) - 1
    else:
        counter += (2 * lst.count(rnum))
    lst.pop()

print(counter if N > 9 else N)

Submission status: Time Limit Exceeded

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

counter = 0
rnum = 0
l = len(lst)

for i in range(l):
    rnum = lst[i][-1] + lst[i][0]
    counter += lst.count(rnum)

print(counter if N > 9 else N)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

c = [[0 for i in range(9)] for i in range(9)]

for i in range(9):
  for j in range(9):
    for n in range(1, N+1):
      if (n % 10) == j+1 and int(str(n)[0]) == i+1:
        c[i][j] += 1
        
ans = 0
for i in range(9):
  for j in range(9):
    ans = ans + c[i][j]*c[j][i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N = int(input())
score = 0
a = [str(x+1) for x in range(N)]
b = [str(x+1) for x in range(N)]
d = deque(a)

score = 0
i = 0
while len(d)!=0:
    for j in b:
        if d[i][0]==j[-1]:
            if d[i][-1]==j[0]:
                score += 1
    d.popleft()
    
print(score)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

count = 0
for x in range(1, n+1):
    for y in range(1, n+1):
        x_s = str(x)[0]
        x_e = str(x)[-1]
        y_s = str(y)[0]
        y_e = str(y)[-1]
        if x_s == y_e and x_e == y_s:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ans = 0
for i in range(1, N+1):
  head_a = int(str(i)[0])
  tail_a = int(str(i)[-1])
  if tail_a == 0:
    continue
  for j in range(1, N+1):
    head_b = int(str(j)[0])
    tail_b = int(str(j)[-1])
    if head_a == tail_b and tail_a == head_b:
      ans += 1
print(ans)

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):
    head_a = int(str(i)[0])
    tail_a = int(str(i)[-1])
    head_b = int(str(j)[0])
    tail_b = int(str(j)[-1])
    if head_a == tail_b and tail_a == head_b:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# Handstand 2
# c_ij = {1<=k<=N : 先頭の桁がiで、末尾の桁の数がjであるもの}

N = int(input())
count = 0

# 先頭の桁をi, 末尾の桁をjとする数の数を求める
c_memo = {}
def c(i, j):
    count_num = 0
    key = str(i) + '_' + str(j)
    if key in c_memo:
        return c_memo[key]
    for a in range(1,N+1):
        a_str = str(a)
        a_i, a_j = int(a_str[0]), int(a_str[-1]) # 各数の先頭桁を求める
        if i==a_i and j==a_j:
            count_num = count_num + 1
    c_memo[key] = count_num
    return count_num


for i in range(10):
    for j in range(10):
        count += c(i,j)*c(j,i)

print(count)

Submission status: Time Limit Exceeded

Solution code:
# Handstand 2
# c_ij = {1<=k<=N : 先頭の桁がiで、末尾の桁の数がjであるもの}

N = int(input())
count = 0

# 先頭の桁をi, 末尾の桁をjとする数の数を求める
def c(i, j):
    count_num = 0
    for a in range(1,N+1):
        a_str = str(a)
        a_i, a_j = int(a_str[0]), int(a_str[-1]) # 各数の先頭桁を求める
        if i==a_i and j==a_j:
            count_num = count_num + 1
    return count_num


for i in range(10):
    for j in range(10):
        count += c(i,j)*c(j,i)

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

table = [[str(x), str(y)] for x in range(1, n+1) for y in range(1, n + 1)]

count = 0
for i in range(len(table)):
    w_x = table[i][0]
    w_y = table[i][1]

    if (w_x[0] == w_y[-1]) and (w_x[-1] == w_y[0]):
        count += 1

print(count)

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):
        c = str(a)
        d = str(b)
        e = c[-1]
        f = d[0]
        g = c[0]
        h = d[-1]
        if e == f and g == h:
            cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def get_first(num):
    k=num
    while True:
        if k < 10:
            break
        k = k//10
    return k

for A in range(1,n+1):
    for B in range(1,n+1):
        A1 = get_first(A)
        B1 = get_first(B)
        A2 = A%10
        B2 = B%10
        if A1==B2 and A2==B1:
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def CheckDigit(A, B):
    # A末とB先端 
    if A[0] != B[len(B) - 1]: return 0
    # A先端とB末
    if B[0] != A[len(A) - 1]: return 0
    return 1 

def ArrayRotation(n):
    ans = 0
    for i in range(n):
        for j in range(n):
            ans += CheckDigit(str(i+1), str(j+1))
    return ans

if N <= 11:
    print(N)
else:
    print(ArrayRotation(N))

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):
     a = [str(x) for x in str(i)]
     b = [str(x) for x in str(j)]
     if a[-1] == b[0] and a[0] == b[-1]:
         count += 1

print(count)

Submission status: Time Limit Exceeded

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

N = int(input())
nums = [str(i) for i in range(1, N+1)]
heads = [s[0] for s in nums]
tails = [s[-1] for s in nums]

count = 0
for A in range(N):
    for B in range(A, N):
        if heads[A] == tails[B] and tails[A] == heads[B]:
            if A == B:
                count += 1
            else:
                count += 2

print(count)

Submission status: Time Limit Exceeded

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

N = int(input())

count = 0
for A in range(1, N+1):
    head_A = str(A)[0]
    tail_A = str(A)[-1]
    for B in range(A, N+1):
        head_B = str(B)[0]
        tail_B = str(B)[-1]
        if head_A == tail_B and tail_A == head_B:
            if A == B:
                count += 1
            else:
                count += 2

print(count)

Submission status: Time Limit Exceeded

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

N = int(input())

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
def solve():
    n=int(stdin.readline())
    cnt=0
    for i in range(1,n+1):
        df=str(i)[0]
        dl=str(i)[-1]
        for j in range(i,n+1):   
            dfb=str(j)[0]
            dlb=str(j)[-1]
            if df==dlb and dl==dfb:
                if i==j:
                    cnt+=1
                else:
                    cnt+=2

    print(cnt)
solve()

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        c1 = 0
        c2 = 0
        for x in range(1,N+1):
            d = 10 ** int(math.log10(x)) 
        
            if x % 10 == i and x // d == j:
                c1 += 1
            if x % 10 == j and x // d == i:
                c2 += 1
        ans += c1 * c2

print (ans)

Submission status: Time Limit Exceeded

Solution code:
import math                                                                                                                                                         
                                                                                                                                                                    
n = int(input())                                                                                                                                                    
                                                                                                                                                                    
def N(n, i, j):                                                                                                                                                     
    num = 0                                                                                                                                                         
    for x in range(1,n+1):                                                                                                                                          
        low = int(x%10)                                                                                                                                             
        high = int(x/(pow(10, int(math.log10(x)))))                                                                                                                 
        if((high == i)and(low == j)):                                                                                                                               
            num = num + 1                                                                                                                                           
    return num                                                                                                                                                      
                                                                                                                                                                    
s = 0                                                                                                                                                               
for i in range(1,10):                                                                                                                                               
    for j in range(1,10):                                                                                                                                           
        s = s + N(n, i, j)*N(n, j, i)                                                                                                                               
                                                                                                                                                                    
print(s)

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())

def N(n, i, j):
    num = 0
    for x in range(1,n+1):
        low = int(x%10)
        high = int(x/(pow(10, int(math.log10(x)))))
        if((high == i)and(low == j)):
            num = num + 1
    return num

s = 0
for i in range(1,10):
    for j in range(1,10):
        s = s + N(n, i, j)*N(n, j, i)

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def last(n):
    ans = 0
    while n > 0:
        ans = n % 10
        n = n // 10
    return ans

def first(n):
    return n%10

n = int(input())
ans = 0
for i in range(1,n+1):
    for j in range(1, i+1):
        if first(i) == last(j) and first(j) == last(i):
            ans += 1 if i == j else 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())

r = 0
for u in range(1, 10):
    for l in range(1, u + 1):
        r1 = 0
        r2 = 0
        for i in range(1, N + 1):
            x = int(i / (10 ** int(math.log10(i))))
            if i % 10 == l and x == u:
                r1 += 1
            if i % 10 == u and x == l:
                r2 += 1
        r += r1 * r2
        if u != l:
            r += r1 * r2
print(r)