Building a Fence

Interview

Time limit1sMemory limit128 MB

Summary
Count the ordered ways to cut a plank of length N into four positive integer pieces whose longest piece is strictly shorter than the other three combined.
Level

Medium5 of 10

Topics
Combinatorics, Math, Brute force, Implementation
Solved
No attempts yet

Problem

Farmer John wants to build a four-sided fence to enclose his cows. He has a single plank of wood of integer length NN (4≤N≤25004 \le N \le 2500). He will cut it at three points to produce four pieces, each of positive integer length.

The four pieces may have any positive integer lengths, as long as they can be arranged into a quadrilateral fence with strictly positive area. In how many different ways can he cut the plank so that the four resulting pieces form a valid fence?

Notes:

  • Two ways of cutting are different if one has a cut at a position where the other does not. Do not eliminate rotations, reflections, or other symmetric duplicates.
  • The fence must enclose an area greater than 00.
  • The answer always fits in a signed 32-bit integer.

Input

A single integer NN.

Output

A single integer: the number of ways to cut the plank into four pieces that form a quadrilateral with positive area.

Hint

For N=6N = 6, there are 1010 ways to cut the plank into four ordered pieces: (1,1,1,3)(1,1,1,3), (1,1,2,2)(1,1,2,2), (1,1,3,1)(1,1,3,1), (1,2,1,2)(1,2,1,2), (1,2,2,1)(1,2,2,1), (1,3,1,1)(1,3,1,1), (2,1,1,2)(2,1,1,2), (2,1,2,1)(2,1,2,1), (2,2,1,1)(2,2,1,1), and (3,1,1,1)(3,1,1,1). Four of them — (1,1,1,3)(1,1,1,3), (1,1,3,1)(1,1,3,1), (1,3,1,1)(1,3,1,1), and (3,1,1,1)(3,1,1,1) — cannot form a quadrilateral, because one side is as long as the other three combined. That leaves 66 valid ways.

Four lengths form a quadrilateral with positive area if and only if the longest piece is strictly shorter than the sum of the other three.

Examples3

  1. Example 1

    Input
    6
    
    Expected output
    6
    
  2. Example 2

    Input
    4
    
    Expected output
    1
    
  3. Example 3

    Input
    8
    
    Expected output
    19