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 |
Tags
- 도커설치하는법
- 스프링사진업로드
- WAS웹서버
- 출처 문어박사
- dockerinstall
- 스프링부트중복예외처리
- springboot_exception_handler
- 출처 메타코딩
- centos도커설치
- 스프링부트팔로잉
- 스프링부트팔로우취소
- 스프링부트
- 스프링부트api
- vm도커설치하는법
- 스프링구독
- 출처 따배도
- 우분투도커설치
- 서버에도커설치
- 파이썬sort
- 스프링익셉션처리
- 스프링이미지업로드
- 스프링사진
- ssh도커설치
- 스프링부트서버에사진전송
- 출처 노마드코더
- 출처 코딩셰프
- 스프링부트구독취소
- 스프링부트사진올리기
- 멀티폼
- 인스타클론
Archives
- Today
- Total
MakerHyeon
[백준] 11051번 이항 계수 2 (python,c++) 본문
https://www.acmicpc.net/problem/11051
11051번: 이항 계수 2
첫째 줄에 \(N\)과 \(K\)가 주어진다. (1 ≤ \(N\) ≤ 1,000, 0 ≤ \(K\) ≤ \(N\))
www.acmicpc.net
숫자가 너무 크기때문에 10,007로 나눈 나머지를 구하라는 것이다.
SOLUTION CODE
# PYTHON
순수 풀이(시간 초과)
import sys
MOD = 10007
sys.setrecursionlimit(10**7)
N,K = map(int,input().split())
def bino(n,k):
if k==0 or k== n:
return 1
return bino(n-1,k-1) + bino(n-1,k)
print(bino(N,K))
재귀
import sys
MOD = 10007
sys.setrecursionlimit(10**7)
cache = [[0]* 1001 for _ in range(1001)]
N,K = map(int,input().split())
def bino(n,k):
if cache[n][k]: # 값이 있는경우 바로 값을 가져다쓴다.
return cache[n][k]
if k==0 or k==n:
cache[n][k] = 1
else:
cache[n][k] = bino(n-1,k-1) + bino(n-1,k) # 10007 초과 할 수있음
cache[n][k] %= MOD
return cache[n][k]
print(bino(N,K))
반복문
MOD = 10007
cache = [[0]* 1001 for _ in range(1001)]
N,K = map(int,input().split())
# 이중 for문
for i in range(1001): # 한 행씩 값 차례로 넣어주기
cache[i][0] = cache[i][i] = 1
for j in range(1,i):
cache[i][j] = cache[i-1][j-1] + cache[i-1][j]
cache[i][j] %= MOD
print(cache[N][K])
# C++
재귀
#include <iostream>
#include <cstring>
using namespace std;
int N, K;
const int MAX = 1001;
int cache[MAX][MAX];
const int MOD = 10007;
int bino(int n, int k)
{
if (n == k || k == 0) return 1;
if (cache[n][k] != -1)
return cache[n][k];
return cache[n][k] = ((bino(n - 1, k) + bino(n - 1, k - 1)))%MOD;
}
int main()
{
memset(cache, -1, sizeof(cache)); // 초기화
cin >> N >> K;
cout << bino(N, K) << endl;
return 0;
}
반복문
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n,k;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
// n+1개 원소, 각 vector는 k+1개의 원소를 가지고 0으로 초기화
vector<vector<int>> dp(n+1,vector<int>(k+1,0));
for(int i=1;i<=n;i++){
for(int j=0;j<=k;j++){
if(j==0||i==j) dp[i][j] = 1;
else{
dp[i][j] = dp[i-1][j] + dp[i-1][j-1];
dp[i][j]%=10007;
}
}
}
cout<<dp[n][k]<<'\n';
}
'Algorithm > backjoon' 카테고리의 다른 글
[백준] 10844번 쉬운 계단 수 (python,c++) (0) | 2023.03.01 |
---|---|
[백준] 11726번 2 X n 타일링 (python,c++) (0) | 2023.02.28 |
[백준] 2748번 피보나치 수 2 (python,c++) (0) | 2023.02.23 |
[백준] 10815번 숫자 카드 (python,c++) (0) | 2023.02.17 |
[백준] 2512번 예산 (python,c++) (0) | 2023.02.17 |
Comments