-
Notifications
You must be signed in to change notification settings - Fork 7
/
sherlock_and_beast.py
54 lines (53 loc) · 1.93 KB
/
sherlock_and_beast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Problem Statement
Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again plotting something diabolical. Sherlock's companion, Dr. Watson, suggests Moriarty may be responsible for MI6's recent issues with their supercomputer, The Beast.
Shortly after resolving to investigate, Sherlock receives a note from Moriarty boasting about infecting The Beast with a virus; however, he also gives him a clue—a number, N. Sherlock determines the key to removing the virus is to find the largest Decent Number having N digits.
A Decent Number has the following properties:
Its digits can only be 3's and/or 5's.
The number of 3's it contains is divisible by 5.
The number of 5's it contains is divisible by 3.
Moriarty's virus shows a clock counting down to The Beast's destruction, and time is running out fast. Your task is to help Sherlock find the key before The Beast is destroyed!
Constraints
1≤T≤20
1≤N≤100000
Input Format
The first line is an integer, T, denoting the number of test cases.
The T subsequent lines each contain an integer, N, detailing the number of digits in the number.
Output Format
Print the largest Decent Number having N digits; if no such number exists, tell Sherlock by printing -1.
Sample Input
4
1
3
5
11
Sample Output
-1
555
33333
55555533333
Explanation
For N=1, there is no such number.
For N=3, 555 is the only possible number.
For N=5, 33333 is the only possible number.
For N=11, 55555533333 and all permutations of these digits are valid numbers; among them, the given number is the largest one.
"""
T = int(input())
for i in range(T):
k = int(input())
n5 = k
n3 = 0
done = False
while n5 >= 0:
if n5%3 == 0 and n3%5 == 0:
for j in range(n5):
print('5',end='')
for j in range(n3):
print('3',end='')
print()
done = True
break
n5 -= 1
n3 += 1
if not done:
print(-1)