首页 > 其他分享 >Sum in Binary Tree

Sum in Binary Tree

时间:2023-07-11 15:45:24浏览次数:36  
标签:Vanya Binary Tree Sum vertex number tree test sum

Sum in Binary Tree time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Vanya really likes math. One day when he was solving another math problem, he came up with an interesting tree. This tree is built as follows.

Initially, the tree has only one vertex with the number 11 — the root of the tree. Then, Vanya adds two children to it, assigning them consecutive numbers — 22 and 33, respectively. After that, he will add children to the vertices in increasing order of their numbers, starting from 22, assigning their children the minimum unused indices. As a result, Vanya will have an infinite tree with the root in the vertex 11, where each vertex will have exactly two children, and the vertex numbers will be arranged sequentially by layers.

Part of Vanya's tree.

Vanya wondered what the sum of the vertex numbers on the path from the vertex with number 1 to the vertex with number n in such a tree is equal to. Since Vanya doesn't like counting, he asked you to help him find this sum.

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases.

This is followed by t lines — the description of the test cases. Each line contains one integer n (1≤n≤10161≤≤1016) — the number of vertex for which Vanya wants to count the sum of vertex numbers on the path from the root to that vertex.

Output

For each test case, print one integer — the desired sum.

Example input Copy 6 3 10 37 1 10000000000000000 15 output Copy
4
18
71
1
19999999999999980
26
Note

In the first test case of example on the path from the root to the vertex 33 there are two vertices 11 and 33, their sum equals 44.

In the second test case of example on the path from the root to the vertex with number 1010 there are vertices 11, 22, 55, 1010

sum of their numbers equals 1+2+5+10=181+2+5+10=18.

// //Sum in Binary Tree
//一个函数 向上搜索就行
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=1e9+7;
string s;
int n,t,a[N],f[N],res,num,ans,m;
bool vis[N];
int dfs(int x)
{
    if(x==1||x==0) return 1;
    else return x+dfs(x/2);
}
signed main()
{
    std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n;
        res=dfs(n); 
        cout<<res<<endl;
    }
    return 0;
}

 

 

标签:Vanya,Binary,Tree,Sum,vertex,number,tree,test,sum
From: https://www.cnblogs.com/o-Sakurajimamai-o/p/17544888.html

相关文章

  • LWC 50:677. Map Sum Pairs
    LWC50:677.MapSumPairs传送门:677.MapSumPairsProblem:ImplementaMapSumclasswithinsert,andsummethods.Forthemethodinsert,you’llbegivenapairof(string,integer).Thestringrepresentsthekeyandtheintegerrepresentsthevalue.Ifthekey......
  • Cesium中的QuadtreeTile.js类
    /***Asingletileina{@linkQuadtreePrimitive}.**@aliasQuadtreeTile*@constructor*@private**@param{Number}options.levelThelevelofthetileinthequadtree.*@param{Number}options.xTheXcoordinateofthetileinthequadtree......
  • 「BalticOI 2011 Day2」Tree Mirroring 题解
    本文网址:https://www.cnblogs.com/zsc985246/p/17539182.html,转载请注明出处。题目大意现在有一棵树\(T\),复制一个完全相同的\(T'\),并将这两棵树的叶子节点全部对应合并在一起,形成一个图,我们称这种图为对称图。给定一个图,判断它是否为对称图。\(3\len,m\le10^5\)思路......
  • [学习笔记] 启发式合并 & DSU on Tree
    一、启发式合并启发式合并多用于合并两个集合,现在有这样一个问题:现在给定\(n\)个集合,第\(i\)个集合初始只有\(\{i\}\),要支持集合的合并操作。如果我们暴力合并,时间复杂度会是\(O(n^2)\)的。参考并查集的按秩合并,考虑将小的集合合并到大的集合上。考虑计算时间复杂度,容......
  • CF1817E Half-sum
    greedy把数分成两个集合\(A,B\),且\(A<B\)。令每个集合的数的个数分别是\(a,b\)。令\(A_1\dotsA_a,B_1\dotsB_b\)分别有序。定理\(1\)\(A\)集合合并的顺序一定是从大往小的,\(B\)集合是从小往大的。应该很好猜到,但是证明需要一点推导。大概可以局部到\(x,y,z,w\)......
  • 使用zTree如何实现悬停事件
    zTree是功能强大的树插件,但本身没有提供鼠标悬停事件,不过我们可以通过jquery实现,同时我们可以自定义一个延迟操作,避免不小心滑过的情况下触发不必要的操作vartimer;//声明一个全局量用于存储延迟操作的定时器$("#tree").on("mouseover",".node_name",function(){var......
  • 【构造,树】【Loj】Loj6669 Nauuo and Binary Tree
    2023.7.3ProblemLink交互库有一棵\(n\)个点的二叉树,你每次可以询问两个点之间的距离,猜出这棵二叉树。\(n\le3000\),询问次数上限\(30000\)。首先给你距离一定是先把每个点的深度问出来,确定一个大致的考虑顺序。然后我们开始仔细思考“距离”这个条件怎么用。发现询问两个......
  • 920 F. SUM and REPLACE
    目录F.SUMandREPLACE题意:思路:F.SUMandREPLACE题目传送门题意:给你n个数,按照顺序排列,再进行m次操作。每次操作要么是问你区间[l,r]的和,要么是让你将区间[l,r]的所有数\(a_i=D(a_i),D(i)=i的因子数\),如:\(D(2)=2(因子:1,2),D(6)=4(因子:1,2,3,6)\)思路:做法一:线段树维护区间......
  • 【线段树】 HDOJ 5274 Dylans loves tree
    用dfs序构建线段树,然后用lca求出两点间路径的xor和。。。#include<iostream>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<cstdio>#include<algorithm>#include<cstring>#include......
  • 【并查集】 HDOJ 4786 Fibonacci Tree
    就是求出搞成最小生成树的最少白边和最多白边的数量。。。。#include<iostream>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<cstdio>#include<algorithm>#include<cstring>#include<......