CJam, 40 35 33 bytes
ri{__,f-_few:+{1b1$=}=|{F}*}:F~],
Thanks to @Optimizer for suggesting few
, which saved 2 bytes.
Try it online in the CJam interpreter.
How it works
ri e# Read an integer from STDIN.
{ e# Define function F(X):
_ e# Push X.
_, e# Push [0 ... X-1].
f- e# Subract each from X. Pushes Y := [X ... 1].
_few e# Push all overlapping slices of Y of length in Y.
:+ e# Consolidate the slices of different lenghts in a single array.
{ e# Find the first slice S such that...
1b e# the sum of its elements...
1$= e# equals X.
}= e# Since Y is in descending order, the first matching slice is also the longest.
| e# Set union with [X]. This adds X to the beginning of the S if S != [X].
{F}* e# Execute F for each element of S except the first (X).
}:F e#
~ e# Execute F for the input.
], e# Count the integers on the stack.