Sherlock and The Beast

  • + 0 comments

    python

    def decomposeNumberBy3and5(n):
        """If possible, get {3: a, 5: b}, such that n = a*3 + b*5.
        3 has the priority."""
        if n in (1, 2, 4, 7):
            return None
    
        match n % 3:
            case 0:
                return {3: n // 3, 5: 0}
            case 1:
                return {3: n // 3 - 3, 5: 2}  # 1 = 5 * 2 - 3 * 3
            case 2:
                return {3: n // 3 - 1, 5: 1}  # 2 = 5 * 1 - 3 * 1
    
    
    def decentNumber(n):
        if (decomp := decomposeNumberBy3and5(n)) is None:
            print(-1)
        else:
            print("5" * decomp[3]*3 + "3" * decomp[5]*5)