Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SubsetSums
- {
- class SubsetSums
- {
- static void Main(string[] args)
- {
- long numbersSumS = long.Parse(Console.ReadLine());
- byte numbersCountN = byte.Parse(Console.ReadLine());
- long[] numbers = new long[numbersCountN];
- for (int i = 0; i < numbersCountN; i++)
- {
- numbers[i] = long.Parse(Console.ReadLine());
- }
- int maximumLoops = (int)Math.Pow(2, numbersCountN) - 1;
- int counter = 0;
- for (int i = 1; i <= maximumLoops; i++)
- {
- long currentSum = 0;
- for (int j = 0; j < numbersCountN; j++)
- {
- long bitValue = ((1 << j) & i) >> j;
- if (bitValue == 1)
- {
- currentSum += numbers[j];
- }
- }
- if (currentSum == numbersSumS)
- {
- counter++;
- }
- }
- Console.WriteLine(counter);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment