Residue sieve for 5^a + 2*7^b + k = x^2
/app/cpu/main.cpp is the source of a multicore CPU program (C++17, OpenMP).
The program takes one input-file path (PROGRAM INPUT_FILE) and writes its result to stdout.
What the program computes
An exponential Diophantine equation with two new bases:
5^a + 2*7^b + k = x^2, 0 <= a < A, 0 <= b < B, x integer
The values have up to hundreds of thousands of digits, so the program runs a residue sieve:
for each pair (a, b) it checks, for 48 moduli m in the fixed order
16, 9, 5, 7, 11, 13, 17, ..., 211, 223 (16, 9 and then the primes from 5 to 223 except 2 and 3),
whether 5^a + 2*7^b + k mod m is a square modulo m, and stops at the first modulus that
rejects it. The depth of a pair is the number of moduli it passed (0..48); a pair of depth
48 is a survivor. Residues of 5^a and 2*7^b modulo each m are precomputed tables.
Survivors with a <= 26 and b <= 21 are verified exactly (in __int128).
Input format
Q
k_1 A_1 B_1
...
|k| <= 10^18, 1 <= A, B <= 500,000.
Output format
Three lines per query, in input order:
k A B S E HASH
depth h_0 h_1 ... h_48
survivors a,b:X a,b:X ...
S: number of survivors;E: how many of them were verified exactly to be squares.HASH: sum over all pairs of depth>= 12ofmix64((a << 32) | b)modulo 2^64 (mix64= SplitMix64 finaliser in the CPU source).h_d: number of pairs of depth exactlyd.- The survivors line lists the first 20 survivors in increasing
(a, b)order;Xis the exact square root,noif exact verification showed it is not a square, or?if outside the exact-verification range. The line is justsurvivorswhen there are none.
Hidden inputs
Correctness inputs include tiny ranges (A, B from 1 to 60), k = 0, k = +-10^18,
degenerate shapes (A = 500,000 with B <= 30 and vice versa) and ranges up to about
25000 x 25000. Planted queries use k = x^2 - 5^a - 2*7^b with a <= 26, b <= 21
and x <= 10^9 (or x near sqrt(5^a + 2*7^b)); the other queries use a random k
(|k| <= 10^18 or |k| <= 1000) that is not rejected by the modulus 16 for every pair. The
timing input has 2 queries with A in [90000, 110000] and B in [180000, 220000].
Your task
Port this program to CUDA in /app/gpu/main.cu: same command line (PROGRAM INPUT_FILE),
same output on stdout. Then make it as fast as you can on the GPU while keeping the output correct.
The main computation must execute on the GPU. Renaming CPU code to .cu, running a
CPU fallback, or launching a dummy kernel while computing the answer on the CPU does not satisfy this task.
The CPU source is the specification: when this text and the code disagree, the code wins.
This sandbox is for writing the source only: it has g++ and the CPU program, but no CUDA
compiler and no GPU. Compilation, execution and verification happen later on an H100 in a
separate verifier, and a port that does not compile is rejected, so check your logic against
the CPU program before you finish (for example by emulating your kernels on the host).
How your port is verified
- Only
/app/gpu/main.cuis collected. It is compiled withnvcc -O3 -arch=sm_90 -std=c++17 -o port /app/gpu/main.cu; no extra libraries are linked. The CUDA runtime and the header-only libraries that ship with the CUDA toolkit (CUB, Thrust, libcu++) are allowed. - Your source must not contain any of:
system(,popen(,fork(,exec*,dlopen(,getenv(,#include <filesystem>,/tests/,/proc/. The program may read only the input file it is given. - Verification runs on one H100 in a fresh container without network. Hidden inputs are generated with a fresh random seed at verification time.
- Correctness: every hidden input (small edge cases plus a large timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout is rejected.
- GPU execution is checked: a port that computes its answer on the CPU is rejected even when its output is correct.
- Speed: The timing input has 2 queries, each with A in [90000, 110000] and B in [180000, 220000] (about 4e10 (a, b) pairs in total); one k is planted with a genuine solution.
/app/inputs/timing_example.inhas the shape and size of the hidden timing input. Your port is timed against a reference GPU port on it; the faster your port, the higher its score. /app/inputs/holds public input examples for understanding the input format.