UVA-12627 erratic Expansion strange balloon swell (divided)

Source: Internet
Author: User

Purple Book Example p245

Piotr found a magical box in heaven. It magic power is so if you place any red balloon inside it then, after one hour, it'll multiply to form 3 red and 1 Blue colored balloons. Then on the next hour, each of the red balloons would multiply in the same fashion, but the blue one would multiply to form 4 Blue Balloons. This trend'll continue indefinitely.

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

As can see, a red balloon in the cell (I, J) (which is i-th row and j-th column) would multiply to produce 3 red balloon s in the cells (i∗2−1, j∗2−1), (i∗2−1, j∗2), (I∗2, j∗2−1) and a blue balloon in the cell (i∗2, j∗2 ). Whereas, a blue balloon in the cell (I, j) would multiply to produce 4 blue balloons in the cells (i∗2−1, j∗2−1), ( i∗2−1, J∗2), (I∗2, j∗2−1) and (I∗2, j∗2). The grid size doubles (in both the direction) after every hour in order to accommodate the extra balloons. In this problem, Piotr are only interested in the count of the red balloons; More specifically, he would like to know the total number of the red balloons in all the rows from A to B after k-th hour.

Input

The first line of input was an integer t (T < a) that indicates the number of test cases. Each case contains 3 integers K, A and B. The meanings of these variables is mentioned above. K'll is in the range [0, +] 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

Test instructions: At first there was a red balloon, every hour, a red balloon would turn into 3 red balloons and a blue balloon, and a blue balloon would turn into 4 blue balloons, after three hours of change.

According to the method of splitting the balloon given in the figure, the number of red balloons in line A to line B after K division is obtained.

As you can imagine, the number of red balloons in line A to line B can be obtained by subtracting the number of red balloons in the A-1 row with the number of the red balloon in the first B row.

Then look at the third hour and the second hour of the figure, found that the second hour of the figure and the third picture is divided into four pieces of the same three pieces, and not the same piece is all blue, do not need to calculate.

Assuming that function f (k,i) represents the number of all red balls in the first row of k hours, the answer to the question is F (k,b)-F (k,a-1). The solution of F (k,i) needs to be discussed according to the size of I and 2 (k-1) sub-square, recursive solution.

#include <cstring>#include<cstdio>#include<iostream>using namespacestd;intt,k,a,b;Long Longc[ *];Long LongFintKinti) {    if(!i)return 0; if(!k)return 1; if(i<1<< (K-1))        return 2*f (K-1, i); Else        returnF (K-1, I-(1<< (K-1)))+2*c[k-1];1<< (K-1) =2 K-1 times}intMain () {c[0]=1;  for(intI=1;i< -; i++) C[i]=3*c[i-1]; CIN>>T;  for(ints=1;s<=t; s++) {cin>>k>>a>>b; Long LongTotal=f (K,b)-F (k,a-1); printf ("Case %d:%lld\n", S,total); }    return 0;}

UVA-12627 erratic Expansion strange balloon swell (divided)

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.