University of Virginia

Source: Internet
Author: User

University of Virginia

 

Zishu example p245

 

Piotr found a magical box in heaven. its magic power is that if you place any red balloon inside it then, after one hour, it will multiply to form 3 red and 1 blue colored balloons. then in the next hour, each of the red balloons will multiply in the same fashion, but the blue one will multiply to form 4 blue balloons. this trend will continue indefinitely.

The arrangements of the balloons after the 0-th, 1-st, 2-nd and 3-rd hour are depicted in the following diwing.

As you can see, a red balloon in the cell (I, j) (that is I-th row and j-th column) will multiply to produce 3 red balloons in the cells (I calculated 2 −1, j calculated 2 −1), (I calculated 2 −1, j calculated 2 ), (I branch 2, j branch 2 −1) and a blue balloon in the cell (I branch 2, j branch 2 ). whereas, a blue balloon in the cell (I, j) will multiply to produce 4 blue balloons in the cells (I need 2 −1, j need 2 −1 ), (I later 2 −1, j later 2), (I later 2, j later 2 −1) and (I later 2, j later 2 ). the grid size doubles (in both the direction) after every hour in order to accommodate the extra balloons. in this problem, Piotr is only interested in the count of the red balloons; more specifically, he wowould like to know the total number of red balloons in all the rows from A to B after K-th hour.

Input

The first line of input is an integer T (T <1000) that indicates the number of test cases. each case contains 3 integers K, A and B. the meanings of these variables are mentioned above. K will be in the range [0, 30] and 1 ≤ A ≤ B ≤ 2 K.

Output

For each case, output the case number followed by the total number of red balloons in rows [A, B] after K-th hour.

Sample Input

3

0 1 1

3 1 8

3 3 7

Sample Output

Case 1: 1

Case 2: 27

Case 3: 14

 

 

At the beginning, there was a red balloon. Every hour, a red balloon would become three red balloons and one blue balloon, and a blue balloon would become four blue balloons ,, after three hours.

Calculate the number of red balloons from row A to row B after the K split according to the split method given in the figure.

So you can get the number of red balloons from row A to row B by subtracting the number of red balloons from Row B in the A-1 line.

Then, observe the diagram for the third and second hours, and find that the diagram for the second hour is the same as that for the third and third images, but the three images for the second and third images are all blue, computing is not required.

Assume that the function f (k, I) represents the k hour, and the number of all the red balls in the first line I, then the answer to the question is f (k, B)-f (k, A-1 ). The solution of f (k, I) is recursive Based on the Size classification of the (k-1) Power of I and 2.

 

# Include <cstring> # include <cstdio> # include <iostream> using namespace std; int T, k, a, B; long c [35]; long f (int k, int I) {if (! I) return 0; if (! K) return 1; if (I <1 <(k-1) return 2 * f (K-1, I); else return f (K-1, i-(1 <(k-1) + 2 * c [k-1]; 1 <(k-1) = 2 to the power of the k-1} int main () {c [0] = 1; for (int I = 1; I <30; I ++) c [I] = 3 * c [I-1]; cin> T; for (int s = 1; s <= T; s ++) {cin> k> a> B; long total = f (k, B) -f (k, A-1); printf ("Case % d: % lld \ n", s, total);} return 0 ;}

 

Related Article

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.