실험실
  • [백준] 16412번: Heir’s Dilemma - 파이썬
    2024년 11월 28일 19시 06분 17초에 업로드 된 글입니다.
    작성자: B1NK
    728x90

    문제

    Your favorite uncle has passed away, leaving you a large estate. Bank account numbers, locations of safe deposit boxes, and GPS coordinates to buried treasures are all locked in an electronic safe in your uncle’s office behind a picture of dogs playing poker. One day he showed you the safe with its 9 digit keypad (digits 1 through 9). He told you he wasn’t worried about anyone breaking into his safe because it’s equipped with a self-destruct mechanism that will destroy the contents if anyone attempts a forced entry.

    입력

    The input is a line with two space-separated integers L and H, where 123 456 ≤ L < H ≤ 987 654

    출력

    Print one integer, the total number of possible combinations to the safe, where each combination c must satisfy the three constraints above, and lie in the range L ≤ c ≤ H.

    코드

    def main():
        import sys
        input = sys.stdin.read
        data = input().strip().split()
    
        a = int(data[0])
        b = int(data[1])
    
        ans = 0
    
        for i in range(a, b + 1):
            tmp = i
            cnt = [0] * 10
    
            while tmp > 0:
                cnt[tmp % 10] += 1
                tmp //= 10
    
            check = True
    
            for j in range(10):
                if cnt[j] >= 2:
                    check = False
    
            if cnt[0] >= 1:
                check = False
    
            if not check:
                continue
    
            for j in range(1, 10):
                if cnt[j] == 0:
                    continue
    
                if i % j != 0:
                    check = False
    
            if check:
                ans += 1
    
        print(ans)
    
    if __name__ == "__main__":
        main()
    
    728x90
    댓글