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: Wrong Answer

Solution code:
from fractions import gcd
# from datetime import date, timedelta
# from heapq import *
import heapq
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
# input = sys.stdin.readline


def lcm(a, b):
    return a * b / fractions.gcd(a, b)


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors


def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct


def is_prime(n):
    if n == 1:
        return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True


def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

def modpow(a, n, mod):
    res = 1
    while n > 0:
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res


def modinv(a, mod):
    return modpow(a, mod - 2, mod)


def cnk(a, b):
    MOD = 10**9+7
    ret = 1
    for i in range(b):
        ret *= (a-i)
        ret %= MOD
        ret = ret * modinv(i+1, MOD) % MOD
    return ret




def main():
    # n = int(input())
    # a = list(map(int, input().split()))
    
    # v = a[0]
    # for i in range(1, n):
    #     v = lcm(v, a[i])
    
    # ans = 0
    # v %= MOD
    # for i in range(n):
    #     ans += v * modinv(a[i], MOD) % MOD
    #     ans %= MOD
    # print(ans)
    n = int(input())
    d = defaultdict(int)
    for i in range(1,n+1):
        if i < 10:
            d[(i, i)] += 1
        else:
            d[(int(str(i)[-1]), int(str(i)[0]))] += 1
    
    ans = 0
    for i in range(1, n):
        if i < 10:
            ans +=d[(i, i)]
        else:
            if int(str(i)[-1]) == int(str(i)[0]):
                ans += d[(int(str(i)[0]), int(str(i)[-1]))]
            else:
                ans += d[(int(str(i)[0]), int(str(i)[-1]))]
    print(ans)

            

    # ans = 0
    # for i in range(1,n+1):
    #     # if set(str(i)) == 1:
    #     #     continue
    #     ss = str(i)[::-1]
    #     if int(ss) <= n and str(ss)[0] != "0":
    #         ans += 1
    #         print(i,str(i)[::-1])
    # print(ans)

    # for i in range(1, 10):
    #     t = 0
    #     s = ""
    #     for j in range(6):
    #         s += str(i)
    #         if int(s) > n:
    #             break
    #         t += 1
    #     if t == 1:
    #         continue
    #     k = 1
    #     for j in range(1, t + 1):
    #         k*=j
    #     ans += k
    #     # print(k)
    # print(ans)   



    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

DP = [[0 for _ in range(10)] for _ in range(10)]

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    strn = input()
    n = int(strn)
    if n < 10:
        print(n)
        return
    m = [0] * 81

    for i in range(n):
        a = int(str(i)[0])
        b = int(str(i)[-1])
        # print(a, b)
        if b == 0:
            continue
        else:
            key = 9 * (a - 1) + b - 1
            # print(key)
            m[key] += 1
    ans = 0
    # print(m)
    for i in range(1, 10):
        for j in range(1, 10):
            key = 9 * (i - 1) + j - 1
            oppose = 9 * (j - 1) + i - 1
            ans += m[key] * m[oppose]
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

memo = dict()

def func(s,f):
    ans = 0
    for i in range(1,n):
        if str(i)[0] == f and str(i)[-1] == s:
            ans += 1
    memo[s+" "+f] = ans

res = 0
j = -1
for i in range(1,n+1):
    s = str(i)[0]
    f = str(i)[-1]
    if len(memo) == 90:
        j = i
        break
    if s == "0" or f=="0":
        continue
    if s + " " + f in memo:
        res += memo[s + " " + f]
    else:
        func(s,f)
        res += memo[s + " " + f]

#何回dict内の組み合わせが出てくるのかをなんとか計算する
#if j != -1:

print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
INF = float("inf")


def solve(N: int):

    SN = str(N)
    keta = int(math.log10(N)+1)
    count = 0
    for i in range(1, N+1):
        first = str(i)[0]
        last = str(i)[-1]
        if last == "0":
            continue
        # ひとけた
        if first == last:
            count += 1
        # ふたけた
        if int(last+first) <= N:
            count += 1
        # 3桁以上keta桁未満
        for j in range(3, keta):
            count += 10**(j-2)
        if keta < 3:
            continue
        # keta桁
        # Nを超えない数
        if last < SN[0]:
            count += 10**(keta-2)
        elif last == SN[0]:
            # 間の桁について、i桁までまで考える。
            DP = [[0]*2 for _ in range(keta-1)]
            DP[0][0] = 1
            for j in range(keta-2):
                sn = int(SN[j+1])
                DP[j+1][0] += DP[j][0]*1
                DP[j+1][1] += DP[j][1]*10 + DP[j][0]*sn
            count += DP[keta-2][1]
            if SN[-1] >= first:
                count += DP[keta-2][0]-1
            # print(first, last, DP[keta-2])
    print(count)
    return


def main():

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
lst = [[0 for i in range(10)]for j in range(10)]
ans = 0
for i in range(N):
    tmp = list(str(i))
    lst[int(tmp[0])][int(tmp[-1])]+=1
for i in range(10):
    for j in range(10):
        if i == 0 or j == 0:
            continue
        if i == j:
            ans+=lst[i][j]**2
        else:
            ans+=lst[i][j]*lst[j][i]
print(ans)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())

if n < 10:
    print(n)
else:
    ans = math.ceil((n/10) * (n/10)) + 8
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

counter = [[0 for _ in range(10)] for _ in range(10)]

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

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

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
cnts = [[0] * 10 for _ in range(10)]
ans = 0
for i in range(N):
    if i % 10 == 0:
        continue
    s = str(i)
    cnts[int(s[0])][int(s[-1])] += 1

for i in range(N):
    if i % 10 == 0:
        continue
    s = str(i)
    ans += cnts[int(s[-1])][int(s[0])]

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import math
INF = float("inf")


def solve(N: int):

    SN = str(N)
    keta = int(math.log10(N)+1)
    count = 0
    for i in range(1, N+1):
        first = str(i)[0]
        last = str(i)[-1]
        if last == "0":
            continue
        # ひとけた
        if first == last:
            count += 1
        # ふたけた
        if int(last+first) <= N:
            count += 1
        # 3桁以上keta桁未満
        for j in range(3, keta):
            count += 10**(j-2)
        if keta < 3:
            continue
        # keta桁
        # Nを超えない数
        if last < SN[0]:
            count += 10**(keta-2)
        elif last == SN[0]:
            # 間の桁について、i桁までまで考える。
            DP = [[0]*2 for _ in range(keta-1)]
            DP[0][0] = 1
            for j in range(keta-2):
                sn = int(SN[j+1])
                DP[j+1][0] += DP[j][0]*1
                DP[j+1][1] += DP[j][1]*10 + DP[j][0]*sn
            count += sum(DP[keta-2])-1
            # print(first, last, DP[keta-2])
    print(count)
    return


def main():

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
ans=0
for i in range(1,n):
    a=str(i)[0]
    b=str(i)[-1]
    for digit in range(1,7):
        if digit == 1:
            if a==b:
                ans+=1
        elif b!='0':
            mn = int(b+'0'*(digit-2)+a)
            mx = int(b+'9'*(digit-2)+a)
            if mn>n:
                break
            if mx<n:
                ans+=10**(digit-2)
            else:
                ns=str(n)
                for j in range(1,len(ns)-1):
                    ans+=int(ns[j])*10**(max(digit-3-j,0))
                break
if n==1:
    ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

if n < 10:
    print(n)
else:
    ans = int((n/10) * (n/10)) + 8
    print(ans)

Submission status: Wrong Answer

Solution code:


s=input()
n=int(s)

d={}

for i in range(1,n):
	ts=str(i)
	hs=ts[0]+" "+ts[len(ts)-1]

	if(hs in d):
		d[hs]+=1
	else:
		d[hs]=1



ans=0

for i in range(1,10):
	for j in range(1,10):
		if(((str(i)+" "+str(j)) in d) and ((str(j)+" "+str(i)) in d)):
			ans+=d[(str(i)+" "+str(j))]*d[(str(j)+" "+str(i))]

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
Atamasiri = [[0 for i in range(10)] for j in range(10)]#Atamasiri[atama][siri]
cursaijou = 1
for n in range(1,N+1):
    if n>=cursaijou*10:
        cursaijou *= 10
    atama = n//cursaijou
    siri = n%10
    Atamasiri[atama][siri] += 1
ans = 0
print(Atamasiri)
for head in range(10):
    for ketsu in range(10):
        ans += Atamasiri[head][ketsu]*Atamasiri[ketsu][head]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
 
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
 
 
N=int(input())
cnt=0
 
for i in range(1,N):
	for j in range(1,N):
		if str(i)[0]==str(j)[-1] and str(i)[-1]==str(j)[0]:
			cnt+=1
 
print(cnt)

Submission status: Wrong Answer

Solution code:
from math import ceil
def solve(N):
    if N < 10:
        return N
    ans = ceil(N/10)**2 + 8

    return ans

def main():
    N = int(input())
    ans = solve(N)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
L = len(str(N))
B = int(str(N)[0])
c = 0
for i in range(1,N+1):
    I = str(i)
    l = len(I)
    a,b = int(I[0]),int(I[l-1])
    if a == 0 or b == 0:
        continue
    else:
        if a == b:
            c += 1
        for m in range(max(L-2,0)):
            c += 10**m
        if L == 1:
            continue
        if b > B:
            continue
        elif b < B:
            c += 10**(L-2)
        elif L > 2:
            c += int(str(N)[1:L-1])
        elif L == 2:
            c += 1
print(c)

Submission status: Wrong Answer

Solution code:
n=int(input())
ct=0
for i in range(n):
    j=str(i)
    if int(j[::-1])<i:
        ct+=1
    elif int(j[::-1])==i:
        ct+=len(j)
print(ct)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = 0

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
L = len(str(N))
B = int(str(N)[0])
c = 0
if L == 1:
    c += N
for i in range(1,N):
    I = str(i)
    l = len(I)
    a,b = int(I[0]),int(I[l-1])
    if a == 0 or b == 0:
        continue
    else:
        if a == b:
            c += 1
        for m in range(max(L-2,0)):
            c += 10**m
        if b > B:
            continue
        elif b < B:
            c += 10**(L-2)
        elif L > 2:
            c += int(str(N)[1:L-1])
        elif L == 2:
            c += 1
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
d = 0
a = [[0 for i in range(10)] for j in range(10)]
if(n == 1):
	print(1)
	exit(0)
for i in range(1,n):
	f = i//10**d
	l = i%10
	a[f][l] += 1
	if(i == 10**(d+1)-1):
		d += 1
ans = 0
for i in range(1,10):
	for j in range(1,10):
		ans += a[i][j]*a[j][i]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
 

N=int(input())
cnt=0

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

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import queue
import math
import copy
"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""

N = int(input())
mp = [[0 for j in range(10)]for i in range(10)]
for i in range(N):
	j = i
	
	while j >= 10:
		j //= 10

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

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

for i in range(n):
    s = str(i)
    front = int(s[0])
    end = int(s[-1])
    cnt[front][end] += 1

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


N = int(input())


# In[15]:


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


# In[23]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def dfs(d, tight, str_N, pre):
    if d >= len(str_N) - 1:
        if int(pre) <= int(str_N[-1]):
            return 1
        else:
            return int(not tight)

    ans = 0
    if tight:
        for i in range(0, int(str_N[d]) + 1):
            ans += dfs(d + 1, str(i) == str_N[d], str_N, pre)
    else:
        for i in range(0, 10):
            ans += dfs(d + 1, False, str_N, pre)

    return ans


def solve():
    N = int(input())
    str_N = str(N)
    D = len(str_N)

    ans = 0
    for A in range(1, N + 1):
        a = str(A)
        pre = a[0]
        post = a[-1]

        if pre == "0" or post == "0":
            continue

        for d in range(1, D + 1):
            if d == 1:
                if pre == post:
                    ans += 1
            elif d != D:
                ans += 10 ** (d - 2)
            else:
                # B = post, xxx, pre
                if post == str(N)[0]:
                    ans += dfs(1, True, str_N, pre)
                elif int(post) < int(str_N[0]):
                    ans += 10 ** (d - 2)

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    ans = 0
    def c(i, j):
        res = 0
        str_i = str(i)
        str_j = str(j)
        for k in range(1, N+1):
            if str(k)[0] == str_i and str(k)[-1] == str_j:
                res += 1
        return res

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

    print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
N = int(input())

N_ =str(N)
n_o=len(N_)

# ans:条件を満たす組みの個数
ans=0

def get_ans(A):
    ans=0
    A_=str(A)
    a_s=A_[0]
    a_e=A_[-1]
#     print(a_s,a_e)
    
    if int(a_e) == 0:
        return 0

    if a_s == a_e:
        ans+=1

    for b_o in range(2,n_o+1):
        B_temp = []
        if b_o < n_o:
            ans += 10**(b_o-2)

        elif b_o == n_o:
            if int(a_e) > int(N_[0]):
                break
            elif int(a_e) < int(N_[0]):
                ans += 10**(b_o-2)
            else:      
                for p in product(range(10),repeat=n_o-2):
                    ans_temp=int(a_e+''.join(map(str,p))+a_s)
                    if ans_temp > N:
                        break
                    ans+=1 
                    B_temp.append(ans_temp)
    #                 print(B_temp)

        else:
            break
        
    return ans

for a in range(1,N+1):
#     print('====',a)
    ans_=get_ans(a)
    ans+=ans_
print(ans)

Submission status: Time Limit Exceeded

Solution code:

def main():
    N = int(input())
    NN = [[0]*10 for _ in range(11)]
    for i in range(11,100):
        As = str(i)[0]
        Ae = str(i)[-1]
        cnt = 0
        for j in range(1,N+1):
            if str(j)[-1]== As and str(j)[0] == Ae:
                cnt +=1
        NN[int(As)][int(Ae)] = cnt


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

    print(ans)

if __name__ == '__main__':
        main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
NN = [[0]*10 for _ in range(11)]
for i in range(11,100):
    As = str(i)[0]
    Ae = str(i)[-1]
    cnt = 0
    for j in range(1,N+1):
        if str(j)[-1]== As and str(j)[0] == Ae:
            cnt +=1
    NN[int(As)][int(Ae)] = cnt


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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#pdf解説をそのまま
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 str(k)[0] == str(i) and str(k)[-1] == str(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())


def calculate(n):
    result = 0
    for i in range(0, 10):
        for j in range(0, 10):

            s1 = 0

            s2 = 0

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

                if i != 0:
                    if (int(str(index)[-1]) == j) and (int(str(index)[0]) == i):
                        s1 += 1

                if j != 0:
                    if (int(str(index)[-1]) == i) and (int(str(index)[0]) == j):
                        s2 += 1

            result += s1 * s2

    print(result)
calculate(N)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def func(i,j,k):
    num = str(k)
    if int(num[0])==i and int(num[-1])==j:
        return True
    return False

n = int(input())
table = np.zeros((9,9))
for i in range(9):
    for j in range(9):
        for k in range(n):
            if func(i+1,j+1,k+1):
                table[i][j]+=1

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ans = 0
for i in range(1,N+1):
  for j in range(i,N+1):
    i = str(i)
    j = str(j)
    ia = i[0]
    ib = i[-1]
    ja = j[0]
    jb = j[-1]
    if ia == jb and ib == ja:
      ans += 2
      if i == j:
        ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
count = 0
for i in range(0,10):
    for j in range(0,10):
        countij = 0
        countji = 0
        for k in range(1,N+1):
            if str(k)[0] == str(i) and str(k)[-1] == str(j):
                countij += 1
            if str(k)[0] == str(j) and str(k)[-1] == str(i):
                countji += 1
        count += countij*countji

            
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
def cnt(i,j,N):
    count=0
    for q in range(1,N+1):
        A=str(q)
        if int(A[0])==i and int(A[-1])==j:
            count+=1
        else:
            pass
    return count
ans=0
for i in range(10):
    for j in range(10):
        ans+=cnt(i,j,N)*cnt(j,i,N)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#D
N=int(input())
ans=0
D={}
for x in range(1,10):
    for y in range(1,10):
        D[(x,y)]=0

for key in D.keys():
    x,y=key
    for i in range(1,N+1):
        a=int(str(i)[0])
        b=int(str(i)[-1])
        if x==a and y==b:
            D[key]+=1
for i in range(1,10):
    for j in range(1,10):
        ans+=D[(i,j)]*D[(j,i)]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
cnt = 0
for i in range(1,N+1):
    a = str(i)
    a0 = a[0]
    an = a[-1]
    if an=="0":continue
    k = 0
    while eval(an+"9"*k+a0)<=N:
        cnt += 10**k
        k += 1
    for j in range(10**k):
        b = str(j)
        b = "0"*(k-len(b))+b
        if eval(an+b+a0)<=N:
            cnt += 1
    if a0==an and int(a0)<=N:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools
# H, W = [int(_) for _ in input().split()]
N = int(input())

comb = itertools.combinations_with_replacement([str(i) for i in range(1, 10)], 2)
count = 0
for c in comb:
    A = c[0]
    B = c[1]
    count_1 = 0
    count_2 = 0
    for i in range(1, N+1):
        if str(i)[0] == A and str(i)[-1] == B:
            count_1 += 1
        elif str(i)[0] == B and str(i)[-1] == A:
            count_2 += 1
    if A == B:
        count += count_1 ** 2
    else:
        count += count_1 * count_2 * 2
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def f(n):
    count = 0
    for a in range(1, n+1):
        for b in range(1, n+1):
            str_a = str(a)
            str_b = str(b)
            if str_a[0] == str_b[-1] and str_b[0] == str_a[-1]:
                count += 1
                # print(str_a, str_b)
    print(count)

n = int(input())
f(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):
    i_str = str(i)
    j_str = str(j)
    if len(i_str) == 1 and len(j_str) == 1:
      if i == j:
        count += 1
    elif len(i_str) > 1 and len(j_str) == 1:
      if i_str[0] == j_str and i_str[len(i_str)-1] == j_str:
        count += 1
    elif len(i_str) == 1 and len(j_str) > 1:
      if i_str == j_str[0] and i_str == j_str[len(j_str)-1]:
        count += 1
    elif len(i_str) > 1 and len(j_str) > 1:
      if i_str[0] == j_str[len(j_str)-1] and i_str[len(i_str)-1] == j_str[0]:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())

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

ab = np.zeros(81,int).reshape(9,9)

cnt=0
a = 0
b = 0

for x in A:
    a = str(x)[0]
    b = str(x)[-1]
    for y in A:
        if str(y)[0]==b and str(y)[-1]==a:
            ab[int(a)-1][int(b)-1] += 1
print(sum(sum(ab)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

for i in range(1, 10):
  for j in range(1, 10):
    for k in range(1, n+1):
      top = int(str(k)[0])
      bottom = k % 10
      if top == i and bottom == j:
        lst[i][j] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
count=0
for i in range(n):
	top_dis=str(i+1)[0]
	bot_dis=str(i+1)[-1]
	if top_dis!="0" and bot_dis!="0":
		for j in range(n):
			top_dis2=str(j+1)[0]
			bot_dis2=str(j+1)[-1]
			if top_dis2==bot_dis and top_dis==bot_dis2:
				count+=1
				#print(i+1,j+1)
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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