#P2666. Counting Representations as Four Squares

    ID: 15931 Type: Default 1000ms 256MiB

Counting Representations as Four Squares

Counting Representations as Four Squares

Farmer John harvested almost an infinite amount of square hay bales, each with a nonnegative integer side length (including 0). Bessie, his cow, discovered them and wants to use the hay by dividing each bale into 1×1 pieces to fill N cells on her secret pasture. In doing so, she chooses 4 hay bales. The side length of each hay bale represents how many units on one side its square has, and thus it contributes the square of that number of cells. Bessie is curious: in how many different ways can she select 4 hay bales such that the sum of their areas is exactly N?

Formally, given a nonnegative integer \(N\), count the number of 4-tuples \((a,b,c,d)\) of nonnegative integers satisfying \[ a^2+b^2+c^2+d^2 = N, \] where the order matters. For example, when \(N=4\), the valid tuples are:

  • \((1,1,1,1)\) since \(1^2+1^2+1^2+1^2=4\)
  • \((2,0,0,0)\), \((0,2,0,0)\), \((0,0,2,0)\), and \((0,0,0,2)\) since \(2^2+0+0+0=4\)

Note that tuples with the same numbers in different orders are counted as distinct. Your task is to calculate the number of ways to represent \(N\) as the sum of four squares.

inputFormat

The input consists of a single nonnegative integer \(N\) representing the total number of 1×1 cells.

outputFormat

Output a single integer: the number of 4-tuples \((a,b,c,d)\) of nonnegative integers satisfying \(a^2+b^2+c^2+d^2=N\).

sample

4
5