UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)
题意:问k小时后,第A~B行一共有多少个红气球。

分析:观察图可发现,k小时后,图中最下面cur行的红气球个数满足下式:
(1)当cur <= POW[k - 1]时,
dfs(k, cur) = dfs(k - 1, cur);
(2)当cur > POW[k - 1]时,
dfs(k - 1, cur) = 2 * dfs(k - 1, cur - POW[k - 1]) + tot[k - 1];
其中,POW[k - 1]为2^(k - 1),tot[k - 1]为k-1小时后图中的红气球总数,为3^(k - 1)。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
LL tot[35];
LL POW[35];//记录2的次方
void init(){
tot[0] = 1;
POW[0] = 1;
for(int i = 1; i <= 30; ++i){
tot[i] = 3 * tot[i - 1];
POW[i] = 2 * POW[i - 1];
}
}
LL dfs(int k, int cur){
if(cur == 0) return 0;
if(k == 0) return 1;
if(cur <= POW[k - 1]){
return dfs(k - 1, cur);
}
else{
return 2 * dfs(k - 1, cur - POW[k - 1]) + tot[k - 1];
}
}
int main(){
int T;
scanf("%d", &T);
int kase = 0;
init();
while(T--){
int k, a, b;
scanf("%d%d%d", &k, &a, &b);
printf("Case %d: %lld\n", ++kase, dfs(k, POW[k] - a + 1) - dfs(k, POW[k] - b));
}
return 0;
}
UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)的更多相关文章
- UVA - 12627 Erratic Expansion 奇怪的气球膨胀 (分治)
紫书例题p245 Piotr found a magical box in heaven. Its magic power is that if you place any red balloon i ...
- UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)
不难发现,每过一个小时,除了右下方的气球全都是蓝色以外,其他都和上一个小时的气球是一样的,所以是可以递推的.然后定义一类似个前缀和的东西f(k,i)表示k小时之后上面i行的红气球数.预处理出k小时的红 ...
- UVa 12627 Erratic Expansion - 分治
因为不好复制题目,就出给出链接吧: Vjudge传送门[here] UVa传送门[here] 请仔细看原题上的那幅图,你会发现,在时间t(t > 0),当前的气球构成的一幅图,它是由三个时间为( ...
- Uva 12627 Erratic Expansion(递归)
这道题大体意思是利用一种递归规则生成不同的气球,问在某两行之间有多少个红气球. 我拿到这个题,一开始想的是递归求解,但在如何递归求解的思路上我的方法是错误的.在研读了例题上给出的提示后豁然开朗(顺便吐 ...
- UVA 12627 - Erratic Expansion
一个红球能够分裂为3个红球和一个蓝球. 一个蓝球能够分裂为4个蓝球. 分裂过程下图所看到的: 设当前状态为k1.下一状态为k2. k1的第x行红球个数 * 2 ⇒ k2第2*x行的红球个数. k1的第 ...
- uva 12627 - Erratic Expansion(递归求解)
递归的边界条件写的多了--不是必需写呢么多的.. 不明确可共同探讨~ #include<cstdio> #include<iostream> #include<cmath ...
- UVa 12627 奇怪的气球膨胀(分治)
https://vjudge.net/problem/UVA-12627 题意:一开始有一个红气球.每小时后,一个红气球会变成3个红气球和1个蓝气球,而1个蓝气球会变成4个蓝气球.如图所示分别是经过0 ...
- 12627 - Erratic Expansion——[递归]
Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside it ...
- 【数形结合】Erratic Expansion
[UVa12627]Erratic Expansion 算法入门经典第8章8-12(P245) 题目大意:起初有一个红球,每一次红球会分成三红一蓝,蓝球会分成四蓝(如图顺序),问K时的时候A~B行中有 ...
随机推荐
- Linux centosVMware shell 管道符和作业控制、shell变量、环境变量配置文件
一.管道符和作业控制 管道符|,用于将前一个指令的输出作为后一个指令的输入 #cat /etc/passwd|wc -l 作业控制:当运行程序时,可以使它暂停(Ctrl+Z组合键),然后使用fg(f ...
- Outer()函数
转载:https://bbs.pinggu.org/thread-7078237-1-1.html R语言中的outer()函数,名为内积函数,但是他执行的功能并不是解析几何中的内积.那该函数到底发挥 ...
- osg 线框模式,点模式切换
需要加 viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet ...
- django+celery+ RabbitMQ实现异步任务实例
背景 django要是针对上传文件等需要异步操作的场景时,celery是一个非常不错的选择.笔者的项目就是使用了这个组合,这里就做一个备忘吧. 安装RabbitMQ 这个安装及使用我已经在前一 ...
- 「LuoguP3979」遥远的国度
传送门 Luogu 解题思路 带换根操作的树剖. 换根只会影响更新或查询子树信息的操作. 我们始终保持初始的根不变,然后只要分类讨论一下: 假设当前被查询的节点是 \(u\) 如果 \(u\) 就是根 ...
- spring bean容器学习
bean是Spring种最核心的东西 ,如果说Spring是个水桶的话,bean就是桶里面的水,桶里面没有水也就没有意义了. public class MyTestBean { private Str ...
- SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
[oracle@jtwy02 ~]$ sqlplus '/as sysdba' SQL*Plus: Release 11.2.0.4.0 Production on Sat Oct 13 14:14: ...
- centos 7安装nodejs
ps: {install_path} 安装目录路径 1.安装wget yum install wget 2. 下载对应文件 wget -c https://nodejs.org/dist/v8.9.1 ...
- java随记 2月16
1.a=a+b 等于 a+=b ,且a+=b隐含强制类型转换 2.^ 表示异或 两个二进制同号为假,异号为真 即 0^0=0,1^1=0,0^1=1 3.三元运算 布尔表达式 ?表达式 ...
- react 如何引入打印控件 CLodop
下载插件,官网地址 http://www.lodop.net/download.html ,选择综合版,解压下载的文件.直接点击 安装,很简单,就不一一说明了. 复制下面几个文件,到react项目中 ...