## CCC '18 S4 - Balanced Trees

View as PDF

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type
##### Canadian Computing Competition: 2018 Stage 1, Senior #4

Trees have many fascinating properties. While this is primarily true for trees in nature, the concept of trees in math and computer science is also interesting. A particular kind of tree, a perfectly balanced tree, is defined as follows.

Every perfectly balanced tree has a positive integer weight. A perfectly balanced tree of weight always consists of a single node. Otherwise, if the weight of a perfectly balanced tree is and , then the tree consists of a root node with branches to subtrees, such that . In this case, all subtrees must be completely identical, and be perfectly balanced themselves.

In particular, all subtrees must have the same weight. This common weight must be the maximum integer value such that the sum of the weights of all subtrees does not exceed , the weight of the overall tree. For example, if a perfectly balanced tree of weight has subtrees, then each subtree would have weight , since .

Given , find the number of perfectly balanced trees with weight .

#### Input Specification

The input will be a single line containing the integer .

For of the marks available, .

For an additional of the marks available, .

For an additional of the marks available, .

#### Output Specification

Output a single integer, the number of perfectly balanced trees with weight .

#### Sample Input 1

4

#### Sample Output 1

3

#### Explanation for Sample Output 1

One tree has a root with four subtrees of weight ; a second tree has a root with two subtrees of weight ; the third tree has a root with three subtrees of weight .

#### Sample Input 2

10

#### Sample Output 2

13

• commented on Feb. 4, 2019, 11:25 a.m.

I still have questions about the second sample input. I tried and got the same result as that guy got--17. Can anyone specify what the situation is when input is 10? ty

• commented on Feb. 3, 2022, 5:07 p.m.

For all trees with a weight of 10 and # of branches in the range of [4,10], we +1 each time to the total number of perfect trees.

With 3 branches (each branch will have a weight of 10/3=3), we add 2 to our answer(since 3->1,1 and 3->1,1,1).

With 2 branches (each branch will have a weight of 10/2=5), we add 4 to our answer(since 5->1,1,1,1,1; 5->1,1,1,1; 5->1,1,1; 5->2,2).

• commented on Jan. 27, 2019, 10:43 p.m.

I don't know why I get seg fault on the last batch... I doubled checked my indexing, and nothing seems to be wrong..

• commented on Jan. 27, 2019, 11:02 p.m.

You're running out of memory