Sort by

recency

|

148 Discussions

|

  • + 2 comments
    def solve(n):
        i = 0
        while True:
            i += 1
            x = int(bin(i)[2:]) * 9
            if x % n == 0:
                return str(x)
    
  • + 0 comments
    import java.math.BigInteger;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    
    public class Solution {
        static final int LIMIT = 500;
        static final int[] DIGITS = { 0, 9 };
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            int T = sc.nextInt();
            for (int tc = 0; tc < T; tc++) {
                int N = sc.nextInt();
                System.out.println(solve(N));
            }
    
            sc.close();
        }
    
        static BigInteger solve(int N) {
            boolean[] visited = new boolean[N];
    
            Queue<BigInteger> queue = new LinkedList<BigInteger>();
            queue.offer(BigInteger.valueOf(DIGITS[0]));
            while (true) {
                BigInteger head = queue.poll();
    
                for (int digit : DIGITS) {
                    if (head.equals(BigInteger.ZERO) && digit == 0) {
                        continue;
                    }
    
                    BigInteger next = head.multiply(BigInteger.TEN).add(BigInteger.valueOf(digit));
    
                    int remainder = next.mod(BigInteger.valueOf(N)).intValue();
                    if (remainder == 0) {
                        return next;
                    } else if (!visited[remainder]) {
                        visited[remainder] = true;
                        queue.offer(next);
                    }
                }
            }
        }
    }
    
  • + 0 comments
    def solve(n):
        queue = deque(['9'])
        visited = set()
        
        while queue:
            current = queue.popleft()
            
            current_num = int(current)
            
            if current_num % n == 0:
                return current
                
            next_num1 = current + '0'
            next_num2 = current + '9'
            
            if next_num1 not in visited:
                queue.append(next_num1)
                visited.add(next_num1)
                
            if next_num2 not in visited:
                queue.append(next_num2)
                visited.add(next_num2)
    
  • + 0 comments

    The multiple sought belongs to the list: {9x1, 9x10, 9x11, 9x100, 9x101, ...} We notice that {1, 10, 11, 100, 101, ...} are the binary writings of the numbers {1, 2, 3, 4, 5, ...}

    def solve(n):
        i = 1
        res = 9*int(bin(i)[2:])
        while res % n:
            i = i + 1
            res = 9*int(bin(i)[2:])
        return str(res)
    
  • + 0 comments

    There is an error with fptr.write(result + '\n'). Change it to fptr.write(str(result) + '\n'). The correct code is below.

        def solve(n):
            if 9 % n == 0:
                    print(9)
                    return 9
    
            curr_str = ['9']
            next_str = []
            found = False
    
            while not found:
                for curr in curr_str:
                        a = curr + '0'
                        b = curr + '9'
    
                        if int(a) % n == 0:
                                print(int(a))
                                found = True
                                return int(a)
    
                        if int(b) % n == 0:
                                print(int(b))
                                found = True
                                return int(b)
    
                        next_str.append(str(a))
                        next_str.append(str(b))
    
            curr_str = next_str
            next_str = []
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())
    
    for t_itr in range(t):
        n = int(input().strip())
    
        result = solve(n)
        # fptr.write(result + '\n')
        # add str(result) because otherwise it does not work
        fptr.write(str(result) + '\n')
    
    fptr.close()