AMR10D - Soccer Teams
My kid's favorite subject is math, as you know by now. He is learning division now, and his teacher has taught him about even numbers being divisible by 2, numbers whose digits add up to a multiple of 3 being exactly divisible by 3 etc.
He was familiar with division by 11 during selection for soccer teams on his playground, and was wondering whether there was any easy rule to see if a number was divisible by 11. For example, he wondered, if he arranged a number of digits 0-9 in a row to form a number, which ones would be divisible by 11?
He decided to start off with d 1's, d 2's ..., d 9's, and seeing what is the minimum factor of 11 that he could get by using all these digits, together with any number of 0's. Please help him figure out how many digits there are in this minimum factor. If he will not be able to form a multiple of 11 in this way, print -1.
The first line will contain the number of test cases T. T lines follow one corresponding to each test case.
Each line has 9 integers d d .... d.
Output T lines one corresponding to each test case. The ith line should contain the required answer for the corresponding test case.
1 <= T <= 100
1 <= d + ... + d <= 100
2 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 1
For the first case, the number 11 can be formed which has 2 digits.
For the second case, number 209 can be formed which is divisible by 11 and has 3 digits.
Worst case complexity of my solution is at most O(n^2*logn) maybe even a little bit better.
Last edit: 2015-09-09 08:27:57
awesome question ! :)