uva 12627 - Erratic Expansion

题目:

Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside itthen, 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 blueballoons. 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 thefollowing diagram.


As you can see, a red balloon in the cell (i,j) (that is i-th row and j-th column) will multiply toproduce 3 red balloons in the cells (i21,j 21), (i21,j 2), (i2,j 21) and a blueballoon in the cell (i 2, j 2). Whereas, a blue balloon in the cell (i, j) will multiply to produce 4 blueballoons in the cells (i21,j 21), (i21,j 2), (i2,j 21) and (i2,j 2). The grid sizedoubles (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 wouldlike 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 casecontains 3 integers K, A and B. The meanings of these variables are mentioned above. K will be inthe 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] afterK-th hour.

Sample Input

3011318337

Sample Output

Case 1: 1
Case 2: 27
Case 3: 14

此题必须得观察到图的特性,就是对称性。然后观察图就能做出来了。

//
//  main.cpp
//  uva 12627 - Erratic Expansion
//
//  Created by XD on 15/8/14.
//  Copyright (c) 2015年 XD. All rights reserved.
//

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
#define ll long long
using namespace std ;
ll arr2[31] ;
ll arr3[31] ;
ll ans ;

//k表示第几个小时,A表示线
ll kA(ll k , ll A)
{
    if (A == 0) {
        return 0 ;
    }
    if (k == 0) {
        return 1  ;
    }
    if (arr2[k-1] < A) {
        return 2 * arr3[k-1] + kA(k-1, A- arr2[k-1]) ;
    }
    else if(arr2[k-1]==A) {
        return 2 * arr3[k-1] ;
    }
    else{
        return 2 * kA(k - 1, A) ;
    }
}

int main(int argc, const char * argv[]) {
    int k , a , b,T,casenum = 0  ;
    scanf("%d" ,&T)  ;
    arr2[0] = 1 ;arr3[0] = 1 ;
    for (int i = 1 ; i < 31; i++) {
        arr2[i] = 1<<i ;
        arr3[i] = 3 * arr3[i-1] ;
    }
    while (T--) {
        scanf("%d%d%d" ,&k ,&a , &b) ;
        ans = kA(k, b)- kA(k, a-1)   ;
        printf("Case %d: %lld\n" ,++casenum ,ans ) ;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值