Skip to content

Commit f4cfa5a

Browse files
committed
[Silver I] Title: 곱셈, Time: 104 ms, Memory: 14236 KB -BaekjoonHub
1 parent 151fde5 commit f4cfa5a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Silver I] 곱셈 - 1629
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1629)
4+
5+
### 성능 요약
6+
7+
메모리: 14236 KB, 시간: 104 ms
8+
9+
### 분류
10+
11+
분할 정복을 이용한 거듭제곱, 수학
12+
13+
### 제출 일자
14+
15+
2024년 11월 12일 11:38:12
16+
17+
### 문제 설명
18+
19+
<p>자연수 A를 B번 곱한 수를 알고 싶다. 단 구하려는 수가 매우 커질 수 있으므로 이를 C로 나눈 나머지를 구하는 프로그램을 작성하시오.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 A, B, C가 빈 칸을 사이에 두고 순서대로 주어진다. A, B, C는 모두 2,147,483,647 이하의 자연수이다.</p>
24+
25+
### 출력
26+
27+
<p>첫째 줄에 A를 B번 곱한 수를 C로 나눈 나머지를 출력한다.</p>
28+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.io.IOException;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
8+
public static long C;
9+
10+
public static void main(String[] args) throws IOException {
11+
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
14+
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
15+
16+
long A = Long.parseLong(st.nextToken());
17+
long B = Long.parseLong(st.nextToken());
18+
C = Long.parseLong(st.nextToken());
19+
20+
System.out.println(pow(A, B));
21+
}
22+
23+
public static long pow(long A, long exponent) {
24+
if(exponent == 1) {
25+
return A % C;
26+
}
27+
long temp = pow(A, exponent / 2);
28+
if(exponent % 2 == 1) {
29+
return (temp * temp % C) * A % C;
30+
}
31+
return temp * temp % C;
32+
33+
}
34+
35+
}

0 commit comments

Comments
 (0)