Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5524
Ask a complete binary tree How many seed trees contain different number of nodes.
First of all, it is possible that a complete binary tree may be full of two forks, and a subtree full of two forks is still full of two forks of trees. But the subtree of a complete binary tree has a tree that is full of two forks and the other is a complete binary tree. Depending on the number of nodes given, it is easy to know the shape of the left and right subtree and then solve it recursively. The number of n is too large for a long long.
1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> + - using namespacestd; + AtypedefLong Longll; at ll N; - Set<ll>ans; - - voidDFS (ll x) { - if(x = =0|| Ans.count (x) >0) { - return; in } - Ans.insert (x); to if(--x%2==0) { +DFS (X/2); - } the Else { *DFS (X/2); $DFS (X/2+1);Panax Notoginseng } - } the + intMain () { A //freopen ("in", "R", stdin); the while(~SCANF ("%i64d", &N)) { + ans.clear (); - DFS (n); $printf"%d\n", Ans.size ()); $ } - return 0; -}
[HDOJ5524] Subtree