https://atcoder.jp/contests/abc115/tasks/abc115_d
thinkingThis problem is a delicate recursive one. Hamburgers of Level-0, level-1 and level-2 are shown on the left, middle and right respectively:
burger.pngFor the level-1 burger, the green part is the level-0 burger.
For the level-2 burger, the green part is the level-1 burger.
When doing a question, you can first list the total number of levels-n and the corresponding P levels.
Then use hand back to solve the problem.
For example, n = 2, x = 7. First remove the bottom B layer, and then calculate the green part of the lower half, a total of 5 layers; then calculate the middle layer. At this time, the cumulative number of layers has reached 7, so it is not necessary to calculate the green part of the upper half and the top B layer.
For details, please refer to the following code. If you cannot understand it for a while, you can substitute n = 2, x = 1; n = 2, x = 2; n = 2, x = 3;...; n = 2, x = 13 into the code one by one for analysis.
AC code#include<bits/stdc++.h> using namespace std; long long burger[52], patty[52]; long long n, x; long long ans = 0; void f(int n) { if(x == burger[n]) // Recursive termination condition { // Test data n=2,x=3 will enter this branch ans += patty[n]; return; } else if(x < burger[n]) { if(x) // x>0 { x--; // The bottom floor is bun, not patty. Take it off if(x > burger[n-1]) { ans += patty[n-1]; x -= burger[n-1]; // Plus patty in the middle ans++; x--; // X > burger [n-1] can be divided into two cases: // First, x > burger [n-1] + 1. Here, 1 refers to the middle patty layer. In this case, go to the following if // The other is x==burger[n-1]+1. In this case, follow else below if(x) { f(n-1); } else // x==0 is the recursive termination condition, which can be omitted { // Test data n=2,x=4 will enter this branch return; } } else // x<=burger[n-1] { f(n-1); } } else // x==0 is the recursive termination condition, which can be omitted { // Test data n=2,x=1 or n=2,x=2 will enter this branch return; } } } int main() { scanf("%lld%lld",&n, &x); burger[0] = 1, patty[0] = 1; // When initializing n = 0 for(int i = 1; i <= 50; i++) { // Calculate the total number of hamburger floors and corresponding patty floors from 1 to 50 floors burger[i] = burger[i-1] * 2 + 3; // Total floors of hamburger patty[i] = patty[i-1] * 2 + 1; // patty level //Cout < "n =" < I < < the total number of Hamburg floors and patty floors: "< burger [i] < < ',' < patty [i] < n '; } f(n); printf("%lld\n", ans); return 0; }
TopCoder & codeforces & atcoder communication QQ group: 648202993