![[백준 / BOJ] 1010번 다리 놓기 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FL18hZ%2FbtrJpReKCPz%2FK65BhibF8ucY0JBXnmx561%2Fimg.png)
[백준 / BOJ] 1010번 다리 놓기 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 6. 12. 17:42
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/1010
1010번: 다리 놓기
입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트케이스에 대해 강의 서쪽과 동쪽에 있는 사이트의 개수 정수 N, M (0 < N ≤ M < 30)이 주어진다.
www.acmicpc.net
문제

문제 풀이
고등학생때 확통에서 많이 풀던 유형의 문제다. 이전에 풀었던 조합 문제의 알고리즘을 따오면 된다.
C++의 경우에 nCr을 아예 풀어 쓴 공식을 활용한 방법이 있어서 구현해보았다.
for (int i = M; i > M - N; --i) {
res = res * i;
res = res / tmp++;
}
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int T; | |
std::cin >> T; | |
for (int t = 0; t < T; ++t) { | |
int N, M, res = 1, tmp = 1; | |
std::cin >> N >> M; | |
for (int i = M; i > M - N; --i) { | |
res = res * i; | |
res = res / tmp++; | |
} | |
std::cout << res << "\n"; | |
} | |
return 0; | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import math | |
input = sys.stdin.readline | |
T = int(input()) | |
for _ in range(T): | |
N, M = map(int, input().split()) | |
print(math.comb(M, N)) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 1676번 팩토리얼 0의 개수 (C++, Python) (0) | 2022.06.13 |
---|---|
[백준 / BOJ] 9375번 패션왕 신해빈 (C++, Python) (0) | 2022.06.12 |
[백준 / BOJ] 11051번 이항 계수 2 (C++, Python) (0) | 2022.06.10 |
[백준 / BOJ] 11050번 이항 계수 1 (C++, Python) (0) | 2022.06.10 |
[백준 / BOJ] 3036번 링 (C++, Python) (0) | 2022.06.09 |
@Reo :: 코드 아카이브
자기계발 블로그