๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[์†Œํ”„ํ‹ฐ์–ด Softeer] ๋ฐ”์ด๋Ÿฌ์Šค (Lv.2) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 6. 24.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://softeer.ai/practice/6284

 

Softeer - ํ˜„๋Œ€์ž๋™์ฐจ๊ทธ๋ฃน SW์ธ์žฌํ™•๋ณดํ”Œ๋žซํผ

 

softeer.ai


๋ฌธ์ œ ํ’€์ด

์˜ค๋‹ต ์ฝ”๋“œ

import sys

input = sys.stdin.readline

k, p, n = map(int, input().split())

answer = k

for i in range(n):
    answer = answer * p 
print(answer % 1000000007)

 

์ฒซ ์ฝ”๋“œ๋Š” ๋ฌธ์ œ์—์„œ ๋ช…์‹œํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์ตœ์ข… ๋ฐ”์ด๋Ÿฌ์Šค ๊ฐœ์ˆ˜๋ฅผ 1000000007๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์˜€๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ œ์•ฝ ์กฐ๊ฑด์ƒ N์ดˆ์˜ ์ตœ๋Œ€๊ฐ€ 10^6์ด์—ˆ๊ณ  ๋‚˜์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์™œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œจ๋Š”์ง€ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•„ ์ฐพ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ 

์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋ฐœ์ƒ

* ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ: ๋ณ€์ˆ˜๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๊ฒฝ์šฐ

์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ณ ์ •๋œ ๋น„ํŠธ ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 32๋น„ํŠธ ์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜๋Š” -2,147,483,648๋ถ€ํ„ฐ 2,147,483,647๊นŒ์ง€์˜ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ๋ฒ”์œ„๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

์œ„ ์˜ค๋‹ต ์ฝ”๋“œ๋Š” K์˜ ์ตœ๋Œ€๊ฐ’์ด 10^8์ด๊ณ  P์˜ ์ตœ๋Œ€๊ฐ’์ด 10^8์ด๊ธฐ ๋•Œ๋ฌธ์—  ๋ฐ˜๋ณต๋ฌธ์ด 100๋งŒ๋ฒˆ ์ˆ˜ํ–‰๋˜๋ฉด์„œ answer์˜ ๊ฐ’์€ ๊ธฐํ•˜๊ธ‰์ˆ˜์ ์œผ๋กœ ์ปค์ ธ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

์ตœ์ข… ์ •๋‹ต ์ฝ”๋“œ

๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์˜ ์„ฑ์งˆ ๋•๋ถ„์— ์˜ค๋‹ต ์ฝ”๋“œ์™€ ์ •๋‹ต ์ฝ”๋“œ์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

์ •๋‹ต ์ฝ”๋“œ๋Š” ๊ฐ ๋‹จ๊ณ„๋งˆ๋‹ค ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ answer์˜ ํฌ๊ธฐ๋ฅผ ์ž‘๊ฒŒ ์œ ์ง€ํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ์™€ ์‹œ๊ฐ„ ํšจ์œจ์„ฑ์ด ๋” ๋†’์Šต๋‹ˆ๋‹ค.

import sys

input = sys.stdin.readline

k, p, n = map(int, input().split())

answer = k

for i in range(n):
    answer = (answer * p) % 1000000007

print(answer)

 

์˜ค๋‹ต ์ฝ”๋“œ์™€ ์ •๋‹ต ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ™์€ ์ด์œ 

๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์˜ ํŠน์„ฑ ๋•Œ๋ฌธ์—, ์ฒซ ๋ฒˆ์งธ ์ฝ”๋“œ์™€ ๋‘ ๋ฒˆ์งธ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
์ด๋Š” ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์˜ ๊ธฐ๋ณธ ์„ฑ์งˆ์— ์˜ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์„ฑ์งˆ์„ ํ†ตํ•ด ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ํ•ด๋„ ์ตœ์ข… ๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๊ฒŒ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.

์ด๋Š” ์ˆ˜ํ•™์ ์ธ ๊ณต๋ถ€๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ๊ธ€๋ง ๋“ฑ์„ ํ†ตํ•ด์„œ ๋ชจ๋“ˆ๋Ÿฌ ์—ฐ์‚ฐ์„ ์ฐพ์•„๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

 

https://ko.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic

 

๋ชจ๋“ˆ๋กœ ์—ฐ์‚ฐ์ด๋ž€? (๊ฐœ๋… ์ดํ•ดํ•˜๊ธฐ) | ์•”ํ˜ธํ•™์ด๋ž€? | Khan Academy

์ˆ˜ํ•™, ์˜ˆ์ˆ , ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ๊ฒฝ์ œ, ๋ฌผ๋ฆฌํ•™, ํ™”ํ•™, ์ƒ๋ฌผํ•™, ์˜ํ•™, ๊ธˆ์œต, ์—ญ์‚ฌ ๋“ฑ์„ ๋ฌด๋ฃŒ๋กœ ํ•™์Šตํ•ด ๋ณด์„ธ์š”. ์นธ์•„์นด๋ฐ๋ฏธ๋Š” ์–ด๋””์—์„œ๋‚˜ ๋ˆ„๊ตฌ์—๊ฒŒ๋‚˜ ์„ธ๊ณ„ ์ตœ๊ณ ์˜ ๋ฌด๋ฃŒ ๊ต์œก์„ ์ œ๊ณตํ•˜๋Š” ๋ฏธ์…˜์„ ๊ฐ€์ง„

ko.khanacademy.org

 

 

 

728x90
๋ฐ˜์‘ํ˜•