679-dropping Balls

Source: Internet
Author: User

Dropping Balls

PS: Because the layout of the problem is more troublesome, here OJ Address: uva679-dropping Balls

There is a binary tree with a maximum depth of D, and all leaves have the same depth. All nodes are numbered 1, 2, 3,..., 2d-1 from top to bottom, from left to right. Place a small ball at junction 1 and it will fall. Each inner node has a switch that is initially all closed, and the state changes every time a ball falls on a switch. When the ball reaches an inner node, if the switch on the node is closed, go left, otherwise go right until you reach the leaf junction, 6-2.

Some small balls begin to fall from Node 1, where will the last ball fall? Enter the leaf depth d and the number of balls I, output the last leaf number of the first ball. Suppose I do not exceed the number of leaves of the whole tree. D≤20. Enter up to 1000 sets of data.

Sample input:

4 2
3 4
10 1
2 2
8 128
16 12345

Sample output:

12
7
512
3
255
36358

#include <cstdio>//The method will time out//#include <cstring>//using namespace Std;//const int maxDepth =;////Tree//int tree[1 << maxDepth];////int Main () {//// L Group Data//int l;//// D layer full two fork tree, I small ball//int d,i;//scanf ("%d", &l);//while (l--) {//scanf ("%d%d", &d, &i);//memset (tree, 0, sizeof (tree));//// max. Node Number//int n = (1 << D)-1;//int k;//for (int i = 0; i < i; i++) {//// node Current location//k = 1;//while (true) {//// reverse//Tree[k] =!tree[k];//// false->true, go left subtree//// True->false, take the right sub-tree//if (Tree[k]) {//k = k * 2;//} else {//k = k * 2 + 1;//                }////if (k > N) {//break;//                }//            }//        }//// Last leaf node number//printf ("%d\n", K/2);//    }//return 0;//}intMain () {//L Group data    intL//D layer full two fork tree, I small ball    intD,i;scanf("%d", &l); while(l--) {scanf("%d%d", &d, &i);intK =1;//Analog path         for(inti =0; I < D-1; i++) {//If I is odd, move to left dial hand tree            //I is even, the right sub-tree moves            if(I%2) {k *=2; i = (i +1) /2; }Else{k = k *2+1; I/=2; }        }printf("%d\n", k); }return 0;}

679-dropping Balls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.