Square Remainders
Let r be the remainder when (a-1)^n + (a+1)^n is divided by a^2. For 3 <= a <= 1000, find sum r_max, where r_max is the maximum value of r over all positive integers n.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let $r$ be the remainder when $(a - 1)^n + (a + 1)^n$ is divided by $a^2$.
For example, if $a = 7$ and $n = 3$, then $r = 42$: $6^3 + 8^3 = 728 \equiv 42 \mod 49$. And as $n$ varies, so too will $r$, but for $a = 7$ it turns out that $r_{\mathrm{max}} = 42$.
For $3 \le a \le 1000$, find $\displaystyle \sum r_{\mathrm{max}}$.
Problem 120: Square Remainders
Correctness
Theorem. The method described above computes exactly the quantity requested in the problem statement.
Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer.
Answer
Mathematical Analysis
Binomial Expansion Modulo
Using the binomial theorem:
When we add these and reduce modulo , only terms with and survive (terms with are divisible by ):
Case Analysis
When is even:
When is odd:
Maximizing for Odd
For odd , . We want to maximize .
Let (since depends only on , as … let me be more precise).
Actually: . So we want to maximize where is odd.
If is odd: As ranges over odd values, takes all values in (since ). The maximum of is , giving .
If is even: As ranges over odd values, ranges over , i.e., . Then can reach at most (when is even). So .
Closed-Form Formula
Summation
Computing this sum yields 333082500.
Proof of the Formula
For odd : Since , as ranges over all odd positive integers, the residues cycle through all residues . The maximum residue is , achieved when , i.e., (using the inverse of 2 mod ). Since is an integer and we can always find an odd achieving this, .
For even : Write . Then . For odd , ranges over values achievable by odd , and the maximum of is . Thus .
Complexity
- Time: where .
- Space: .
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
long long total = 0;
for (int a = 3; a <= 1000; a++) {
if (a % 2 == 1) {
total += (long long)a * (a - 1);
} else {
total += (long long)a * (a - 2);
}
}
cout << total << endl;
return 0;
}
"""
Problem 120: Square Remainders
For 3 <= a <= 1000, find the sum of r_max where r_max is the maximum
remainder when (a-1)^n + (a+1)^n is divided by a^2.
Key insight: r_max = a*(a-1) for odd a, a*(a-2) for even a.
"""
def r_max(a):
if a % 2 == 1:
return a * (a - 1)
else:
return a * (a - 2)
answer = sum(r_max(a) for a in range(3, 1001))
print(answer)