Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 백준 1487번
- 백준 1246번
- 이항계수
- DP 알고리즘
- 1
- 1141번
- 해싱
- 챗봇
- 연산자 문제
- 풍선터뜨리기
- 문제 풀이
- BTREE
- 0의 개수
- 해설
- hashing
- 백준 11050번
- 가치규범의 공공외교
- N과 M
- B-tree
- html
- 알고리즘
- 파이썬
- CSS
- 백준 14501번
- 풀이
- 백준
- 한반도평화와공공외교
- 실버
- 주창형 공공외교
- Python
Archives
- Today
- Total
SunFly의 코딩 및 정보 블로그
[파이썬(Python)] 백준 2004번 : 조합 0의 개수 본문
풀이
팩토리얼을 사용하면 재귀함수를 사용하게 되어서 시간초과 가 일어난다.
import sys input = sys.stdin.readline def factorial(x): res = 1 for i in range(1, x+1): res *= i return res def cnt_zero(x): cnt = 0 for i in x: if i == '0': cnt += 1 else: break return cnt n, m = map(int, input().split()) result = str(factorial(n) // (factorial(n-m) * factorial(m))) result = reversed(result) cnt = cnt_zero(result) print(cnt) |
따라서 다시 생각해보았다.
재귀함수를 빼고 뒤에 0이 나오는 경우의 수를 생각해보면 5와 2가 나올때, 계산 결과의 뒤에 0이 존재할 수 있다.
import sys input = sys.stdin.readline def ans(x, y): cnt = 0 while x: x//=y cnt += x return cnt x, y = map(int, input().split()) a5 = ans(x, 5) - ans(y, 5) - ans(x-y, 5) a2 = ans(x, 2) - ans(y, 2) - ans(x-y, 2) result = min(a5, a2) print(result) |
솔직히 생각하기 쉽지 않았기 때문에 구글링을 통해 아이디어를 얻어 코딩을 하게 되었다.
'백준(BaekJoon)' 카테고리의 다른 글
[파이썬(Python)] 백준 2217번 : 로프 (0) | 2022.02.26 |
---|---|
[파이썬(Python)] 백준 2981번 : 검문 (0) | 2022.02.26 |
[파이썬(Python)] 백준 11051번 : 이항계수 2 (0) | 2022.02.26 |
[파이썬(Python)] 백준 1676번 : 팩토리얼 0의 개수 (0) | 2022.02.26 |
[파이썬(Python)] 백준 11050번 : 이항 계수 1 (0) | 2022.02.25 |