Submission #2530847


Source Code Expand

import java.io.IOException;
import java.io.InputStream;
import java.util.Deque;
import java.util.LinkedList;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int k = sc.nextInt();

		Deque<Pair> deque = new LinkedList<>();
		boolean[] visited = new boolean[k];
		deque.offer(new Pair(1, 1));
		while (!deque.isEmpty()) {
			Pair p = deque.poll();
			if (p.node == 0) {
				System.out.println(p.sumDigits);
				return;
			}
			visited[p.node] = true;
			int add1 = (p.node + 1) % k;
			int multi10 = (p.node * 10) % k;
			if (!visited[add1]) {
				deque.offerLast(new Pair(add1, p.sumDigits + 1));
			}
			if (!visited[multi10]) {
				deque.offerFirst(new Pair(multi10, p.sumDigits));
			}
		}
	}

	public static class Pair {
		int node;
		int sumDigits;

		Pair(int node, int sumDigits) {
			this.node = node;
			this.sumDigits = sumDigits;
		}
	}
}

class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}

	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}

	private void skipUnprintable() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
	}

	public boolean hasNext() {
		skipUnprintable();
		return hasNextByte();
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}

	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}

}

Submission Info

Submission Time
Task D - Small Multiple
User mochimochi
Language Java8 (OpenJDK 1.8.0)
Score 700
Code Size 2863 Byte
Status AC
Exec Time 125 ms
Memory 42708 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 67
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 69 ms 19412 KB
02.txt AC 69 ms 18900 KB
03.txt AC 71 ms 19028 KB
04.txt AC 71 ms 15956 KB
05.txt AC 71 ms 18388 KB
06.txt AC 70 ms 18900 KB
07.txt AC 70 ms 19412 KB
08.txt AC 71 ms 17876 KB
09.txt AC 71 ms 18644 KB
10.txt AC 69 ms 19412 KB
11.txt AC 73 ms 19924 KB
12.txt AC 71 ms 21332 KB
13.txt AC 70 ms 18772 KB
14.txt AC 69 ms 19540 KB
15.txt AC 71 ms 15828 KB
16.txt AC 69 ms 19028 KB
17.txt AC 69 ms 21460 KB
18.txt AC 69 ms 19412 KB
19.txt AC 71 ms 19028 KB
20.txt AC 69 ms 19284 KB
21.txt AC 70 ms 20180 KB
22.txt AC 115 ms 42452 KB
23.txt AC 97 ms 22740 KB
24.txt AC 111 ms 27732 KB
25.txt AC 97 ms 26708 KB
26.txt AC 86 ms 23380 KB
27.txt AC 95 ms 23764 KB
28.txt AC 99 ms 27348 KB
29.txt AC 84 ms 21460 KB
30.txt AC 111 ms 24532 KB
31.txt AC 70 ms 18260 KB
32.txt AC 71 ms 23380 KB
33.txt AC 97 ms 22612 KB
34.txt AC 81 ms 20308 KB
35.txt AC 95 ms 23508 KB
36.txt AC 84 ms 21972 KB
37.txt AC 83 ms 20948 KB
38.txt AC 91 ms 18644 KB
39.txt AC 91 ms 22356 KB
40.txt AC 76 ms 21204 KB
41.txt AC 93 ms 22740 KB
42.txt AC 117 ms 33748 KB
43.txt AC 89 ms 17876 KB
44.txt AC 74 ms 19408 KB
45.txt AC 89 ms 21076 KB
46.txt AC 106 ms 23764 KB
47.txt AC 79 ms 18900 KB
48.txt AC 113 ms 34388 KB
49.txt AC 107 ms 23124 KB
50.txt AC 91 ms 21716 KB
51.txt AC 85 ms 22612 KB
52.txt AC 86 ms 20308 KB
53.txt AC 106 ms 24916 KB
54.txt AC 87 ms 23508 KB
55.txt AC 105 ms 23764 KB
56.txt AC 92 ms 23380 KB
57.txt AC 109 ms 32084 KB
58.txt AC 111 ms 35796 KB
59.txt AC 125 ms 40660 KB
60.txt AC 110 ms 37716 KB
61.txt AC 77 ms 15956 KB
62.txt AC 91 ms 22996 KB
63.txt AC 93 ms 23380 KB
64.txt AC 111 ms 42708 KB
s1.txt AC 71 ms 22612 KB
s2.txt AC 69 ms 18900 KB
s3.txt AC 107 ms 26452 KB