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:
a = int(input())
ans = 0
for i in range(1,a+1):
    s = str(i)
    if s[-1]== '0':continue
    elif len(s) == 1:
        ans+=1
    elif s[0] == s[1] and len(s)==2:
        ans+=3
    elif int(s[0])>int(s[1]) and len(s)==2 and '0' not in s:
        ans+=2
    elif len(s)==2:continue
    elif s[0] == s[2] and len(s)==3:
        ans += 3+2*(int(s[1])+1)
    elif len(s) == 3 and int(s[0])>int(s[2]):
        ans +=22
    elif len(s) == 3 and s[2] != '0':
        ans +=2 

    elif len(s) == 4 and s[0] == s[-1]:
        ans += 3+2*(10+int(s[1]+s[2])+1)
    elif len(s) == 4 and int(s[0])>int(s[-1]):
        ans += 222
    elif len(s)==4:
        ans +=22

    elif len(s) == 5 and s[0] == s[-1]:
        ans += 3+2*(110+int(s[1]+s[2]+s[3])+1)
    elif len(s) == 5 and int(s[0])>int(s[-1]):
        ans += 2222
    elif len(s)==5:
        ans +=222

    elif len(s) == 6 and s[0] == s[-1]:
        ans += 3+2*(1110+int(s[1]+s[2]+s[3]+s[4])+1)
    elif len(s) == 6 and int(s[0])>int(s[-1]):
        ans += 22222
    elif len(s)==6:
        ans +=2222
print(ans)

Submission status: Accepted

Solution code:
import sys

n = sys.stdin.readline().rstrip()
len_n = len(n)
m = n[1:-1]
m = int(m) if m else 0
l = int(n[0])
r = int(n[-1])
n = int(n)

def main():
    res = [[0] * 10 for _ in range(10)]

    for i in range(1, 10):
        for j in range(1, 10):
            if i == j:
                if i <= n:
                    res[i][j] += 1
            for k in range(len_n-2+1):
                if k < len_n - 2:
                    res[i][j] += 10 ** k
                else:
                    if i < l:
                        res[i][j] += 10 ** k
                    elif i == l:
                        if j <= r:
                            res[i][j] += m + 1
                        else:
                            res[i][j] += m
                    else:
                        pass

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

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

Submission status: Accepted

Solution code:
n = int(input())

a = [0] * 100
for i in range(n+1):
    if i % 10 == 0:
        continue
    k = int(str(i)[0]) * 10 + i % 10
    a[k] += 1

result = 0
for i in range(11,100):
    if i % 10 != 0:
        result += a[i] * a[int(str(i)[::-1])]

print(result)

Submission status: Accepted

Solution code:
N=int(input())
b=[]
for i in range(9):
	b.append([0]*9)
for i in range(N):
	c=str(i+1)
	d=int(c[0])
	e=int(c[-1:])
	if d*e==0:
		continue
	b[d-1][e-1]+=1
f=0
for j in range(9):
	f+=b[j][j]**2
for i in range(8):
	for j in range(i+1):
		f+=b[j][i+1]*b[i+1][j]*2
print(f)

Submission status: Accepted

Solution code:
n=int(input())
s=str(n)
m=len(s)
ans=0
if n<=9:
    print(n)
    exit()
if n<=99:
    for i in range(1,n+1):
        for j in range(1,n+1):
            if str(i)[-1]=="0":
                continue
            if str(i)[0]==str(j)[-1] and str(j)[0]==str(i)[-1]:
                ans+=1
    print(ans)
    exit()
y=int(s[1:m-1])
for i in range(1,n+1):
    a,b=str(i)[0],str(i)[-1]
    if b=="0":
        continue
    if a==b:
        ans+=1
    dig=1
    for j in range(m-2):
        ans+=dig
        dig*=10
    if int(b)==int(s[0]):
        if int(a)>int(s[-1]):
            ans+=y
        else:
            ans+=y+1
    elif int(b)<int(s[0]):
        ans+=dig
print(ans)

Submission status: Accepted

Solution code:
sn=input()
l=len(sn)
n=int(sn)
s=0
t=[[0 for i in range(9)]for j in range(9)]

if l==1:
    print(n)
else:
    s1=0
    for i in range(l-2):
        s1+=(10**i)
    s1+=1
    for i in range(9):
        t[i][i]=s1

    s2=0
    for i in range(l-2):
        s2+=(10**i)

    for i in range(9):
        for j in range(9):
            if i!=j:
                t[i][j]=s2
    for i in range(9):
        for j in range(9):
            if int(sn[0])<i+1:
                s1=s2
            elif int(sn[0])==i+1:
                if int(sn[l-1])<j+1:
                    if len(sn[1:l-1])!=0:
                        t[i][j]+=int(sn[1:l-1])
                else:
                    if len(sn[1:l-1])!=0:
                        t[i][j]+=int(sn[1:l-1])+1
                    else:
                        t[i][j]+=1
            else:
                t[i][j]+=10**(l-2)

    for i in range(9):
        for j in range(9):
                s+=t[i][j]*t[j][i]
    print(s)

Submission status: Accepted

Solution code:
N = int(input())

#print(int(str(N)[0]))
#print(int(str(N)[-1]))

ans = 0

ansDict = {}
for i in range(10, 100):
    inAns = 0
    head = int(i / 10)
    tail = int(i % 10)

    #先頭行が0の場合は考慮しない（0件）
    if tail == 0:
        ansDict[i] = 0
        continue

    # 先頭と末尾が同じ桁数の時は、1桁の数値も組み合わせに含める
    if(head == tail):
        inAns = inAns + 1

#    print("key = " + str(head * 10 + tail))
    B = tail * 10 + head
    keta = 1
    while(B <= N):
#        print(B)
        inAns = inAns + 1
        next = B + 10
        if(int(str(next)[0]) != tail):
            keta = keta + 1
            B = tail * (10 ** keta) + head
        else:
            B = next

    ansDict[i] = inAns


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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
cnt = 0
ls = []
"""
def chk(a,b):
    if a[len(a)-1] == '0' or b[len(b)-1] == '0':
        return 0
    elif a[0] == b[len(b)-1] and a[len(a)-1] == b[0] and a == b:
        return 1
    elif a[0] == b[len(b)-1] and a[len(a)-1] == b[0]:
        return 2
    else:
        return 0
"""
ls = []
dic = {}
for i in range(1,10):
    for j in range(1,10):
        ls.append([str(i),str(j)])

for i in range(81):
    dic[i] = 0


if n <= 50000:
    for i in range(1,n+1):
        a = str(i)
        if a[len(a)-1] != '0':
            #print([a[0],str(i)[len(a)-1]])
            if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
                #print(a,1)
                #print([str(i)[len(a)-1],a[0]])
                cnt += 1
            elif a[0] == a[len(a)-1]:
                #print(a,2)
                #print([str(i)[len(a)-1],a[0]])
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
            elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
                #print(a)
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
            dic[ls.index([a[0],str(i)[len(a)-1]])] += 1

elif n <= 100000:
    dic = {0: 1112, 1: 1111, 2: 1111, 3: 1111, 4: 1111, 5: 1111, 6: 1111, 7: 1111, 8: 1111, 9: 1111, 10: 1112, 11: 1111, 12: 1111, 13: 1111, 14: 1111, 15: 1111, 16: 1111, 17: 1111, 18: 1111, 19: 1111, 20: 1112, 21: 1111, 22: 1111, 23: 1111, 24: 1111, 25: 1111, 26: 1111, 27: 1111, 28: 1111, 29: 1111, 30: 1112, 31: 1111, 32: 1111, 33: 1111, 34: 1111, 35: 1111, 36: 111, 37: 111, 38: 111, 39: 111, 40: 112, 41: 111, 42: 111, 43: 111, 44: 111, 45: 111, 46: 111, 47: 111, 48: 111, 49: 111, 50: 112, 51: 111, 52: 111, 53: 111, 54: 111, 55: 111, 56: 111, 57: 111, 58: 111, 59: 111, 60: 112, 61: 111, 62: 111, 63: 111, 64: 111, 65: 111, 66: 111, 67: 111, 68: 111, 69: 111, 70: 112, 71: 111, 72: 111, 73: 111, 74: 111, 75: 111, 76: 111, 77: 111, 78: 111, 79: 111, 80: 112}
    cnt = 25000008
    for i in range(50001,n+1):
        a = str(i)
        if a[len(a)-1] != '0':
            #print([a[0],str(i)[len(a)-1]])
            if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
                #print(a,1)
                #print([str(i)[len(a)-1],a[0]])
                cnt += 1
            elif a[0] == a[len(a)-1]:
                #print(a,2)
                #print([str(i)[len(a)-1],a[0]])
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
            elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
                #print(a)
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
            dic[ls.index([a[0],str(i)[len(a)-1]])] += 1
elif n <= 150000:
    dic = {0: 1112, 1: 1111, 2: 1111, 3: 1111, 4: 1111, 5: 1111, 6: 1111, 7: 1111, 8: 1111, 9: 1111, 10: 1112, 11: 1111, 12: 1111, 13: 1111, 14: 1111, 15: 1111, 16: 1111, 17: 1111, 18: 1111, 19: 1111, 20: 1112, 21: 1111, 22: 1111, 23: 1111, 24: 1111, 25: 1111, 26: 1111, 27: 1111, 28: 1111, 29: 1111, 30: 1112, 31: 1111, 32: 1111, 33: 1111, 34: 1111, 35: 1111, 36: 1111, 37: 1111, 38: 1111, 39: 1111, 40: 1112, 41: 1111, 42: 1111, 43: 1111, 44: 1111, 45: 1111, 46: 1111, 47: 1111, 48: 1111, 49: 1111, 50: 1112, 51: 1111, 52: 1111, 53: 1111, 54: 1111, 55: 1111, 56: 1111, 57: 1111, 58: 1111, 59: 1111, 60: 1112, 61: 1111, 62: 1111, 63: 1111, 64: 1111, 65: 1111, 66: 1111, 67: 1111, 68: 1111, 69: 1111, 70: 1112, 71: 1111, 72: 1111, 73: 1111, 74: 1111, 75: 1111, 76: 1111, 77: 1111, 78: 1111, 79: 1111, 80: 1112}
    cnt = 100000008
    for i in range(100001,n+1):
        a = str(i)
        if a[len(a)-1] != '0':
            #print([a[0],str(i)[len(a)-1]])
            if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
                #print(a,1)
                #print([str(i)[len(a)-1],a[0]])
                cnt += 1
            elif a[0] == a[len(a)-1]:
                #print(a,2)
                #print([str(i)[len(a)-1],a[0]])
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
            elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
                #print(a)
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
            dic[ls.index([a[0],str(i)[len(a)-1]])] += 1
else:
    dic = {0: 6112, 1: 6111, 2: 6111, 3: 6111, 4: 6111, 5: 6111, 6: 6111, 7: 6111, 8: 6111, 9: 1111, 10: 1112, 11: 1111, 12: 1111, 13: 1111, 14: 1111, 15: 1111, 16: 1111, 17: 1111, 18: 1111, 19: 1111, 20: 1112, 21: 1111, 22: 1111, 23: 1111, 24: 1111, 25: 1111, 26: 1111, 27: 1111, 28: 1111, 29: 1111, 30: 1112, 31: 1111, 32: 1111, 33: 1111, 34: 1111, 35: 1111, 36: 1111, 37: 1111, 38: 1111, 39: 1111, 40: 1112, 41: 1111, 42: 1111, 43: 1111, 44: 1111, 45: 1111, 46: 1111, 47: 1111, 48: 1111, 49: 1111, 50: 1112, 51: 1111, 52: 1111, 53: 1111, 54: 1111, 55: 1111, 56: 1111, 57: 1111, 58: 1111, 59: 1111, 60: 1112, 61: 1111, 62: 1111, 63: 1111, 64: 1111, 65: 1111, 66: 1111, 67: 1111, 68: 1111, 69: 1111, 70: 1112, 71: 1111, 72: 1111, 73: 1111, 74: 1111, 75: 1111, 76: 1111, 77: 1111, 78: 1111, 79: 1111, 80: 1112}
    cnt = 225000008
    for i in range(150001,n+1):
        a = str(i)
        if a[len(a)-1] != '0':
            #print([a[0],str(i)[len(a)-1]])
            if dic[ls.index([str(i)[len(a)-1],a[0]])] == 0 and a[0] == a[len(a)-1]:
                #print(a,1)
                #print([str(i)[len(a)-1],a[0]])
                cnt += 1
            elif a[0] == a[len(a)-1]:
                #print(a,2)
                #print([str(i)[len(a)-1],a[0]])
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])] * 2 + 1
            elif dic[ls.index([str(i)[len(a)-1],a[0]])] != 0:
                #print(a)
                cnt += dic[ls.index([str(i)[len(a)-1],a[0]])]*2
            dic[ls.index([a[0],str(i)[len(a)-1]])] += 1


print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
import numpy as np
ans = 0
lis = [[0]*9]*9
lis = np.array(lis)
for i in range(1,N+1):
    a = i%10
    b = i//(10**(len(str(i))-1))
    if a != 0:
        lis[a-1][b-1] += 1
for j in range(9):
    for k in range(9):
        ans += lis[k][j]*lis[j][k]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

t = np.array([[0] * 10] * 10)

ans = 0
for i in range(1, N + 1):
    if i % 10 == 0:
        continue
    else:
        a = int(str(i)[0])
        b = int(str(i)[-1])
        t[a][b] = t[a][b] + 1

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

Submission status: Accepted

Solution code:
n = int(input())

cnt = 0
for i in range(1, n+1):
    i_str = str(i)
    if len(i_str) == 1:
        cnt += 1
        continue
    if len(i_str) == 2:
        if int(i_str[0]) == int(i_str[1]):
            cnt += 3
        elif int(i_str[1]) != 0 and int(i_str[0]) > int(i_str[1]):
            cnt += 2
        continue
    if len(i_str) == 3:
        if int(i_str[0]) == int(i_str[2]):
            cnt += 4 + (2 * (int(i_str[1])+1) - 1)
        elif int(i_str[2]) != 0 and int(i_str[0]) > int(i_str[2]):
            cnt += 22
        elif int(i_str[0]) < int(i_str[2]):
            cnt += 2
        continue
    if len(i_str) == 4:
        if int(i_str[0]) == int(i_str[3]):
            cnt += 24 + 20 * int(i_str[1]) + 2 * (int(i_str[2])+1) - 1
        elif int(i_str[3]) != 0 and int(i_str[0]) > int(i_str[3]):
            cnt += 222
        elif int(i_str[0]) < int(i_str[3]):
            cnt += 22
        continue
    if len(i_str) == 5:
        if int(i_str[0]) == int(i_str[4]):
            cnt += 224 + 200 * int(i_str[1]) + 20 * int(i_str[2]) + 2 * (int(i_str[3])+1) - 1
        elif int(i_str[4]) != 0 and int(i_str[0]) > int(i_str[4]):
            cnt += 2222
        elif int(i_str[0]) < int(i_str[4]):
            cnt += 222
        continue
    if len(i_str) == 6:
        if int(i_str[0]) == int(i_str[5]):
            cnt += 2224 + 2000 * int(i_str[1]) + 200 * int(i_str[2]) + 20 * int(i_str[3]) + 2 * (int(i_str[4])+1) - 1
        elif int(i_str[5]) != 0 and int(i_str[0]) > int(i_str[5]):
            cnt += 22222
        elif int(i_str[0]) < int(i_str[5]):
            cnt += 2222
        continue

print(cnt)

Submission status: Accepted

Solution code:
words = lambda t : list(map(t, input().split()))
n = int(input())
n_lst = n % 10
n_hd = n
n_d = 1
while n_hd // 10 != 0:
    n_hd //= 10
    n_d += 1
#print(n_hd, n_lst, n_d)
ans = 0
for i in range(1,n+1):
    if i % 10 == 0:
        continue
    d = 2
    b_hd = i % 10
    b_lst = i
    while b_lst // 10 != 0:
        b_lst //= 10
    if b_hd == b_lst:
        if b_hd <= n:
            #print("add0")
            ans += 1
    while 10**(d-1) < n:

        if d < n_d:
            #print("add1", 10**(d-2))
            ans += 10**(d-2)
        elif d == n_d:
            if b_hd < n_hd:
                #print("add2",10**(d-2) )
                ans += 10**(d-2)
            elif b_hd == n_hd:
                #print(i,d, b_hd, b_lst)
                #print("add3",n - (n_hd)*(10**(d-1)) - (n_lst) )
                ans += (n - (n_hd)*(10**(d-1)) - (n_lst)) // 10
                if b_lst <= n_lst:
                    #print("add4")
                    ans += 1
        d += 1

print(ans)

Submission status: Accepted

Solution code:
import sys


def solve(inp):
    N = int(inp.readline().strip())

    nums = [[0 for i in range(11)] for i in range(11)]

    for s in range(1, 10):
        for e in range(0, 10):
            n = 0
            # print('--- {} {}'.format(s, e))
            if s == e and s <= N:
                # 1けた
                n += 1
            for d in (10, 100, 1000, 10000, 100000, 1000000):
                start = end = s * d + e
                if start > 200000:
                    continue
                d2 = int(d / 10)
                while d2 >= 10:
                    end += 9 * d2
                    d2 = int(d2 / 10)
                end = min(end, 200000)
                # print('{} {}'.format(start, end))
                if end <= N:
                    n += int(d / 10)
                else:
                    j = start
                    while j <= N:
                        n += 1
                        j += 10
            # print('  --> {}'.format(n))
            # print()
            nums[s][e] = n

    r = 0
    for s in range(1, N + 1):
        if s % 10 != 0:
            r += nums[s % 10][int(str(s)[0])]

    return str(r)


def solve1(inp):
    N = int(inp.readline().strip())

    r = 0
    for i in range(1, N + 1):
        first = int(str(i)[0])
        last = i % 10
        for j in range(1, N + 1):
            if int(str(j)[0]) == last and j % 10 == first:
                r += 1

    return str(r)


def main():
    result = solve(sys.stdin)
    if result:
        print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''
'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''
'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''
'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''
'''
.append(要素)
.extend(要素)
.insert(場所,要素)
del 配列名[場所]
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
'''
copy.copy(配列名)
'''
'''
sort()
sorted(reverse=true)
'''
'''
2分探索
bisect.bisect_left(リスト, 要素)
'''
'''
リスト同士の演算
list(map(lambda x, y: x+y, el2, el3))
'''
'''
配列の初期化
配列名=[a]*n+[b]*m
'''
'''
重複
list(set())
２次元配列
list(map(list, set(map(tuple, addition))))
'''
'''
順列
for el in itertools.permutations(配列のパターン):
'''
'''
Decimal()
'''
# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()




from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
import itertools
from functools import reduce
from operator import mul
def gcd(*numbers):
    return reduce(math.gcd, numbers)


def gcd_list(numbers):
    return reduce(math.gcd, numbers)


def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)


def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]


N = int(stdin.readline().rstrip())
count = 0
add = 0
bs = []
for k in range(1, N+1):
    a = k
    bf = str(a)[len(str(a)) - 1]
    if bf == '0':
        continue
    bb = str(a)[0]
    if str(a)[0] == str(a)[len(str(a)) - 1]:
        if int(bf) > N:
            continue
        bs.append((a, int(bf)))
    if int(bf + bb) > N:
        continue
    bs.append((a, int(bf + bb)))
    if len(str(N)) >= 3:
        if bf == str(N)[0]:
            for m in range(1, len(str(N)) - 2):
                add += 10 ** m
            if int(bb) <= int(str(N)[len(str(N)) - 1]):
                add += int(str(N)[1: len(str(N)) - 1]) + 1
            if int(bb) > int(str(N)[len(str(N)) - 1]):
                add += int(str(N)[1: len(str(N)) - 1])
        if bf < str(N)[0]:
            tmp = 0
            for m in range(1, len(str(N)) - 1):
                tmp += 10 ** m
            add += tmp
        if bf > str(N)[0]:
            for m in range(1, len(str(N)) - 2):
                add += 10 ** m
print(len(bs)+add)

Submission status: Accepted

Solution code:
N = int(input())

L = [0] * 100

for i in range(1,N+1):
    if i % 10 == 0:
        continue
    a = (i // (10 ** (len(str(i)) - 1))) * 10 + i % 10
    L[a] += 1
    
ans = 0
for i in range(100):
    if L[i] == 0:
        continue
    if i // 10 == i % 10:
        ans += L[i] ** 2
    else:
        a = i // 10 + (i % 10) * 10
        ans += L[i] * L[a]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
n_digit = len(str(n))
ans = 0
for i in range(1,n+1):
  max_a = list(str(i))[0]
  min_a = list(str(i))[-1]
  if min_a == "0":
    continue
  else:
    if max_a == min_a:
      if int(max_a)<=n:
        ans += 1
    for j in range(n_digit-1):
      max_b = int(min_a+"9"*j+max_a)
      min_b = int(min_a+"0"*j+max_a)
      if max_b <= n:
        ans += int(10**j)
      elif min_b > n:
        continue
      else:
        ans += (n-min_b)//10+1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import itertools

N = int(input().split()[0])
s = str(N)
l = list(range(1, 10))
p_gen = itertools.product(l, repeat=2)
p_list = list(p_gen)
K = len(str(s))


def count_kouho(x, y):
    count = 0
    for k in range(1, K + 1):
        # 1桁目
        if k == 1:
            if x == y:
                if x <= N:
                    count += 1
            continue
        # 2桁目
        if k == 2:
            if int(str(x) + str(y)) <= N:
                count += 1
            continue
        # 3桁目以上
        if k == K:
            # 一番大きい桁の場合
            # TODO
            if x < int(s[0]):
                count += 10 ** (k - 2)
            elif x > int(s[0]):
                pass
            else:
                if y <= int(s[-1]):
                    count += int(s[1:-1]) + 1
                else:
                    count += int(s[1:-1])
            continue
        count += 10 ** (k - 2)
    return count


c = 0

for p in p_list:
    head, tail = p
    kouho_1 = count_kouho(head, tail)
    if head == tail:
        kouho_2 = kouho_1
    else:
        kouho_2 = count_kouho(tail, head)
    if kouho_1 < 1 or kouho_2 < 1:
        # print("{} skip".format((head, tail)))
        continue
    new_c = kouho_1 * kouho_2
    # print("{}: {} ({})".format((head, tail), (kouho_1, kouho_2), new_c))
    c += new_c

ans = c
print(ans)

Submission status: Accepted

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

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

for i in range(1, n+1):
    s = str(i)
    b = int(s[0])
    e = int(s[-1])
    l[b][e] += 1

for b in range(10):
    for e in range(10):
        ans += l[b][e] * l[e][b]
        # if a[b][e] * a[e][b] > 0:
    # print(b, e, l[b][e], l[e][b])

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
x=[0 for i in range(n+1)]

def find(a,n,x):
  k =0
  l =0
  for j in range(a,n):
    if j % 10!= 0:
      if a%10 == j//(10**(len(str(j))-1)) and j%10 == a//(10**(len(str(a))-1)):
        k+=1
        x[j] = 1
      if a%10 == j%10 and j//(10**(len(str(j))-1)) == a//(10**(len(str(a))-1)):
        l+=1
        x[j] = 1
  if a%10 == a//(10**(len(str(a))-1)):
    return k*l
  return 2*k*l

ans=0
for i in range(1,n+1):
  if x[i] == 0 and i%10!=0:
    ans += find(i,n+1,x)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
m = len(list(str(n)))
ans = 0
count = 0
c = 1
for i in range(m-1):
    if i == 0:
        count += 1
    else:
        count += c
        c *= 10
#print(count)
k = n//(10**(m-1))
a = n%10
ans += (count+c)*(count+c)*(k-1)
#print(ans)
ans += (count)*(count)*(9-k)
#print(ans)
count += (n-k*(10**(m-1)))//10+1
if a < k:
    count -= 1
ans += (count)*(count)
#print(ans)
c = 1
count = 0
for i in range(1,m-1):
    count += c
    c *= 10
#print(count,c)
if k < 9:
    ans += count*count*(9-k)*(8-k)
if k > 1:
    ans += (count+c)*(count+c)*(k-1)*(k-2)
ans += (count+c)*(count)*(k-1)*(9-k)*2
for i in range(1,a+1):
    if i < k:
        ans += (count+c)*(count+1+(n-k*(10**(m-1)))//10)*2
    elif i > k:
        ans += count*(count+1+(n-k*(10**(m-1)))//10)*2
for i in range(a+1,10):
    if i < k:
        ans += (count+c)*(count+(n-k*(10**(m-1)))//10)*2
    elif i > k:
        ans += count*(count+(n-k*(10**(m-1)))//10)*2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
cmb = [[0 for _ in range(9)] for _ in range(9)]
if n <= 10:
    print(min(n,9))
    exit(0)
for i in range(1,n+1):
    if str(i)[0] != '0' and str(i)[-1] != '0':
        cmb[int(str(i)[0])-1][int(str(i)[-1])-1] += 1
for i in range(9):
    for j in range(9):
        if cmb[i][j] != 0 and cmb[j][i] != 0:
            ans += (cmb[i][j] * cmb[j][i]) 
print(ans)

Submission status: Accepted

Solution code:

N = int(input())

Nm = int(str(N)[0])
N0 = int(str(N)[-1])
ORD_N = len(str(N))
N_LEFT = int(str(N)[1:-1]) if 2 < ORD_N else -1

# print(N, ORD_N)
# print(Nm, N0, N_LEFT)
# print('!' * 10)
# print()
# 

def get_number_less_than_N(bm, b0):
    """ 先頭bm, 後ろb0 を持ち、N より小さい値の数 """
    count = 0
    
    # b が1桁
    if bm == b0:
        count += 1
        # print('1k', count)
    # b が2桁 (N以下ならば該当)
    if bm*10 + b0 <= N:
        count += 1
        # print('2k', count)
    # order(N) 未満の桁
    # たとえばN=10000 なら、3桁(bm x b0), 4桁(bm xx b0) のパターンは全てOK
    for i in range(3, ORD_N):
        count += 10 ** (i-2)
        # print('M', i, count)
    
    # order(N) のけた
    # bm xx.. b0 <= Nm N1 N2.. N0
    if ORD_N >= 3 and bm <= Nm:
        if bm < Nm:
            count += 10 ** (ORD_N-2)
            # print('L++', count)
        elif b0 <= N0:
            count += N_LEFT + 1
            # print('L+', count)
        else:
            count += N_LEFT
            # print('L', count)
    # print('='*10)

    return count
        

count = 0
for i in range(1, N+1):
    # couniprint("<<", i)
    bm, b0 = int(str(i)[-1]), int(str(i)[0])
    if bm == 0:
        continue
    cold = count
    count += get_number_less_than_N(bm, b0)
print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
S = str(N)
l = len(S)
if l == 1:
    print(N)
    exit()
s = int(S[0])
t = int(S[-1])
mn = min(s,t)

ans = 0
dp = defaultdict(int)
            
for i in range(1,10):
    for j in range(1,10):
        if i == j:
            dp[(i,j)] += 1
        if l >= 3: 
            for k in range(0,l-2):
                dp[(i,j)] += 10**k
        # equal digit
        if i < s:
            dp[(i,j)] += 10**(l-2)
        elif i == s and j <= t:
            dp[(i,j)] += 1+((N-s*(10**(l-1))) // 10)
        elif i == s and j > t:
            dp[(i,j)] += (N-s*(10**(l-1))) // 10
#print(dp)
ans = 0
for i in range(1,10):
    for j in range(1,10):
        ans += dp[(i,j)]*dp[(j,i)]
print(ans)

'''
real = 0
for i in range(1,N+1):
    for j in range(1,N+1):
        S = str(i)
        T = str(j)
        if S[0] == T[-1] and S[-1] == T[0]:
            real += 1
print(real)
'''

Submission status: Accepted

Solution code:
n = int(input())

ans = [[0 for _ in range(9)] for _ in range(9)]
for i in range(1,n+1):
    mm = list(str(i))
    a = 0
    for j in range(len(mm)):
        a += int(mm[j])*(10**j)
    
    mm1 = int((list(str(i)))[0])
    mm2 = int((list(str(i)))[-1])
    if not(mm2!=0):
        continue
    #print(mm1,mm2,i)
    ans[mm1-1][mm2-1] +=1

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


print(cnt)

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

Submission status: Accepted

Solution code:
n = int(input())
n_list = [[0  for s in range(10)] for i in range(10)]
for m in range(1,n+1):
  str_m = str(m)
  n_list[int(str_m[0])][int(str_m[-1])] += 1
ans = 0
for a in range(1,10):
    for b in range(1,10):
        ans += n_list[a][b] * n_list[b][a]
print(ans)

Submission status: Accepted

Solution code:
import collections

num = int(input())
count = 0
ll = []
lr = []
for nn1 in range(1, num + 1):
  left = (str(nn1).lstrip('0')[0])
  right = (str(nn1)[-1])
  ll.append(left + '+' + right)
  lr.append(right + '+' + left)

c = collections.Counter(lr)

for i in range(num):
  count += int(c[ll[i]])

print(count)

Submission status: Accepted

Solution code:
N = int(input())

def count(a):
  if N < 10:
    if N < a:
      return 0
    else:
      return 1
  else:
    cnt_a = 1
    keta = 2
    while True:
      if N > int(str(a)+str(9)*(keta-2)+str(a)):
        cnt_a += 10 ** (keta-2)
        keta += 1
      elif N >= int(str(a)+str(0)*(keta-2)+str(a)):
        cnt_a += (N - a*(10**(keta-1)) - a) // 10 + 1
        break
      else:
        break
  return cnt_a
        
  
def count_(a, b):
  if N < 10:
    return 0, 0
  else:
    cnt_ = 0
    cnt__  = 0
    keta = 2
    while True:
      if N > int(str(b)+str(9)*(keta-2)+str(a)):
        cnt_ += 10 ** (keta-2)
        cnt__ += 10 ** (keta-2)
        keta += 1
      elif N >= int(str(b)+str(0)*(keta-2)+str(a)):
        cnt_ += (N - b*(10**(keta-1)) - a) // 10 + 1
        cnt__ += 10 ** (keta-2)
        break
      elif N > int(str(a)+str(9)*(keta-2)+str(b)):
        cnt_ += 10 ** (keta-2)
        break
      elif N >= int(str(a)+str(0)*(keta-2)+str(b)):
        cnt_ += (N - a*(10**(keta-1)) - b) // 10 + 1
        break
      else:
        break
  return cnt_, cnt__

ans = 0
for i in range(1, 10):
  for j in range(i, 10):
    # start, end  =  i, j
    if i == j:
      # １桁が可能
      #print(i, count(i))
      ans += count(i)**2
    else:
      pa, pa_ = count_(i,j)
      #print(i, j, pa, pa_)
      ans += pa * pa_ * 2
      
print(ans)

Submission status: Accepted

Solution code:
# ABC152d


def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10 ** 6)

    n = int(input())
    cou = 0
    for i in range(1, n + 1):
        if str(i)[-1] == '0':
            continue
        # print(i)
        if str(i)[0] == str(i)[-1]:
            cou += 1
        k = 2
        while True:
            b = int(str(i)[-1] + '0' * (k - 2) + str(i)[0])
            if b <= n:
                if k > 2:
                    if n - b > 10 ** (k - 1):
                        cou += 10 ** (k - 2)
                    else:
                        cou += (n - b)//10 + 1
                else:
                    cou += 1
                #print(i, k, cou)
                k += 1
                continue
            #print(i, cou)
            break
    print(cou)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import log10, floor

def k(a):
    return len(str(a))
N = int(input())
top = str(N)[0]
btm = str(N)[-1]
mid = str(N)[1:-1]
res = 0
for i in range(1,N+1):
    a = str(i)[0]
    b = str(i)[-1]
    ia = int(a)
    ib = int(b)
    if b == "0":
        continue
    if a == b and ia <= N:
        res += 1 # a
    if(ib * 10 + ia <= N):
        res += 1
    for d in range(1,7):
        if ib * (10**(d+1)) + ia > N:
            continue
        ng = 10**10
        ok = 0
        while ng - ok > 1:
            mid = (ng+ok)//2
            if ib * (10**(d + k(mid))) + mid*10 + ia > N:
                ng = mid
            else:
                ok = mid
        res += ok + 1
    # print(i)
    # print(b)
    # print(a)
    # print(ok)
    # print("----")
    # res += ok + 1

print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] * 100
for i in range(1,n+1):
  msb = str(i)[0]
  lsb = str(i)[-1]
  if msb == '0' or lsb == '0':
    continue
  a[int(msb) * 10 + int(lsb)] += 1

tmp = 0
for i, ai in enumerate(a):
  if '0' not in str(i):
    if i % 11 == 0:
      tmp += ai * ai
    else:
      tmp += ai * a[int(str(i)[::-1])]
  else:
    continue
    
print(tmp)