본문 바로가기
Problem Solving/Baekjoon

[백준] 11322 Numbers are Easy - Graph Theory / Java

by graycode 2023. 11. 16.

 문제 링크

 

11322번: Numbers are Easy

For each test case, output the smallest positive number X such that X is divisible by N and it contains only digits ’0’ and ’1’. Given the constraints, it is guaranteed that there always is a solution (for any positive N) and, in this problem, it w

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        int t = read(), n;
        while (t-- > 0) sb.append((n = read()) == 1 ? 1 : bfs(n)).append("\n");

        bw.write(sb.toString());
        bw.flush();
    }

    private static long bfs(int n) {
        Queue<Long> q = new ArrayDeque<>();
        q.offer(1L);

        while (!q.isEmpty()) {
            long x = q.poll() * 10;

            if (x % n == 0) return x;
            q.offer(x);

            if (++x % n == 0) return x;
            q.offer(x);
        }

        return -1;
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글