zoukankan      html  css  js  c++  java
  • 12627

      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 ≤ 2K.
    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

    解题思路:

      以f(k,i)表示第k小时时前i行的红色气球数,则有如下递归表达式:

      f(k,i) = 2*f(k-1,i)                ,i<=2^(k-1)

          或= 3^(k-1)+f(k-1,i-2^(k-1))     ,i>2^(k-1)

    注意:数据要用long long 类型

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <assert.h>
    using namespace std;
    typedef long long LL;
    LL C(int k){
        LL ans=1;
        for(int i=0;i<k;i++)
            ans*=3;
        return ans;
    }
    LL f(int k, int i){
        //assert(0<=i&&i<=(1<<k));
        if(i<=0) return 0;
        if(k==0) return 1;
        
        if(i<=(1<<(k-1))) return 2*f(k-1,i);
        else return 2*C(k-1)+f(k-1,i-(1<<(k-1)));
    }
    int main(int argc, const char * argv[]) {
        int T;
        scanf("%d",&T);
        int kase =1;
        while(T--){
            printf("Case %d: ",kase++);
            int k,A,B;
            scanf("%d%d%d",&k,&A,&B);
            printf("%lld
    ",f(k,B)-f(k,A-1));
        }
        return 0;
    }
  • 相关阅读:
    利用dockerfile定制镜像
    发布Docker 镜像到dockerhub
    Docker 停止容器
    133. Clone Graph
    132. Palindrome Partitioning II
    131. Palindrome Partitioning
    130. Surrounded Regions
    129. Sum Root to Leaf Numbers
    128. Longest Consecutive Sequence
    127. Word Ladder
  • 原文地址:https://www.cnblogs.com/Kiraa/p/5447271.html
Copyright © 2011-2022 走看看