【HDU - 5573 Binary Tree】 构造

在一个无限深度的二叉树中,青蛙国王需要通过增加或减少访问节点的值来收集特定数量的灵魂宝石。本文介绍了一种策略,即沿着最左侧路径前进,并通过数学计算确定在哪些节点上应该增加或减少值,以确保最终收集到的目标宝石数量。

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

F - Binary Tree


The Old Frog King lives on the root of an infinite tree. According to the law, each node should connect to exactly two nodes on the next level, forming a full binary tree. 

Since the king is professional in math, he sets a number to each node. Specifically, the root of the tree, where the King lives, is  1 1. Say  froot=1 froot=1

And for each node  u u, labels as  fu fu, the left child is  fu×2 fu×2 and right child is  fu×2+1 fu×2+1. The king looks at his tree kingdom, and feels satisfied. 

Time flies, and the frog king gets sick. According to the old dark magic, there is a way for the king to live for another  N N years, only if he could collect exactly  N Nsoul gems. 

Initially the king has zero soul gems, and he is now at the root. He will walk down, choosing left or right child to continue. Each time at node  x x, the number at the node is  fx fx (remember  froot=1 froot=1), he can choose to increase his number of soul gem by  fx fx, or decrease it by  fx fx

He will walk from the root, visit exactly  K K nodes (including the root), and do the increasement or decreasement as told. If at last the number is  N N, then he will succeed. 

Noting as the soul gem is some kind of magic, the number of soul gems the king has could be negative. 

Given  N N K K, help the King find a way to collect exactly  N N soul gems by visiting exactly  K K nodes.
Input
First line contains an integer  T T, which indicates the number of test cases. 

Every test case contains two integers  N N and  K K, which indicates soul gems the frog king want to collect and number of nodes he can visit. 

  1T100 1≤T≤100

  1N109 1≤N≤109

  N2K260 N≤2K≤260.
Output
For every test case, you should output "  Case #x:" first, where  x x indicates the case number and counts from  1 1

Then  K K lines follows, each line is formated as 'a b', where  a a is node label of the node the frog visited, and  b b is either '+' or '-' which means he increases / decreases his number by  a a

It's guaranteed that there are at least one solution and if there are more than one solutions, you can output any of them. 

Sample Input
2
5 3
10 4
Sample Output
Case #1:
1 +
3 -
7 +
Case #2:
1 +
3 +
6 -
12 +

题意:一颗无限深度的树,根节点编号为1,左右儿子分别为1 << x和1 << x + 1(和线段树的节点编号方法相同)。现需要在访问k个节点的条件下,加上或减去节点编号的值,使之结果为n。


分析:从给的数据范围可以看到n <= 2^k,所以我们可以选择树上最左边的那条路走。n为偶数的情况可以先进行n-1的处理,在最后一个节点的时候使之走向右儿子。

因为2 ^ 0 + 2 ^ 1 + … +2 ^ (k-1) + 1 == 2 ^ k,所以我们先求出左边那条路上的节点值之和sum = 2 ^ k - 1。然后x ==(sum-n)/2,这个x就是我们需要在这条路上减去的值(x的计算可以想想,因为不但不加上,还要减去那个值,所以要除以2)。


代码如下:

#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mod 835672545
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int n, k;
int main(){
    int t, cas = 0;
    scanf("%d", &t);
    while(t--){
        int flag = 0;
        scanf("%d%d", &n, &k);
        if(!(n & 1)){
            n--;
            flag = 1;
        }
        LL sum = ((LL)pow(2, k) - 1 - n) / 2;
        printf("Case #%d:\n", ++cas);
        for(int i = 0; i < k-1; i++){
            if((sum >> i) & 1){
                printf("%I64d -\n", (LL)pow(2, i));
            }
            else    printf("%I64d +\n", (LL)pow(2, i));
        }
        printf("%I64d %c\n", (LL)pow(2, k-1) + (flag ? 1 : 0), (sum>>(k-1)) & 1 ? '-' : '+');
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值