您的位置:首页 > 其它

uva 12627 - Erratic Expansion

2015-08-14 21:51 246 查看
题目:

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 (i∗2−1,j
∗2−1),
(i∗2−1,j
∗2), (i∗2,j
∗2−1)
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 (i∗2−1,j
∗2−1),
(i∗2−1,j
∗2), (i∗2,j
∗2−1)
and (i∗2,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: