Sherlock and The Beast

Sherlock and The Beast

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, . Sherlock determines the key to removing the virus is to find the largest Decent Number having digits.

A Decent Number has the following properties:

  1. Its digits can only be 3‘s and/or 5‘s.
  2. The number of 3‘s it contains is divisible by 5.
  3. The number of 5‘s it contains is divisible by 3.
  4. If there are more than one such number, we pick the largest one.

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

Input Format

The first line is an integer, , denoting the number of test cases.

The subsequent lines each contain an integer, , detailing the number of digits in the number.

Output Format

Print the largest Decent Number having 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 , there is no decent number having digit (so we print ).
For , is the only possible number. The number appears three times in this number, so our count of ‘s is evenly divisible by (Decent Number Property 3).
For , is the only possible number. The number appears five times in this number, so our count of ‘s is evenly divisible by (Decent Number Property 2).
For , and all permutations of these digits are valid numbers; among them, the given number is the largest one.

 

<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$t);
for($a0 = 0; $a0 < $t; $a0++){
    fscanf($handle,"%d",$n);
    $max = -1;   
    if ( ( $t >= 1) && ( $t <= 20 )  && ( $n >= 1 ) && ( $n <= 100000 ) ){
        $fives = $n; // we begin from the max number of fives
        $threes = 0;
        while ( $fives >= 0 ){
            if ( $fives % 3 == 0 ){ //we check the division by 3
                if ( $threes % 5 == 0 ){ //we check the division by 3
                    //if YES - we found the solution and for sure is the max
                    $power = $n-1;
                    $max = "";
                    while ( $power >= $threes){
                        $max = $max."5";
                        $power = $power - 1;                                              
                    }
                    while ( $power >= 0){
                        $max = $max."3";                        
                        $power = $power - 1;                                                              
                    } 
                    break; 
                }
            }

           $fives = $fives - 1;
           $threes = $threes + 1;
           if( $fives < 0 ) 
               break;
           if( $threes > $n )
               break;
        }
    }
    echo $max." \n"; // we print the max value
}

?>

Related Articles