hades

[Baekjoon] 1629๋ฒˆ: ๊ณฑ์…ˆ ๋ณธ๋ฌธ

๐Ÿ‘Š PS/Algorithm

[Baekjoon] 1629๋ฒˆ: ๊ณฑ์…ˆ

hades1 2024. 8. 3. 09:03

๐Ÿฅ… ๋ฌธ์ œ

https://www.acmicpc.net/problem/1629

 

๐Ÿ” ์„ค๊ณ„

๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ„์† a๋ฅผ ๊ณฑํ•˜๊ณ  c๋กœ ๋‚˜๋ˆ„๋ฉด์„œ ๊ฐฑ์‹ ํ•œ๋‹ค๋ฉด, b๊ฐ€ 20์–ต ์ด์ƒ์ด๋ฏ€๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

๊ฑฐ๋“ญ์ œ๊ณฑ์˜ ํŠน์„ฑ ์ค‘์— x^(y+z) = x^y * x^z๊ฐ€ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ™€์ˆ˜ ์ง์ˆ˜๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.

 

๐Ÿ‘Š ํ’€์ด

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

long long a, b, c;

long long square(long long b) {
	if (b == 0) {
		return 1;
	}
	else if (b % 2 == 0) {
		long long half = square(b / 2);
		return half * half % c;
	}
	else if (b % 2 == 1) {
		return square(b - 1) * a % c;
	}
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> a >> b >> c;

	cout << square(b) << "\n";

	return 0;
}