It's compiled pretty fast if N·M ≤ 30 and works, for example, when N = 5 and M = 100 (but it can't be used to calculate answers for a bigger values, because the depth of instantiation doesn't exceed 900 in GNU C++). This solution works correctly, but I have several questions. As you can see, the template
metaFor is instantiated for all values
cur_mask. But this argument
cur_mask is just a simple loop counter. Is there any way to exclude it from arguments of the template without losing the ability to use it for iterating? If it could be done, this program would be able to calculate answers for much bigger N and M. And how can I speed up the above solution?