ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UVA-12627 Erratic Expansion

2019-02-16 17:44:19  阅读:683  来源: 互联网

标签:blue hour int Expansion will 12627 UVA balloons red


题目链接

https://vjudge.net/problem/UVA-12627

题面

Description

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

题意

按题中所给方式进行扩展,问第k次扩展后\([A,B]\)中的红气球数量

题解

直接递归处理,首先对于每一次扩展后的气球,很容易发现气球数量为\(3^k\),先将这个预处理出来,然后递归处理,

计算从第一列到第B行的红气球数减去第一列到第A-1列的红气球数即为答案,如果要计算的列数大于一半,左半边就可以直接使用预处理出的答案,右边递归处理,不大于一半直接递归即可。

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#define N 50
using namespace std;
long long f[N];
int fpow(int a, int b) {
    int ans = 1;
    while (b) {
        if (b & 1) ans *= a;
        a *= a;
        b >>= 1;
    }
    return ans;
}
long long dfs(int k,int i){
    if(i == 0)
        return 0;
    if(k == 0)
        return 1;
    if(i <= fpow(2, k - 1))
        return 2 * dfs(k - 1, i);
    else
        return 2 * f[k - 1] + dfs(k - 1, i - fpow(2, k - 1));
}
int main() {
    f[0] = 1;
    for (int i = 1; i <= 30; i++) {
        f[i] = 3 * f[i - 1];
    }
    int t;
    scanf("%d", &t);
    int cnt = 0;
    while (t--) {
        int k, a, b;
        cnt++;
        scanf("%d%d%d", &k, &a, &b);
        printf("Case %d: %lld\n", cnt, dfs(k, b) - dfs(k, a - 1));
    }
    return 0;
}

标签:blue,hour,int,Expansion,will,12627,UVA,balloons,red
来源: https://www.cnblogs.com/artoriax/p/10388468.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有