combinationzero

강기호·2022년 10월 7일
0

Algorithm

목록 보기
9/14

문제

n명의 사람중 m명을 순서에 상관없이 뽑는 경우의 수를 조합이라고 하며 nCm으로 나타낸다.

nCm은 수식으로 n!/m!(n-m)! 으로 구할 수 있다. (5! = 1 2 3 4 5)

n과 m이 주어졌을때 nCm의 끝자리 0의 개수를 출력하는 프로그램을 작성하시오.


입력
첫째 줄에 정수 n, m(0≤m≤n≤1,000,000)이 들어온다.

출력
첫째 줄에 0의 개수를 출력한다.

예제 입력
25 12
예제 출력
2

## template
# 2랑 5가 몇개 있는지 알면 됨

def Get_num_twofive(num):
  num_two = 0
  num_five = 0
  for i in range(2,num+1):
    tmp1 = i
    tmp2 = i
    while(tmp1 % 2 == 0 or tmp1 % 5 == 0):
      if tmp1 % 2 == 0:
        tmp1 //= 2
        num_two +=1
      else:
        tmp1 //=5
        num_five += 1
  # print(f'num_two = {num_two} , num_five = {num_five}')
  return num_two , num_five

n , m = map(int , input().split())
# ncm =  n! / (m! * (n-m)!)
n_two , n_five = Get_num_twofive(n)
m_two , m_five = Get_num_twofive(m)
nm_two , nm_five = Get_num_twofive(n-m)
total_two = n_two -(m_two + nm_two)
total_five = n_five - (m_five + nm_five)
total_ten = min(total_two , total_five)
print(total_ten)

0개의 댓글