Given an integer N denoting the Length of a line segment. you need to cut the line segment in such a way that the cut length of a line segment each time is integer either x , y or z. and after performing all cutting operation the total number of cutted segments must be maximum.
#include <iostream>
#include<vector>
#include<math.h>
#include<climits>
#include<algorithm>
using namespace std;
int count(int*dp,int x,int y,int z,int N)
{
if(N==0)
{
return 1;
}
if(N<0)
{
return 0;
}
if(dp[N]!=INT_MAX)
{
return dp[N];
}
dp[N]=max(count(dp,x,y,z,x)+count(dp,x,y,z,N-x)
,max(count(dp,x,y,z,y)+count(dp,x,y,z,N-y),
count(dp,x,y,z,z)+count(dp,x,y,z,N-z)));
return dp[N];
}
int main()
{
//code
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;
cin>>n;
int x,y,z;
cin>>x;
cin>>y;
cin>>z;
//cout<<x<<y<<z<<n;
int*dp=(int*)malloc(sizeof(int)*n+1);
// int dp[n+1];
dp[0]=1;
for(int i=1;i<=n;i++)
{
dp[i]=INT_MAX;
}
cout<<x<<y<<z<<n;
cout<<count(dp,x,y,z,n)<<endl;
}
return 0;
}