Problem Link
In this problem the constraints is very low it can only be 15, that's why it gives a hints that it could be solve with bitmask.
Bitmask is a masking technique, by which we can have the all possible combination in 2^n complexity rather than n! complexity. To learn more about bitmask click here
In this problem we have to calculate number of ways to choose a sum where number of elements in the sum is greater than 1 and the sum itself is at least l and at most r. And difference between maximum and minimum element which are contributed to the sum is at least x.
To do that we will check all possible subset of the set and check the requirements satisfies or not.
Code:
/// Bismillahir Rahmanir Rahim
/* Mohammad Morsalin
Dept of ICE, NSTU
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define highest(x) numeric_limits<x>::max()
#define IOS ios::sync_with_stdio(false);
///solution
void solution(){
ll n,l,r,x;
cin>>n>>l>>r>>x;
ll arr[n];
for(int i=0;i<n;i++)cin>>arr[i];
ll ans=0;
for(int i=0;i<=pow(2,n);i++){
int cnt=0;
ll sum=0,mini=highest(ll),maxx=0;
for(int j=0;j<n;j++){
if(i&(1<<j)){
cnt++;
sum+=arr[j];
mini=min(mini,arr[j]);
maxx=max(maxx,arr[j]);
}
}
if(sum>=l && sum<=r && cnt>=2 && maxx-mini>=x){
ans++;
}
}
cout << ans << endl;
}
signed main()
{
IOS
int t;
t=1;
//cin>>t;
while(t--){
solution();
}
return 0;
}
///Alhamdulillah
No comments:
Post a Comment
If you have any doubts, let me know through comments