UVA 12627 <递归找规律,紫书p245页例题>

UVA - 12627 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

这个题一开始拿到手都没怎么看明白,然后朋友说紫书上有这个题,去翻了翻,发现这个题的规律所在了,

紫书上的思路是:将整个区域分为上下两块,分别求最上面若干行和最下面若干行的红色气球数,其实我们直接定一个方向求解就好了

AC思路:定方向为向上求解,分别求出B行,A行以上有多少气球,然后用后者减去前者的上一行,即f(b)-f(a-1),得到答案

对于f函数,可以找到这样的规律:

当我们需要求解的行数i小于等于2^(k-1)时,所得的红气球数即为(k-1)时刻的红气球数

当我们需要求解的行数i大于2^(k-1)时,所得的红气球数位2*(k-1)时刻的红气球数+(k-1)时刻(i-2^(k-1))行所包含的红气球数

其实拿题目所给的图来讲,就会发现,eg 第三张图片,前4行所包含的红气球总数为第二张图片的2倍,大于4行往下的气球数,每一行包含的红气球数和第二张图片每一行所包含的红气球数一致,所以用递归能够求解出来。

注意一点优化,每一个完整的区域,即k时刻全部的红色气球数量为3的k次方个,详情看代码。

AC代码:

#include<cstdio>
#include<algorithm>
#include<set>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
long long san[35]={1};
long long  f(int k,int i){
    if(i<=0)
        return 0;
    if(k==0)
        return 1;
    if(i<=pow(2,k-1))
        return 2*f(k-1,i);
    else
        return 2*san[k-1]+f(k-1,i-pow(2,k-1));//根据上面的讲解,其实这里可以写成2*f(k-1,pow(2,k-1))+f(k-1,i-pow(2,k-1)),但是提交的时候会超时,后来发现可以直接算出来就没必要用递归来求了
}
int main ()
{
   int t;
   scanf("%d",&t);
   int cou=1;
   for(int i=1;i<=30;i++)
    san[i]=3*san[i-1];

   while(t--){
        int k,a,b;
        scanf("%d%d%d",&k,&a,&b);

        printf("Case %d: %lld\n",cou++,f(k,b)-f(k,a-1));
   }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值