little monkey falling
Time Limit:
3000
ms | Memory Limit:
65535
KB
Difficulty:
3
- describe
-
There is a binary tree with a maximum depth of D, and all leaves have the same depth. All nodes are numbered from left to right and from top to bottom as 1, 2, 3, ···, 2 to the D power minus 1. Put a little monkey at node 1 and it will run down. There is a switch on each inner node, which is initially closed. When a little monkey runs to a switch, its state will change. When reaching an inner node, if the switch is closed, the little monkey will turn left. Go, otherwise go right until you reach the leaf node.
Some little monkeys start running down from node 1, where will the last little monkey run?
- enter
-
Enter the depth D of the leaves of the binary tree, and the number of little monkeys I, assuming that I does not exceed the number of leaves in the entire tree, D<=20. Finally, it ends with 0 0
- output
- Output the leaf number where the ith little monkey is located.
- sample input
-
4 2 3 4 0 0
- Sample output
-
12 7
#include<iostream> #include<algorithm> #include<vector> using namespace std; // 1 int main() // 2 3 { // 4 5 6 7 int d,s; // 8 9 10 11 12 13 14 while(cin>>d>>s,s&&d) { vector< int > vec(100000,0); int k=1; for(int i=0;i<s;i++) { k=1; for(int i=1;i<d;i++) { if (vec [k] == 0) { vec [k] = 1; k*=2; } else { vec [k] = 0; k=k*2+1; } } } cout<<k<<endl; } }