using
System;
using
System.Collections.Generic;
using
System.Linq;
namespace
NumberOfSubsequence
{
class
Program
{
static
void
Main(
string
[] args)
{
int
[] arr = { 5, 7, 9, 12 };
NumberOfSubsequence(arr);
}
static
void
NumberOfSubsequence(
int
[] arr)
{
int
maxElement = arr.Max();
string
maxBinary = Convert.ToString(maxElement, 2);
Dictionary<
int
,
int
[]> dic =
new
Dictionary<
int
,
int
[]>();
foreach
(
int
i
in
arr)
{
string
strr = Convert.ToString(i, 2);
if
(strr.Length < maxBinary.Length)
{
int
diff = maxBinary.Length - strr.Length;
strr =
new
string
(
'0'
, diff) + strr;
}
int
zeros = strr.Length - strr.Replace(
"0"
,
""
).Length;
int
ones = strr.Length - strr.Replace(
"1"
,
""
).Length;
dic.Add(i,
new
int
[] { zeros, ones });
}
List<List<
int
>> allCombinations =
new
List<List<
int
>>();
allCombinations.Add(
new
List<
int
>());
foreach
(
int
i
in
arr)
{
List<List<
int
>> newCombinations =
new
List<List<
int
>>();
foreach
(List<
int
> j
in
allCombinations)
{
List<
int
> combination =
new
List<
int
>(j);
combination.Add(i);
newCombinations.Add(combination);
}
allCombinations.AddRange(newCombinations);
}
int
count = 0;
for
(
int
i = 1; i < allCombinations.Count; i++)
{
int
sum0 = 0;
int
sum1 = 0;
foreach
(
int
j
in
allCombinations[i])
{
sum0 += dic[j][0];
sum1 += dic[j][1];
}
if
(sum0 == sum1)
{
count += 1;
}
}
string
str = Convert.ToString(count, 2);
if
(str.Length < maxBinary.Length)
{
int
diff = maxBinary.Length - str.Length;
str =
new
string
(
'0'
, diff) + str;
}
Console.WriteLine(str);
}
}
}