| Target | Construction |
|---|---|
| any as a subset sum | powers of two |
| any as a subset sum with few items | and the remainder (binary splitting, used in bounded knapsack) |
| set with no 3-term arithmetic progression | numbers with no digit in base 3 (greedy Stanley sequence) |
| Sidon set (all pairwise sums distinct) of size | , prime (Erdős–Turán) |
| pairwise but total | products of all but one of primes: , e.g. |
| array with every subarray sum (or any prefix trick) | build prefix sums first, then take differences |
| distinct positive integers with sum | and (needs ) |
| numbers with a given xor and sum | when and is even |
| sum-free set in | the upper half , or all odd numbers |
vector<int> apFree(int m) { // first m naturals (from 0) with no digit 2 in base 3: no 3-term AP
vector<int> r;
for (int x = 0; sz(r) < m; x++) { int y = x; bool ok = true; while (y) { if (y % 3 == 2) ok = false; y /= 3; } if (ok) r.push_back(x); }
return r;
}
vector<int> sidonSet(int p) { // p prime: {2pk + (k^2 mod p)}, k < p, all pairwise sums distinct
vector<int> r;
for (int k = 0; k < p; k++) r.push_back(2 * p * k + (k * k) % p);
return r;
}Proof: no 3-term AP, and the Sidon property
No AP. Suppose with all having base-3 digits in . Then has digits in with no carries, and also has no carries (digit sums are at most ). Comparing digit by digit, every digit of equals the corresponding digit of , which is or , so and agree in every digit, contradicting . Sidon. Write with . If , then reading the sum modulo and dividing gives (the residues add up to less than ) and . Then and have the same sum and, mod , the same sum of squares, hence the same product mod ; they are the roots of the same quadratic over , so . Checked for primes up to and the AP-free set up to 300 elements.
Proof: xor and sum
If and , then , so and is even are necessary. With , the triple has xor and sum . Two numbers suffice iff additionally , namely .
Related: Reachability, Bit Tricks.