bjfu1277 简单递归

本文介绍了一个简单的递归问题,并提供了一种通过将图形平均分为四个部分来解决该问题的方法。其中三个部分的图形相同,而第四个部分不包含特定元素。通过递归算法计算这些部分的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

比较简单的递归问题。对于第k时刻的图形,可以平均分成四块,左上,右上,左下这三块的图形是一模一样的,右下的那一块不包含红毛僵尸,所以把那三块里的加起来就是结果了。

/*
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
LL three[40];

LL work(int k, int a, int b) {
    int side = 1 << (k - 1);
    if (a > side || b < 1) {
        return 0;
    }
    if (a <= 1 && b >= side) {
        return three[k - 1];
    }
    int aa = a - (1 << (k - 2));
    int bb = b - (1 << (k - 2));
    return 2 * work(k - 1, a, b) + work(k - 1, aa, bb);
}

int main() {
    int T, a, b, k;
    three[0] = 1;
    for (int i = 1; i <= 30; i++) {
        three[i] = three[i - 1] * 3;
    }
    scanf("%d", &T);
    for (int t = 1; t <= T; t++) {
        scanf("%d%d%d", &k, &a, &b);
        printf("Case %d: %lld\n", t, work(k, a, b));
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/moonbay/p/4283549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值