• 문제 링크
9471번: 피사노 주기
첫째 줄에 테스트 케이스의 개수 P가 주어진다. 각 테스트 케이스는 N과 M으로 이루어져 있다. N은 테스트 케이스의 번호이고, M은 문제에서 설명한 m이다.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int p = read();
while (p-- > 0) {
int n = read(), m = read(), a = 0, b = 1, cnt = 0;
do {
int tmp = (a + b) % m;
a = b;
b = tmp;
cnt++;
} while (a != 0 || b != 1);
sb.append(n).append(" ").append(cnt).append("\n");
}
bw.write(sb.toString());
bw.flush();
}
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;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 14584 암호 해독 - Brute Force / Java (0) | 2023.10.03 |
---|---|
[백준] 1812 사탕 - Brute Force / Java (0) | 2023.10.02 |
[백준] 20152 Game Addiction - Dynamic Programming / Java (0) | 2023.09.30 |
[백준] 17202 핸드폰 번호 궁합 - Dynamic Programming / Java (0) | 2023.09.29 |
[백준] 17291 새끼치기 - Dynamic Programming / Java (0) | 2023.09.28 |
댓글