UVALive12627 Erratic Expansion【组合+递推】

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 diagram.

  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 ∗ 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) will 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 is only interested in the count of the red balloons; more specifically, he would 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

问题链接UVALive12627 Erratic Expansion

问题描述:(略)

问题分析

  这个题的关键是找出递推函数来。代码是CV来的,不解释。

程序说明:(略)

参考链接:(略)

题记:(略)

AC的C++语言程序如下:

/* UVALive12627 Erratic Expansion */

#include <iostream>

using namespace std;

// how many red balloons after k hours
long long c(int i) {
  return i == 0 ? 1 : c(i - 1) * 3;
}

// how many red balloons in the first i rows, after k hours
long long f(int k, int i) {
    if(i == 0) return 0;
    if(k == 0) return 1;

    long long k2 = 1 << (k - 1);
    if(i >= k2) return f(k - 1, i - k2) + 2 * c(k - 1);
    else     return 2*f(k-1,i);
}

int main()
{
    int t;
    cin >> t;
    for(int i = 1; i <= t; i++) {
        int k, a, b;
        cin >> k >> a >> b;
        cout << "Case " << i << ": " << f(k, b) - f(k, a - 1) << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/82250695