[백준] 1629 곱셈

0

백준

목록 보기
164/271
post-thumbnail

[백준] 1629 곱셈

#include <iostream>
using namespace std;

typedef long long ll;

ll a, b, c;

ll calc(int n) {
	if (n == 0) return 1;
	if (n == 1) return a % c;
	
	ll res;
	if (n % 2 == 0) {
		res = calc(n / 2) * calc(n / 2);
		res %= c;
	}
	else {
		res = calc((n - 1) / 2) * calc((n - 1) / 2);
		res %= c;
		res *= a;
		res %= c;
	}
    
	return res;
}

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

	cout << calc(b);

	return 0;
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글