I think it could be improved by not calling the task permutations"permutations". Permutations are rearrangements, it. It seems you are moremostly producing all combinations.
I refer you to these: http://www.cplusplus.com/reference/algorithm/next_permutation/ http://en.wikipedia.org/wiki/Permutation