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
inde nitely. 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
cnt(i,j)表示第i分钟后上j行的红气球数。
分类讨论进行递推即可。
注意边界条件。
不用求出来所有状态,用搜索比动规好。
#include<cstdio>
long long pow[35];
long long cnt(long long k,long long p)
{
if (p==0) return 0;
if (k==0) return 1;
long long x=1<<(k-1);
if (p>=x) return 2*pow[k-1]+cnt(k-1,p-x);
return 2*cnt(k-1,p);
}
int main()
{
long long a,b,i,j,k,m,n,p,q,y,z;
int T,x;
scanf("%d",&T);
x=0;
pow[0]=1;
for (i=1;i<=33;i++)
pow[i]=pow[i-1]*3;
while (T--)
{
x++;
scanf("%lld%lld%lld",&k,&a,&b);
printf("Case %d: %lld\n",x,cnt(k,b)-cnt(k,a-1));
}
}