UVA - 12627

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 ≤ 2K.

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

图形的变化可以理解成:把上一次的图形在左上,右上,左下复制三次,右下以蓝色填充,则可以知道红色总数是上一次的三倍。
求B行到C行的红色数,则可以转化为求B到末尾行的数目减去C到末尾行的数目,由于第K小时的图形是由第K-1小时的图形组成的,可以利用递推分别求上下两部分的红色数量。

#include <iostream>

using namespace std;
 long long T,i,r,j,k,a,b,c;
 long long s[40],len[40];
 long long g( long long x, long long y){
     long long re;
    if(y<=0||y>len[x]){
        return 0;
    }
    if(x<=0){
        return 1;
    }
    if(y<len[x-1]){
        re=g(x-1,y);
    }else{
        re=g(x-1,y-len[x-1])*2+s[x-1];
    }
    return re;
}
int main()
{

    s[0]=1;len[0]=1;
    for(i=1;i<=32;i++){
        s[i]=s[i-1]*3;
        len[i]=len[i-1]*2;
        //cout<<s[i]<<endl;
        //cout<<g(i,len[i])<<endl;
    }

    cin>>T;
     for(k=1;k<=T;k++){
        cin>>a>>b>>c;
        r=g(a,len[a]-b+1)-g(a,len[a]-c);
        cout<<"Case "<<k<<": ";
        cout<<r<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值