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

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r1 = 0
        r2 = 0
        for i in range(1, N + 1):
            x = int(str(i)[0])
            if i % 10 == l and x == u:
                r1 += 1
            if i % 10 == u and x == l:
                r2 += 1
        r += r1 * r2
print(r)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

r = 0
for u in range(1, 10):
    for l in range(1, 10):
        r1 = 0
        r2 = 0
        for i in range(1, N + 1):
            if i % 10 == l and int(str(i)[0]) == u:
                r1 += 1
            if i % 10 == u and int(str(i)[0]) == l:
                r2 += 1
        r += r1 * r2
print(r)

Submission status: Time Limit Exceeded

Solution code:
import math
N=int(input())

n=0

for i in range(N):
  A=i+1
  for j in range(N):
    B=j+1

    if A%10==math.floor(B/(10**(len(str(B))-1))):
      if math.floor(A/(10**(len(str(A))-1)))==B%10:
        n+=1

print(n)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
memo = {}

def func(i, j):
    if (i, j) in memo:
        return memo[(i, j)]

    count = 0
    for n in range(1, N+1):
        str_n = str(n)
        if str_n[-1] == str(i) and str_n[0] == str(j):
            count += 1
    
    memo[(i,j)] = count
    return count

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

print(ret)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
n_digit = len(str(n))
count = 0
for i in range(1,n+1):
    a = i
    for j in range(1,n+1):
        if j > n:
            break
        b = j
        str_a = str(a)
        str_b = str(b)
        if str_a[0] == str_b[len(str_b)-1] and str_a[len(str_a)-1] == str_b[0]:
            count += 1
print(count)

Submission status: Time Limit Exceeded

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

cnt = 0
inter = 0
for i, c in enumerate(C):
  index = str(i + 1)

  for v in c:
    if index[0] == v[-1] and index[-1] == v[0]:
      cnt += 1
      inter = inter + 1 if int(index) == int(v) else inter
      
print( 2 * cnt - inter)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
cnt = 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]:
      cnt += 1
      
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())


ans = 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]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def getCount(num, i,j):
    cnt = 0
    for n in range(1,num+1):
        s = str(n)
        if s[0] == str(i) and s[-1] == str(j):
            cnt+=1
    return cnt        

n = int(input())

res = 0
for i in range(0,10):
    for j in range(0,10):
        res += getCount(n,i,j) * getCount(n,j,i)
print(res)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=1
for i in range(1,N):
  p=str(i+1)
  l=0
  c=0
  for j in range(1,i+2):
    q=str(j)
    if p[0]==q[-1] and p[-1]==q[0]:
      if j==i+1:
        l=1
      else:
        c=c+1
  S=S+2*c+l
print(S)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

d = [[int((str(x))[0]),x % 10] for x in range(1,N + 1)] 
a = [[x % 10,int((str(x))[0])] for x in range(1,N + 1)]

count = 0
for i in range(0,N):
    for j in range(0,N):
        if d[i] == a[j]:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
N_list = [str(i) for i in range(1, N + 1)]
nums = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

count = 0
for i in nums:
    for j in nums:
        cij, cji = 0, 0
        for k in N_list:
            if k[0] == i and k[-1] == j:
                cij += 1
            if k[0] == j and k[-1] == i:
                cji += 1
        count += cij * cji

print(count)

Submission status: Time Limit Exceeded

Solution code:
def getamtcombos(n):
  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(a)[0] == str(b)[-1]:
        count += 1
  return count

n = int(input())

print(getamtcombos(n))

Submission status: Time Limit Exceeded

Solution code:
def sigma(n,i,j):
    icnt=0
    stri=str(i)
    strj=str(j)
    for k in range(n):
        strk=str(k+1)
        if strk[0]==stri and strk[-1]==strj:
            icnt=icnt+1
    return icnt
            

n=int(input())
ncnt=0
for k in range(10):
    for m in range(10):
        s1=sigma(n,k,m)
        s2=sigma(n,m,k)
        ncnt=ncnt+s1*s2
print(ncnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def main1(N):
    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)

def main2(N):
    #9*9の表
    lst = list()
    for i in range(1,10):
        lst.append([0]*9)

    for n in range(1, N+1):
        for i in range(1,10):
            for j in range(1,10):
                head = str(n)[0]
                tail = str(n)[-1]
                if i == int(head) and j == int(tail):
                    lst[i-1][j-1] += 1
    
    count = 0
    for i in range(0, len(lst)):
        for j in range(0, len(lst[i])):
            count += lst[i][j] * lst[j][i]

    print(count)

main2(N)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
nlist = []

ijcount = 0
jicount = 0

total_count = 0

for num in range(1, n+1):
    nlist.append(str(num))

for i in range(1,10):
    for j in range(1,10):
        ijcount = len([s for s in nlist if s.startswith(str(i)) and s.endswith(str(j))])
        jicount = len([s for s in nlist if s.startswith(str(j)) and s.endswith(str(i))])
        total_count += ijcount * jicount
print(total_count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

str_N = str(N)
num = 0
char_set = {str(i) for i in range(1, N)}

for n1 in range(1, N + 1):
    for n2 in range(1, N + 1):
        str_n1, str_n2 = map(str, [n1, n2])
        length1 = len(str_n1)
        length2 = len(str_n2)

        if str_n1[0] == str_n2[length2-1] and str_n1[length1-1] == str_n2[0]:
            num += 1


print(int(num))

Submission status: Time Limit Exceeded

Solution code:
def N(x,y,n): # no of x...y < n
    r=0
    for i in range(1,n+1):
        if str(i)[0]==str(x) and str(i)[-1]==str(y):
            r+=1
    return r

ans=0
n=int(input())
for x in range(1,10):
    for y in range(1,10):
        ans+=N(x,y,n)*N(y,x,n)
        
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

count = 0

for i in range(N):
    ii = str(i+1)
    #ll = len(ii)
    for j in range (N):
        jj = str(j+1)
        #l = len(jj)
        if ((ii[0] == jj[-1]) and (ii[-1] == jj[0])):
            count +=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

count = 0
for i in range(1, 10):
    for j in range(1, 10):
        cij = 0
        cji = 0
        
        for k in range(1, n+1):
            if str(k)[0] == str(i) and str(k)[-1] == str(j):
                cij += 1
                
            if str(k)[0] == str(j) and str(k)[-1] == str(i):
                cji += 1
        
        count += cij * cji

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ans = 0
As = [0] * N
Bs = [0] * N
for i in range(N):
    As[i] = i+1
    Bs[i] = i+1

for A in As:
    for B in Bs:
        if str(A)[0] == str(B)[-1] and str(A)[-1] == str(B)[0]:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
count=[[0 for i in range(10)] for j in range(9)]
ans=0
for i in range(1,n+1):
    num=list(str(i))
    l,r=eval(num[0]),eval(num[-1])
    #print(str(l)+"  "+str(r))
    #print(l+r)
    count[l-1][r]+=1
#print(count)
for i in range(1,10):
    for j in range(1,10):
        if i==j:
            continue
        ans+=(count[i-1][j])*(count[j-1][i])
for i in range(1,10):
    ans+=(count[i-1][i]**2)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
N=int(input())
kotae= []
count = 0
A=list(i for i in range(1,N+1))
B=A.copy()
C=A+B
for j in itertools.permutations(C,2):
    kotae.append(j)
kari = set(kotae)
ans = list(kari)
for v in range(len(ans)):
    if str(ans[v][0])[-1]==str(ans[v][1])[0] and str(ans[v][0])[0]==str(ans[v][1])[-1]:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
pairs = []
answer = 0
def StartEnd(x):
    start = str(x)[0]
    end = str(x)[-1]
    return int(start), int(end)

for i in range(1, N+1):
    pair = ()
    a, b = StartEnd(i)
    pair += (a, b)
    pairs.append(pair)
    
for i in range(N):
    p = pairs[i]
    q = p[::-1]
    answer += pairs.count(q)
    
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
strN = str(N)
order = len(strN)
Ns = int(strN[0])
Ne = int(strN[-1])
count = 0
for e in range(1, 10):
  e = str(e)
  for s in range(1, 10):
    s = str(s)
    ces = 0
    cse = 0
    for n in range(1, N + 1):
      sn = str(n)
      if sn[0] == s and sn[-1] == e:
        ces += 1
      if sn[0] == e and sn[-1] == s:
        cse += 1
    count += ces * cse
    
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
X=[]
for i in range(1,10):
    x=[]
    for j in range(1,10):
        c=0
        for k in range(1,N+1):
            if((str(k)[0]==str(i))&(str(k)[-1]==str(j))):
                c+=1
        x.append(c)
    X.append(x)
Ans=0
for i in range(9):
    for j in range(9):
        Ans+=X[i][j]*X[j][i]
    
print(Ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools as it
N=int(input())

count=0

A=list(it.combinations_with_replacement(range(1,N+1),2))

#print(A)
for i in range(len(A)):
    if str(A[i][0])[:1]==str(A[i][1])[-1:] and str(A[i][0])[-1:]==str(A[i][1])[:1]:
        if A[i][0]==A[i][1]:
            count+=1
        else:
            count+=2
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def c_ab(n,a,b):
    ret = 0
    for i in range(1,n+1):
        max_digit = int(str(i)[0]) #iを文字列にしたときの頭
        min_digit = int(str(i)[-1]) #iを文字列にしたときの末尾
        if max_digit == a and min_digit == b:
            ret += 1
    return ret

C_ab = [[0 for i in range(10)] for j in range(10)]
for i in range(10):
    for j in range(10):
            C_ab[i][j] = c_ab(N,i,j)

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
def c(m,n,N):
    count=0
    for i in range(1,N+1):
        p=str(i)
        a=int(p[0])
        b=int(p[-1])
        if a==m and b==n:
            count+=1
    return count
z=0
for i in range(10):
    for j in range(10):
        z+=c(i,j,N)*c(j,i,N)

print(z)

Submission status: Time Limit Exceeded

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


def chose(i,j):
    count=0
    for x in range(1,N+1):
        X=str(x)
        
        if int(X[0])==i and int(X[-1])==j:
            count+=1
    return count

score = 0
for i in range(10):
    for j in range(10):
        score += chose(i,j)*chose(j,i)
        
print(score)

Submission status: Time Limit Exceeded

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


def chose(i,j):
    count=0
    for x in range(1,N+1):
        X=str(x)
        if X[0]==str(i) and X[-1]==str(j):
            count+=1
    return count

score = 0
for i in range(10):
    for j in range(10):
        score += chose(i,j)*chose(j,i)
        
print(score)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = []

for i in range(1,10):
    a = []
    for j in range(1,10):
        c = 0
        for k in range(1,N+1):
            if (str(k)[0] == str(i))&(str(k)[-1] == str(j)):
                c +=1
        a.append(c)
    X.append(a)
    
ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

ans = 0

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

for i in range(1,10):
    for j in range(1, 10):
        number = 1
        while number <= n:
            n_str = str(number)
            if int(n_str[0]) == i and int(n_str[-1]) == j:
                table[i][j] += 1
            number += 1

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
t = [0 for _ in range(81)]
ans = 0
for i in range(1, 10):
    for l in range(1, 10):
        for q in range(1, N+1):
            s = 0
            a = str(q)[0]
            b = str(q)[-1]
            if int(a) == i and int(b) == l:
                s += 1
            t[9*(i - 1)+l - 1] += s
for i in range(1, N+1):
    a = str(i)[0]
    b = str(i)[-1]
    if b == "0":
        continue
    elif a == b:
        ans += t[9*(int(a)-1)+int(a)-1]
    else:
        ans += t[9*(int(b)-1)+int(a)-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

ans = 0
ctable = [[-1 for _ in range(10)] for _ in range(10)]

def c(x, y):
    cnt = 0
    strx = str(x)
    stry = str(y)
    for i in range(1, N+1):
        stri = str(i)
        if stri[0] == strx and stri[-1] == stry:
            cnt += 1
    return cnt

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
nl = str(n)
ll = len(nl)
lis = []
ans = 0
if ll == 1:
  print(n)
  exit()
  
def countn(a,b):
  cur = 0
  for i in range(1,n+1):
    fff=str(i)
    if int(fff[0]) == a and int(fff[-1]) == b:
      cur += 1
  return cur

for i in range(1, 10):
  for j in range(1, i+1):
    if i == j:
      ans += countn(i, j)*countn(j, i)
    else:
      ans += 2*countn(i, j)*countn(j, i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
t = [0 for _ in range(81)]
ans = 0
for i in range(1, 10):
    for l in range(1, 10):
        for q in range(1, N+1):
            s = 0
            if int(str(q)[0]) == i and int(str(q)[-1]) == l:
                s += 1
            t[9*(i - 1)+l - 1] += s
for i in range(1, N+1):
    a = str(i)[0]
    b = str(i)[-1]
    if b == "0":
        continue
    elif a == b:
        ans += t[9*(int(a)-1)+int(a)-1]
    else:
        ans += t[9*(int(b)-1)+int(a)-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
def cnt(b,e,n_str):
  res=0
  for i in range(int(n_str)+1):
    if int(str(i)[0])==b and i%10==e:
      res+=1
  return res
def sol():
  n_str=input()
  c={i:{j:cnt(i,j,n_str) for j in range(1,10)} for i in range(1,10)}
  return sum(c[b][e]*c[e][b] 
             for b,e in product(range(1,10),repeat=2))

print(sol())

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

ans = 0
ctable = [[-1 for _ in range(10)] for _ in range(10)]

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

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

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

print(ans)

Submission status: Time Limit Exceeded

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

def chose(i,j):
    count=0
    for x in range(1,N+1):
        if x//10**(len(str(x))-1)==i and x%10==j:
            count+=1
            
    return count

score = 0
for i in range(10):
    for j in range(10):
        score += chose(i,j)*chose(j,i)
        
print(score)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

def c(i,j):
  ret=0
  for k in range(1,n+1):
    if str(k)[0]==str(i) and str(k)[-1]==str(j):
      ret+=1
  return ret

ans=0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
def cnt(b,e,n_str):
  res=0
  for i in range(int(n_str)+1):
    if int(str(i)[0])==b and i%10==e:
      res+=1
  return res
def sol():
  n_str=input()
  return sum(cnt(b,e,n_str)*cnt(e,b,n_str) 
             for b,e in product(range(1,10),repeat=2))

print(sol())

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

ans = 0

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

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 f(n):
    s=str(n)
    return int(s[0]),int(s[-1])

def g(x,y,N):
    ans=0
    for i in range(1,N+1):
        a,b=f(i)
        if a==x and b==y:
            ans+=1
    return ans

A=[[0 for _ in range(10)] for __ in range(10)]


for i in range(10):
    for j in range(10):
        A[i][j]=g(i,j,N)

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



print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
n=0
for a in range(N):
    aa=a+1
    a1=len(str(a+1))
    for b in range(N):
        bb=b+1
        b1=len(str(b+1))
        if str(aa)[-1]==str(bb)[-b1] and str(aa)[-a1]==str(bb)[-1]:
            n+=1
print(n)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
t = [[0 for i in range(9)] for _ in range(9)]
ans = 0
for i in range(1, 10):
    for l in range(1, 10):
        for q in range(1, N+1):
            s = 0
            if int(str(q)[0]) == i and int(str(q)[-1]) == l:
                s += 1
            t[i - 1][l - 1] += s
for i in range(1, N+1):
    a = str(i)[0]
    b = str(i)[-1]
    if b == "0":
        continue
    elif a == b:
        ans += t[int(a)-1][int(a)-1]
    else:
        ans += t[int(b)-1][int(a)-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
check = tuple(i for i in range(1, N+1))
t = [[0 for i in range(9)] for _ in range(9)]
ans = 0
for i in range(1, 10):
    for l in range(1, 10):
        for q in check:
            s = 0
            if int(str(q)[0]) == i and int(str(q)[-1]) == l:
                s += 1
            t[i - 1][l - 1] += s
for i in range(1, N+1):
    if str(i)[-1] == "0":
        continue
    elif len(str(i)) == 1:
        ans += t[i-1][i-1]
    else:
        ans += t[int(str(i)[-1])-1][int(str(i)[0])-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
check = tuple(i for i in range(1, N+1))
t = [[0 for i in range(9)] for _ in range(9)]
ans = 0
for i in range(1, 10):
    for l in range(1, 10):
        for q in check:
            if int(str(q)[0]) == i and int(str(q)[-1]) == l:
                t[i-1][l-1] += 1
for i in range(1, N+1):
    if str(i)[-1] == "0":
        continue
    elif len(str(i)) == 1:
        ans += t[i-1][i-1]
    else:
        ans += t[int(str(i)[-1])-1][int(str(i)[0])-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
if N <= 9:
    print(N)
    exit()

tot_count = 0
for af in map(str, range(1, 10)):
    for bf in map(str, range(1, 10)):
        a_count = 0
        b_count = 0
        for a in map(str, range(1, N + 1)):
            if a[0] == af and a[-1] == bf:
                a_count += 1
        for b in map(str, range(1, N + 1)):
            if b[0] == bf and b[-1] == af:
                b_count += 1

        tot_count += a_count * b_count

print(tot_count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
check = [i for i in range(1, N+1)]
t = [[0 for i in range(9)] for _ in range(9)]
ans = 0
for i in range(1, 10):
    for l in range(1, 10):
        for q in check:
            if int(str(q)[0]) == i and int(str(q)[-1]) == l:
                t[i-1][l-1] += 1
for i in range(1, N+1):
    if str(i)[-1] == "0":
        continue
    elif len(str(i)) == 1:
        ans += t[i-1][i-1]
    else:
        ans += t[int(str(i)[-1])-1][int(str(i)[0])-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import deque
input = sys.stdin.readline
N = int(input())
check = [i for i in range(1, N+1)]
t = [[0 for i in range(9)] for _ in range(9)]
ans = 0
for i in range(1, 10):
    for l in range(1, 10):
        for q in check:
            if int(str(q)[0]) == i and int(str(q)[-1]) == l:
                t[i-1][l-1] += 1
for i in range(1, N+1):
    if str(i)[-1] == "0":
        continue
    elif len(str(i)) == 1:
        ans += t[i-1][i-1]
    else:
        ans += t[int(str(i)[-1])-1][int(str(i)[0])-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

c = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1, 10):
  for j in range(1, 10):
    cnt = 0
    for n in range(1, N + 1):
      if n // (10 ** (len(str(n)) - 1)) == i and n % 10 == j:
        cnt += 1
    c[i - 1][j - 1] = cnt

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())
def f(n):
    s=str(n)
    return int(s[0]),int(s[-1])

def g(x,y,N):
    ans=0
    for i in range(1,N+1):
        a,b=f(i)
        if a==x and b==y:
            ans+=1
    return ans

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

cnt = 0
for i in range(1, N + 1):
    n_str = str(i)

    pre = n_str[0]
    suf = n_str[-1]

    if suf == '0':
        continue

    for j in range(1, N + 1):
        m_str = str(j)

        pre1 = m_str[0]
        suf1 = m_str[-1]

        if suf1 == '0':
            continue

        if pre == suf1 and suf == pre1:
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

c_list = []
for i in range(10):
    temp_list = []
    for j in range(10):
        temp_count = 0
        for n in range(1, N+1):
            if n%10==j and n//(10**(len(str(n))-1) )==i:
                temp_count += 1
        temp_list.append(temp_count)
    c_list.append(temp_list)
sum=0
for i in range(10):
    for j in range(10):
        sum += c_list[i][j]*c_list[j][i]
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
cnt = 0
for i in range(0, 10):
    for j in range(0, 10):
        cij, cji = 0, 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
        cnt += cij * cji
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import math
N=int(input())
def f(n):
    a=int(math.log10(n)+1)
    b=n//(10**(a-1))
    c=n%10
    return b,c

def g(x,y,N):
    ans=0
    for i in range(1,N+1):
        a,b=f(i)
        if a==x and b==y:
            ans+=1
    return ans

ans=0
for i in range(10):
    for j in range(10):
        ans+=g(i,j,N)*g(j,i,N)
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):
      str_i, str_j=str(i), str(j)
      size_i, size_j=len(str_i), len(str_j)
      if int(str_i[0])==int(str_j[size_j-1]):
          if int(str_j[0])==int(str_i[size_i-1]):
              count+=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(N):
            if topList[k] == i and bottomList[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:
import itertools

N = int(input())
def keta(n):
    count = 1
    a = n
    while(1):
        a = a//10
        if a < 10:
            break
        count +=1
    return count
al = []
ap = []
bl = []
bp = []
P = dict()
L = dict()
ans = 0
R = []
#自己完結型を求める

count = keta(N)
kali_count = count - 1

don = 0
if N < 10:
    ans += N
else:
    ans += 9
    for i in range(kali_count):
        if i == 0:
            ans += 9
            don += 9
        else:
            rui = 10**(i+1)
            don += 9*rui
            sdon = str(don)
            if len(sdon) == 1:
                don = 9
            elif len(sdon) == 2:
                don = 90
            elif len(sdon) ==3:
                don = 990
            elif len(sdon) == 4:
                don = 9900
            elif len(sdon) == 5:
                don = 99000
            ans += don
W = 10**count
for i in range(W,N+1):
    s = str(i)
    a = s[0]
    b = s[-1]
    if a==b:
        ans += 1


P = list(itertools.permutations(range(1,N+1),2))
for i in range(len(P)):
    a = P[i][0]
    b = P[i][1]
    a = str(a)
    b = str(b)
    ap = a[0]
    al = a[-1]
    bp = b[0]
    bl = b[-1]
    if ap == bl:
        if al == bp:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

n=int(input())
n_keta_su = len(list(str(n)))
a=b=cnt=0

if n_keta_su<10**2: # Nが100未満の場合
    for i in range(n):
        for j in range(n):
            a=list(str(i+1))
            b=list(str(j+1))
            
            if (a[-1]==b[0])and(b[-1]==a[0]):
                cnt+=1
    print(cnt)
    exit()

elif (n_keta_su<=10**2)and(n_keta_su<10**3): # Nが100以上1000未満の場合
    for i in range(99,n):
        for j in range(99,n):
            a=list(str(i+1))
            b=list(str(j+1))
            
            if (a[-1]==b[0])and(b[-1]==a[0]):
                cnt+=1
    print(cnt+108)
    exit()

elif (n_keta_su<=10**3)and(n_keta_su<10**4): # Nが1000以上10000未満の場合
    for i in range(999,n):
        for j in range(999,n):
            a=list(str(i+1))
            b=list(str(j+1))
            
            if (a[-1]==b[0])and(b[-1]==a[0]):
                cnt+=1
    print(cnt+10008)
    exit()

elif (n_keta_su<=10**4)and(n_keta_su<10**5): # Nが10000以上100000未満の場合
    for i in range(9999,n):
        for j in range(9999,n):
            a=list(str(i+1))
            b=list(str(j+1))
            
            if (a[-1]==b[0])and(b[-1]==a[0]):
                cnt+=1
    print(cnt+1000008)
    exit()

elif (n_keta_su<=10**5)and(n_keta_su<10**6): # Nが100000以上1000000未満の場合
    for i in range(99999,n):
        for j in range(99999,n):
            a=list(str(i+1))
            b=list(str(j+1))
            
            if (a[-1]==b[0])and(b[-1]==a[0]):
                cnt+=1
    print(cnt+100000008)
    exit()

elif (n_keta_su<=10**6)and(n_keta_su<10**7): # Nが1000000以上10000000未満の場合
    for i in range(999999,n):
        for j in range(999999,n):
            a=list(str(i+1))
            b=list(str(j+1))
            
            if (a[-1]==b[0])and(b[-1]==a[0]):
                cnt+=1
    print(cnt+10000000008)
    exit()

elif (n_keta_su<=10**7)and(n_keta_su<10**8): # Nが10000000以上100000000未満の場合
    for i in range(9999999,n):
        for j in range(9999999,n):
            a=list(str(i+1))
            b=list(str(j+1))
            
            if (a[-1]==b[0])and(b[-1]==a[0]):
                cnt+=1
    print(cnt+1000000000008)
    exit()