
Untitled
By: a guest on
May 5th, 2012 | syntax:
C++ | size: 1.69 KB | hits: 16 | expires: Never
#include <cstdio>
#include <algorithm>
using std::swap;
const int maxN = 50;
typedef unsigned int uint;
typedef unsigned long long ull;
int n;
uint target;
uint m[maxN];
ull total;
void reorderFirstMax()
{
for (int i = 1; i < n; ++i)
if (m[i] > m[0])
swap(m[0], m[i]);
}
uint flp2(uint x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
return x - ( x >> 1);
}
void countSomePossibilites(uint high)
{
ull withHigh = 0;
ull withoutHigh = 1;
for (int i = 1; i < n; ++i)
{
ull optHigh;
ull optLow;
if (m[i] >= high)
{
optHigh = (m[i] ^ high) + 1;
optLow = high;
}
else
{
optHigh = 0;
optLow = m[i] + 1;
}
ull newWithHigh = withHigh * optLow + withoutHigh * optHigh;
ull newWithoutHigh = withHigh * optHigh + withoutHigh * optLow;
withHigh = newWithHigh;
withoutHigh = newWithoutHigh;
}
if (target == 0)
total += withoutHigh;
else
total += withHigh;
}
bool phase()
{
reorderFirstMax();
if (m[0] == 0 || m[0] < target)
{
if (target == 0)
++total;
return false;
}
uint high = flp2(m[0]);
countSomePossibilites(high);
m[0] ^= high;
target ^= high;
return true;
}
void readData()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%u", &m[i]);
}
int main()
{
readData();
while(phase())
;
--total; //policzylismy pusty krysztal
printf("%Lu\n", total);
}