原题:
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行之间有多少个红气球。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll K,A,B;
ll power3[31],power2[31];ll dfs(ll i,ll j)//i小时  j行
{if(j<=0)return 0;if(i==0)return 1;if(j<=power2[i-1])return dfs(i-1,j)*2;elsereturn power3[i-1]*2+dfs(i-1,j-power2[i-1]);
}int main()
{ios::sync_with_stdio(false);power2[0]=power3[0]=1;for(int i=1;i<=30;i++){power3[i]=power3[i-1]*3;power2[i]=power2[i-1]*2;}int t;cin>>t;int k=1;while(t--){cin>>K>>A>>B;cout<<"Case "<<k++<<": "<<dfs(K,B)-dfs(K,A-1)<<endl;}return 0;
}

解答:

紫书上面的例题

规律很好找,设第n个图的样式为f(n),那么如图,X代表蓝气球

由于n值可以取到30,所以没有办法打表,只能一个一个的算。

设dfs(i,j)为第i个小时的以后,前j行的红气球数。

分成两种情况,当j大于2n−12n−12^{n-1}时,红气球的个数可以递推给3n−1×2+dfs(i−1,j−2n−1)3n−1×2+dfs(i−1,j−2n−1)3^{n-1}×2+dfs(i-1,j-2^{n-1})

其中3n−13n−13^{n-1}是f(n-1)的图样式时红气球的个数,很好理解。

当j小于2n−12n−12^{n-1}时,红气球的个数可以递推为2×dfs(i−1,j)2×dfs(i−1,j)2×dfs(i-1,j)

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 ...

最新文章

  1. Visual Paradigm 教程[UML]:如何在SoaML中建模多方服务?
  2. linux wps 中文输入法_linux_从windows到ubuntu再到manjaro
  3. 推荐系统笔记(常见架构)
  4. Thymeleaf中设置每个页面引入公共css样式
  5. 中两个数做减法_四年级数学下册 | 第1单元加、减法的意义和各部 分之间的关系(P13)...
  6. 《大道至简》第七八章读后感
  7. 四十三、Scrapy 爬取前程无忧51jobs
  8. WebJars——web端静态资源的jar包
  9. Java认证授权框架Spring Security介绍
  10. springboot2.x 与 elasticsearch2.4.x整合出错:None of the configured nodes are available
  11. (转)C#网络编程(基本概念和操作) - Part.1
  12. 如何找出MySQL数据库中的低效SQL语句
  13. Nagios监控平台完全攻略 (二)
  14. PHP 接收 UDP包_UDP详解(广播,组播)(转)
  15. 王琪你计算机学院,计算机学院“计忆时光”2019元旦联欢会暨年度颁奖典礼圆满举行...
  16. 对话机器人---智能客服
  17. 11最接近target的值
  18. 一个长方体玻璃容器从里面量长宽_葡萄干这样吃,功效翻倍,含铁量是葡萄的15倍!葡萄干的功效和作用...
  19. 【译】Objectively Speaking 2: A Crash Course in Objective-C for iOS 6
  20. IT 技能发展:10 大基本 IT 技能(精通一半你就是人才)

热门文章

  1. NDK学习笔记:一起来变萝莉音!FMOD学习总结(下)
  2. 4399笔试面试——感受
  3. 2022生物发酵展(济南)紧扣行业脉搏,把握市场动向,突破变局
  4. 痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU启动那些事(3)- Serial Downloader模式(sdphost/MfgTool)...
  5. 深度学习100问之提高深度学习模型训练效果(调参经验)
  6. 3.3 Options
  7. 欧拉的问题:凸多边形划分为三角形的方法数
  8. 苹果手机与手表怎么通信_苹果手表是新的入门手机
  9. DTCC2022 | openGauss打造企业级开源数据库,服务行业核心系统
  10. centos 7.6——Nginx中rewrite模块应用(location)——基于域名的跳转等