• 문제 링크
1351번: 무한 수열
첫째 줄에 3개의 정수 N, P, Q가 주어진다.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
public class Main {
static long p, q;
static Map<Long, Long> map = new HashMap<>();
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
long n = read();
p = read();
q = read();
map.put(0L, 1L);
bw.write(String.valueOf(find(n)));
bw.flush();
}
private static long find(long i) {
if (map.containsKey(i)) return map.get(i);
map.put(i, i = find(i / p) + find(i / q));
return i;
}
private static long read() throws IOException {
long c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 2605 줄 세우기 - Data Structure / Java (0) | 2023.12.09 |
---|---|
[백준] 1835 카드 - Data Structure / Java (0) | 2023.12.08 |
[백준] 5052 전화번호 목록 - Data Structure / Java (0) | 2023.12.06 |
[백준] 17074 정렬 - Dynamic Programming / Java (0) | 2023.12.05 |
[백준] 3372 보드 점프 - Dynamic Programming / Java (0) | 2023.12.04 |
댓글