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: Accepted

Solution code:
N = int(input())
cnt = 0
lenn = len(str(N))
for i in range(1, N+1):
    num = str(i)
    s = int(num[-1])
    e = int(num[0])
    if s != 0:
        if lenn == 1:
            cnt += 1
        elif lenn == 2:
            if s == e:
                cnt += 1
            if s * 10 + e <= N:
                cnt += 1
        else:
            if s == e:
                cnt += 1
            for k in range(2, lenn + 1):
                if k == lenn:
                    if s < int(str(N)[0]):
                        cnt += 10 ** (k-2)
                    elif s == int(str(N)[0]) and e <= int(str(N)[-1]):
                        cnt += int(str(N)[1:lenn-1]) + 1
                    elif s == int(str(N)[0]) and e > int(str(N)[-1]):
                        if int(str(N)[1:lenn-1]) >= 0:
                            cnt += int(str(N)[1:lenn-1])
                else:
                    cnt += 10 ** (k-2)
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=[]
for i in range(9):
    s.append([0,0,0,0,0,0,0,0,0])
ans=0
for i in range(1,n+1):
    if i%10==0:
        pass
    else:
        s[(int(str(i)[0]))-1][(i%10)-1]+=1
for i in range(9):
    for k in range(9):
        ans+=s[i][k]*s[k][i]
print(ans)

Submission status: Accepted

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


oo = {}
for i in range(1, 2*(10**5)):
    if i > N:
        break
    a = str(i)
    if a[-1] == '0':
        continue
    k = (int(a[0]),int(a[-1]))

    if k not in oo:
        oo[k] = 0
    oo[k] += 1
#print(oo)

d1 = {}
for k, c in oo.items():
    if k[0] == k[1]:
        d1[k] = c
#print('d1', d1)

d2 = {}
for k, c in list(oo.items()):
    if k[0] == k[1]:
        continue
    if (k[1], k[0]) in oo:
        d2[k] = c * oo[(k[1], k[0])]
#print(d2)

a = list(d1.values())
#print(a)
a2 = list(map(lambda x:x*x,a))
#    print(a2)
#print('a2', sum(a2))
b = sum(d2.values())
#print(b, list(d2.values()))

print(sum(a2) + b)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
res = 0
d = len(str(N))
same = defaultdict(int)
odd1 = defaultdict(int)
odd2 = defaultdict(int)

for i in range(1, N+1):
    if i % 10 == 0:
        continue

    left = str(i)[0]
    right = str(i)[-1]
    if left == right:
        same[left] += 1
    elif int(left) < int(right):
        odd1[left + right] += 1
        odd2[left + right] += 0
    else:
        odd2[right + left] += 1

for n in same.values():
    res += n**2

for x, y in zip(odd1.values(), odd2.values()):
    res += x*y*2

print(res)

Submission status: Accepted

Solution code:
n = int(input())
if n > 30:
    x = n // 10
    basenum = x * x + 8
    addnum = 0
    #print("{0} {1}".format(x* 10 + 1, n))
    hassame = False
    for i in range(x * 10 + 1, n+1):
        #print(i)
        for j in range(1, n+1):
            #print(j)
            if str(i)[0] == str(j)[-1] and str(i)[-1] == str(j)[0]:
                addnum += 1
                if i == j:
                    hassame=True
                #print("{0},{1}".format(i, j))
    res = basenum + addnum * 2
    if hassame:
        res-=1
    #print("n={0}, x={1}, basenum={2}, addnum={3} res ={4}".format(n,x,basenum,addnum,res))
    print(res)
elif n==1:
    print(1)
else:
    res = 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]:
                res += 1
    print("{1}".format(n, res))

Submission status: Accepted

Solution code:
def p(x, y, n):
  #print('p({},{},{})'.format(x,y,n))
  if x == 0:
    #print('return 0')
    return 0
  count = 0
  if x == y:
    if x <= n:
      count += 1

  i = 0
  flag = True
  while flag:
    for j in range(10**i):
      if (x * 10**(i+1) + y + j *10) <= n:
        #print(': ' + str(x * 10**(i+1) + y + j))
        count+=1
      else:
        flag = False
        break
    i += 1
  #print('return {}'.format(count))
  return count

n = int(input())

#print(p(1, 2, 111))
c = 0
for NF in range(1, 10):
  for NL in range(0, 10):
    #print('({}, {}= {})'.format(NF, NL, p(NF, NL, n)))
    
    c +=  p(NF, NL, n) * p(NL, NF, n)
print(c)

Submission status: Accepted

Solution code:
# print('input >>')
N = int(input())
mx = [[0] * 9 for i in range(9)]

for i in range(1, N+1):
    stri = str(i)
    if not stri[-1] == '0':    
        mx[int(stri[0])-1][int(stri[-1])-1] += 1


ans = 0
for j in range(1, N+1):
    strj = str(j)
    # print(strj[-1],"で始まり",strj[0],"で終わる",mx[int(strj[-1])-1][int(strj[0])-1])
    if not strj[-1] == '0':
        ans += mx[int(strj[-1])-1][int(strj[0])-1]

# print('-----output-----')
print(ans)

Submission status: Accepted

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

order = int(math.log10(n))
first = int(n/(10**order))


hund = [0]*99

def calc(a,b):
    global n,order,first
    num = 0

    if(a>n):
        return 0
    if(a==b and a<=n):
        num += 1
    if(order==0):
        return num
    if(a!=first):
        if(first>a):
            num += (10**(order)-1)/9
        else:
            num += (10**(order-1)-1)/9
    else:
        num += (10**(order-1)-1)/9
        if(n%10>=b):
            num += int((n - first * (10**order))/10)+1
        else:
            num += int((n - first * (10**order))/10)
    return num

def eq(a,b):
    if(a!=b):
        return 2
    return 1

for i in range(1,10):
    for j in range(1,10):
        hund[(i-1)*10+(j-1)] = calc(i,j)

count = 0

for i in range(1,10):
    for j in range(1,i+1):
        count += hund[(i-1)*10+(j-1)]*hund[(j-1)*10+(i-1)]*eq(i,j)


print(int(count))

Submission status: Accepted

Solution code:
import sys
bbn=1000000007
	
#+++++

		
def main():
	n = int(input())
	nnn=[0]*10
	for i in range(10):
		nnn[i]=[0]*10
	ret=0
	for i in range(1,n+1):
		bb=str(i)
		t=int(bb[0])
		b=int(bb[-1])
		nnn[t][b]+=1
		
	ret=0
	for i in range(10):
		for j in range(10):
			ret += nnn[i][j]*nnn[j][i]
	
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]

def calc(a,b,n):
    ret = 0
    if a==b and a<=n:
        ret += 1
    if a*10+b <= n:
        ret += 1
    for i in range(2,7):
        if a*(10**i)+b>n:
            break
        if a*(10**i)+(10**i)-10+b<=n:
            ret += 10**(i-1)
            continue
        tmp = n
        while(tmp%10!=b):
            tmp -= 1
        tmp -= a*(10**i)
        tmp //= 10
        tmp += 1
        ret += tmp
    return ret

def main():
    N = I()
    ans = 0
    for A in range(1,10):
        for B in range(1,10):
            ans += calc(A,B,N) * calc(B,A,N)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

k = [0 for i in range(81)]

A = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
B = [0, 9, 18, 27, 36, 45, 54, 63, 72, 1, 10, 19, 28, 37, 46, 55, 64, 73, 2, 11, 20, 29, 38, 47, 56, 65, 74, 3, 12, 21, 30, 39, 48, 57, 66, 75, 4, 13, 22, 31, 40, 49, 58, 67, 76, 5, 14, 23, 32, 41, 50, 59, 68, 77, 6, 15, 24, 33, 42, 51, 60, 69, 78, 7, 16, 25, 34, 43, 52, 61, 70, 79, 8, 17, 26, 35, 44, 53, 62, 71, 80]
for i in range(1, N+1):
    h = str(i)[0]
    t = str(i)[-1]
    if t!="0":
        k[(int(h)-1)*9 + int(t) - 1] += 1

ans = sum([k[a]*k[b] for a, b in zip(A, B)])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
book = [[0] * 9 for i in range(9)]

ans = 0

for i in range(1, n+1) :
    reverse = 0
    tmp = i
    time = 0
    flag = False
    end = -1
    while(tmp!=0) :
        remind = tmp % 10
        if time == 0 :
            if remind == 0 :
                flag = True
                break
            pre = remind
        tmp = tmp // 10
        time += 1
    if flag == True :
        continue
    if remind != 0 :
        book[remind-1][pre-1] += 1

for i in range(9) :
    for j in range(i, 9) :
        if i == j :
            ans += book[i][j] * book[j][i]
            #print('=', i, j, book[i][j] * book[j][i])
            continue
        if book[i][j] != 0 and book[j][i] != 0 :
            ans += book[i][j] * book[j][i] * 2
            #print('!', i, j, book[i][j] * book[j][i] * 2)

#for i in range(9) :
#    print(book[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
count = 0
for i in range(n):
    head = str(i + 1)[0]
    tail = str(i + 1)[-1]
    if tail == "0":
        continue
    if head == tail:
        count += 1
    for j in range(len(str(n)) - 2):
        count += (10 ** j)
    min_num = int(tail + "0" * (len(str(n)) - 2) + head)
    max_num = int(tail + "9" * (len(str(n)) - 2) + head)
    if min_num <= n:
        count += min((n - min_num) // 10 + 1, (max_num - min_num) // 10 + 1)
print(count)

Submission status: Accepted

Solution code:
def f(x):
    if 1<=x<=9:
        return 1
    if x%10==0:
        return 0
    x = str(x)
    a = x[0]
    b = x[-1]
    k = len(x)
    if int(a)<int(b):
        if k==2:
            return 0
        else:
            return 2*(10**(k-2)-1)//9
    elif int(a)>int(b):
        return 2*(10**(k-1)-1)//9
    else:
        if k==2:
            return 3
        else:
            return (1+(10**(k-2)-1)//9+int(x[1:k-1]))*2+1

N = int(input())
dp = [0 for _ in range(N+1)]
for i in range(min(N,9)+1):
    dp[i] = i
for i in range(10,N+1):
    dp[i] = dp[i-1]+f(i)
print(dp[N])

Submission status: Accepted

Solution code:
n = int(input())
a = [[0 for j in range(10)] for i in range(10)]
for i in range(1, n + 1):
    s = str(i)
    a[int(s[0])][int(s[-1])] += 1
sol = 0
for i in range(1, 10):
    sol += (a[i][i] * a[i][i])
for i in range(2, 10):
    for j in range(1, i):
        sol += (a[i][j] * a[j][i] * 2)
print(sol)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def my_count(a, b, n):
    ret = 0
    if a == b and a <= n:
        ret = ret + 1
    if a * 10 + b <= n:
        ret = ret + 1
    keta = len(str(n))
    if keta > 3:
        ret = ret + (int('1'*(keta-2)) - 1)
    if keta >= 3:
        candidate = a * (10 ** (keta-1)) + b
        if n - candidate >= 0:
            num = ((n - candidate) // 10) + 1
            if num >= (10 ** (keta-2)):
                ret = ret + (10 ** (keta-2))
            else:
                ret = ret + num
    return ret


n = int(input())

ret = 0

for i in range(1, 10):
    for j in range(1, 10):
        ret = ret + my_count(i, j, n) * my_count(j, i, n)

print(ret)

Submission status: Accepted

Solution code:
n=int(input())
n_ls=list(str(n))
n_top,n_bottom=int(n_ls[0]),int(n_ls[-1])
if n<100:
    n_middle=0
else:
    n_middle=int("".join(n_ls[1:-1]))
len_n=len(n_ls)
count=0
for i in range(1,n+1):
    stred_ls=list(str(i))
    top,bottom=int(stred_ls[0]),int(stred_ls[-1])
    if bottom==0:
        continue
    # 頭とケツが一緒の時は, 1桁の数も候補になる
    if top==bottom:
        count+=1
    #print("1:",count)
    if len_n<=2:
        if int(str(bottom)+str(top))<=n:
            count+=1
        #print("2:",count)
    else:
        for x in range(len_n-2):
            count+=10**x
        #print("2:",count)
        judged_x=x+1
        #print('比較', str(bottom)+("X"*judged_x)+str(top), n)
        if n_top>bottom:
            count+=10**judged_x
        elif n_top==bottom:
            if n_bottom>=top:
                count+=n_middle+1
            else:
                count+=n_middle
        #print("3:",count)
print(count)

Submission status: Accepted

Solution code:


N = int(input())

if len(str(N))<4:
    ans = 0
    
    for i in range(1, N+1):
        for j in range(1, N+1):
            if str(i)[0]==str(j)[-1] and str(i)[-1]==str(j)[0]:
                ans += 1
    
    print(ans)
    exit()
    
L = len(str(N))
n = int(str(N)[1:])
ans = 0
F = int(str(N)[0])
L2 = int(str(N)[-1])
S = str(N)
M = int(S[1:len(S)-1])

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

    if f==0 or l==0:
        continue
    
    for k in range(1, L+1):
        
        if k==1 and f!=l:
            continue
        
        if k==1:
            ans += 1
            continue
        
        if k<L:
            ans += 10**(k-2)
        else:
            if F<l:
                continue
                
            if l<F:
                ans += 10**(k-2)
            else:
                if len(S)<=2:
                    continue
                
                ans += M
                
                if L2>=f:
                    ans += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

count = 0
for i in range(1, N + 1):
    str_i = str(i)

    # 末尾が0の時は何もしない
    if int(str_i[-1]) == 0:
        continue

    # 1桁のとき
    if len(str_i) == 1:
        count += 1
        continue

    # 2桁のとき
    if len(str_i) == 2:
        if int(str_i[0]) > int(str_i[-1]):
            count += 2 # (例：21のとき (12, 21)と(21, 12))
        # 先頭と末尾の数字が等しい時
        if int(str_i[0]) == int(str_i[-1]):
            count += 3  # (例：77のとき (77, 77)と(7, 77)と(77,7))
        continue

    # 自分と同じ桁でひっくりかえしたとき
    # 例：3421のとき、 1**3  (**は00~99)
    if int(str_i[0]) > int(str_i[-1]):
        count += (10 ** (len(str_i) - 2)) * 2

    # 先頭と末尾の数字が等しい時
    # 例：2432のとき、2**2 (**は00~42と43)
    if int(str_i[0]) == int(str_i[-1]):
        count += (int(str_i[1:-1])) * 2 + 1
        count += 2  # 例：121のとき、1

    # 自分より少ない桁でひっくり返した時
    # 例：3431のとき、1*3と13
    for j in range(len(str_i) - 2):
        count += (10 ** j) * 2

print(count)

Submission status: Accepted

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

num=0

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

l4 = list(map(lambda x: str(x)[0]+str(x)[-1], li))
ll = set(l4)
l5 = list(map(lambda x: str(x)[-1]+str(x)[0], li))
c4 = collections.Counter(l4)
c5 = collections.Counter(l5)

for l in ll:
  num += c4[l] * c5[l]
print(num)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    d = [[0] * 10 for _ in range(10)]
    for i in range(1, N + 1):
        s = str(i)
        h, t = int(s[0]), int(s[-1])
        if h * t == 0:
            continue
        d[h][t] += 1
    r = 0
    for h in range(10):
        for t in range(10):
            r += d[h][t] * d[t][h]
    print(r)


main()

Submission status: Accepted

Solution code:
N=input()
N_int=int(N)
order = len(N)
ans = 0
for i in range(1,int(N)+1):
    str_i = str(i)
    if i%10==0:
        continue
    if str_i[0]==str_i[-1]:
        ans += 1
    for j in range(1,order):
        num = int(str_i[-1])*10**j+int(str_i[0])
        if num <= N_int:
            ans += min(10**(len(str(num))-2),(N_int-num)//10+1)
print(ans)

Submission status: Accepted

Solution code:
def calc1(x,y,N): # x != y
    d = len(str(N))
    if d == 1:
        re = 0
    elif d == 2:
        if N >= x*(10**(d-1)) + y:
            re = 1
        else:
            re = 0
    else:
        s_high = str(x) + '9'*(d-2) + str(y)
        s_low = str(x) + '0'*(d-2) + str(y)
        if N >= int(s_high):
            re = int(str('1'*(d - 1)))
        else:
            re = int('1'*(d-2))
            if N >= int(s_low):
                re += (N - int(s_low))//10 + 1
    return re

def calc2(x,y,N): # x == y
    d = len(str(N))
    if d == 1:
        if x <= N:
            re = 1
        else:
            re = 0
    elif d == 2:
        if N >= x*(10**(d-1)) + y:
            re = 2
        else:
            re = 1
    else:
        s_high = str(x) + '9'*(d-2) + str(y)
        s_low = str(x) + '0'*(d-2) + str(y)
        if N >= int(s_high):
            re = int(str('1'*(d - 1))) + 1
        else:
            re = int('1'*(d-2)) + 1
            if N >= int(s_low):
                re += (N - int(s_low))//10 + 1
    return re
N = int(input())
ans = 0
for i in range(1,10):
    for j in range(1,10):
        if i != j:
            ans += calc1(i,j,N)*calc1(j,i,N)
for i in range(1,10):
    ans +=calc2(i,i,N)**2
print(ans)

Submission status: Accepted

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
  if ll >= 3:
    cur += (10**(ll-2)-1)//9
    if a < int(nl[0]):
      cur += 10**(ll-2)
    elif a == int(nl[0]):
      nnl = nl[1:ll-1]
      if b > int(nl[-1]):
        cur += int(nnl)
      else:
        cur += (int(nnl)+1)
  elif ll == 2:
    if 10*a+b <= n:
      cur += 1
  if a == 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: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    str_N=str(N)
    ans=0
    ans2=0
    for i in range(1,N+1):
        s=str(i)
        if s[-1]=='0':
            continue
        if s[0]==s[-1]:
            ans+=1
        if int(s[-1]+s[0])<=N:
            ans+=1
        f=True
        n=1
        while f:
            z=int(s[-1]+str(10**n-1)+s[0])
            if z<=N:
                ans+=10**n
            else:
                if int(s[-1]+str('0'*n)+s[0])>N:
                    pass
                else:
                    m=int(str_N[1:-1])
                    ans+=m
                    if int(str_N[-1])>=int(s[0]):
                        ans+=1
                f=False
                break
            n+=1
    print(ans-ans2)
                

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

d = {(i, j): 0 for i in range(1, 10) for j in range(1, 10)}

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

res = 0
for i in range(1, 10):
    for j in range(1, 10):
        res += d[(i, j)] * d[(j, i)]

print(res)

Submission status: Accepted

Solution code:
N = int(input())

tb = [[0] * 9 for i in range(9)]

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

  if head > 0 and tail > 0:
    tb[head-1][tail-1] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
box={}
if N<10:
  ans=N
if N>=10:
  for i in range(1,100):
    box[i]=0
  for i in range(1,N+1):
    A_first=str(i)[0]
    if i<=9:
      A_first=0
    A_back=i%10
    index=int(A_first)*10+int(A_back)
    box[index]+=1
  ans=0
  a=0
  b=0
  for i in range(1,100):
    if len(str(i))==1:
      ans+=(box[i]+box[i*11])**2
    if i>10 and str(i)[0]!=str(i)[1] and i%10!=0:
      t=i//10+(i%10)*10
      ans+=(box[i]*box[t])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a_list = [ [ 0 for j in range(9) ] for i in range(9) ]
b_list = [ [ 0 for j in range(9) ] for i in range(9) ]
for i in range(1,n+1):
    a = i // ( 10 ** (len(str(i)) - 1 ) )
    b = i % 10

    if b != 0:
        if (a != b):
            a_list[a-1][b-1] += 1
        else:
            a_list[a-1][b-1] += 1

for i in range(9):
    for j in range(9):
        if i == j:
            b_list[i][j] = a_list[i][j]**2
        else:
            b_list[i][j] = (a_list[i][j] * a_list[j][i]) 
        
ans = 0

for i in b_list:
    ans += sum(i)
print(ans)

Submission status: Accepted

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




def f(N, a, b):
    s = 0
    d = 1

    if a == b and a <= N:
        s += 1

    N0 = N

    N //= 10
    while(N > a):
        s += d
        d *= 10
        N //= 10
    
    if N == a:
        s += (N0 - d*a*10 - b)//10 + 1

    return s


N = int(input())

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

for a in range(1, 10):
    for b in range(1, 10):
        p[a][b] = f(N, a, b)

s = 0
for a in range(1, 10):
    for b in range(1, 10):
        s += p[a][b]*p[b][a]

print(s)

Submission status: Accepted

Solution code:
import numpy as np
def count(a,b):
    if ln_n > 1:
        ret = int('1' * (ln_n-1))
    else:
        ret = 0
    
    if a == b and n >= a:
        ret += 1
    
    if ln_n >= 1:
        if a < n_top:
            ret += 10**(ln_n-1)
        elif a == n_top:
            ret += max(n_inner, 0)
            if b <= n_bottom:
                ret += 1
    return ret

n, = [list(map(int, s.split())) for s in open(0)][0]
ln_n = int(np.log10(n))
n_top = n // (10**ln_n)
n_bottom = n % 10
n_inner = 0
if ln_n > 1:
    n_inner = int(str(n)[1:-1])

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

Submission status: Accepted

Solution code:
n = int(input())
if n <= 9:
    print(n)
else:
    y = 9
    z = 9
    for i in range(11,n+1):
        p = str(i)
        if p[0] == p[len(p)-1]:
            y += 2
            z += 1
            if len(p) >= 4:
                for j in range(3, len(p)):
                    y += 10 ** (j-2)
            if len(p) >= 3:
                y += int(p[1:len(p)-1])+1
        elif int(p[0]) > int(p[len(p)-1]) > 0:
            for j in range(2, len(p)+1):
                y += 10 ** (j-2)
        elif len(p) >= 3 and not i%10 == 0: 
            for j in range(2, len(p)):
                y += 10 ** (j-2)
    print(2*y-z)

Submission status: Accepted

Solution code:
def count(x, y, N):
    l = len(str(N)) # Nの桁数
    a = int(str(N)[0]) # Nの最上桁
    b = int(str(N)[-1]) # Nの最下桁

    if l == 1:
        if x == y and a >= x:
            return 1
        else:
            return 0
    elif l == 2:
        res = 0
        if x == y:
            res += 1
        if a >= x and b >= y:
            res+=1
        return res
    elif l >=3:
        res = 0
        if x == y:
            res += 1 # 1桁
        res += 1 # 2桁        
        for i in range(1, l - 2): # 3桁 ~ l-1桁
            res += 10 ** i
        if a > x:
            res += 10 ** (l - 2) # l桁
        elif a == x and b >= y:
            res += int(str(N)[1:-1]) + 1
        elif a == x and b < y:
            res += int(str(N)[1:-1])
        return res

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

print(ans)

Submission status: Accepted

Solution code:
from functools import lru_cache
import sys

sys.setrecursionlimit(10 ** 7)


@lru_cache(maxsize=None)
def check(l, r, n, k=1):
    t = l + '0' * k + r
    e = l + '9' * k + r

    if int(e) <= n:
        return pow(10, k) + check(l, r, n, k + 1)

    if int(t) <= n < int(e):
        x = int(str(n)[1:-1])
        if int(str(n)[-1]) < int(r):
            x -= 1
        return x + 1

    if n < int(t):
        return 0


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

    ret = 0
    for x in range(1, n + 1):
        if x % 10 == 0:
            continue
        s = str(x)
        t = s[-1] + s[0]
        if s[0] == s[-1]:
            ret += 1
        if int(t) <= n:
            ret += 1
        ret += check(s[-1], s[0], n, k=1)

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
ls=len(str(n))
for ai in range(1,10):
    for bi in range(1,10):
        # print(count,ai,bi)
        p=q=0
        if ai==bi:
            if n>=ai: q+=1
            for i in range(ls-2):
                q+=10**i
            if int(str(n)[0])==ai:
                if (n-ai*(10**(ls-1))-ai)>=0:
                    q+=(n-ai*(10**(ls-1))-ai)//10+1
            elif int(str(n)[0])>ai:
                q+=10**(ls-2)
            count+=q**2
            continue
        for i in range(ls-2):
            p+=10**i; q=p
        if int(str(n)[0])==ai:
            if (n-ai*(10**(ls-1))-bi)>=0:
                p+=(n-ai*(10**(ls-1))-bi)//10+1
        elif int(str(n)[0])>ai:
            p+=10**(ls-2)
        if int(str(n)[0])==bi:
            if (n-bi*(10**(ls-1))-ai)>=0:
                q+=(n-bi*(10**(ls-1))-ai)//10+1
        elif int(str(n)[0])>bi:
            q+=10**(ls-2)
        count+=p*q
print(count)

Submission status: Accepted

Solution code:
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

N=II()
cnt = 0
for i in range(1,10):
    for i_keta in range(6):
        for j in range(1,10):
            for j_keta in range(6):
                A = i if i_keta == 0 else int(str(i) + ("0"*(i_keta-1)) + str(j))
                B = j if j_keta == 0 else int(str(j) + ("0"*(j_keta-1)) + str(i))
                if A <= N and B <=N:
                    if i_keta == 0 or j_keta == 0:

                        patA = 0
                        if i_keta:
                            if (i+1)*(10**i_keta) <= N:
                                patA = 10**(i_keta-1)
                            else:
                                for k in range(10000):
                                    if A+k*10 <= N:
                                        patA += 1
                                    else:
                                        break
                        else:
                            patA=1

                        patB = 0
                        if j_keta:
                            if (j+1)*(10**j_keta) <= N:
                                patB = 10**(j_keta-1)
                            else:
                                for k in range(10000):
                                    if B+k*10 <= N:
                                        patB += 1
                                    else:
                                        break
                        else:
                            patB = 1


                        if i == j:
                            # print(A,B,patA*patB)
                            cnt += patA*patB
                    else:
                        patA = 0
                        if (i+1)*(10**i_keta) <= N:
                            patA = 10**(i_keta-1)
                        else:
                            for k in range(10000):
                                if A+k*10 <= N:
                                    patA += 1
                                else:
                                    break

                        patB = 0
                        if (j+1)*(10**j_keta) <= N:
                            patB = 10**(j_keta-1)
                        else:
                            for k in range(10000):
                                if B+k*10 <= N:
                                    patB += 1
                                else:
                                    break
                        cnt += patA*patB
                        # print(A,B,patA*patB)

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
S = str(N)
D = len(S)
M = N%10
SE = N//(10**(D-1))
ans = 0
for i in range(1,N+1):
    x = i
    t = str(x)
    d = len(t)
    m = x%10
    s = x//(10**(d-1))
    if m == 0:
        continue
    for j in range(1,D+1):
        if j == 1:
            if s == m:
                ans += 1
        elif 2 <= j <= D-1:
            ans += 10**(j-2)
        else:
            if SE > m:
                ans += 10**(j-2)
            elif SE == m:
                p = N%(10**(D-1))
                p = p//10
                ans += p+1
                if M < s:
                    ans -= 1
print(ans)

Submission status: Accepted

Solution code:
def run():
  N = int(input())
  ans = 0
  di = {}
  for a in range(1, 10):
    for b in range(1, 10):
      key = str(a)+str(b)
      di[key] = 0
  for n in range(1, N+1):
    if n%10 == 0:
      continue
    n = str(n)
    start, end = n[0], n[-1]
    di[start+end] += 1
    if start == end:
      ans += max(0, di[end+start]*2-1)
    else:
      ans += di[end+start]*2
  print(ans)
    

if __name__ == '__main__':
  run()

Submission status: Accepted

Solution code:
N = int(input())
len_N = len(str(N))

memo = {}

if str(N)[1:len_N-1]:
    center_val = int(str(N)[1:len_N-1])
else:
    center_val = 0
for a in range(1, 10):
    for b in range(1, 10):
        count = 0
        if a == b and a <= N:
            count += 1
        if len_N >= 2 and 10*a + b <= N:
            count += 1
        if len_N >= 3:
            for i in range(3, len_N):
                count += 10**(i-2)
            if a < int(str(N)[0]):
                count += 10**(len_N-2)
            elif a == int(str(N)[0]):
                if b <= int(str(N)[-1]):
                    count += center_val + 1
                else:
                    count += center_val
        memo[(a, b)] = count
# print(memo)

ans = 0
for A in range(1, N+1):
    A1 = str(A)[0]
    A2 = str(A)[-1]
    if A1 == "0" or A2 == "0":
        continue
    ans += memo[(int(A2), int(A1))]
    # print(A, memo[(int(A2), int(A1))])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

import numpy as np

X = np.arange(N+1)
A = X.copy()

for i in [1,10,100,1000,10000,100000]:
    A[i:i*10] = X[i:i*10] // i
B = X % 10

C = np.zeros((10,10), dtype=int)

for i in range(1,N+1):
    C[A[i], B[i]] += 1
count = 0

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

print(count)

Submission status: Accepted

Solution code:
N = int(input())
d = len(str(N))
if d == 1:
    print(N)
    exit()
x = int(str(N)[0])
y = int(str(N)[-1])
if d == 2:
    M = 0
else:
    M = int(str(N)[1:-1])
count = 0
for A in range(1, N + 1):
    count0 = 0
    a = int(str(A)[0])
    b = int(str(A)[-1])
    if b == 0:
        continue
    if a == b:
        count0 += 1
    count0 += (10 ** (d - 2) - 1) // 9
    if b < x:
        count0 += (10 ** (d - 2))
    if b == x:
        count0 += M
        if a <= y:
            count0 += 1
    count += count0
print(count)