Advertisement
aslv

Subset Sums

Mar 31st, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Task5
  7. {
  8.     class SubsetSums
  9.     {
  10.         static long s;
  11.         static byte n;
  12.         static long[] a;
  13.         static long count = 0;
  14.  
  15.         static void Sum(byte index, long sumSoFar)
  16.         {
  17.             if (sumSoFar + a[index] == s)
  18.             {
  19.                 count++;
  20.             }
  21.             sumSoFar += a[index];
  22.             for (byte i = (byte)(index + 1); i < n; i++)
  23.             {
  24.                 Sum(i, sumSoFar);
  25.             }
  26.         }
  27.  
  28.         static void Main()
  29.         {
  30.             s = long.Parse(Console.ReadLine());
  31.             n = byte.Parse(Console.ReadLine());
  32.             a = new long[n];
  33.             for (int i = 0; i < n; i++)
  34.             {
  35.                 a[i] = long.Parse(Console.ReadLine());
  36.             }
  37.             for (byte i = 0; i < n; i++)
  38.             {
  39.                 Sum(i, 0);
  40.             }
  41.             Console.WriteLine(count);
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement