Hdu2175 tower IX

Source: Internet
Author: User
Tower IX Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 775 accepted submission (s): 462


Problem description1, 2,..., n indicates n plates. The number of plates is big. N are placed on 1st columns. The tray cannot be placed on a small disk.
The plates on the 1st columns are a [1], a [2],..., A [n]. A [1] = n, a [2] = n-1 ,..., A [n] = 1. that is, a [1] is the lowest
Plate above. Move n plates to 3rd columns. Only one plate can be moved at a time, and the tray cannot be placed on a small disk.
Ask the M-mobile plate.
Input two integers n (1 ≤ n ≤ 63) per line, m ≤ 2 ^ n-1.n = m = 0 exit
Output The number of plates that are moved MB.
Sample Input
63 163 20 0
 
Sample output
12
 

The idea is to first find the first few disks that can be completely moved at least, and then determine whether the disks are just completed. If the disks are just completed, the last one must be 1. If the disks are not completed, the next one is the next one completed this time, so we can determine if there is just one more. If there is one more, the last one will be it; otherwise, we will take the remaining number-1 (because we need to take a big one, if you don't understand it, think about it.) Repeat the above operation.
# Include <iostream>
# Include <cstring>
Using namespace STD;
Int N, lstn;
Long long M, ANS, num [10000];
Long long MYPOW (int x)
{
If (Num [x]) return num [x];
If (x = 0) return 1;
Num [x] = 2 * MYPOW (x-1 );
Return num [x];
}


Long long test (long mm ){
Lstn = 1;
While (MYPOW (lstn)-1 <mm ){
Lstn ++;
}

If (MYPOW (lstn)-1 = mm) return 0;
Else if (MYPOW (lstn-1) = mm) Return-1;
Else return mm-MYPOW (lstn-1 );
}


Int main (){
Memset (Num, 0, sizeof (Num ));
While (CIN> N> M & N ){
Ans = test (m );
While (ANS> 0 ){
Ans = test (ANS );
}
If (ANS = 0) cout <1 <Endl;
Else cout <lstn <Endl;


}
}

Hdu2175 tower IX

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.