Sherlock and The Beast

  • + 80 comments

    You can solve this easily using a bit of math:

    y=int(raw_input()) z=y while(z%3!=0): z-=5 if(z<0): print '-1' else:
    print z*'5'+(y-z)*'3'

    If the number(say 66317) is not divisible by 3, it will leave a modulo of either 0,1 or 2. If I decrease the number by 5, I am basically making it a multiple of 3, and the remaning digits will be a multiple of 5 as I am subtracting it from the number.

    modulo 0 implies number divisibile modulo 1 implies 5 needs to be subtracted twice. modulo 2 implies 5 needs to be subtracted once.

    Correct me if I am wrong. Completed all the test cases in 0-0.01s