分治处理问题,需要注意的是用一个数组d[i]记录第i次分裂过后红气球的总数(也就是3 ^ i)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
LL dp[50]; /*第i次分裂之后1到该行红气球的总数?*/
LL dfs_top(int k,int i){
LL top = 1 << (k - 1);
if(k == 0)
return i >= 1;
if((1 << k) == i)
return dp[k];
if(i <= top)
return 2 * dfs_top(k - 1, i);
return 2 * dp[k - 1] + dfs_top(k - 1, i - top);
}
int main(){
int T;
int Case = 1;
for(int i = 0 ; i < 35 ; i++){
double t = pow(1.0 * 3,1.0 * i);
dp[i] = (LL)t;
}
scanf("%d",&T);
while(T--){
int time,x,y;
scanf("%d%d%d",&time,&x,&y);
LL ans = dfs_top(time,y) - dfs_top(time,x - 1);
printf("Case %d: %lld\n",Case++,ans);
}
return 0;
}