All Euler problems
Project Euler

Saving Paper

When wrapping n unit cubes individually, each cube requires 6 square units of paper (surface area of a unit cube). Alternatively, one can arrange the cubes into a rectangular box of dimensions a x...

Source sync Apr 19, 2026
Problem #0775
Level Level 30
Solved By 285
Languages C++, Python
Answer 946791106
Length 473 words
modular_arithmeticgeometryoptimization

Problem Statement

This archive keeps the full statement, math, and original media on the page.

When wrapping several cubes in paper, it is more efficient to wrap them all together than to wrap each one individually. For example, with 10 cubes of unit edge length, it would take 30 units of paper to wrap them in the arrangement shown below, but 60 units to wrap them separately.

Problem illustration

Define $g(n)$ to be the maximum amount of paper that can be saved by wrapping $n$ identical $1\times 1\times 1$ cubes in a compact arrangement, compared with wrapping them individually. We insist that the wrapping paper is in contact with the cubes at all points, without leaving a void.

With 10 cubes, the arrangement illustrated above is optimal, so $g(10)=60-30=30$. With 18 cubes, it can be shown that the optimal arrangement is as a $3\times 3\times 2$, using 42 units of paper, whereas wrapping individually would use 108 units of paper; hence $g(18) = 66$.

Define $$G(N) = \displaystyle \sum_{n=1}^N g(n)$$ You are given that $G(18) = 530$, and $G(10^6) \equiv 951640919 \pmod {1\,000\,000\,007}$.

Find $G(10^{16})$. Give your answer modulo $1\,000\,000\,007$.

Problem 775: Saving Paper

Mathematical Foundation

Lemma 1 (Box Surface Area). For positive integers abca \le b \le c, the surface area of an a×b×ca \times b \times c box is 2(ab+bc+ca)2(ab + bc + ca). For fixed volume V=abcV = abc, the surface area is minimized when a,b,ca, b, c are as close to V1/3V^{1/3} as possible.

Proof. By the AM-GM inequality, for fixed abc=Vabc = V, ab+bc+ca3(abc)2/3=3V2/3ab + bc + ca \ge 3(abc)^{2/3} = 3V^{2/3}, with equality when a=b=c=V1/3a = b = c = V^{1/3}. Since a,b,ca, b, c must be integers and we only require abcnabc \ge n (not =n= n), the optimal box may have volume strictly greater than nn. \square

Definition. For a triple (a,b,c)(a, b, c) with abca \le b \le c, define σ(a,b,c)=2(ab+bc+ca)\sigma(a,b,c) = 2(ab + bc + ca).

Theorem 1 (Reformulation of G(N)G(N)). We have

G(N)=n=1N(6nS(n))=6N(N+1)2n=1NS(n)=3N(N+1)n=1NS(n).G(N) = \sum_{n=1}^{N} \bigl(6n - S(n)\bigr) = 6 \cdot \frac{N(N+1)}{2} - \sum_{n=1}^{N} S(n) = 3N(N+1) - \sum_{n=1}^{N} S(n).

Thus the problem reduces to computing n=1NS(n)\sum_{n=1}^{N} S(n).

Proof. Direct algebraic manipulation. \square

Lemma 2 (Counting by Box). For each triple (a,b,c)(a, b, c) with 1abc1 \le a \le b \le c, the box (a,b,c)(a, b, c) is optimal for a contiguous range of nn values. Specifically, box (a,b,c)(a,b,c) is optimal for nn in some interval (La,b,c,Ua,b,c](L_{a,b,c}, U_{a,b,c}]. Thus

n=1NS(n)=(a,b,c)1abcσ(a,b,c){nN:S(n)=σ(a,b,c) achieved by (a,b,c)}.\sum_{n=1}^{N} S(n) = \sum_{\substack{(a,b,c) \\ 1 \le a \le b \le c}} \sigma(a,b,c) \cdot |\{n \le N : S(n) = \sigma(a,b,c) \text{ achieved by } (a,b,c)\}|.

Proof. As nn increases, the optimal box dimensions grow. For a given (a,b,c)(a,b,c), it is optimal for nn values where (i) abcnabc \ge n, (ii) no smaller box covers nn, and (iii) using a larger box is more expensive. The transitions occur at values of nn that are products abca'b'c' for nearby triples. \square

Theorem 2 (Summation via Dimension Enumeration). The sum n=1NS(n)\sum_{n=1}^{N} S(n) can be computed by iterating over the first dimension aa from 11 to N1/3N^{1/3}, and for each aa, iterating over bb from aa to (N/a)1/2(N/a)^{1/2}, and for each (a,b)(a, b), determining the optimal range of nn covered. This yields:

n=1NS(n)=a=1N1/3b=a(N/a)1/2c=b?σ(a,b,c)(min(abc,N)prev_volume)\sum_{n=1}^{N} S(n) = \sum_{a=1}^{\lfloor N^{1/3} \rfloor} \sum_{b=a}^{\lfloor (N/a)^{1/2} \rfloor} \sum_{c=b}^{?} \sigma(a,b,c) \cdot (\min(abc, N) - \text{prev\_volume})

where the summation over cc accounts for the number of nn-values for which (a,b,c)(a,b,c) is the optimal wrapping.

Proof. We enumerate boxes (a,b,c)(a,b,c) in order of increasing surface area for each (a,b)(a,b)-pair. For fixed (a,b)(a,b), increasing cc increases both volume and surface area. The box (a,b,c)(a,b,c) covers nn-values from the previous box’s volume +1+1 to abcabc. The surface area contribution is σ(a,b,c)\sigma(a,b,c) times the count of nn-values in this range. The outer loop over aa runs up to N1/3N^{1/3} and the middle loop over bb runs up to (N/a)1/2(N/a)^{1/2}, giving an overall iteration count of O(N2/3)O(N^{2/3}). \square

Lemma 3 (Optimal Box Characterization). For a given nn, the optimal box (a,b,c)(a^*, b^*, c^*) satisfies an1/3a^* \le n^{1/3}, b(n/a)1/2b^* \le (n/a^*)^{1/2}, and c=n/(ab)c^* = \lceil n/(a^* b^*) \rceil. Moreover, one need not always have abc=nabc = n; sometimes abc>nabc > n gives smaller surface area.

Proof. If a>n1/3a > n^{1/3}, then bc<n2/3<a2bc < n^{2/3} < a^2, meaning b<ab < a or c<ac < a, contradicting abca \le b \le c. Similarly, b>(n/a)1/2b > (n/a)^{1/2} would force c<bc < b. For the last claim, consider n=2n = 2: the box 1×1×21 \times 1 \times 2 has surface area 1010, while 1×2×21 \times 2 \times 2 has surface area 1616. Indeed c=n/(ab)c = \lceil n/(ab) \rceil may exceed n/(ab)n/(ab), making abc>nabc > n. \square

Editorial

Wrap nn unit cubes together vs separately. g(n)g(n) = max paper savings. G(N)=g(n)G(N)=\sum g(n). Given G(18)=530,G(106)951640919(mod109+7)G(18)=530, G(10^6)\equiv 951640919 \pmod{10^9+7}. Find G(1016)mod109+7G(10^{{16}}) \bmod 10^9+7. We iterate over c from b to …, box (a,b,c) covers n in (prev_vol, abc]. We then we need to compute contribution of all valid c values. Finally, actually: iterate c from b upward.

Pseudocode

For each a from 1 to N^{1/3}
For each b from a to floor((N/a)^{1/2})
For c from b to ..., box (a,b,c) covers n in (prev_vol, a*b*c]
We need to compute contribution of all valid c values
Actually: iterate c from b upward
The range of n for box (a,b,c) is (a*b*(c-1), a*b*c] intersected with [1, N]
Handle overcounting: a box (a,b,c) might not be optimal for all n in its range
Need to take minimum over all boxes -- this naive approach overcounts
Correct approach: for each n, S(n) = min over (a,b,c) with abc >= n
The enumeration must ensure we pick the minimum surface area box
Refined approach: for each (a, b), compute S(a,b) = 2(ab + b*ceil(n/ab) + a*ceil(n/ab))
and track the running minimum
More refined: use the "hyperbola method" style enumeration
Enumerate all (a, b) pairs, for each compute the c = ceil(n/(ab)) contribution
Sum over n by grouping n-values with same ceil(n/(ab))

Complexity Analysis

  • Time: O(N2/3)O(N^{2/3}) — the double loop over aN1/3a \le N^{1/3} and b(N/a)1/2b \le (N/a)^{1/2} contributes a=1N1/3(N/a)1/2=O(N1/2N1/6)=O(N2/3)\sum_{a=1}^{N^{1/3}} (N/a)^{1/2} = O(N^{1/2} \cdot N^{1/6}) = O(N^{2/3}) iterations. For N=1016N = 10^{16}, this is approximately 1010.6710^{10.67}, which requires further optimization (e.g., grouping consecutive cc-values with the same ceiling, reducing to O(N1/2)O(N^{1/2}) or better).
  • Space: O(1)O(1) auxiliary space (all computations are streaming).

Answer

946791106\boxed{946791106}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_775/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 775: Saving Paper
 *
 * Wrap $n$ unit cubes together vs separately. $g(n)$ = max paper savings. $G(N)=\sum g(n)$. Given $G(18)=530, G(10^6)\equiv 951640919 \pmod{10^9+7}$. Fi
 */

int main() { printf("Problem 775: Saving Paper\n"); return 0; }