PROBLEM LINK:Author: Jay Pandya DIFFICULTY:EASY PREREQUISITES:DP, bits PROBLEM:Given a set S of N integers the task is decide if it is possible to divide them into K nonempty subsets such that the sum of elements in every of the K subsets is equal. QUICK EXPLANATION:We use dynamic programming to solve it. If the solution exists, each subsets has to have a sum of its elements equal to the sum of all elements in S divided by K. Let X be that value. Let dp[k][bitmask] = 1 if and only if it is possible to divide a subset A of S denoted by the bitmask into k subsets each with sum X or to divide A into k  1 subsets of sum X and one subset of sum < X. For a single dp[k][bitmask] entry, we will iterate over all elements of S which are not in A and try to extend current solution. The answer is "yes" if and only if dp[K][bitmask denoting the whole set S] = 1, and because we try to extend dp states only for k < K, we are sure than dp[K][bitmask] denotes if it is possible to divide a subset denoted by bitmask into K subsets with equal sums (see explanation below). EXPLANATION:Let SUM be the sum of all elements in S. If SUM % K != 0, then the answer is "no" because there is no way to divide elements equally. Let X = SUM / K. Let dp[k][bitmask] = 1 if and only if it is possible to divide a subset A of S denoted by the bitmask into k subsets each with sum X or to divide A into k  1 subsets with sum X and one subset with sum < X. Initially all entries in dp are set to 0. At the beginning, we set dp[0][0] = 1 because it is possible to create 0 subsets with equal sums using no elements. In the main loop, we iterate over all values k = 0, 1, ..., K  1 denoting the number of subsets for which we try to extend our solution and over all subsets of S denoted by a bitmask. Let A be a subset denoted by a bitmask. For a given dp[k][bitmask] we compute how much we have to add to the k+1th subset in order to get k+1 subsets, each with a sum X. We do that by subtracting k * X from the sum of elements in A. Then we iterate over all elements of S which are not in A and we try to extend our solution (see the code below): Let a be an array denoting the set S. I omit the case when SUM % K != 0. for k = 0 to K  1: for bitmask = 0 to 2^n  1: if not dp[k][bitmask]: continue sum = 0 for i = 0 to n  1: if (bitmask & (1LL << i)): sum += a[i] sum = k * x for i = 0 to n  1: if (bitmask & (1LL << i)): continue //there is nothing to extend new_mask = bitmask  (1LL << i) //bitmask denoting a set with ith element added if sum + a[i] == x: //we can fill the k+1 th subset with elements of sum X using a set of elements denoted by new_mask dp[k + 1][new_mask] = 1 else if sum + a[i] < x: //we can fill k subsets with elements of sum X and one subsets with sum < X using a set of elements denoted by new_mask dp[k][new_mask] = 1 if dp[K][2^n  1] == 1: print "yes" else: print "no" Time Complexity: The time complexity per one testcase is O(K * 2^N * N) SOLUTIONS:Author's solution can be found here. Tester's solution can be found here. Editorialist's solution can be found here. RELATED PROBLEMS:To be uploaded soon.
This question is marked "community wiki".
asked 15 Dec '14, 19:36

I spent days trying to solve this problem, but I kept getting WA on the last test. Turns out the problem description is wrong! The problem description makes it clear that empty sets are valid:
The sum of [0, 0, 0] is zero and the sum of an empty set is also zero. answered 16 Dec '14, 14:21
You need to think of one thing here : assigning SANSKAR with 0 intensities is different than assigning nothing at all.
(17 Dec '14, 19:27)

I was just wondering shouldn't it be "Let dp[k][bitmask] = 1 if and only if it is possible to divide a subset A of S denoted by the bitmask into k subsets each with sum X or to divide A into k subsets with sum X and one subset with sum < X." instead of "Let dp[k][bitmask] = 1 if and only if it is possible to divide a subset A of S denoted by the bitmask into k subsets each with sum X or to divide A into k  1 subsets with sum X and one subset with sum < X." answered 16 Dec '14, 17:02

Is there any other solution with out DP ? answered 15 Dec '14, 19:38
Constraint are so small, that no DP is needed, check my solution  http://www.codechef.com/viewsolution/5540066
(15 Dec '14, 19:58)
Yes, I used graphs.
(15 Dec '14, 20:18)
2
@betlista your solution is incorrect and the test cases are weak. It fails for the following test 1 8 3 4 4 5 6 8 3 3 6 Your code produces "no". The correct answer is "yes".
(15 Dec '14, 22:01)
1
As I said, that would be so much fun :D
(15 Dec '14, 22:08)
Recursion: http://www.codechef.com/viewsolution/5552989
(15 Dec '14, 23:45)
It was difficult to stop those wrong solutions with few test cases. We will be careful next time.
(16 Dec '14, 12:23)
1
@damn_me
(17 Dec '14, 15:44)
showing 5 of 7
show all

I tried bruteforcing all possibilities, and managed to get it within the time limit, but am getting WA. I've tried all sorts of test cases, but could'nt find any case where my code fails.. can anyone help me? This is my code: http://www.codechef.com/viewsolution/5593901 Finally I got it accepted here: http://www.codechef.com/viewsolution/5605439 Though it was not clearly mentioned in the problem, in the editorial each of the subsets is assumed to be nonempty. answered 15 Dec '14, 19:44

My solution contains Bitmask DP for 1st task and General DP(subset sum) for the 2nd subtask.
You can already see the Bitmask DP above. I will discuss my approach of subset sum.
For subtask 2 :
I took special care for intensity 0 and duplicate intensities. Sorry for my style of writing code for this problem as I was in a hurry to get 100 pts. Happy Coding :) answered 15 Dec '14, 20:07

@chaitan, since the limit is small , we can solve it using bruteforce. here is my solution http://www.codechef.com/viewsolution/5587907 it gets tricky when sanskar's intensity is 0. suppose there are 3 sanskars and 2 followers and let the intensity be 0{for all 3 sanskars}.But we can give a "yes",since the total sum adds up to 0.But consider 2 sanskars and 3 followers,and intensity with 0. Here the o/p is "no".Though the intensity is 0 , its greater than having no sankars. This might be the problem.This occured to me. answered 15 Dec '14, 20:17
2
Unfortunately, your solution is incorrect and the test cases are weak. It fails for this test: 1 8 3 4 4 6 5 8 3 3 6 The correct answer is "yes". Your code produces "no".
(15 Dec '14, 21:54)

I am getting WA in last test case of SANSKAR... Here is my algo.. I have used simple back tracking and recursion along with dp(by the means of map itachi) to find whether a number is not found previously or not in the same derivation tree... I am getting WA only in the last test case ... Can anyone tell me where my code is wrong... Or provide the test cases for which my code gives WA... Thnx for the help http://www.codechef.com/submit/complete/4469948759548f1837882bb answered 15 Dec '14, 23:04
Your code fails for the test case 1 1 2 0 . Point here is a sanskar with zero intensity and no sanskar are not the same things. Your gode gives yes for this test case, while the answer is no. See here: http://ideone.com/7mjBOX
(15 Dec '14, 23:43)

Even this solution is giving a TLE for second subtask!! answered 16 Dec '14, 02:49
It passes. I'm the editorialist and here is my solution which I described in the editorial: http://www.codechef.com/status/SANSKAR,pkacprzak
(16 Dec '14, 04:43)
@pkacprzak , check this http://ideone.com/EtDTSI this is your solution with Max constraints, giving TLE Even my own DP solution gives TLE for this case though.
(16 Dec '14, 14:55)
(16 Dec '14, 20:14)

I have got a very simple solution by simple bruteforcing, and also got AC. here is the method:
note: in my solution the variable names "big" is "sum", array a is the main set, and array b for subset with sum y, then all the b's elements are removed from a this method does not need dp, only basic knowledge is enough. Here is the link to my answer : my code (http://www.codechef.com/viewsolution/5510448) got AC Please upvote if u have understood, so that many others can get benefitted answered 16 Dec '14, 10:11
This is a wrong solution ! You can't greedily choose a subset with valid sum and remove those elements. Such a solution would most probably fail the following test case: 1 8 3 4 4 5 6 8 3 3 6
(16 Dec '14, 11:52)

i am getting WA for 6 subtasks can you provide any testcase where my solution failed or any bug. here is the link http://www.codechef.com/viewsolution/5600554 answered 16 Dec '14, 11:15

Can't figure out what's wrong with my code. It passes all the test cases. Plzzzz help me! Here is the link to my code http://www.codechef.com/viewsolution/5602314 answered 16 Dec '14, 15:20

Some solution sames not right. For this input: 1 9 3 50 40 30 25 11 5 3 3 1 Answer: yes answered 16 Dec '14, 18:29

Simple brute forcing using recursion works. But for some reason, I was getting WA when I was using a static variable, and later when I rather put that variable as a paramter passed in the function, I got AC. Here is my simple solution with 0.00 time in all test cases: http://www.codechef.com/viewsolution/5597230 answered 16 Dec '14, 21:15

I have one query. My friend made this solution wherein he found all the masks whose sum was (total/k) and pushed them in a vector. After that he did this method of checking. bool flag = false;
Link: http://www.codechef.com/viewsolution/5513412 Can anyone explain me how does this method covers all the subsets of choosing 'k' masks? I know that a recursive method of finding 'k' masks will surely give the correct answer but how does this method do that? Any help is appreciated. answered 17 Dec '14, 12:52

It is really really disappointing to see solutions implementing bruteforce being accepted. Not just that, even wrong solutions are accepted which fail in simple test cases. answered 17 Dec '14, 15:26

bruteforce (backtracking ) is acceped here!!! http://www.codechef.com/viewsolution/5591154 (my solution) answered 17 Dec '14, 18:20

I made an array of all bits configurations with sum S/k(of course,if S%k==0) and used recursive function to choose the configuration for my kth follower,than for the (k1)th.Also to boost my time ,I made some improvements such as taking individually the configurations of 1 and 2 elements(because those pairs are unique). I think I submitted around 30 code sources,before I got 100 points.My question is how cand you say that O(k * (2^n) * n) for EACH TESTCASE can get AC?That complexity takes around 1s for each test case... From now on please give a solution that really fits in the worst testcases or make the constraints lower.It's not nice to see that some stupid optimizations give you 100 points. I'll let my source code here,if someone is interested in it. http://www.codechef.com/viewsolution/5560280 answered 18 Dec '14, 21:28

@ prasadram126 (sum_of_intensities%follower == 0) but ((sum_of_intensities/follower) < max_value_in_list)Check this test case: 9 4 0 0 4 1 5 4 2 0 8Answer should be no but your code output yes. answered 19 Dec '14, 00:58

You may use these test cases to check your code: 5 3 0 0 0 0 0 yes answered 19 Dec '14, 01:03

@ishaangupta it actually isn't trying to choose all the possibilities, it just tries to find whether the required subset is present. The entire thing would be like this: You have a vector with all the masks. Any 2 masks can be used together only when both have no element common, since one Sanskar can be given to only one follower. This is checked using the bitwise &. Further, the condition for having found a successful subset is that all the sanskars have been chosen, and K values are present in the subset selected, this is checked in the if using the 1<<n  1 and other expression. The flow would be of this sort, First the outer for loop selects a starting element. The inner loop selects the values which are 'compatible' with the current selection, I.e. which do not use sanskars already is use, and after finding these values, updates the cur variable to make notice of this addition of a value. And then basically it goes through all values, doing the same thing. If there are n sanskars, then the number ((1<<n)1) simply is a series of 1s, it is the number corresponding to the case when each and every Sanskar has been used. Eg if n is 4, then it would be 1111, which corresponds to the selection of all 4 sanskars. Using this method, if at any point the required condition is reached, we break after setting a flag. answered 20 Dec '14, 19:45

hello everyone I solved this problem with a simple backtracking if you think in the complexity , you will give me the razon answered 21 Dec '14, 02:04

Hi,
answered 21 Dec '14, 17:14

can anyone tell me why don't we do sum of all n intensity and if sum%by k followers == 0 then ans is YES otherwise NO. I only passed starting 2 test cases why this logic is not correct according the statement given into problem. answered 02 Oct '18, 16:56

can anyone tell me why don't we do sum of all n intensity and if sum%by k followers == 0 then ans is YES otherwise NO. I only passed starting 2 test cases why this logic is not correct according the statement given into problem. answered 02 Oct '18, 17:01
