Problem:


Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

1 \leq N \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008



Sample input:
25


Sample output:
17

Submission status: Wrong Answer

Solution code:
import math

def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

N = int(input())
sN = str(N)
l = len(sN)

ans = 0
for n in range(1, min(10, N+1)):
    sn = str(n)
    ans += 1
    
    cnt = 1
    for i in range(1, l-1):
        cnt *= 10
        ans += cnt

    if l == 2:
        if sN >= sn + sn:
            ans += 1
    elif l > 2 and sN[0] >= sn[-1]:
        hoge = int(sN[1:-1])
        if sN[-1] < sn[0] and sN[0] == sn[-1]:
            hoge -= 1
        ans += hoge + 1

for n in range(11, N+1):
    sn = str(n)
    if sn[-1] != "0":
        if n > 10 and sn[0] == sn[-1]:
            ans += 1


        cnt = 1
        for i in range(2, l-1):
            ans += cnt
            cnt *= 10

        if l == 2:
            if sN >= sn[-1] + sn[0]:
                ans += 1
        elif l > 2 and sN[0] >= sn[-1]:
            hoge = int(sN[1:-1])
            if sN[-1] < sn[0] and sN[0] == sn[-1]:
                hoge -= 1
            ans += hoge + 1


print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())

def pair(x):
    a=x%10
    b=int(str(x)[0])
    return (a,b)
dic={}
for i in range(1,n):
    p=pair(i)
    if(dic.get(p)==None):dic[p]=1
    else:dic[p]+=1
ans=0
for i in range(1,n):
    a,b=pair(i)
    p=(b,a)
    if(not dic.get(p)==None):ans+=dic[p]
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(s)
keta = len(s)
cnt = [[0] * (10) for _ in range(10)]
for i in range(1, 10):
    for j in range(1, 10):
        for k in range(1, 6):
            if k == 1:
                if i == j:
                    num = 1
                else:
                    continue
            else:
                num = i * (10 ** (k - 1)) + j
            if num <= n:
                cnt[i][j] += 1
            if num == 10:
                print("10", i, j, k)
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += cnt[i][j] * cnt[j][i]
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n = int(raw_input())
def g( a, b , n):
	count = 0
	## CASE 1
	if a == b and a <=n : 
		count +=1
	## CASE 2
	if a *10 + b <= n:
		count +=1
	
	if len(str(n)) <=2:
		return count

	## CASE 3
	s = str(n)
	if len(s) - 3 >= 1:
		count += 10 ** (len(s) - 3)

	## CASE 4

	s = str(n)
	if a == int(s[0]):
		m = s[1:-1]

		if m != '':

			#0,m-1
			count += int(m)
			if b <= int(s[-1]):
				count +=1
	elif a < int(s[0]):
		count += 10 ** (len(s) - 2)

	return count


def f(a,b, n):
	return g(a,b,n) * g(b,a,n)

s= 0
for a in range(1,10):
	for b in range(1,10):
		s += f(a,b,n)

print s

Submission status: Wrong Answer

Solution code:
n=int(input())
count=[[0]*9 for _ in range(9)]
for i in range(1,n+1):
  if i%10>0:
    i=str(i)
    if int(i[::-1])<=n:  
      x,y=int(i[0])-1,int(i[-1])-1
      count[x][y]+=1
ans=0
for i in count:
  for j in i:
    ans+=j**2
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [[0]*10 for _ in range(10)]
for i in range(N):
  a = i%10 #1の位
  b = i // (10**(len(str(i))-1))
  if a!= 0:
    A[a][b] += 1
t = 0
for i in range(1,10):
  s = A[i][i]
  if s != 0:
    t += int(s + (s*(s-1)))
for i in range(1,9):
  for j in range(i+1,10):
    t += A[i][j] * A[j][i]*2
print(t)

Submission status: Wrong Answer

Solution code:
def main():
    n = input()
    m = len(n)
    ans = 0
    x = int(n[0])
    for i in range(1, m + 1):
        c = 10 ** max(0, i - 2)
        for j in range(1, m + 1):
            d = 10 ** max(0, j - 2)
            if i == 1 or j == 1:
                if max(i, j) == m:
                    e = x
                else:
                    e = 9
            else:
                if i == m or j == m:
                    e = x ** 2
                else:
                    e = 9 ** 2
            ans += c * d * e

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = input()
    m = len(n)
    ans = 0
    x = min(int(n[0]), int(n[-1]))
    for i in range(1, m + 1):
        c = 10 ** max(0, i - 2)
        for j in range(i, 0, -1):
            d = 10 ** max(0, j - 2)
            e = 9 ** 2
            if j == 1 and i == m:
                e = x
            elif j == 1:
                e = 9
            elif i == m:
                e = x ** 2

            if i == j:
                ans += c * d * e
            else:
                ans += c * d * e * 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())

l = np.zeros((9,9), dtype=np.int)
# l[i][j] : i = 頭文字　j = 最後の文字

for k in range(N):
    x = str(k+1)
    i = int(x[0]) - 1
    j = int(x[-1]) - 1

    l[i][j] += 1

ans = 0
for i in range(9):
    for j in range(i,9):
        if i == j:
            n = l[i][j]
            ans += n**2
        else:
            ans += l[i][j] * l[j][i] * 2
            

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N = int(input())
ans = 0
c = defaultdict(int)

for i in range(1, N+1):
  s = int(str(i)[0])
  t = int(str(i)[-1])
  if s != 0 and t != 0:
    c[(s, t)] += 1

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

Submission status: Wrong Answer

Solution code:
import itertools

N = input()
if len(N)==1:
    print(N)
    exit()


gen = (1,2,3,4,5,6,7,8,9)
A = list(itertools.product(gen,gen))
count =0

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

for a,b in A:
    tmp = 0
    if a < int(N[0]):
        for i in range(0,len(N)-1,1):
            tmp+= pow(10,i)
    elif a > int(N[0]):
        for i in range(0,len(N)-2,1):
            tmp+= pow(10,i)
    else:
        tmp +=1
        for i in range(1,len(N)-2,1):
            tmp+= pow(10,i)
        if len(N) > 2:
            tmp+=int(N[1:len(N)-1])+1
            if int(N[len(N)-1]) < b:
                tmp-=1
    if a==b:
        tmp+=1
    print(a,b,tmp)
    mem[a][b]=tmp

for a,b in A:
    count+=mem[a][b]*mem[b][a]

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

N_s = int(str(N)[0])
N_e = int(str(N)[-1])
if len(str(N)) > 2:
    N_mid = int(str(N)[1:-1])
else:
    N_mid = 0

def search(s, e):
    ans = 0
    for i in range(1, len(str(N))+1):
        if i == 1:
            if s == e and s <= N:
                ans += 1
                continue
        else:
            if i != len(str(N)):
                ans += 10**(i-2)
            else:
                if N_s > s:
                    ans += 10**(i-2)
                elif N_s == s:
                    if N_e >= e:
                        ans += N_mid + 1
                    else:
                        ans += N_mid

    return ans

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

Submission status: Wrong Answer

Solution code:
N = int(input())

N_s = int(str(N)[0])
N_e = int(str(N)[-1])
if len(str(N)) > 2:
    N_mid = int(str(N)[1:-1])
else:
    N_mid = 0

def search(s, e):
    ans = 0
    for i in range(1, len(str(N))+1):
        if i == 1:
            if s == e and s <= N:
                ans += 1
                continue
        else:
            if i != len(str(N)):
                ans += 10**(i-2)
            else:
                if N_s > s:
                    ans += 10**(i-2)
                elif N_s == s:
                    if N_e > e:
                        ans += N_mid + 1
                    else:
                        ans += N_mid

    return ans

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

Submission status: Wrong Answer

Solution code:
N = int(input())

N_se = int(str(N)[0] + str(N)[-1])
if len(str(N)) > 2:
    N_mid = int(str(N)[1:-1])
else:
    N_mid = 0

def search(s, e):
    ans = 0
    se = int(str(s) + str(e))
    for i in range(1, len(str(N))+1):
        if i == 1:
            if s == e and s <= N:
                ans += 1
                continue
        else:
            if i != len(str(N)):
                ans += 10**(i-2)
            else:
                if N_se > se:
                    ans += 10**(i-2)
                elif N_se == se:
                    ans += N_mid

    return ans

ret = 0
for i in range(1, 10):
    for j in range(1, 10):
        # print(i, j, search(i, j), search(j, i))
        ret += search(i, j) *  search(j, i)
print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())

N_se = int(str(N)[0] + str(N)[-1])
if len(str(N)) > 2:
    N_mid = int(str(N)[1:-1])
else:
    N_mid = 0

def search(s, e):
    ans = 0
    se = int(str(s) + str(e))
    for i in range(1, len(str(N))+1):
        if i == 1:
            if s == e and s <= N:
                ans += 1
                continue
        else:
            if i != len(str(N)):
                ans += 10**(i-2)
            else:
                if N_se > se:
                    ans += 10**(i-2)
                elif N_se == se:
                    ans += N_mid

    return ans

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N<=10:
    print(N)
else:
    print(ans-1)

Submission status: Wrong Answer

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

if N==1:
    print(ans)
else:
    print(ans-1)

Submission status: Wrong Answer

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

for n in range(N):
    head=int(str(n)[0])
    tail=n%10
    if head!=0 and tail!=0:
        A[head-1][tail-1]+=1

count=0

for i in range(9):
    for j in range(9):
        count+=A[i][j]*A[j][i]

print(count)

Submission status: Wrong Answer

Solution code:
def input_multiple_number():
    return map(int, input().split())

N = int(input())


memo = [[-1]*10 for i in range(10)]

def getHead(n):
    while n >= 10:
        n //= 10
    return n
def getTail(n):
    return n%10

def getCount(head,tail):
    if memo[head][tail] != -1:
        return memo[head][tail]

    cnt = 0
    for i in range(N):
        if getHead(i) == head and getTail(i) == tail:
            cnt += 1
    memo[head][tail] = cnt
    return cnt




    return cnt
def getHead(n):
    while n >= 10:
        n //= 10
    return n
def getTail(n):
    return n%10

ans = 0
for i in range(1,N+1):
    ans += getCount(getTail(i),getHead(i))
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = [[0 for i in range(10)] for i in range(10)]
ans = 0

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = [0] * 100
for i in range(11, 100):
  msb = i // 10
  lsb = i % 10
  if lsb == 0:
    continue
  for j in range(1, n+1):
    if j % 10 == lsb and int(str(j)[0]) == msb:
      t[i] += 1

ans = 0
for i in range(11, 100):
  msb = i // 10
  lsb = i % 10
  if lsb == 0:
    continue
  if msb == lsb:
    ans += t[i] * (t[i] - 1) // 2
  else:
    ans += t[i] * t[lsb * 10 + msb]
print(ans)

Submission status: Wrong Answer

Solution code:
# 掛け算で求める，までは正しかった
# 式がよく分からない
# https://scrapbox.io/pocala-kyopro/D_-_Handstand_2 この考え方もやった

n = int(input())

if n <= 9:
    print(n)
    exit()

# cij = #{1 <= k <= N | kの先頭の桁の数はi，末尾の桁の数はjに等しい}
# ans = \sum_{i=0}^9 \sum_{j=0}^9 cij * cji

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

for k in range(1, n):
    k0 = int(str(k)[0])
    kn = int(str(k)[-1])
    if k0 != 0 and kn != 0:
        cij[k0][kn] += 1

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

print(ans)

Submission status: Wrong Answer

Solution code:
# 掛け算で求める，までは正しかった
# 式がよく分からない

n = int(input())

if n <= 9:
    print(n)
    exit()

# cij = #{1 <= k <= N | kの先頭の桁の数はi，末尾の桁の数はjに等しい}
# ans = \sum_{i=0}^9 \sum_{j=0}^9 cij * cji

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

for k in range(1, n):
    k0 = int(str(k)[0])
    kn = int(str(k)[-1])
    cij[k0][kn] += 1

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

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
K=[[0 for i in range(9)] for j in range(9)]

for i in range(1,N+1):
  if int(str(i)[-1])==0:
    pass
  else:
    h=int(str(i)[0])
    t=int(str(i)[-1])
    K[h-1][t-1] += 1
  
ans=0
for i in range(9):
  for j in range(i+1):
    if i!=j:
      ans += 2*K[i][j]* K[j][i]
      print(i+1,j+1)
    else:
      ans += K[i][j]**2
    #print(i,j,K[i][j],K[j][i],K[i][j]*K[j][i])
      
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = 1
#if N>=10:
    #k=9
#else:
    #k=N
k=0
c=[]
while(a<=N):
    c=list(str(a))
    for i in range(1,len(str(N))+1):
        if (int(c[-1])*(10**i)+int(c[0]))<=N and int(c[-1]) != 0:
            #print(a,int(c[-1])*(10**i)+int(c[0]))
            k+=1
    if int(c[-1])==int(c[0]):
        #print(a,int(c[0]),int(c[-1]))
        k+=1
    a+=1
print(k)

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


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

    head_N = str(N)[0]
    res = 0
    for a in range(1, N + 1):
        str_a = str(a)
        head_a = str_a[0]
        tail_a = str_a[-1]

        if tail_a == '0':
            continue

        # 1桁（必ずN以下になる）
        if head_a == tail_a:
            res += 1

        # 2桁
        if int(str(tail_a + head_a)) <= N:
            res += 1

        if len(str(N)) <= 2:
            continue

        # 3桁以上
        keta = 3
        while keta < len(str(N)):
            res += 10 ** (keta - 2)
            keta += 1

        # bの先頭がNの先頭より小さい場合
        if tail_a < head_N:
            res += 10 ** (keta - 2)
            continue

        last = ['0' for _ in range(keta)]
        last[0] = tail_a
        last[-1] = head_a
        last = int(''.join(last))
        if last <= N:
            print(last)
            res += int(str(N)[1:-1]) + 1

    print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import math
N = input()
ans = 0
def counths(n,i,j):
    s = int(n)
    bot = sum([10**x for x in range(len(n)-2)])
    if int(n[0]) > i:
        top = 10**(len(n)-2)
    elif int(n[0]) < i:
        top = 0
    else:
        a = i * 10 ** (len(n)-1) + j
        if a < s:
            top = math.ceil((s-a)/10)
        elif a == s:
            top = 1
        else:
            top = 0
    if i == j:
        if len(n) > 1:
            bot += 1
        else:
            if s >= i:
                bot += 1
    return bot + top
for i in range(1,10):
    for j in range(1,10):
        ans += counths(N,i,j) * counths(N,j,i)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
N=int(input())
#A=list(map(int,input().split()))
digit=int(math.log(N,10))+1 
def c_ij(i,j):
  count=0
  if i==0:
    return (0)
  if N<=9:
    if i==j<=N:
      return (1)   
  elif digit == 2:
    if i*10+j<=N:
      count +=1
    if i == j:
      count +=1
  else:
    for a in range(2,digit):
      count += 10**(a-2)
    if i == j:
      count +=1
    if int(str(N)[0])>i:
      count += 10**(digit-2)
    else:  
      calc=N-int(str(i)+'0'*(digit-2)+str(j))
      if calc > 0:
        count += sum([(int(str(calc)[-i-1])+1)*10**(i-1) for i in range(1,min(digit-1, int(math.log(calc,10))+1 ))])
  return (count)


ans=0
for a in range(10):
  for b in range(10):
    ans += c_ij(a,b)*c_ij(b,a)
    #print(a,b,c_ij(a,b),c_ij(b,a))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N=int(input())
#A=list(map(int,input().split()))
digit=int(math.log(N,10))+1 
def c_ij(i,j):
  count=0
  if i==0:
    return (0)
  if N<=9:
    if i==j:
      return (1)   
  elif digit == 2:
    if i*10+j<=N:
      count +=1
    if i == j:
      count +=1
  else:
    for a in range(2,digit):
      count += 10**(a-2)
    if i == j:
      count +=1
    if int(str(N)[0])>i:
      count += 10**(digit-2)
    else:  
      calc=N-int(str(i)+'0'*(digit-2)+str(j))
      if calc > 0:
        count += sum([(int(str(calc)[-i-1])+1)*10**(i-1) for i in range(1,min(digit-1, int(math.log(calc,10))+1 ))])
  return (count)


ans=0
for a in range(10):
  for b in range(10):
    ans += c_ij(a,b)*c_ij(b,a)
    #print(a,b,c_ij(a,b),c_ij(b,a))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N=int(input())
#A=list(map(int,input().split()))
digit=int(math.log(N,10))+1 
def c_ij(i,j):
  count=0
  if i==0:
    return (0)
  if N<=9:
    if i==j:
      return (1)   
  elif digit == 2:
    if i*10+j<=N:
      count +=1
    if i == j:
      count +=1
  else:
    for a in range(2,digit):
      count += 10**(a-2)
    if i == j:
      count +=1
    if int(str(N)[0])>i:
      count += 10**(digit-2)
    else:  
      calc=N-int(str(i)+'0'*(digit-2)+str(j))
      if calc > 0:
        count += sum([(int(str(calc)[-i-1])+1)*10**(i-1) for i in range(1,min(digit-1, int(math.log(calc,10))+1 ))])
  return (count)

ans=0
for a in range(10):
  for b in range(10):
    ans += c_ij(a,b)*c_ij(b,a)
    print(a,b,c_ij(a,b),c_ij(b,a))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N=int(input())
#A=list(map(int,input().split()))
digit=int(math.log(N,10))+1 
def c_ij(i,j):
  count=0
  if i==0:
    return (0)
  if N<=9:
    if i==j:
      return (1)   
  elif digit == 2:
    if i*10+j<=N:
      count +=1
    if i == j:
      count +=1
  else:
    for a in range(2,digit):
      count += 10**(a-2)
    if i == j:
      count +=1
    if int(str(N)[0])>i:
      count += 10**(digit-2)
    else:  
      calc=N-int(str(i)+'0'*(digit-2)+str(j))
      if calc > 0:
        count += sum([(int(str(calc)[-i-1])+1)*10**(i-1) for i in range(1,min(digit-1, int(math.log(calc,10))+1 ))])
  return (count)

print(c_ij(1,1))

ans=0
for a in range(10):
  for b in range(10):
    ans += c_ij(a,b)*c_ij(b,a)
   # print(a,b,c_ij(a,b),c_ij(b,a))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N=int(input())
#A=list(map(int,input().split()))
digit=int(math.log(N,10))+1 
def c_ij(i,j):
  count=0
  if i==0:
    return (0)
  if N<=9:
    if i==j:
      return (1)   
  elif digit == 2:
    if i*10+j<=N:
      count +=1
    if i == j:
      count +=1
  else:
    for a in range(2,digit):
      count += 10**(a-2)
    if i == j:
      count +=1
    if int(str(N)[0])>i:
      count += 10**(digit-2)
    else:  
      calc=N-int(str(i)+'0'*(digit-2)+str(j))
      if calc > 0:
        count += sum([(int(str(calc)[-i-1])+1)*10**(i-1) for i in range(1,min(digit-1, int(math.log(calc,10))+1 ))])
  return (count)

print(c_ij(1,1))

ans=0
for a in range(10):
  for b in range(10):
    ans += c_ij(a,b)*c_ij(b,a)
    print(a,b,c_ij(a,b),c_ij(b,a))
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
#A=list(map(int,input().split()))

def c_ij(i,j):
  count=0
  for k in range(N):
    if k % 10 == j and int(str(k)[0]) == i:
      count += 1
  return(count)

ans=0
for a in range(1,10):
  for b in range(1,10):
    ans += c_ij(a,b)*c_ij(b,a)
print(ans)

Submission status: Wrong Answer

Solution code:
def main(n):
    c=[[0]*10 for _ in range(10)]
    for k in range(1,n+1):
        n=str(k)
        i = int(n[0])
        j = int(n[-1])
        c[i][j] += 1
    ans =0
    for i in range(10):
        ans+=sum(c[i])
    print(ans)
             
if __name__=='__main__':
    main(int(input()))

Submission status: Wrong Answer

Solution code:

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

  s_N = str(N)

  K = len(s_N)

  N0, Nn = s_N[0], s_N[-1]

  cnt_all= 0

  for a in range(1,N+1):
    #print(a)
    s_a = str(a)
    K_a = len(s_a)
    b0, bn = s_a[-1], s_a[0]

    cnt = 0

    if b0=="0":
      continue

    if b0==bn:
      cnt += 1
    
    if K>1:
      if int(b0+bn)<N+1:
        cnt += 1

    if K>2:
      for k in range(1, K-2):
        cnt += 10**k

      if int(N0)>int(b0):
        cnt += 10**(K-2)
      elif int(N0)==int(b0):
        if int(Nn)<int(bn):
          cnt-=1
        tmp = 1
        for j in range(1, K-1):
          tmp *= (int(s_N[j])+1)
        cnt += tmp

    cnt_all += cnt
  print(cnt_all)
main()

Submission status: Wrong Answer

Solution code:
n = int(input())
X = ['']
for i in range(1, 6):
    for j in range(10**i):
        X.append(str(j).zfill(i))
cnt = 0
for a in range(1, n+1):
    x = str(a)[0]
    y = str(a)[-1]
    if x == y:
        cnt += 1
    if y == '0':
        continue
    l, r = 0, len(X)
    while r-l > 1:
        c = (l+r)//2
        if int(y+X[c]+x) <= n:
            l = c
        else:
            r = c
    cnt += r
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())


def calc(num):
    s = list(str(num))[::-1]
    s2 = int(s[-1])
    s1 = int(s[0])

    keta = 0
    while True:
        num = num // 10
        keta += 1
        if num == 0:
            break

    if s1 == s2:
        if keta == 1:
            return 1
        elif keta == 2:
            return 1 + 2
        else:
            return (int(''.join(s[1:-1][::-1]))+1) * 2 - 1 + 3

    elif s1 > s2:
        if keta == 2:
            return 0
        else:
            ans = 0
            for i in range(keta - 2):
                ans += 10 ** i
            return ans * 2
    else:
        if s1 == 0:
            return 0
        else:
            ans = 0
            for i in range(keta - 1):
                ans += 10 ** i
            return ans * 2


# print(10, calc(10))

dp = [0] * (N+1)

for i in range(1, N+1):
    dp[i] = dp[i-1] + calc(i)

# print(dp)
print(dp[N])

Submission status: Wrong Answer

Solution code:

N = int(input())


def calc(num):
    s = list(str(num))[::-1]
    s2 = int(s[-1])
    s1 = int(s[0])

    keta = 0
    while True:
        num = num // 10
        keta += 1
        if num == 0:
            break

    if s1 == s2:
        if keta == 1:
            return 1
        elif keta == 2:
            return 1 + 2
        else:
            return int(''.join(s[1:-1][::-1])) * 2 - 1 + 3

    elif s1 > s2:
        if keta == 2:
            return 0
        else:
            ans = 0
            for i in range(keta - 2):
                ans += 10 ** i
            return ans * 2
    else:
        if s1 == 0:
            return 0
        else:
            ans = 0
            for i in range(keta - 1):
                ans += 10 ** i
            return ans * 2


# print(10, calc(10))

dp = [0] * (N+1)

for i in range(1, N+1):
    dp[i] = dp[i-1] + calc(i)

# print(dp)
print(dp[N])

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[[0]*9 for i in range(9)]
sum=0
for i in range(N):
  s=str(i)
  if(int(s[0])!=0 and int(s[-1])!=0):
    A[int(s[0])-1][int(s[-1])-1]+=1
for i in range(9):
  for j in range(9):
    sum+=A[i][j]*A[j][i]
  #print(A[i][i])
#print(A)  
print(sum)

Submission status: Wrong Answer

Solution code:
n=int(input())
res=0
M=[[0 for _ in range(10)] for _ in range(10)]
for i in range(n):
  a=i%10
  b=int(str(i)[0])
  M[a][b]+=1
for i in range(1,10):
  for j in range(1,10):
    res+=M[i][j]*M[j][i]
    
print(res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def main():
    N = int(input())
    combs = [(S[0], S[-1]) for S in map(str, range(1, N+1))]
    d = defaultdict(int)
    for pre, suf in combs:
        if suf != "0":
            a, b = sorted([pre, suf])
            d[(a, b)] += 1
            
    ans = 0
    for key, val in d.items():
        if key[0] == key[1]:
            ans += val ** 2
        elif val > 1:
            ans += 2
        
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
ans_lis = [[0] * 9 for  i in range(9)]
for i in range(1,N+1):
  t = str(i)
  mae = int(t[0])
  ushiro = int(t[-1])
  ans_lis[mae-1][ushiro-1] += 1
ans = 0
for i in range(9):
  for j in range(9):
    ans += ans_lis[i][j] * ans_lis[j][i]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

n = 1

M = N
while M//10 != 0:
  M //= 10
  n += 1
#nはNの桁数

c = N//(10**(n-1)) #cはNの最高位
d = N&10           #dはNの最低位

def f(a,b):#N以下の最高位a、最低位bの正整数の個数
  if a < c:
    if a == b:
      return ((10**(n-1))//9)+1
    else:
      return ((10**(n-1))//9)
  elif a > c:
    if a == b:
      if n == 1:
        return 0
      else:
        return ((10**(n-2))//9)+1
    else:
      if n == 1:
        return 0
      else:
        return ((10**(n-2))//9)
  else:
    if a == b:
      if n == 1:
        return 1
      elif n == 2:
        if b <= d:
          return 2
        else:
          return 1
      else:
        if b <= d:
          return 2 + (10**(n-3))//9 + (N-c*(10**(n-1)))//10
        else:
          return 1 + (10**(n-3))//9 + (N-c*(10**(n-1)))//10
    else:
      if n == 1:
        return 0
      elif n == 2:
        if b <= d:
          return 1
        else:
          return 0
      else:
        if b <= d:
          return 1 + (10**(n-3))//9 + (N-c*(10**(n-1)))//10
        else:
          return 0 + (10**(n-3))//9 + (N-c*(10**(n-1)))//10
          
ans = 0
for i in range(1,10):
  for j in range(1,10):
    ans += f(i,j)*f(j,i)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
count = 0
work1 = [0,1,10,10**2,10**3,10**4,10**5]
work2 = [0,1,1,10,10**2,10**3,10**4]
for i in range(1, n+1):
    if i//10 == 0:
        count += 1
    elif int(str(i)[0]) > int(str(i)[-1]) and str(i)[-1] != '0':
        temp = len(str(i))
        count += sum(work1[:temp])*2
    elif str(i)[0] == str(i)[-1]:
        temp = len(str(i))
        count += sum(work2[:temp])*2+1
        if temp > 2:
            count += (int(str(i)[1:-1]))*2
print(count)

Submission status: Wrong Answer

Solution code:
n = 20000
dict_par = dict()
for i in range(1,10):
    i = str(i)
    for k in range(1,10):
        k = str(k)
        dict_par[i+k] = 0
        for num in range(1, n+1):
            num = str(num)
            if (num[0] == i) & (num[-1] ==k):
                dict_par[i+k] +=1
ans = 0
for i in range(1,10):
    i = str(i)
    for k in range(1,10):
        k = str(k)
        ans+= dict_par[i+k] * dict_par[k+i]
print(ans)

Submission status: Wrong Answer

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

if l==1:
    print(n)
    exit()
cnt = 1
if l>2:
    cnt = int(s[1:l-1])

temp = [[0]*10 for i in range(10)] ##temp[i][j]::一桁目i,最期の桁jの数
for i in range(1,10):
    for j in range(1,10):
        if (i==j ):
            temp[i][j]+=1 ##k==1
        for k in range(2,l):##2 ~ l-1
            temp[i][j]+=10**(k-2) ##000 ~999
        ##k== l
        if i<a :
            temp[i][j]+=10**(l-2)
        if i==a:
            temp[i][j]+=cnt


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

Submission status: Wrong Answer

Solution code:
N = int(input()) + 1

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input()) + 1

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans_list = [[0 for i in range(10)] for i in range(10)]

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans_list = [[0 for i in range(10)] for i in range(10)]

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

c = [[0 for i in range(10)] for j in range(10)]
for i in range(N):
    str_i = str(i)
    c[int(str_i[0])][int(str_i[-1])] += 1

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

print(ans)