ellapt

SubSetSums

Dec 14th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. using System;
  2. class SubSetSums
  3. {
  4. static void Main()
  5. {
  6. long s = long.Parse(Console.ReadLine());
  7. int n = int.Parse(Console.ReadLine());
  8. long[] nums = new long[n];
  9. for (int i = 0; i < n; i++)
  10. {
  11. nums[i] = long.Parse(Console.ReadLine());
  12. }
  13. int maxI = 1;
  14. for (int i = 1; i <= n; i++)
  15. {
  16. maxI *= 2;
  17. }
  18. maxI -= 1;
  19. int count = 0;
  20. for (int i = 1; i <= maxI; i++)
  21. {
  22. long currentSum = 0;
  23. for (int j = 0; j < n; j++)
  24. {
  25. int mask = 1 << j;
  26. int nAndMask = i & mask;
  27. int bit = nAndMask >> j;
  28. if (bit == 1)
  29. {
  30. currentSum+=nums[j];
  31. }
  32. }
  33. if (currentSum == s)
  34. {
  35. count++;
  36. }
  37. }
  38. Console.WriteLine(count);
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment