250250
Find the number of non-empty subsets of {1^1, 2^2, 3^3,..., 250250^250250} whose element sum is divisible by 250. Give the answer modulo 10^16.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Find the number of non-empty subsets of \(\{1^1, 2^2, 3^3,\dots , 250250^{250250}\}\), the sum of whose elements is divisible by \(250\). Enter the rightmost \(16\) digits as your answer.
Problem 250: 250250
Mathematical Foundation
Theorem 1 (Residue reduction). For divisibility of a sum by , only the residues matter. That is, a subset has if and only if .
Proof. For any integers , . Apply with and .
Theorem 2 (DP on residue classes). Let denote the number of subsets (including the empty set) of whose element sum is congruent to , reduced modulo . The recurrence
processed for correctly computes for all .
Proof. Induction on the number of elements processed. Initially, (empty set), for .
For element with residue : every subset of either excludes (counted by old ) or includes (counted by old , since including adds to the residue). We must update all 250 entries simultaneously (using a temporary copy or in-place with a fresh array) to avoid double-counting.
By induction, after processing all elements, equals (mod ) the number of subsets with sum .
Lemma 1 (Periodicity of residues). The sequence has period dividing .
Proof. By CRT, . We show periodicity modulo each factor:
- Mod 2: depends only on , period 2.
- Mod 125: Write . For , depends on (for the base) and (for the exponent). Since , the period divides 500. For , similar analysis via lifting-the-exponent gives the same bound.
- Combined: .
Theorem 3 (Final answer). The number of non-empty subsets with sum divisible by 250 is
where counts all subsets (including empty) with sum .
Proof. includes the empty subset, which has sum 0. Subtracting 1 excludes it.
Editorial
.., 250250^250250} with sum divisible by 250. DP on residue classes mod 250. Answer mod 10^16. Key: Only need i^i mod 250 for each element. DP array of size 250. We use fast modular exponentiation. We then dP on residue classes. Finally, subtract empty subset.
Pseudocode
Compute residues r_i = i^i mod 250
Use fast modular exponentiation
DP on residue classes
Subtract empty subset
Complexity Analysis
- Time: modular additions. Computing takes via modular exponentiation ( per element). Total: dominates.
- Space: for the DP array.
Answer
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() {
const long long MOD = 10000000000000000LL; // 10^16
const int N = 250250;
const int M = 250;
vector<long long> dp(M, 0);
dp[0] = 1;
for (int i = 1; i <= N; i++) {
// Compute i^i mod 250
long long base = i % M;
long long exp = i;
long long r = 1;
long long b = base;
long long e = exp;
while (e > 0) {
if (e & 1) r = (r * b) % M;
b = (b * b) % M;
e >>= 1;
}
int ri = (int)r;
// Update dp
vector<long long> ndp(M);
for (int j = 0; j < M; j++) {
ndp[j] = (dp[j] + dp[((j - ri) % M + M) % M]) % MOD;
}
dp = ndp;
}
// Answer: dp[0] - 1 (exclude empty subset)
long long answer = (dp[0] - 1 + MOD) % MOD;
cout << answer << endl;
return 0;
}
"""
Problem 250: 250250
Find non-empty subsets of {1^1, 2^2, ..., 250250^250250} with sum divisible by 250.
DP on residue classes mod 250. Answer mod 10^16.
Key: Only need i^i mod 250 for each element. DP array of size 250.
"""
def solve(N=250250, M=250, MOD=10**16):
"""DP on residues mod M. O(N * M) time."""
dp = [0] * M
dp[0] = 1
for i in range(1, N + 1):
ri = pow(i, i, M)
ndp = dp.copy()
for j in range(M):
ndp[(j + ri) % M] = (ndp[(j + ri) % M] + dp[j]) % MOD
dp = ndp
return (dp[0] - 1) % MOD
def solve_brute(N=20, M=250):
"""Brute force for small N: enumerate all subsets."""
residues = [pow(i, i, M) for i in range(1, N + 1)]
count = 0
for mask in range(1, 1 << N):
s = sum(residues[i] for i in range(N) if mask & (1 << i))
if s % M == 0:
count += 1
return count
# Cross-check
dp_small = solve(20, 250, 10**18)
bf_small = solve_brute(20, 250)
assert dp_small == bf_small, f"Mismatch: {dp_small} vs {bf_small}"
answer = solve()
assert answer == 1425480602091519, f"Expected 1425480602091519, got {answer}"
print(answer)