CCCGuide

Problem S4: Balanced Trees

2018 · S4 · Original problem · DMOJ

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 11 always consists of a single node. Otherwise, if the weight of a perfectly balanced tree is ww and w≥2w\ge 2, then the tree consists of a root node with branches to kk subtrees, such that 2≤k≤w2\le k \le w. In this case, all kk subtrees must be completely identical, and be perfectly balanced themselves.

In particular, all kk subtrees must have the same weight. This common weight must be the maximum integer value such that the sum of the weights of all kk subtrees does not exceed ww, the weight of the overall tree. For example, if a perfectly balanced tree of weight 88 has 33 subtrees, then each subtree would have weight 22, since 2+2+2=6≤82+2+2=6 \le 8.

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

Input Specification

The input will be a single line containing the integer NN (1≤N≤109)(1\le N\le 10^9).

For 55 of the 1515 marks available, N≤1000N\le 1000.

For an additional 22 of the 1515 marks available, N≤50000N\le 50000.

For an additional 22 of the 1515 marks available, N≤106N\le 10^6.

Output Specification

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

Sample Input 1

4

Output for Sample Input 1

3

Explanation for Output for Sample Input 1

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

Sample Input 2

10

Output for Sample Input 2

13