➤ Problem Link : BEHAPPY
👉 Hint : Simple 2D DP
✅ C++ Solution :
#include<bits/stdc++.h> using namespace std; pair<int,int> arr[21]; int dp[21][101]; int main() { while(1) { int m,n,a,b; cin>>m>>n; if(m==0 && n==0) break; memset(dp,0,sizeof(dp)); for(int i=0;i<m;i++) cin>>arr[i].first>>arr[i].second; for(int i=0;i<=n;i++) { if(i>=arr[0].first && i<=arr[0].second) dp[0][i]=1; } for(int i=1;i<m;i++) { for(int j=0;j<=n;j++) { for(int k=arr[i].first;k<=arr[i].second && k<=j;k++) dp[i][j]+=dp[i-1][j-k]; } } cout<<dp[m-1][n]<<endl; } }
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!