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:
#!/usr/bin/env python3
def main():
    from itertools import product

    N = int(input())

    digit = len(str(N))
    cnt = 0
    # aはAの先頭の桁,bはBの先頭の桁を示す
    for a_first, b_first in product(range(1, 10), repeat=2):
        for a_digit, b_digit in product(range(digit), repeat=2):
            # Aの製作
            if a_digit == 0:
                a = str(a_first)
            else:
                a = str(a_first) + '0' * (a_digit - 1) + str(b_first)
            # Bの製作
            if b_digit == 0:
                b = str(b_first)
            else:
                b = str(b_first) + '0' * (b_digit - 1) + str(a_first)
            if int(a) <= N and int(b) <= N and a[0] == b[-1] and a[-1] == b[0]:
                pattern = 1
                if len(a) >= 3:
                    if N == int(a):
                        pass
                    elif len(str(N - int(a))) < len(str(N)):
                        pattern *= N // 10 - int(a[:-1])
                    else:
                        pattern *= 10 ** (len(a) - 2)

                if len(b) >= 3:
                    if N == int(b):
                        pass
                    elif len(str(N - int(b))) < len(str(N)):
                        pattern *= N // 10 - int(b[:-1])
                    else:
                        pattern *= 10 ** (len(b) - 2)
                cnt += pattern
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def main():
    from itertools import product

    N = int(input())
    # N = 2020

    digit = len(str(N))
    cnt = 0
    # aはAの先頭の桁,bはBの先頭の桁を示す
    for a_first, b_first in product(range(1, 10), repeat=2):
        for a_digit, b_digit in product(range(digit), repeat=2):
            # Aの製作
            if a_digit == 0:
                a = str(a_first)
            else:
                a = str(a_first) + '0' * (a_digit - 1) + str(b_first)
            # Bの製作
            if b_digit == 0:
                b = str(b_first)
            else:
                b = str(b_first) + '0' * (b_digit - 1) + str(a_first)
            if int(a) > N or int(b) > N:
                continue
            if int(a) <= N and int(b) <= N and a[0] == b[-1] and a[-1] == b[0]:
                pattern = 1
                if len(str(N - int(a))) < len(str(N)) and len(a) >= 3:
                    pattern *= N // 10 - int(a[:-1])
                elif len(a) >= 3:
                    pattern *= 10 ** (len(a) - 2)

                if len(str(N - int(b))) < len(str(N)) and len(b) >= 3:
                    pattern *= N // 10 - int(b[:-1])
                elif len(b) >= 3:
                    pattern *= 10 ** (len(b) - 2)
                cnt += pattern
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())

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

Submission status: Wrong Answer

Solution code:
N=int(input())
n=len(str(N))
res=0
for a in range(1,N+1):
    a0=str(a)[0]
    a1=str(a)[-1]
    if a0=='0' or a1=='0':
        continue
    for i in range(1,n+1):
        if i==1:
            if a0==a1:
                res+=1
        elif i==2:
            if 10*int(a1)+int(a0)<=N:
                res+=1
        elif i==n:
            n0=str(N)[0]
            n1=str(N)[1]
            if n0<a1:
                res+=0
            elif n0>a1:
                res+=10**(i-2)
            else:
                res+=(N-int(a1)*10**(i-1))//10
                if n1>a0:
                    res-=1
        else:
            res+=10**(i-2)
print(res)

Submission status: Wrong Answer

Solution code:
N=int(input())
n=len(str(N))
res=0
for a in range(1,N+1):
    a0=str(a)[0]
    a1=str(a)[-1]
    print(a0,a1)
    if a0=='0' or a1=='0':
        continue
    for i in range(1,n+1):
        if i==1:
            if a0==a1:
                res+=1
        elif i==2:
            if 10*int(a1)+int(a0)<=N:
                res+=1
        elif i==n:
            n0=str(N)[0]
            n1=str(N)[1]
            if n0<a1:
                res+=0
            elif n0>a1:
                res+=10**(i-2)
            else:
                res+=(N-int(a1)*10**(i-1))//10
                if n1>a0:
                    res-=1
        else:
            res+=10**(i-2)
print(res)

Submission status: Wrong Answer

Solution code:
N=int(input())
n=len(str(N))
res=0
for a in range(N):
    a0=str(a)[0]
    a1=str(a)[-1]
    if a0=='0' or a1=='0':
        continue
    for i in range(1,n+1):
        if i==1:
            if a0==a1:
                res+=1
        elif i==2:
            if 10*int(a1)+int(a0)<=N:
                res+=1
        elif i==n:
            n0=str(N)[0]
            n1=str(N)[1]
            if n0<a1:
                res+=0
            elif n0>a1:
                res+=10**(i-2)
            else:
                res+=(N-int(a1)*10**(i-1))//10
                if n1>a0:
                    res-=1
        else:
            res+=10**(i-2)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    d = {}
    for a in range(1, 10):
        for b in range(1, 10):
            cnt = 0
            # 1桁の整数が条件を満たすケース
            if a == b:
                cnt += 1
            # 2桁以上の整数で条件を満たすケース
            i = 1
            while True:
                _min = a * (10**i) + b  # 整数が a0000b

                _max = a * (10**i) + b
                _i = i - 1
                while _i > 0:
                    _max += 9 * (10**_i)  # 整数が a999b
                    _i -= 1
                if _max <= n:
                    cnt += 10**(i - 1)
                    i += 1
                    continue
                elif _min > n:
                    break
                else:  # a0..0b <= b < a9..9b
                    cnt += ((n - _min) // 10) + 1
                    break
            d[10 * a + b] = cnt
    ans = 0
    for a in range(1, 10):
        for b in range(1, 10):
            ans += d[a * 10 + b] * d[b * 10 + a]
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[str(i)for i in range(1,10)]
ans=0
for a in A:
    for b in A:
        countA=0
        if a==b and int(a)<=N:
            countA+=1
        for i in range(5):
            m=int(a+'0'*i+b)
            M=int(a+'9'*i+b)
            if m<=N:
                if M<=N:
                    countA+=10**i
                else:
                    countA+=N-m+1

        countB=0
        if b==a and int(b)<=N:
            countB+=1
        for i in range(5):
            m=int(b+'0'*i+a)
            M=int(b+'9'*i+a)
            if m<=N:
                if M<=N:
                    countB+=10**i
                else:
                    countB+=N-m+1

        ans+=countA*countB
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
capa=len(str(n))
ans=0

if n<100:
    for i in range(1,n+1):
        if i%10==0:
            continue
        if i<10:
            if i*11<=n:
                ans+=2
            else:
                ans+=1
        elif i%11==0:
            ans+=2
    
        else:
            z=str(i)
            a,b=z[1],z[0]
            z=a+b
            z=int(z)
            if z<=n:
                ans+=1
    
    print(ans)
    exit()


for i in range(1,n+1):
  if i%10==0:
    continue
  x=str(i)
  if x[0]==x[-1]:
    ans+=1
  
  for j in range(2,capa):
    ans+=10**(j-2)
    
  if int(str(n)[0])>i%10:
    ans+=10**(capa-2)
    
  elif int(str(n)[0])<i%10:
    continue
    
  else:
    if int(str(n)[-1])>int(str(i)[0]):
      ans+=int(str(n)[1:-1])+1
    else:
      ans+=int(str(n)[1:-1])
      
      
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
d={}
for i in range(1,10):
  for j in range(1,10):
    d[i*10+j]=0
for i in range(1,min(10,N)):
  d[i*10+i]=1
p,h,t,c=10,10,1,1
for i in range(11,N+1):
  if t==10:
    t=1
  else:
    d[h+t]+=1
    t+=1
  if p==c:
    h+=10
    c=0
  if h==100:
    h=10
    p*=10
  c+=1
ans=0
for i in range(1,9):
  for j in range(i+1,10):
    ans+=(d[i*10+j]*d[j*10+i])*2
for i in range(1,10):
  ans+=d[i*10+i]**2
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())

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

ans = 0
for k, v in c.items():
  if k[0] == k[1]:
    ans += v*(v-1)//2
  else:
    ans += v*v
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
dp = [[0]*10 for _ in range(10)]
for a in range(N):
    A = str(a)
    dp[int(A[0])][int(A[-1])] += 1
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += dp[i][j]*dp[j][i]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
m = int(len(str(n)))
ls = [e for e in range(1,10)]
di = [[0]*10 for _ in range(10)]
ans = 0
for i in ls:
    for j in ls:
        ans = 0
        if m >= 4:
            ans += 10*(10**(m-3)-1)//9 + 1
            if i == j:
                ans += 1
            if i == int(str(n)[0]):
                ans += int(str(n)[1:m-1])
            elif i < int(str(n)[0]):
                ans += 10**(m-2)
        elif m == 3:
            if i == j:
                ans += 2
            else:
                ans += 1
            if i == int(str(n)[0]):
                ans += int(str(n)[1:m-1])
            elif i < int(str(n)[0]):
                ans += 10**(m-2)
        elif m == 2:
            if 10*i+j > n:
                if i == j:
                    ans += 1
                di[i][j] = ans
                continue
            if i == j:
                ans += 2
            else:
                ans += 1
        else:
            if i <= n and i == j:
                ans += 1            
        di[i][j] = ans
cnt = 0
for i in ls:
    for j in ls:
        cnt += di[i][j]*di[j][i]
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
import math

# a != b のとき


def count(a, b, N):

    keta = len(str(N))

    cnt = 0

    # k == 1 なら a == b
    if a == b:
        cnt += 1 if a < N else 0

    if keta == 1:
        return cnt

    # k == 2 なら ba のみ
    if keta == 2:
        cnt += 1 if b * 10 + a <= N else 0
        return cnt

    # k 桁より少ない数の場合 : 好きな数を間に挟む
    for k in range(2, keta):
        cnt += 10**(k - 2)

    # k 桁の数字の場合
    s = N // (10**(keta - 1))
    t = N % 10
    if b > s:
        return cnt
    elif b < s:
        cnt += 10**(keta - 2)
    else:
        tempN = (N - s * (10**(keta - 1)) - t) // 10
        # print(f"tempN {tempN}")
        cnt += (tempN + 1)  # tempN が 1 なら 0,1 を選べる
        if a > t:
            cnt -= 1

    return cnt


def count_aa(a, N):

    keta = len(str(N))

    cnt = 0

    # keta == 1
    if keta == 1:
        return 1 if a <= N[-1] else 0

    cnt += 1  # a

    # k == 2 なら aa のみ
    if keta == 2:
        return cnt + 1 if a <= N[-1] else 0

    # 3桁以上
    # k 桁より少ない数の場合
    for k in range(2, keta):
        cnt += 10**(k - 2)

    # k 桁の数字の場合
    s = N // (10**(keta - 1))
    t = N % 10
    if b > s:
        return cnt
    elif b < s:
        cnt += 10**(keta - 2)
    else:
        tempN = (N - s * (10**(keta - 1)) - t) // 10
        # print(f"tempN {tempN}")
        cnt += (tempN + 1)  # tempN が 1 なら 0,1 を選べる
        if a > t:
            cnt -= 1

    return cnt


N = int(input())

c = 0
for i in range(1,N+1):
    a = int(str(i)[0])
    b = i % 10
    # print(a,b)
    if b == 0:
        continue
    c += count(a,b,N)

print(c)

Submission status: Wrong Answer

Solution code:
memo = [[0] * 10 for i in range(10)]

def kazu(start_num,end_num):
    global memo
    if memo[start_num][end_num] > 0:
        #print('memo',start_num,end_num)
        return memo[start_num][end_num]
    else:
        ans = 0
        #1桁
        if start_num == end_num:
            if start_num <= n:
                #print(start_num)
                ans += 1
        #2桁
        if start_num * 10 + end_num <= n:
            #print(start_num * 10 + end_num)
            ans += 1
        #3桁
        for i in range(10):
            if start_num * 100 + i * 10 + end_num <= n:
                #print(start_num * 100 + end_num)
                ans += 1
            else:
                break
        #4桁
        for i in range(100):
            if start_num * 1000 + i * 10 + end_num <= n:
                #print(start_num * 1000 + end_num)
                ans += 1
            else:
                break
        #5桁
        for i in range(1000):
            if start_num * 10000 + i * 10 + end_num <= n:
                #print(start_num * 10000 + end_num)
                ans += 1
            else:
                break
        memo[start_num][end_num] = ans
        #print(start_num,end_num,'---',ans)
        return ans

n = int(input())
str_n = str(n)
kotae = 0
#print(memo)
for a in range(1,n+1):
    if a%10!=0:
        str_a = str(a)
        #print('-----',a,'-----',int(str_a[-1]),int(str_a[0]))
        kotae += kazu(int(str_a[-1]),int(str_a[0]))
        #print(memo)
print(kotae)

Submission status: Wrong Answer

Solution code:
def calc_b(n,a):
    ans = 0
    str_n = str(n)
    str_a = str(a)
    if len(str_a) == 1:
        #1
        if int(str_a) <= n:
            #print(a,str_a)
            ans += 1
        #2
        if int(str_a[0]+str_a[0]) <= n:
            #print(a,str_a+str_a)
            ans += 1
        #3over
        if len(str_n) >= 3:
            if int(str_n[0]) > int(str_a[0]):
                tmp = '*' * (len(str_n) -2)
                #print(a,str_a + tmp + str_a)
                ans += 10 ** (len(str_n) -2)
            
            if int(str_n[0]) < int(str_a[0]):
                if len(str_n) >= 4:
                    tmp = '*' * (len(str_n) -3)
                    #print(a,str_a + tmp + str_a)
                    ans += 10 ** (len(str_n) -3)
            
            if int(str_n[0]) == int(str_a[0]):
                tmp_str = str_n[1:len(str_n)-1]
                tmp_num = 0
                if n%10<int(str_a[0]):
                    tmp_num = 1
                ans += int(tmp_str) + 1 - tmp_num
                #print(a,str_a + '*' + str(int(tmp_str) + 1 - tmp_num) + str_a)
                
    if len(str_a) >= 2:
        #1
        if str_a[0] == str_a[len(str_a)-1]:
            if int(str_a[0]) <= n:
                #print(a,str_a[0])
                ans += 1
        #2
        if int(str_a[len(str_a)-1]+str_a[0]) <= n:
            #print(a,str_a[len(str_a)-1]+str_a[0])
            ans += 1
        #3over
        if len(str_n) >= 3:
            if int(str_n[0]) > int(str_a[len(str_a)-1]):
                tmp = '*' * (len(str_n) -2)
                #print(a,str_a[len(str_a)-1] + tmp + str_a[0])
                ans += 10 ** (len(str_n) -2)
                
            if int(str_n[0]) < int(str_a[len(str_a)-1]):
                if len(str_n) >= 4:
                    tmp = '*' * (len(str_n) -3)
                    #print(a,str_a[len(str_a)-1] + tmp + str_a[0])
                    ans += 10 ** (len(str_n) -3)
                    
            if int(str_n[0]) == int(str_a[len(str_a)-1]):
                tmp_str = str_n[1:len(str_n)-1]
                tmp_num = 0
                if n%10<int(str_a[0]):
                    tmp_num = 1
                ans += int(tmp_str) + 1 - tmp_num
                #print(a,str_a[len(str_a)-1] + '*' + str(int(tmp_str) + 1 - tmp_num) + str_a[0])
    return ans

n = int(input())
kotae = 0
for i in range(1,n+1):
    if i%10!=0:
        kotae += calc_b(n,i)
print(kotae)

Submission status: Wrong Answer

Solution code:
def calc_b(n,a):
    ans = 0
    str_n = str(n)
    str_a = str(a)
    if len(str_a) == 1:
        if int(str_a) <= n:
            print(a,str_a)
            ans += 1
        if int(str_a[0]+str_a[0]) <= n:
            print(a,str_a+str_a)
            ans += 1
    if len(str_a) == 2:
        if str_a[0] == str_a[1]:
            if int(str_a[0]) <= n:
                print(a,str_a[0])
                ans += 1
        if int(str_a[1]+str_a[0]) <= n:
            print(a,str_a[1]+str_a[0])
            ans += 1
    return ans

n = int(input())
kotae = 0
for i in range(1,n+1):
    if i%10!=0:
        #print(i)
        kotae += calc_b(n,i)
print(kotae)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    from collections import defaultdict

    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    nd = defaultdict(int)
    for i in range(1, n):
        tmp = str(i)
        h, t  = int(tmp[0]), int(tmp[-1])
        nd[(h, t)] += 1
    
    ans = 0
    for i in range(1, 10):
        for j in range(1, 10):
            ans += nd[(i, j)]*nd[(j, i)]
    print(ans)



    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
# N = random.randint(1,2*10**5)

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]
    
    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:
                if len(N_[1:-1])>0:
                    ans += int(N_[1:-1])
                else:
                    ans += 1

        else:
            break
        
    return ans

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

Submission status: Wrong Answer

Solution code:
n = int(input()) 

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

for i in range(n):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    if a == 0 or b == 0:
        continue
    cnt[a][b] += 1

ans = 0
for i in range(n):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    ans += cnt[b][a]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


def check(min_, max_, x):
  if max_ <= x:
    return 10**max(0, len(str(min_))-2)
  elif min_ <= x:
    return max(1, int(str(x)[1:-1]) if len(str(x)) > 2 else 1)
  else:
    return 0


def main():
  N = int(input())

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      # max_ = int(str(l) + '9'*(len(str(N))-3) + str(r))
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      # print(l, r, sum_ - temp)

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


def check(min_, max_, x):
  if max_ < x:
    return 10**max(0, len(str(min_))-2)
  elif min_ <= x:
    return max(1, int(str(x)[1:-1]) if len(str(x)) > 2 else 1)
  else:
    return 0


def main():
  N = int(input())

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      # max_ = int(str(l) + '9'*(len(str(N))-3) + str(r))
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      # print(l, r, sum_ - temp)

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


def bisect(ok, ng):
  while abs(ok - ng) > 1:
    mid = int((ok + ng) / 2)
    if solve(mid):
      ok = mid
    else:
      ng = mid

  return mid


def check(min_, max_, x):
  if max_ < x:
    return 10**max(0, len(str(min_))-2)
  elif min_ <= x:
    return int(str(x)[1:-1]) if len(str(x)) > 2 else 1
  else:
    return 0


def main():
  N = int(input())

  if len(str(N)) == 1:
    print(N)
    return
  sum_ = 0
  for l in range(1, 10):
    for r in range(l, 10):
      temp = sum_
      min_ = int(str(l) + '0'*(len(str(N))-2) + str(r))
      max_ = int(str(l) + '9'*(len(str(N))-2) + str(r))
      A = check(min_, max_, N)
      # max_ = int(str(l) + '9'*(len(str(N))-3) + str(r))
      for gap in range(len(str(N))-2):
        A += (10**gap)

      min_ = int(str(r) + '0'*(len(str(N))-2) + str(l))
      max_ = int(str(r) + '9'*(len(str(N))-2) + str(l))
      B = check(min_, max_, N)
      for gap in range(len(str(N))-2):
        B += (10**gap)

      if l == r:
        A += 1
        B += 1
        sum_ += A*B
      else:
        sum_ += 2*A*B
      # print(l, r, sum_ - temp)

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
strN=input()
lenN=len(strN)
N=int(strN)

if N<=9:print(N)
else:
  count = defaultdict(int)
  for i in range(10,N+1):
    a,b=int(str(i)[0]),int(str(i)[1])
    if b==0:continue
    count[(a,b)] += 1

  ans=0
  for i in range(1,10):
    count[(i,i)] += 1
  count = dict(count)
  for key in count.keys():
    if key[0]<key[1] and count.get((key[1],key[0])):
      ans += count[key] * count[(key[1],key[0])] * 2
    if key[0]==key[1]:
      ans += count[key]**2
  print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

dp = [[0] * 10 for _ in range(10)]
for x in range(1, N + 1):
    j = x % 10
    while x > 10:
        x //= 10
    i = x % 10
    dp[i][j] += 1
    
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += dp[i][j] * dp[j][i]
        
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  N = int(input())
  cnt = 0
  
  for i in range(1, N+1):
    iString = str(i)
    for l in range(1, N+1):
      if l == i:
        cnt += 1
      elif str(l) == iString[-1] + iString[0]:
        cnt += 1
    
  print(cnt)
  
  
main()

Submission status: Wrong Answer

Solution code:
N=int(input())
for i in range(6):
    if 10**i <=N< 10**(i+1):
        a=i+1
        break
b=N//(10**(a-1))
m=0
if a>=3:
    for j in range(a-2):
        m+=10**j
    
table=[[0+m]*9 for _ in range(9)]

if a==2:
    for i in range(0,9):
        for j in range(0,9):
            if (i+1)*10+j<=N:
                table[i][j]+=1


if a>=2:
    for i in range(0,9):
        table[i][i]+=1
else:
    for i in range(0,9):
        if i+1<=N:
            table[i][i]+=1
        
        
if a>=3:       
    for i in range(0,b-1):
        for j in range(0,9):
            table[i][j]+=10**(a-2)

    for j in range(0,9):
        if (N-(b*10**(a-1)+j+1))//10>=1:
            table[b-1][j]+=(N-(b*10**(a-1)+j+1))//10

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

B=0
for i in range(0,9):
    B+=table[i][i]*table[i][i]
    

print((A-B)*2+B)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
cou = 0
for i in range(1,n+1):
  i = str(i)
  if i[0]!="0" and i[-1]!="0":
    if int(i[0])>int(i[-1]):
      cou +=  (9**(len(i)-1)-1)//8
    elif int(i[0])<int(i[-1]):
      cou += (9**(len(i)-2)-1)//8
  #print(i,cou)

cou *= 2
    
l = [0 for i in range(9)]
for i in range(1,n+1):
  i = str(i)
  if i[0]==i[-1]:
    l[int(i[0])-1] += 1
l1 = [i*i for i in l]
print(sum(l1)+cou)

Submission status: Wrong Answer

Solution code:
n=int(input())
def side(n):
  list_n=list(str(n))
  start=int(list_n[0])
  end=int(list_n[-1])
  return [start,end]
def count_n(i,j):
  s=0
  for i in range(1,n+1):
    if side(i)==[i,j]:
      s+=1
  return s
ans=0
for i in range(0,9):
  for j in range(0,9):
    ans+=count_n(i,j)*count_n(j,i)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
from itertools import combinations
def dis(n):
  list_n=list(str(n))
  a=int(list_n[0])
  b=int(list_n[-1])
  return [a,b]
N=[_ for _ in range(1,n+1)]
comb=list(combinations(N,2))
s=0
for A,B in comb:
  if dis(A)[0]==dis(B)[1] and dis(B)[0]==dis(A)[1]:
    s+=1
t=0
for A in N:
  if dis(A)[0]==dis(A)[1]:
    t+=1
print(s+t)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
digits = len(str(N))
ans = [0] * (2 * (10 ** 6))
digits_to_insert = 0

for i in range(1, N+1):
    if i % 10 == 0:
        continue
    else:
        i_digits = len(str(i))
        if i_digits == 1:  # iが1桁のとき
            ans[i] += 1  # i自身が相方となる
            if digits >= 2:
                ans[i * 11] += 1
            if digits >= 3:  # Nが3桁以上の時
                digits_to_insert = digits - 2
                for j in range(10 ** digits_to_insert):
                    num = str(i)[-1] + str(j) + str(i)[0]
                    if int(num) > N:
                        break
                    ans[max(i, int(num))] += 1

        else:  # iが2桁以上のとき
            if i_digits == 2:  # iが2桁のとき
                if str(i)[-1] == str(i)[0]:  # iの最初と最後が同じ数字ならば、1桁の相方が存在
                    ans[int(str(i)[0])] += 1
                rev = int(str(i)[-1] + str(i)[0])  # 2桁の数字の相方が存在
                ans[max(i, int(rev))] += 1  # iと相方の大きい方を記録
                if digits >= 3:  # Nが3桁以上のとき
                    digits_to_insert = digits - 2
                    for j in range(10 ** digits_to_insert):
                        num = str(i)[-1] + str(j) + str(i)[0]
                        if int(num) > N:
                            break
                        ans[max(i, int(num))] += 1
                digits = len(str(N))
                digits_to_insert = 0

print(sum(ans[:N+1]))

# if digits >= 2:  # Nが2桁以上の時
#     for j in range(1, 10):
#         num = str(i)[0] + str(j)
#         rev = num[::-1]
#         ans[max(i, int(rev))] += 1

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
table,ans = np.zeros((9,9)),0

for k in range(n):
    num = str(k)
    if int(num[0])!=0 and int(num[-1])!=0:
        table[int(num[0])-1][int(num[-1])-1]+=1

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

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
tb = [[0]*10]*10

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

point =0

for i in range(0,10):
    for j in range(0,10):
        point += tb[i][j] * tb[j][i]

print(point)

Submission status: Wrong Answer

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

for i in range(1, N+1):
  if i%10==0: continue
  strI = str(i)
  if len(strI) > 1:
    f,l = strI[-1], strI[0]
    dp[int(f)][int(l)] += 1
  else:
    dp[i][i] += 1

res = 0
for i in range(1,10):
  for j in range(1,10):
    if i==j or dp[i][j]>1:
      res += dp[i][j] * dp[j][i]

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())

c = [[0] * 10] * 10
for x in range(1, N+1):
    check = str(x)
    it = int(check[0])
    jt = int(check[-1])
    c[it][jt] += 1

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

Solution code:
n = int(input())

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

for i in range(1, n):
    str_i = str(i)
    c[int(str_i[0])][int(str_i[len(str_i)-1])] += 1

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

Solution code:
A=int(input())
c=A//10
if A%10==0:
  print(c**2+8)
elif A<10:
  print(A)
else:
  n=A%10
  t=len(str(A))
  b=A//10**(t-1)
  if n>=b:
    print(c**2+2*c+9)
  else:
    print(c**2+2*c+6)

Submission status: Wrong Answer

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
    j = eval(an+"0"*k+a0)
    if N-j+1>=0:
        cnt += N-j+1
    if a0==an and int(a0)<=N:
        cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
power=0
for i in range(1,n+1):
  if i>=10**(power+1):
    power+=1
  a=i%10
  if a==0:
    continue
  b=(i-i%(10**power))//(10**power)
  if a==b:
    ans+=1
    if power>0:
      ans+=2
    r=max(0,((i-(b*(10**power))-a)//10))
    ans+=2*r
    for j in range(0,power-1):
      ans+=2*(10**j)
  elif a<b:
    for j in range(0,power-1):
      ans+=2*(10**j)
  else:
    for j in range(0,power):
      ans+=2*(10**j)
print(ans)