I just solved a problem that need 3^n recursion 0,1,2 0=not choose, 1=choose one way, 2=choose 2nd way like this, and its messy cause we need the path we took also it itself doesnt seem nice, can you guys please write your ways of writing .
sorry i will reply hours late cause i will sleep rn
Please send the link
https://codeforces.net/problemset/problem/525/E
here you go
You could generate 2^n masks and then iterate over submasks, it'll be 3^n in total (e.g. outer mask 0 => option #1, inner mask 0 => #2, inner mask 1 => #3)
Or just write a recursion, it's not really much code in modern c++ and a lot more flexible
thank you very much!
Similar to the power of two version but you can't use the bitwise operation here. You can also replace
3
with other positive integer to get a more generalized version.link