BOJ - 5425 - 자리합

Updated:

import sys

def SUM(number):
    if number <= 0: return 0
    arr = [0 for _ in range(10)]
    num = 1
    ret = 0

    while number > 0:
        div = number // (num * 10)
        mod = number % (num * 10)

        for i in range(10):
            arr[i] += div * num
        for i in range(1, (mod // num) + 1):
            arr[i] += num

        arr[(mod // num + 1) % 10] += mod % num

        number -= 9 * num
        num *= 10

    for i in range(1, 10):
        ret += arr[i] * i
    return ret

def solution():
    T = int(sys.stdin.readline())
    for _ in range(T):
        A, B = map(int, sys.stdin.readline().split())
        print(SUM(B)-SUM(A-1))
solution()

https://www.acmicpc.net/problem/5425

Categories:

Updated:

Leave a comment