-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 1629번: 곱셈 - <img src="https://static.solved.ac/tier_small/10.svg" style="height:20px" /> Silver I | ||
|
||
<!-- performance --> | ||
|
||
<!-- 문제 제출 후 깃허브에 푸시를 했을 때 제출한 코드의 성능이 입력될 공간입니다.--> | ||
|
||
<!-- end --> | ||
|
||
## 문제 | ||
|
||
[문제 링크](https://boj.kr/1629) | ||
|
||
|
||
<p>자연수 A를 B번 곱한 수를 알고 싶다. 단 구하려는 수가 매우 커질 수 있으므로 이를 C로 나눈 나머지를 구하는 프로그램을 작성하시오.</p> | ||
|
||
|
||
|
||
## 입력 | ||
|
||
|
||
<p>첫째 줄에 A, B, C가 빈 칸을 사이에 두고 순서대로 주어진다. A, B, C는 모두 2,147,483,647 이하의 자연수이다.</p> | ||
|
||
|
||
|
||
## 출력 | ||
|
||
|
||
<p>첫째 줄에 A를 B번 곱한 수를 C로 나눈 나머지를 출력한다.</p> | ||
|
||
|
||
|
||
## 소스코드 | ||
|
||
[소스코드 보기](곱셈.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: ::: ::: */ | ||
/* Problem Number: 1629 :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: fkdl4878 <boj.kr/u/fkdl4878> +#+ +#+ +#+ */ | ||
/* +#+ +#+ +#+ */ | ||
/* https://boj.kr/1629 #+# #+# #+# */ | ||
/* Solved: 2024/05/19 17:29:57 by fkdl4878 ### ### ##.kr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
long long mod_exp(long long a, long long b, long long c){ | ||
if (b == 0) return 1; | ||
if (b == 1) return a % c; | ||
|
||
long long half = mod_exp(a, b / 2, c); | ||
long long result = (half * half) % c; | ||
|
||
if (b % 2 != 0){ | ||
result = (result * a) % c; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
int main(){ | ||
long long a, b, c; | ||
|
||
cin >> a >> b >> c; | ||
cout << mod_exp(a, b, c) << '\n'; | ||
|
||
return 0; | ||
} |
Binary file not shown.