I've been trying to solve a problem that states, determining the number of ways to distribute N items to three-person. The distribution would consider best if the total weight of items the first person gets is divisible by 3, the weight of items the second person got is divisible by 5 and the total weight of item the last person got is divisible by 7 & the amount of items each person gets should not be 0.
The first input N is considered the number of items & the 2nd line of input denotes N integers representing the weights of items. The i-th integer a[i] denotes the weight of i-th items. There were such constraints as,
3≤ N ≤12
1≤ a[i] ≤10^10
sample input 1:
6
2 3 5 7 9 13
sample output 1:
6
I tried a way to solve the problem but couldn't figure out how could the solution would be better so that the weight of the items that the first, second & third persons receive would be divisible by 3, 5 & 7 respectively. I tried so that one person gets the maximum amount of items among the three persons & each person gets at least one item, but couldn't figure out the "best" solution that the total weights of items each person gets would be divisible by 3, 5 & 7 respectively. Could you suggest what part of my code I should do the trick? Here's my solution to a simple distribution between three people. TIA
#include<bits/stdc++.h>
using namespace std;
int countWays(int N)
{
int ans = ((N-1)*(N-2))/2;
//storing the number of distribution that is not possible
int s = 0;
for(int i=2; i<=N-3; i++){
for(int j=1; j<i; j++){
//possibilities of 2 persons receiving the maximum
if (N == 2 * i + j)
s++;
}
}
if(N%3 == 0)
s = 3*s+1;
else
s = 3*s;
//final ways to distribute
return ans - s;
}
int main()
{
int n, N=0;
cin >> n;
if(n<3 && n>12)
return 0;
vector<int> a(n);
for(int i=0; i<n; i++){
cin>>a[i];
N += a[i];
}
cout<<countWays(N);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire