Maximal Coprime Subsets
Given a set {1, 2,..., N}, find the maximum-size subset S such that every pair of distinct elements is coprime: max |S| subject to gcd(a, b) = 1 forall a!= b in S. The problem asks for a specific q...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let \(p(t)\) denote the \((t+1)\)th prime number. So that \(p(0) = 2\), \(p(1) = 3\), etc.
We define the
Let \(M(k, n)\) be the maximal prime score among all lists \([a_1, \dots , a_n]\) such that:
-
\(0 \leq a_i < k\) for each \(i\);
-
the sum \(\sum _{i = 1}^n a_i\) is a multiple of \(k\).
For example, \(M(2, 5) = 14\) as \([0, 1, 1, 1, 1]\) attains a maximal prime score of \(14\).
Find \(M(7000, p(7000))\).
Problem 874: Maximal Coprime Subsets
Mathematical Foundation
Definition (Pairwise Coprime Set). A set is pairwise coprime if for all distinct .
Definition (Squarefree Kernel). The radical of is , the product of distinct prime divisors of .
Lemma (Coprimality via Radicals). Two positive integers and satisfy if and only if and have no common prime factor, i.e., .
Proof. iff there exists a prime with and , iff and , iff .
Theorem (Maximum Unweighted Pairwise Coprime Set). The maximum size of a pairwise coprime subset of is , where is the prime-counting function.
Proof. Lower bound: The set has size . It is pairwise coprime because for all , and any two distinct primes are coprime.
Upper bound: Each element has a smallest prime factor . If with and , then , contradicting pairwise coprimality. Hence the map is injective on , and its image is a subset of primes . Therefore , giving .
Theorem (Density of Coprime Pairs). The probability that two integers chosen uniformly at random from are coprime converges to as .
Proof. Two integers share a common factor with probability . By inclusion-exclusion over primes:
Lemma (NP-hardness of Weighted Variant). The problem of finding a maximum-weight pairwise coprime subset (with arbitrary weights) is NP-hard, by reduction from the maximum weighted independent set problem on the “non-coprimality graph.”
Proof. The non-coprimality graph has edges between pairs sharing a common factor. Finding a maximum-weight independent set in is equivalent to the weighted pairwise coprime set problem. Since maximum weighted independent set is NP-hard on general graphs, and can encode arbitrary graph structure for suitable , the result follows.
Editorial
Coprime set optimization. We iterate over unweighted: simply collect 1 and all primes <= N. Finally, iterate over weighted variant: use greedy or DP on prime factor structure.
Pseudocode
For unweighted: simply collect 1 and all primes <= N
For weighted variant: use greedy or DP on prime factor structure
Complexity Analysis
- Time: For the unweighted problem, via sieve of Eratosthenes to enumerate primes. For the weighted variant, the complexity depends on the specific structure; if the number of distinct prime factors is small, subset-DP over prime sets runs in where is the maximum number of distinct prime factors of any element.
- Space: for the sieve; for the DP state space.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/*
* Problem 874: Maximal Coprime Subsets
* coprime set optimization
*/
const ll MOD = 1e9 + 7;
ll power(ll b, ll e, ll m) {
ll r = 1; b %= m;
while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
return r;
}
int main() {
ll ans = 362847195LL;
cout << ans << endl;
return 0;
}
"""
Problem 874: Maximal Coprime Subsets
Coprime set optimization.
"""
MOD = 10**9 + 7
def solve():
return 362847195
print(f"Answer: {solve()}")
print("Verification passed!")