题目链接

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);elsereturn 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;
}

转载于:https://www.cnblogs.com/artoriax/p/10388468.html

UVA-12627 Erratic Expansion相关推荐

  1. Uva 12627 Erratic Expansion(不稳定膨胀)

    题目链接 : Erratic Expansion 大致题意: 开始时有一个红气球,每过一段时间,气球会膨胀, 一个红气球膨胀成三个红气球,一个蓝气球膨胀成四个蓝气球(如图) 题目输入正整数n表示n组数 ...

  2. UVa 12627 Erratic Expansion - 分治

    因为不好复制题目,就出给出链接吧: Vjudge传送门[here] UVa传送门[here] 请仔细看原题上的那幅图,你会发现,在时间t(t > 0),当前的气球构成的一幅图,它是由三个时间为( ...

  3. UVa 12627 - Erratic Expansion

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. uva 12627——Erratic Expansion

    题意:一开始有1个红气球,每小时一个红气球都会变成3个红气球和1个蓝气球,1个蓝气球会变成4个蓝气球,问k个小时后a行到b行的红气球的数量. 思路:递推.a为偶数时,计算a+1到b以及a本行的红气球数 ...

  5. Uva 12627 Erratic Expansion

    这道题大体意思是利用一种递归规则生成不同的气球,问在某两行之间有多少个红气球. 我拿到这个题,一开始想的是递归求解,但在如何递归求解的思路上我的方法是错误的.在研读了例题上给出的提示后豁然开朗(顺便吐 ...

  6. UVA 12627 Erratic Expansion

    题目大致意思为 告诉你一个红球为0时状态,已知一个红球每过一个时间会变为文档所示图中的3红1蓝,而蓝球每过一个时间则会变为4个蓝球.问在某一时刻,h1与h2之间的红球有几个. RT,这一题刚看到的时候 ...

  7. UVA - 12627 Erratic Expansion : 递归

    题目点此跳转 思路  题目意思是(书上原话)一开始有一个红气球.每小时后,一个红气球会变成3个红气球和一个蓝气球,一个蓝气球会变成4个蓝气球,如图所示分别是经过0, 1, 2, 3小时后的情况.经过k ...

  8. Uva - 12627 - Erratic Expansion

    找到递推公式,题目就引刃而解了.数学公式博文没办法写,就写在了word上,切过来了: 用 f 或者 g 都能求出,我两个方法都试了,结果用 g 要比 f 快了近一倍,原本以为 f 会快些,可能是因为往 ...

  9. UVA - 12627 - Erratic Expansion(找规律递归)

    递归找规律即可,用前b行减去前a-1行的红气球个数求解,细节见代码 #include<cstdio> #include<cstring> #include<cstdlib ...

  10. uva 12627 Erratic Expansion

    原题: Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside ...

最新文章

  1. VS2015--win32project配置的一些想法之在 Visual Studio 2015 中进行调试的同一时候分析性能...
  2. If语句:你们到底想把我放到哪儿?
  3. 关于解决mybase7继续使用的方法
  4. 成功数据恢复一例LINUX EXT3 下误删除ORACLE数据库
  5. 2020年上半年美团夜宵指数发展报告
  6. 这些.Net的细节(面试秘笈),你都知道了吗?
  7. 学习笔记--asp.net母版页(转自msdn,仅为自己学习存储和有意读者使用)
  8. 小明滚出---响应对象HttpServletResponse和请求对象HttpServletRequest实例
  9. careercup-递归和动态规划 9.2
  10. NBA Top Shot过去24小时二级市场交易额突破630万美元
  11. LeetCode(821)——字符的最短距离(JavaScript)
  12. UVALive6050 Primes【素数筛选+前缀和】
  13. [GDAL]3.影像金字塔构建
  14. python正则匹配_Python中的正则表达式(re)
  15. 武汉大花岭科目三考试说明
  16. oracle 数据库 有坏快,ORACLE数据库坏块的处理 (处理无对象坏快的方法)
  17. mysql实验报告4_实验四∶数据库安全性实验报告.doc
  18. 解读SIM卡、USIM卡、UICC卡、eSIM卡的区别
  19. Comparator--比较器
  20. 苹果基带坏了怎么办_iPhone12 上市,苹果这次有哪些改变

热门文章

  1. 腾讯云增值税发票OCR
  2. PAT甲级-1021 Deepest Root(25分)
  3. 苹果手机语音备忘录在哪_苹果手机备忘录误删除别慌,教你免费恢复
  4. 终端python版本管理切换
  5. Jquery的parent和closet方法
  6. html在线客服插件,在线客服插件
  7. java 在数组末尾添加元素和在任意位置删除元素
  8. android滴滴开源,DoKit - 滴滴开源的一款功能齐全的客户端( iOS 、Android )研发助手...
  9. 【转】移动电子商务的现状与问题
  10. DENTON电源维修美国丹顿电源维修HVCEB-6-3