How do I generate all the submasks for a given mask?

Rating changes for the last round are temporarily rolled back. They will be returned soon.
×

# | User | Rating |
---|---|---|

1 | tourist | 3557 |

2 | Radewoosh | 3468 |

3 | Um_nik | 3429 |

4 | Petr | 3354 |

5 | Benq | 3286 |

6 | mnbvmar | 3280 |

7 | LHiC | 3276 |

8 | wxhtxdy | 3258 |

9 | ecnerwala | 3214 |

10 | yutaka1999 | 3190 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 191 |

2 | Radewoosh | 180 |

3 | tourist | 173 |

4 | Vovuh | 167 |

5 | antontrygubO_o | 166 |

6 | PikMike | 164 |

7 | rng_58 | 160 |

8 | majk | 157 |

9 | Um_nik | 155 |

9 | 300iq | 155 |

How do I generate all the submasks for a given mask?

↑

↓

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Oct/23/2019 15:13:16 (e1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Auto comment: topic has been updated by skpro19 (previous revision, new revision, compare).The easiest way I know is:

If you want 0 as a submask:

You have to know if we want to do something with the submasks of every mask till

2^Nthe time complexity will beO(3^N)notO(4^N).Thanks man! It was really helpful.

Can somebody explain please, why is it 3^n ?

$$$3^n=\sum_{k=0}^{n} \binom{n}{k}2^k$$$ by binomial expansion. $$$2^k$$$ is number of submasks if we have $$$k$$$ ones. $$$\binom{n}{k}$$$ is number of masks with $$$k$$$ ones.

Oh, thanks. Ive just read it here https://cp-algorithms.com/algebra/all-submasks.html :)

Better way to think about it is, if you have N items, then each item being considered is either 1. not in the mask, 2. in the mask but not in the submask, 3. in the submask. So 3^N.

Time complexity = O(3^n) if we are generating all submasks for all 2^n numbers for the n bit number else for a particular number having k out of n bits set complexity is O(2^k)