5573 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 11. Say froot=1froot=1.
And for each node uu, labels as fufu, the left child is fu×2fu×2 and right child is fu×2+1fu×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 NN years, only if he could collect exactly NNsoul 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 xx, the number at the node is fxfx (remember froot=1froot=1), he can choose to increase his number of soul gem by fxfx, or decrease it by fxfx.
He will walk from the root, visit exactly KK nodes (including the root), and do the increasement or decreasement as told. If at last the number is NN, 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 NN, KK, help the King find a way to collect exactly NN soul gems by visiting exactly KK nodes.
Input
First line contains an integer TT, which indicates the number of test cases.
Every test case contains two integers NN and KK, which indicates soul gems the frog king want to collect and number of nodes he can visit.
⋅⋅ 1≤T≤1001≤T≤100.
⋅⋅ 1≤N≤1091≤N≤109.
⋅⋅ N≤2K≤260N≤2K≤260.
Output
For every test case, you should output " Case #x:
Then KK lines follows, each line is formated as 'a b', where aa is node label of the node the frog visited, and bb is either '+' or '-' which means he increases / decreases his number by aa.
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 +
這是一道思維題差不多的吧,一個向下延伸的二叉樹,問能否在b步的限制下獲得a的總價值~~;
我們可以注意得到的是2^b>=a,也就是說b與二進位制下的a有關~~
於是我們可以從常理上推得到,只要從1 2 4 8 16這樣的從最左邊走,一定能夠在n(2^n剛好>=a)的步數下到達。如果a是2的冪的話,最後一步只要走右邊那條路就好了。
而如果多餘的步數我們可以吧最後一步的(+)改為減(可以得出一定是+),然後向這個最後一步的兩倍的下方延伸。
比如最後一步是+15,我們可以把多餘的步數(假如多3步)改為-15 -30 -60 +120.這樣的話,就能完美抵消多餘步數的影響了。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define ll long long int
ll a,b;
ll su[70];
bool check(){
su[0]=1;
for(int i=1;i<=62;i++){
su[i]=su[i-1]*2;
}
}
int main(){
int te;
cin>>te;
check();
for(int cas=1;cas<=te;cas++){
printf("Case #%d:\n",cas);
cin>>a>>b;
if(a==1&&b==1){
cout<<"1 +"<<'\n';
continue;
}
int mi=lower_bound(su+0,su+62,a)-su;
// cout<<mi<<endl;
if(su[mi]==a){
queue<char>q;queue<ll>qu;
ll now=1;
for(int i=1;i<mi;i++){
qu.push(now);
q.push('+');
now*=2;
}
for(int i=mi;i<b;i++){
qu.push(now);
q.push('-');
now*=2;
}
qu.push(now+1);
q.push('+');
while(!q.empty()){
cout<<qu.front()<<" ";
cout<<q.front()<<"\n";
qu.pop();
q.pop();
}
}
else{
ll now=1;
ll md=su[mi]-a;
// cout<<md<<endl;
int spot=md%2;md/=2;
queue<char>q;queue<ll>qu;
for(int i=1;i<mi;i++){
qu.push(now);
if(md%2)q.push('-');
else q.push('+');
now*=2;
md/=2;
}
if(spot==0)
now+=1;
for(int i=mi;i<b;i++){
qu.push(now);
q.push('-');
now*=2;
}
qu.push(now);
q.push('+');
while(!q.empty()){
cout<<qu.front()<<" ";
cout<<q.front()<<"\n";
qu.pop();
q.pop();
}
}
}
}
相關推薦
HDU 5573 Binary Tree (構造)
mat tom 方式 title span cat 但是 them special Binary Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
5573 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 l
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 l
HDU5573-Binary Tree(思維)
題目意思: 你是一隻青蛙,你在一個無窮大的二叉樹上,按照線段樹那種標號方式就像下面這個一樣。 1234567 上圖只是意思一樣,這個二叉樹是無窮大的,每個節點你可以得到標號數量的續命寶石或者失去該數量的續命寶石。你要從第1層走到第k層,走k步,對於每個節點你可
LeetCode 543. Diameter of Binary Tree (二叉樹的直徑)
tween res edge public level 距離 sent java dot Given a binary tree, you need to compute the length of the diameter of the tree. The diamet
LeetCode 226 Invert Binary Tree(轉換二叉樹)
public pretty mar containe ret clas move 出錯 uil 翻譯 將下圖中上面的二叉樹轉換為以下的形式。詳細為每一個左孩子節點和右孩子節點互換位置。 原文 如上圖 分析 每次關於樹的題目出
hdu 6161--Big binary tree(思維--壓縮空間)
style ons desc stream 我們 value chan 向上 while 題目鏈接 Problem Description You are given a complete binary tree with n nodes. The root no
Binary Tree(偽二叉樹)
right div bin esc nta i++ bsp repr 相減 Description Background Binary trees are a common data structure in computer science. In this prob
LeetCode 145. 二叉樹的後序遍歷(Binary Tree Postorder Traversal)
binary 算法 一個 rsa 記錄 輸出 struct stack roo 題目描述 給定一個二叉樹,返回它的 後序 遍歷。 示例: 輸入: [1,null,2,3] 1 2 / 3 輸出: [3,2,1] 進階: 遞
Gym101158G Placing Medals on a Binary Tree(二進制模擬)
EDA eps sin als == const 路徑 map n) Gym101158G-Placing Medals on a Binary Tree 題意 一顆完全二叉樹,給出n個點,xi的值表示深度為xi的點。問能否在當前狀態下使得從根節點到該點的路徑中不會遇到其他
110. Balanced Binary Tree(python+cpp)
題目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree
LeetCode94. 二叉樹的中序遍歷(Binary tree Inorder Traversal)
題目描述 給定一個二叉樹,返回它的中序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,3,2] 進階: 遞迴演算法很簡單,你可以通過迭代演算法完成嗎? 遞迴思路: 每次先判斷樹是否為空,
1151 LCA in a Binary Tree(30 point(s))
1151 LCA in a Binary Tree(30 point(s)) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V
LeetCode演算法題-Minimum Depth of Binary Tree(Java實現)
這是悅樂書的第168次更新,第170篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第27題(順位題號是111)。給定二叉樹,找到它的最小深度。最小深度是沿從根節點到最近的葉節點的最短路徑上的節點數。葉子節點是沒有子節點的節點。例如: 給定二叉
LeetCode演算法題-Balanced Binary Tree(Java實現)
這是悅樂書的第167次更新,第169篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第26題(順位題號是110)。給定二叉樹,判斷它是否是高度平衡的。對於此問題,高度平衡二叉樹定義為:一個二叉樹,其中每個節點的兩個子樹的深度從不相差超過1。例如:
LeetCode145. 二叉樹的後序遍歷(Binary tree Postorder Traversal)
題目描述 給定一個二叉樹,返回它的 後序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [3,2,1] 進階: 遞迴演算法很簡單,你可以通過迭代演算法完成嗎? 解題思路1: 用兩個棧和一個數
LeetCode144. 二叉樹的前序遍歷(Binary Tree Preorder Traversal)
題目描述 給定一個二叉樹,返回它的 前序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,2,3] 進階: 遞迴演算法很簡單,你可以通過迭代演算法完成嗎? 遞迴程式碼: /** *
654. Maximum Binary Tree(python+cpp)
題目: Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number
leetcode-94-二叉樹的中序遍歷(binary tree inorder traversal)-java
題目及測試 package pid094; import java.util.List; /*中序遍歷二叉樹 給定一個二叉樹,返回它的中序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,3,2] 進階:
PAT (Advanced Level) Practice 1102 Invert a Binary Tree (25 分)樹的遍歷
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on