dev/algorithm
BOJ / 2004๋ฒ / ์กฐํฉ 0์ ๊ฐ์ [Go][Python3]
crscnt
2020. 11. 5. 21:00
๐ฉ๐ป๐ป ๋ฌธ์
2004๋ฒ: ์กฐํฉ 0์ ๊ฐ์
์ฒซ์งธ ์ค์ ์ ์ n, m (0 ≤ m ≤ n ≤ 2,000,000,000, n ≠ 0)์ด ๋ค์ด์จ๋ค.
www.acmicpc.net
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/2004
// nCm์ ๋์ 0์ด ์ผ๋ง๋ ๋ง์ด ์ค๋์ง ๊ตฌํ๋ ๋ฌธ์
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n, m, count, twoCount, fiveCount int
fmt.Fscanln(reader, &n, &m)
twoCount = getTwoCount(n) - getTwoCount(n-m) - getTwoCount(m)
fiveCount = getFiveCount(n) - getFiveCount(n-m) - getFiveCount(m)
if twoCount > fiveCount {
count = fiveCount
} else {
count = twoCount
}
fmt.Fprintln(writer, count)
}
func getTwoCount(num int) (twoCount int) {
for i := 2; i <= num; i *= 2 {
twoCount += num / i
}
return twoCount
}
func getFiveCount(num int) (fiveCount int) {
for i := 5; i <= num; i *= 5 {
fiveCount += num / i
}
return fiveCount
}
/* ์๊ฐ์ด๊ณผ..
// reader := bufio.NewReader(os.Stdin)
// writer := bufio.NewWriter(os.Stdout)
// defer writer.Flush()
// var n, m, count, twoCount, fiveCount int
// fmt.Fscanln(reader, &n, &m)
// for i := n - m + 1; i <= n; i++ {
// var temp = i
// for temp%2 == 0 && temp > 0 {
// twoCount++
// temp /= 2
// }
// temp = i
// for temp%5 == 0 && temp > 0 {
// fiveCount++
// temp /= 5
// }
// }
// for i := 1; i <= m; i++ {
// var temp = i
// for temp%2 == 0 && temp > 0 {
// twoCount--
// temp /= 2
// }
// temp = i
// for temp%5 == 0 && temp > 0 {
// fiveCount--
// temp /= 5
// }
// }
// if twoCount > fiveCount {
// count = fiveCount
// } else {
// count = twoCount
// }
// fmt.Fprintln(writer, count)
*/
๐จ Python3
# https://www.acmicpc.net/problem/2004
# nCm์ ๋์ 0์ด ์ผ๋ง๋ ๋ง์ด ์ค๋์ง ๊ตฌํ๋ ๋ฌธ์
import sys
def get_two_count(n):
i = 2
two_count = 0
while i <= n:
two_count += n//i
i*=2
return two_count
def get_five_count(n):
i = 5
five_count = 0
while i <= n:
five_count += n//i
i*=5
return five_count
if __name__ == "__main__":
n, m = list(map(int, sys.stdin.readline().split()))
two_count = get_two_count(n) - get_two_count(n-m) - get_two_count(m)
five_count = get_five_count(n) - get_five_count(n-m) - get_five_count(m)
print(min(two_count, five_count))
728x90