ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

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
๋Œ“๊ธ€