UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)
Posted SomnusMistletoe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(奇怪的气球膨胀)(递归)的主要内容,如果未能解决你的问题,请参考以下文章
8-12 Erratic Expansion uva12627
Uva 12627 Erratic Expansion(递归)
(记忆化+暴力)UVA - 12627 Erratic Expansion