### kartik8800's blog

By kartik8800, history, 4 years ago,

Hello Codeforces!

This series of videos are focused on explaining dynamic programming by illustrating the application of DP with bitmasking through the use of selected problems from platforms like Codeforces, Codechef, SPOJ, CSES and Atcoder.

After going through this series, you should find yourself confident in solving problems which require the use of dp and bitmasks and also implementing them in a reasonable amount of time.

I will also live code and submit the solutions to the problem on the coding platform from where the problem comes.

## Some Basic elements of Dynamic Programming

Part 1: https://youtu.be/24hk2qW_BCU
1. What is Divide and Conquer?
2. What is Dynamic Programming?
3. Types of DP problems.

Part 2: https://youtu.be/yfgKw6BUZUk
1. What is a DP-state?
2. Characterizing a DP-state.
3. What is a recurrence?
4. Top Down v/s Bottom Up.

Part 3: https://youtu.be/X-3HklSPx6k
1. Directed Acyclic Graph(DAG) Representation of DP solution
2. Visualizing Top-Down and Bottom-Up.
3. Evaluation order for bottom-up codes.
4. Analyzing the space and time complexity for a DP solution(derivation).

You may also want to checkout these video tutorials:
- Beginner Friendly Series on Dynamic Programming
- Dynamic Programming on Trees

Short video where I talk about the playlist: https://youtu.be/6sEFap7hIl4

I talk about what is bitmasking before we actually start solving problems that use dp with bitmasking.
https://youtu.be/7FmL-WpTTJ4

## Illustration: Solving the Job Assignment Problem

Using a simple problem, I will illustrate how to solve and implement problems which use dp+bitmask concepts.
Solution: https://youtu.be/685x-rzOIlY

## Illustration: Travelling Salesman Problem

Another great problem to illustrate bitmask dp.
I will discuss the following:
1. What is the TSP?
2. Brute force solution.
3. Intuition towards an efficient solution.
4. Define a DP state, write a recurrence.
5. How do I implement this? ANS: bitmasking!
6. Analyzing time and space complexities.

## Codeforces Problem: Div2E

I'll discuss a problem named Fish that comes from a Codeforces Divison 2 round.
Solution: https://youtu.be/d7kvyp6dfz8

## Codechef long Challenge: Medium

The problem comes from Codechef long challenge and is rated MEDIUM by codechef.
Solution: https://youtu.be/Smem2tVQQXU

## CSES: Counting Tilings

The problem comes from CSES problemset and was introduced to the problemset in 2021.
Solution: https://youtu.be/lPLhmuWMRag

If people find this helpful then I will try and add more problems to this list :)

• +148

| Write comment?
 » 4 years ago, # |   +6 You are worth more than 1000 greens
•  » » 3 years ago, # ^ |   +1 Greens don't like this comment
 » 4 years ago, # |   0 Auto comment: topic has been updated by kartik8800 (previous revision, new revision, compare).
 » 4 years ago, # |   +10 This blog has some really nice problems on bitmask dp.
•  » » 4 years ago, # ^ |   +1 thanks for sharing, will add links to some of these in the blog.
 » 4 years ago, # |   +6 For a long time, I am unable to understand masks and sub masks, but now I get it. Thanks to these tutorials! You are doing a great job. Thank you!
•  » » 4 years ago, # ^ |   +4 glad to know, you found it helpful.
•  » » 4 years ago, # ^ |   0 What are sub masks ? I guess 2nd problem from practice problems list which is U: Grouping required knowledge of sub mask.Can somebody please explain this concept or explain how can we solve this problem?
 » 4 years ago, # |   +14 Is this what hope looks like?
 » 4 years ago, # |   0 Auto comment: topic has been updated by kartik8800 (previous revision, new revision, compare).
 » 12 months ago, # | ← Rev. 10 →   0 Solution: Grouping. Precompute Precompute costs for every set of rabbits. TC for this (2^n)*(n^2). 2^n for the traverse overall sub mask/subset. n^2 to calculate the cost for a particular set. Dp mask=set bit represents that the ith rabbit is present in the set. dp[mask]= maximum score that can be achieved by this set of rabbits. dp[mask]=max of traverse overall sub mask(m1) of given mask(m) such that m1!=m =dp[m1]+dp[m^m1]. Answer for the question will be dp[(1<> n; for (ll i = 0; i < n; ++i){ for (ll j = 0; j < n; ++j){ cin>>aa[i][j]; } } for (ll i = 0; i < (1 << n); ++i){ for (ll j = 0; j < n; ++j){ for (ll k = j + 1; k < n; ++k){ if (i & (1 << j) && i & (1 << k)){ cc[i] += aa[j][k]; } } } } memset(dp,-1,sizeof(dp)); cout<