Crystals
Time limit1sMemory limit128 MB
Count tuples a_i with 0 <= a_i <= m_i whose XOR is zero and whose sum is at least 1, with n <= 50 and bounds near 2^32.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Dynamic programming, Combinatorics, Math
- Solved
- No attempts yet
Problem
Byteman is a scientist who studies how crystals form from the atoms of different elements. He has designed a special process for growing crystals and derived a formula that describes which combinations of atoms make a valid crystal. He now wants to know how many different crystals his process can produce.
For non-negative integers and , let denote their bitwise exclusive or (XOR). On single bits it is defined by and .
There are elements, numbered from to . For each element there is an upper bound on the number of atoms of that element that a single crystal may contain. A crystal that uses atoms of element (for ) can be formed if and only if:
- for every ,
- , and
- .
The last condition simply states that every crystal must contain at least one atom. Two crystals are considered different if they differ in the number of atoms of at least one element.
Write a program that reads the number of elements and the per-element upper bounds, computes the number of different crystals that can be formed, and prints that number.
Input
The first line contains the number of elements (). The second line contains positive integers separated by single spaces, where .
Output
Print a single integer: the total number of different crystals that can be formed. This number is guaranteed to be smaller than .
Example
For the input with and upper bounds , the answer is . Written as , the five crystals are , , , , and .