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:
import sys
sys.setrecursionlimit(10000000)
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math
INF = 500000
mod = 10**9+7

N = int(input())
dic = defaultdict(lambda : 0)
for i in range(1, 10):
    for j in range(1, 10):
        if i == j:
            if i <= N:
                dic[(i, j)] = 1
        for k in range(1, 6):
            l = 10**k
            x = i*l + j
            if N < x:
                break
            if x + l-10 <= N:
                dic[(i, j)] += l//10
            else:
                n = N%10
                if n < j:
                    dic[(i, j)] += N%l//10
                else:
                    dic[(i, j)] += N%l//10+1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += dic[(i, j)] * dic[(j, i)]
print(ans)

Submission status: Accepted

Solution code:
import copy
from collections import defaultdict


num = int(input())

d = defaultdict(int)
for i in range(1, num+1):
    s = str(i)
    start = int(s[0])
    end = int(s[-1])
    d[(start, end)] += 1

ans = 0
queue = sorted(d.keys())
for (start, end) in queue:
    if start > end:
        continue
    elif start == end:
        ans += d[(start, end)] ** 2
    else:
        ans += (d[(start, end)] * d[(end, start)]) * 2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
strn = str(n)
keta = len(strn)
n_head = int(strn[0])
n_tail = int(strn[-1])
naka = strn[1:-1]
if len(naka)  == 0:
    nakan = 0
else:
    nakan = int(naka)

ans = 0

if keta <= 2:
    for i in range(1,n+1):
        for j in range(1,n+1):
            head1 = int(str(i)[0])
            tail1 = int(str(i)[-1])
            head2 = int(str(j)[0])
            tail2 = int(str(j)[-1])
            if head1 == tail2 and head2 == tail1:
                ans += 1
                #print(i,j)
    print(ans)

else:
    hoj = []
    tmp = 1
    mog = 1
    for i in range(keta):
        hoj.append(mog)
        tmp = tmp*10
        mog = mog + tmp
    #print(hoj)
    
    for i in range(1,n+1):
        head = int(str(i)[0])
        tail = int(str(i)[-1])
        if tail == 0:
            continue

        if tail < n_head:
            ans += hoj[keta-2]
        elif tail > n_head:
            ans += hoj[keta-3]
        else:
            ans += hoj[keta-3]
            if head <= n_tail:
                ans += nakan + 1
            else:
                ans += max(nakan ,0)

        if head == tail:
            ans += 1

        #print(i,ans)
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
k=len(str(n))
f=int(str(n)[0])
l=int(str(n)[-1])
if k==1:
    print(n)
    exit()
m=(n%(10**(k-1))-l)//10
ans=0
for i in range(1,10):
    if i<f:
        ans+=((10**(k-1)-1)//9+1)**2
    elif i==f:
        if i<=l:
            ans+=((10**(k-2)-1)//9+m+2)**2
        else:
            ans+=((10**(k-2)-1)//9+m+1)**2
    else:
        ans+=((10**(k-2)-1)//9+1)**2
for i in range(1,10):
    for j in range(1,10):
        if i!=j:    
            if i<f:
                a=(10**(k-1)-1)//9
            elif i==f:
                if j<=l:
                    a=(10**(k-2)-1)//9+m+1
                else:
                    a=(10**(k-2)-1)//9+m
            else:
                a=(10**(k-2)-1)//9
            if j<f:
                b=(10**(k-1)-1)//9
            elif j==f:
                if i<=l:
                    b=(10**(k-2)-1)//9+m+1
                else:
                    b=(10**(k-2)-1)//9+m
            else:
                b=(10**(k-2)-1)//9
            ans+=a*b
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
str_N = str(N)
digits = len(str_N)
lim = min(str_N[0], str_N[-1])

#print(digits, lim)

count = 0
for a in range(1,N+1):
    str_a = str(a)
    a_s = str_a[0]
    a_e = str_a[-1]
    dig_a = len(str_a)
    
    if int(a_e) > 0:
        #one digit b
        if a_s == a_e:
            count += 1
        #two or more digits
        for dig_b in range(2,digits):
            count += 10**(dig_b-2)
            
        reverse = int(a_e)*10**(digits-1) + int(a_s)
        if reverse <= N:
            if int(a_e) < int(str_N[0]):
                count += 10**(digits-2)
            else:
                count += (N - reverse)//10 + 1

    #print(a, count)

print(count)

Submission status: Accepted

Solution code:
N = int(input())

tab = [[0] * 9 for _ in range(9)]

for head in range(1, 10):
    for tail in range(1, 10):
        val = 0
        if head == tail and head <= N: val += 1
        # if head * 10 + tail <= N: val += 1
        N_tmp = head * 10 + tail
        for level in range(100):
            N_nxt = (N_tmp - tail) * 10 + tail
            if N_nxt > N:
                val += min(max(0, (N - N_tmp) // 10 + 1), 10 ** level)
            else:
                val += 10 ** level
            #
            N_tmp = N_nxt
        #
        tab[head - 1][tail - 1] = val
        
# for i in range(9):
#     print("#", '\t'.join(map(str, tab[i])))

ans = 0
for head in range(1, 10):
    for tail in range(1, 10):
        ans += (tab[head - 1][tail - 1] * tab[tail - 1][head - 1])

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    nines = {(i, j): 0 for i in num for j in num}

    for i in range(1, n+1):
        if i % 10 == 0:
            continue
        s = str(i)
        ht = (int(s[0]), int(s[-1]))
        nines[ht] += 1

    print(sum(nines[i, j] * nines[j, i] for i in num for j in num))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

cnt=[[0,0,0,0,0,0,0,0,0,0] for _ in range(10)]
ans = 0


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

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())

istart_jend = [[0 for _ in range(10)] for i in range(10)]
base = 1

for i in range(1,n+1):
    if i%(base*10)==0:
        base*=10
    istart_jend[i//base][i%10]+=1

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

Submission status: Accepted

Solution code:
#a, b = map(int, input().split())
n = int(input())

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

ans = 0
for i in range(n):
    s = str(i+1)
    t = len(s)
    u = int(s[0])
    v = int(s[len(s)-1])

    if u == 0:
        continue
    if v == 0:
        continue

    if u == v:
        ans += 1

    z = 2 * l[v][u]
    ans += z

    l[u][v] += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

def count(a1,a2):
    res = 0
    for b in range(1,N+1):
        sb = str(b)
        if sb[0]==a2 and sb[-1]==a1:
            res += 1
    return res

d = {}
for a1 in range(1,10):
    for a2 in range(10):
        d[(str(a1),str(a2))] = count(str(a1),str(a2))

ans = 0
for a in range(1,N+1):
    sa = str(a)
    a1 = sa[0]
    a2 = sa[-1]
    ans += d[(a1,a2)]

print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8

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

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

    mtrx[s][e] += 1
    
sumv = 0
for i in range(10):
    for j in range(10):
        sumv += mtrx[i][j] * mtrx[j][i]
print(sumv)

Submission status: Accepted

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


def solve(N: int):
    sN = str(N)
    lN = len(sN)
    if lN == 1:
        print(N)
        return
    l1 = int(sN[0])
    l2 = int(sN[-1])
    d = {}
    for k1, k2 in itertools.product(range(1, 10), repeat=2):
        p = 10 ** (lN - 2) // 9
        if k1 == k2:
            p += 1
        if k1 < l1:
            p += 10 ** (lN - 2)
        elif k1 == l1:
            p += int(sN[1:-1] or "0")
            if k2 <= l2:
                p += 1
        d[k1, k2] = p

    ans = 0
    for k1, k2 in itertools.product(range(1, 10), repeat=2):
        ans += d[k1, k2] * d[k2, k1]
    print(ans)


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = {}
ans = 0
for i in range(1,10):
    for j in range(1,10):
        a[i,j] = 0

for i in range(1,N+1):
    top = int(str(i)[0])
    low = int(str(i)[-1])
    if top == 0 or low == 0:
        continue
    a[top, low] += 1
    if top == low:
        ans += a[low,top] * 2 -1
    else:
        ans += a[low,top] * 2


print(ans)

Submission status: Accepted

Solution code:
N=int(input())

num_end_dic={}
for i in range(1,N+1):  
  digit_i=len(str(i))
  head=i//(10**(digit_i-1))
  tail=i%10
  
  if tail==0:
    continue
  elif (head,tail) not in num_end_dic:
    num_end_dic[(head,tail)]=1
  else:
    num_end_dic[(head,tail)]+=1
  
#print(num_end_dic)
answer=0
for h,t in num_end_dic:
  if (t,h) in num_end_dic:
    answer+=num_end_dic[(h,t)]*num_end_dic[(t,h)]

print(answer)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

BegEndTbl = []
for _ in range(10):
    BegEndTbl.append([0] * 10)

ans = 0
for i in range(1, N + 1):
    left = int(str(i)[0])
    right = i % 10
    if left == right:
        ans += 1
    ans += BegEndTbl[right][left] * 2
    BegEndTbl[left][right] += 1
print(ans)

Submission status: Accepted

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

for i in range(1, N + 1):
    if str(i)[-1] != '0':
        d[str(i)[0] + str(i)[-1]] += 1

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

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


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


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

    # divisors.sort()
    return divisors


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


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

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

    return True


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

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


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


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




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

            


    




    


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def up(m):
    while m>0:
        if m//10>0:
            m=m//10
        else:
            break
    return m


n=int(input())
a=[[0]*9 for x in range(9)]
for c in range(1,n+1):
    i=c%10
    if i==0:
        continue
    j=up(c)
    a[i-1][j-1]+=1
t=0
for i in range(9):
    for j in range(9):
        t+=a[i][j]*a[j][i]
print(t)

Submission status: Accepted

Solution code:
n=int(input())
C=[]
for i in range(9):
    C.append([0]*9)
for i in range(1,n+1):
    l=int(str(i)[0])
    r=int(str(i)[-1])
    if r!=0:
        C[l-1][r-1]+=1
ans=0
for i in range(9):
    for j in range(9):
        ans+=C[i][j]*C[j][i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
di = defaultdict(int)

for i in range(1, N+1):
    k = str(i)[0] + str(i)[-1]
    di[k] += 1
# print(di)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
table = [[0 for _ in range(9)] for _ in range(9)]

for i in range(1, n+1):
    s = str(i)
    f, l = s[0], s[-1]
    if "0" in [f, l]: continue
    table[int(f)-1][int(l)-1] += 1

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
lists=[[ [] for j in range(10)]for i in range(10)]
for i in range(1,n+1):
    a=str(i)
    lists[int(str(a[0]))][int(str(a[-1]))].append(i)
c=0
for i in range(1,10):
    for j in range(1,10):
        c+=len(lists[i][j])*len(lists[j][i])
print(c)

Submission status: Accepted

Solution code:
N = int(input())

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

arr[1][1] += 1
arr[2][2] += 1
arr[3][3] += 1
arr[4][4] += 1
arr[5][5] += 1
arr[6][6] += 1
arr[7][7] += 1
arr[8][8] += 1
arr[9][9] += 1

if N < 10:
  print(N)
  exit()

for i in range(11, N+1):
  if i % 10 == 0:
    continue
  tmp = str(i)
  first = tmp[0]
  last = tmp[-1]
  # print(first, last)
  arr[int(first)][int(last)] += 1

sum_diag = 0
sum_other = 0

for i in range(1, 10):
  for j in range(1, 10):
    if i == j:
      sum_diag += arr[i][j]*arr[i][j]
    else:
      sum_other += arr[i][j] * arr[j][i] * 2

# print(arr)

print(int(sum_diag + sum_other/2))

Submission status: Accepted

Solution code:
n=int(input())
a=[[0]*9 for i in range(9)]
for i in range(1,n+1):
    x,y =int(str(i)[0]), int(str(i)[-1])
    if y>0:
        a[x-1][y-1]+=1
count=0
for i in range(9):
    for j in range(9):
        if i>j:
            continue
        if i==j:
            count+=a[i][j]**2
        else:
            count+=a[i][j]*a[j][i]*2
print(count)

Submission status: Accepted

Solution code:
n = int(input())
if n<10:
  print(n)
  exit()
ans = 0
for i in range(10,n+1):
  if i%10 == 0:
    continue
  x,y = str(i)[0],str(i)[-1]
  x,y = int(x),int(y)
  k = len(str(n))
  for j in range(1,7):
    if (y+1)*10**j+x-10<=n:
      ans += 10**(j-1)
    else:
      if y*10**j+x>n:
        break
      else:
        ans += (n-y*10**j-x)//10+1
        break
for i in range(1,10):
  for j in range(1,7):
    if (i+1)*10**j+i-10<=n:
      ans += 10**(j-1)*2
    else:
      if i*10**j+i>n:
        break
      else:
        ans += ((n-i*10**j-i)//10+1)*2
        break
ans += 9
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

lN = int(str(N)[0])
rN = int(str(N)[-1])
dN = len(str(N))

ans = 0
for a in range(1, N+1):
    la = int(str(a)[0])
    ra = int(str(a)[-1])
    # print(la, ra)
    if la == 0 or ra == 0:
        continue
    #  1桁
    if la == ra:
        ans += 1
        # print(1)
    #  2桁以上
    d = 2
    while d <= dN:
        if d == dN:
            if ra < lN:
                ans += 10**(d-2)
                # print(10**(d-2))
            elif ra == lN:
                if dN == 2:
                    ans += 1
                    # print(1)
                elif dN > 2:
                    midN = int(str(N)[1:-1])
                    if rN >= la:
                        ans += midN + 1
                        # print(midN + 1)
                    else:
                        ans += midN
                        # print(midN)
            else:
                ans += 0
                # print(0)
            break
        ans += 10**(d-2)
        # print(10**(d-2))
        d +=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = int(str(n)[0])
b = int(str(n)[-1])

l = [[-1]*9 for _ in range(9)]

c = len(str(n))-2

for i in range(1, 10):
    for j in range(1, 10):
        cnt = 0
        if c >= 1:
            a_b = int(str(n)[1:-1])
            for k in range(1, c+1):
                if k == c:
                    if a > i:
                        cnt += 10**k
                    elif a == i:
                        if b >= j:
                            cnt += a_b + 1
                        else:
                            cnt += a_b
                else:
                    cnt += 10**k

        if int(str(i)+str(j)) <= n:
            cnt += 1

        if i == j and i <=n:
            cnt += 1

        l[i-1][j-1] = cnt

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

if n < 10:
    print(n)
    exit()

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

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

    if s + " " + f in memo:
        res += memo[s + " " + f]
    else:
        func(s,f)
        res += memo[s + " " + f]

print(res)

Submission status: Accepted

Solution code:
n=int(input())
ct=[[0]*10 for i in range(10)]
for i in range(1,10):
  for j in range(1,10):
    if n>=10:
      if i==j:
        ct[i][j]+=1
    if n>=10**2:
      ct[i][j]+=1
    if n>=10**3:
      ct[i][j]+=10
    if n>=10**4:
      ct[i][j]+=100
    if n>=10**5:
      ct[i][j]+=1000
    
    if n<10:
      if n>=i:
        ct[i][j]+=1
    
    elif 10<n<10**2:
      if n>=i*10+j:
        ct[i][j]+=1
    
    elif 10**2<n<10**3:
      if int(str(n)[0])>i:
        ct[i][j]+=10
      elif int(str(n)[0])==i:
        if int(str(n)[-1])>=j:
          ct[i][j]+=int(str(n)[1])+1
        else:
          ct[i][j]+=int(str(n)[1])

    elif 10**3<n<10**4:
      if int(str(n)[0])>i:
        ct[i][j]+=10**2
      elif int(str(n)[0])==i:
        if int(str(n)[-1])>=j:
          ct[i][j]+=int(str(n)[1:3])+1
        else:
          ct[i][j]+=int(str(n)[1:3])

    elif 10**4<n<10**5:
      if int(str(n)[0])>i:
        ct[i][j]+=10**3
      elif int(str(n)[0])==i:
        if int(str(n)[-1])>=j:
          ct[i][j]+=int(str(n)[1:4])+1
        else:
          ct[i][j]+=int(str(n)[1:4])
    
    elif n>10**5:
      if int(str(n)[0])>i:
        ct[i][j]+=10**4
      elif int(str(n)[0])==i:
        if int(str(n)[-1])>=j:
          ct[i][j]+=int(str(n)[1:5])+1
        else:
          ct[i][j]+=int(str(n)[1:5])
ans=0
for i in range(1,10):
  for j in range(1,10):
    ans+=ct[i][j]*ct[j][i]
print(ans)

Submission status: Accepted

Solution code:


N = int(input())
sn = str(N)

ans = 0
dp = [[[0] * 2 for i in range(10)] for j in range(10)]
dp[0][0][0] = 1
for nketa in range(len(str(N))):

    ndp = [[[0] * 2 for i in range(10)] for j in range(10)]

    for top in range(10):

        for app in range(10):

            for fl in range(2):

                for nadd in range(10):

                    if top == 0 and  fl == 0 and int(sn[nketa]) == nadd:
                        ndp[nadd][nadd][0] += dp[top][app][fl]
                    if top == 0 and fl == 0 and  int(sn[nketa]) > nadd:
                        ndp[nadd][nadd][1] += dp[top][app][fl]
                    if top == 0 and fl == 1:
                        ndp[nadd][nadd][1] += dp[top][app][fl]

                    if top != 0 and fl == 0 and int(sn[nketa]) == nadd:
                        ndp[top][nadd][0] += dp[top][app][fl]
                        
                    if top != 0 and fl == 0 and int(sn[nketa]) > nadd:
                        ndp[top][nadd][1] += dp[top][app][fl]

                    if top != 0 and fl == 1:
                        ndp[top][nadd][fl] += dp[top][app][fl]
                        
    dp = ndp

    #print (dp[0][0][1])
    #print (dp[9][9][1])

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

print (ans)

Submission status: Accepted

Solution code:
n = int(input())

pattern = {str(h) + str(t): 0 for h in range(1, 10) for t in range(1, 10)}
for a in range(1, n + 1):
  a_str = str(a)
  a_head = a_str[0]
  a_tail = a_str[-1]
  if a_tail != "0":
    pattern[a_head + a_tail] += 1

ans = 0
for k, v in pattern.items():
  ans += v * (pattern.get(k[::-1]) or 0)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ans = 0
c = {}
for i in range(1,n+1):
  s = str(i)
  if s[-1] == 0:
    continue
  k = (s[-1],s[0])
  k_ = (s[0],s[-1])
  pre = 0
  if k_ in c:
    pre = c[k_]
    ans += pre*2
  if k in c:
    v = c[k]
    v += 1
    c[k] = v
  else:
    c[k] = 1
  if k_ in c:
    ans += c[k_]-pre
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

N = int(input())
ans = 0
d = collections.defaultdict(int)
for i in range(1, N+1):
    str_i = str(i)
    key = str_i[0] + str_i[-1]
    if "0" in key:
        continue
    d[key] += 1

for k, v in d.items():
    rev_k = k[::-1]
    rev_v = d.get(rev_k)
    if rev_v is None:
        continue
    ans += v * rev_v
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [[[] for i in range(10)] for j in range(10)]
ans = 0
for i in range(1, N+1):
  a[int(str(i)[0])][int(str(i)[-1])].append(i)
#print(a)
for i in range(1, 10):
  ans += len(a[i][i]) ** 2
for i in range(1, 9):
  for j in range(i+1, 10):
    ans += len(a[i][j]) * len(a[j][i]) * 2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

m = n
lsn = []
d = 0
while m > 0:
    lsn.append(m%10)
    m //= 10
    d += 1
lsn.reverse()

ans = 0
for i in range(1, n+1):
    ls = []
    while i > 0:
        ls.append(i%10)
        i //= 10
    
    sentou = ls[0]
    matsubi = ls[len(ls)-1]

    if sentou == 0 or matsubi == 0:
        continue

    for j in range(1, d+1):
        if j == 1:
            if sentou == matsubi:
                ans += 1
        elif j < d:
            ans += 10**(j-2)
        else:
            dp = [[0 for k in range(2)] for l in range(d+1)]
            dp[0][0] = 1

            for k in range(d):
                for l in range(2):
                    if k == 0:
                        if l == 1 or sentou < lsn[0]:
                            dp[k+1][1] += dp[k][l]
                        elif sentou <= lsn[0]:
                            dp[k+1][0] += dp[k][l]
                    elif k == d-1:
                        if l == 1 or matsubi < lsn[d-1]:
                            dp[k+1][1] += dp[k][l]
                        elif matsubi <= lsn[d-1]:
                            dp[k+1][0] += dp[k][l]
                    else:
                        lim = lsn[k]
                        if l == 1:
                            lim = 9
                        for o in range(lim+1):
                            if l == 1 or o < lsn[k]:
                                dp[k+1][1] += dp[k][l]
                            elif o <= lsn[k]:
                                dp[k+1][0] += dp[k][l]
            
            ans += dp[d][0]+dp[d][1]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
count=[[0 for _ in range(10)] for _ in range(10)]
for i in range(1,n+1):
    stri=str(i)
    count[int(stri[0])][int(stri[-1])]+=1

for i in range(10):
    for j in range(i,10):
        if(i==j):
            ans+=count[i][j]*count[j][i]
        else:
            ans+=2*(count[i][j]*count[j][i])

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left
from itertools import groupby, permutations, combinations
from fractions import gcd
import numpy as np
from sys import stdin, stderr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def main():
    N = int(input())
    st = dict()
    for i in range(1, N+1):
        str_i = str(i)
        s = int(str_i[-1])
        t = int(str_i[0])
        if s == 0:
            continue
        # if s > t:
        #     s, t = t, s
        num = s if s == t else s + t*10
        if num not in st:
            st[num] = 0
        st[num] += 1
    # print(st)
    ret = 0
    for k in st.keys():
        v = st[k]
        if k < 10:
            ret += v*v
        else:
            kk = (k % 10) * 10 + (k // 10)
            if kk in st:
                ret += v * st[kk]
            # else:
            #     print(k, kk)

    return ret


if __name__ == "__main__":
    print(main())

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve(n):
    tbl = [0] * 100
    for i in range(1, n + 1):
        st = str(i)
        tbl[int(st[0] + st[-1])] += 1

    ans = 0
    for i in range(1, n + 1):
        st = str(i)
        ans += tbl[int(st[-1] + st[0])]
    return ans


n = int(input())
print(solve(n))

Submission status: Accepted

Solution code:
def examA():
    N, M = LI()
    if N==M:
        print("Yes")
    else:
        print("No")
    return

def examB():
    A, B = LI()
    if A>B:
        ans = str(B)*A
    else:
        ans = str(A)*B
    print(ans)
    return

def examC():
    N = I()
    P = LI()
    minP = N+1
    ans = 0
    for i in range(N):
        if minP>P[i]:
            minP=P[i]
            ans +=1
    print(ans)
    return

def examD():
    N = I()
    ans = 0
    keep = [[0]*10 for _ in range(10)]
    for i in range(1,N+1):
        l = int(str(i)[0]); r = int(str(i)[-1])
        keep[l][r]+=1

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

    print(ans)
    return

def examE():
    def gcd(x, y):
        if y == 0:
            return x
        while (y != 0):
            x, y = y, x % y
        return x
    def lcm(x, y):
        return x * y // gcd(x, y)
    N = I()
    A = LI()


    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examD()

"""

"""

Submission status: Accepted

Solution code:
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]


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

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(n):
    x=i+1
    l = x % 10
    f = x
    while f // 10 > 0:
        f = f // 10
#    print("{}: {},{}".format(i,f,l))    
    t[f][l] = t[f][l] + 1

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

print("{}".format(count))