[๋ฐฑ์ค€-1629] ๊ณฑ์…ˆ / Python

๐Ÿ“š Problem Solving/Baekjoon

 

1629๋ฒˆ: ๊ณฑ์…ˆ

์ฒซ์งธ ์ค„์— A, B, C๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. A, B, C๋Š” ๋ชจ๋‘ 2,147,483,647 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

import sys

input = sys.stdin.readline

a, b, c = map(int, input().split())


def func(length):
    if length == 1:
        return a % c
    if length % 2 == 0:
        temp = func(length // 2)
        return temp * temp % c
    else:
        temp = func(length // 2)
        return temp * temp * a % c


print(func(b))

 

ํ•ด์„ค

๊ธฐ๋ณธ ์—ฐ์‚ฐ์ด๋‚˜ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ’€๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค.

๋ถ„ํ• ์ •๋ณต์„ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ–ˆ๋‹ค.