기본 콘텐츠로 건너뛰기

11051 : 이항계수2 (Binomial Coefficient) (Dynamic Programming) [C++]

저번 이항 계수의 문제와 비슷하지만

입력 값의 범위가 1000까지 늘어서 DP를 이용하기에 딱 좋은 문제이다.


왜 DP를 이용하는가?


이항계수의 원리를 보면 파스칼의 삼각형 형태!

1
1(1C0) 1(1C1)
1(2C0) 2(2C1) 1(2C2)
1(3C0) 3(3C1) 3(3C2) 1(3C3)
1(4C0) 4(4C1) 6(4C2) 4(4C3) 1(4C4)
1(5C0) 5(5C1) 10(5C2) 10(5C3) 5(5C4) 1(5C5)
1(6C0) 6(6C1) 15(6C2) 20(6C3) 15(6C4) 6(6C5) 1(6C6)


<파스칼의 삼각형(이항원리)>


DP식을 써보면

///////////////////////////////////////////////////////////
if (k == 0 || k == n)
       DP[i][j] = 1;
else
       DP[i][j] = DP[i-1][j-1] + DP[i-1][j];
///////////////////////////////////////////////////////////



BinomialCoefficient::BinomialCoefficient(int N)
{
    for(int i = 0; i < N; i++)
    {
        vector<int> Temp;
        Temp.resize(i+2);
        DP.push_back(Temp);
    }
    
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < i + 2; j++)
        {
            if(j == 0 || j == i + 1)
                DP[i][j] = 1;
            else
                DP[i][j] = DP[i-1][j-1] % 10007 + DP[i-1][j] % 10007;
        }
    }
}

void BinomialCoefficient::Print(int N, int K)
{
    cout << DP[N-1][K] % 10007;
}
<소스 코드>

*Source of the problem = https://www.acmicpc.net/problem/11051
*문제 출처 : BAEKJOON ONLINE JUDGE

댓글

이 블로그의 인기 게시물

6359 : 만취한 상범 (Dynamic Programming) [C++]

# include < iostream > # include < vector > using namespace std ; int Num_of_Divisor ( int n ) { int Count = 0 ; for ( int i = 1 ; i < = n ; i + + ) if ( n % i = = 0 ) Count + + ; return Count ; } int main ( ) { int Testcase ; cin > > Testcase ; while ( Testcase - - ) { int Rooms ; cin > > Rooms ; vector < int > Prisons ; for ( int i = 0 ; i < Rooms ; i + + ) Prisons . push_back ( 0 ) ; for ( int i = 1 ; i < Prisons . size ( ) + 1 ; i + + ) { if ( ( Num_of_Divisor ( i ) % 2 ) = = 0 ) Prisons [ i - 1 ] = 0 ; else Prisons [ i - 1 ] = 1 ; } int Fleer = 0 ; for ( int i = 0 ; i < Prisons . size ( ) ; i + + ) if ( Prisons ...

11004 : K번째 수 [C++]

# include < iostream > # include < cstdio > # include < algorithm > int main ( ) { int * Number = new int [ 5000000 ] ; int N , K ; scanf ( " %d %d " , & N , & K ) ; for ( int i = 0 ; i < N ; i + + ) scanf ( " %d " , Number [ i ] ) ; std :: sort ( Number , Number + N ) ; printf ( " %d " , Number [ K - 1 ] ) ; return 0 ; }

1149 : RGB Street Coloring (Dynamic Programming) [C,C++]

The key to this problem lies in understanding the principles. Let me explain the algorithm to solve the problem by using DP. First, you need the same storage space like input data's size. When you draw any color of the nth house, the space will contain the minimum value. If you paint the red in the second house, this value is sum of blue or green of the first house.  You must use DP because you must use the previous value.  Of course, you can also use the recursive algorithm to solve it. But if it gets bigger, it will take a lot of time.  If you paint the red in the nth house in the same way, you should add the lower value of the blue and green of the n-1th house.  Therefore, the minimum value can be found in the value of the storage space (n-1) index. <pesudo code> *Source of the problem =  https://www.acmicpc.net/problem/1149 *문제 출처 : BAEKJOON ONLINE JUDGE