Digit Sum Numbers, S(2020) mod 10^16. Uses i128 modular arithmetic mod (2020 * 10^16) with Russian peasant mulmod to avoid overflow, then divides by 2020 for exact result mod 10^16.
# Project Euler 725
# Digit Sum Numbers, S(2020) mod 10^16.
# Uses i128 modular arithmetic mod (2020 * 10^16) with Russian peasant mulmod
# to avoid overflow, then divides by 2020 for exact result mod 10^16.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>)
function memset(p: ptr<void>, c: i32, n: i64) -> ptr<void>
function printf(fmt: ptr<i8>, ...) -> i32
}
const MOD: i64 = 10000000000000000
const N: i32 = 2020
const MAXP: i32 = 200
# M = 2020 * 10^16, stored as i128
const M_HI: i64 = 0 # M fits in i128 but not i64, so we use i128 directly
# M = 20200000000000000000
function m_add(a: i128, b: i128) -> i128 {
let s: i128 = a + b
let m: i128 = 20200000000000000000
if s >= m { return s - m }
if s < 0 { return s + m }
return s
}
# Russian peasant multiplication mod M to avoid i128 overflow
function m_mul(a0: i128, b0: i128) -> i128 {
let m: i128 = 20200000000000000000
let mut a: i128 = a0 % m
if a < 0 { a = a + m }
let mut b: i128 = b0 % m
if b < 0 { b = b + m }
let mut result: i128 = 0
while b > 0 {
if (b & 1) != 0 {
result = result + a
if result >= m { result = result - m }
}
a = a + a
if a >= m { a = a - m }
b = b >> 1
}
return result
}
function m_pow(base0: i128, e: i32) -> i128 {
let m: i128 = 20200000000000000000
let mut r: i128 = 1
let mut b: i128 = base0 % m
let mut ee: i32 = e
while ee > 0 {
if (ee & 1) != 0 { r = m_mul(r, b) }
b = m_mul(b, b)
ee = ee >> 1
}
return r
}
function mod_add(a: i64, b: i64) -> i64 {
let s: i64 = a + b
if s >= MOD { return s - MOD }
if s < 0 { return s + MOD }
return s
}
function vp_factorial(p: i32, n: i32) -> i32 {
let mut e: i32 = 0
let mut pk: i32 = p
while pk <= n {
e = e + n / pk
pk = pk * p
}
return e
}
function main() -> i32 {
let m: i128 = 20200000000000000000
# Sieve primes up to N
let sieve: ptr<i8> = calloc((N + 1) as i64, 1)
let mut i: i32 = 2
while i <= N {
sieve[i] = 1
i = i + 1
}
let mut p: i32 = 2
while p * p <= N {
if sieve[p] != 0 {
let mut mm: i32 = p * p
while mm <= N {
sieve[mm] = 0
mm = mm + p
}
}
p = p + 1
}
let primes: ptr<i32> = calloc(400, 4)
let mut nprimes: i32 = 0
let mut i2: i32 = 2
while i2 <= N {
if sieve[i2] != 0 {
primes[nprimes] = i2
nprimes = nprimes + 1
}
i2 = i2 + 1
}
# Build partitions
let lens: ptr<i32> = calloc(10 * MAXP, 4)
let digs: ptr<ptr<i32> > = calloc(10 * MAXP, 8)
let counts: ptr<i32> = calloc(10, 4)
counts[0] = 1
lens[0] = 0
digs[0] = calloc(20, 4)
let mut opt: i32 = 1
while opt <= 9 {
let mut i: i32 = 0
while i <= 9 - opt {
let pc: i32 = counts[i]
let mut pp: i32 = 0
while pp < pc {
let dst_idx: i32 = counts[i + opt]
let dst_d: ptr<i32> = calloc(20, 4)
let src_d: ptr<i32> = digs[i * MAXP + pp]
let src_len: i32 = lens[i * MAXP + pp]
let mut j: i32 = src_len
while j > 0 {
dst_d[j] = src_d[j - 1]
j = j - 1
}
dst_d[0] = opt
digs[(i + opt) * MAXP + dst_idx] = dst_d
lens[(i + opt) * MAXP + dst_idx] = src_len + 1
counts[i + opt] = dst_idx + 1
pp = pp + 1
}
i = i + 1
}
opt = opt + 1
}
# rep = 111...1 (2020 ones) mod M
let mut rep: i128 = 0
let mut i3: i32 = 0
while i3 < N {
rep = m_mul(rep, 10)
rep = m_add(rep, 1)
i3 = i3 + 1
}
let mut total: i64 = 0
let mut k: i32 = 0
while k <= 9 {
let mut pi: i32 = 0
while pi < counts[k] {
let p_len: i32 = lens[k * MAXP + pi]
let p_d: ptr<i32> = digs[k * MAXP + pi]
let d: ptr<i32> = calloc(10, 4)
let mut sumd: i32 = 0
let mut j: i32 = 0
while j < p_len {
d[p_d[j]] = d[p_d[j]] + 1
j = j + 1
}
d[k] = d[k] + 1
let mut j2: i32 = 1
while j2 <= 9 {
sumd = sumd + d[j2]
j2 = j2 + 1
}
d[0] = N - sumd
if d[0] >= 0 {
if p_len < N {
# Compute multinomial mod M via prime factorization
let mut comb: i128 = 1
let mut pi2: i32 = 0
while pi2 < nprimes {
let pr: i32 = primes[pi2]
let mut e: i32 = vp_factorial(pr, N)
let mut v: i32 = 0
while v < 10 {
e = e - vp_factorial(pr, d[v])
v = v + 1
}
if e > 0 {
comb = m_mul(comb, m_pow((pr as i128), e))
}
pi2 = pi2 + 1
}
let mut val: i128 = m_mul(rep, comb)
val = m_mul(val, ((2 * k) as i128))
# Exact division by N=2020
val = val / (N as i128)
# Reduce mod 10^16
let vmod: i64 = (val % (MOD as i128)) as i64
total = mod_add(total, vmod)
}
}
free(d)
pi = pi + 1
}
k = k + 1
}
printf("%lld\n", total)
let mut i4: i32 = 0
while i4 < 10 {
let mut j4: i32 = 0
while j4 < counts[i4] {
free(digs[i4 * MAXP + j4])
j4 = j4 + 1
}
i4 = i4 + 1
}
free(lens)
free(digs)
free(counts)
free(sieve)
free(primes)
return 0
}
Generated C
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
while (flow_temp_head) {
flow_temp_node* n = flow_temp_head;
flow_temp_head = n->next;
free(n);
}
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
if (!node) return NULL;
node->next = flow_temp_head;
flow_temp_head = node;
if (!flow_temp_atexit_set) {
flow_temp_atexit_set = 1;
atexit(flow_temp_free_all);
}
return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
char* r = (char*)flow_temp_alloc(la + lb + 1);
if (!r) return NULL;
if (la) memcpy(r, a, la);
if (lb) memcpy(r + la, b, lb);
r[la + lb] = '\0';
return r;
}
#define __flow_in_arr(arr, val) __extension__ ({ \
int _found = 0; \
size_t _n = sizeof(arr)/sizeof((arr)[0]); \
for (size_t _i = 0; _i < _n; _i++) { \
if ((arr)[_i] == (val)) { _found = 1; break; } \
} _found; })
/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
abort();
#if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")
#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#include <math.h>
void* _ui_state = NULL;
static inline float i32_to_f32(int32_t v) { return (float)v; }
/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }
__int128 m_add_i128_i128(__int128 a, __int128 b);
__int128 m_mul_i128_i128(__int128 a0, __int128 b0);
__int128 m_pow_i128_i32(__int128 base0, int32_t e);
int64_t mod_add_i64_i64(int64_t a, int64_t b);
int32_t vp_factorial_i32_i32(int32_t p, int32_t n);
int32_t main(void);
static const int64_t MOD = 10000000000000000;
static const int32_t N = 2020;
static const int32_t MAXP = 200;
static const int64_t M_HI = 0;
__int128 m_add_i128_i128(__int128 a, __int128 b) {
__int128 s = (a + b);
__int128 m = ((__int128)0x1ULL << 64 | (__int128)0x1854D0F9CEE40000ULL);
if (s >= m) {
return (s - m);
}
if (s < 0) {
return (s + m);
}
return s;
}
__int128 m_mul_i128_i128(__int128 a0, __int128 b0) {
__int128 m = ((__int128)0x1ULL << 64 | (__int128)0x1854D0F9CEE40000ULL);
__int128 a = FLOW_CHECKED_MOD((a0), (m));
if (a < 0) {
a = (a + m);
}
__int128 b = FLOW_CHECKED_MOD((b0), (m));
if (b < 0) {
b = (b + m);
}
__int128 result = 0;
while (b > 0) {
if ((b & 1) != 0) {
result = (result + a);
if (result >= m) {
result = (result - m);
}
}
a = (a + a);
if (a >= m) {
a = (a - m);
}
b = FLOW_CHECKED_SHR((b), (1));
}
return result;
}
__int128 m_pow_i128_i32(__int128 base0, int32_t e) {
__int128 m = ((__int128)0x1ULL << 64 | (__int128)0x1854D0F9CEE40000ULL);
__int128 r = 1;
__int128 b = FLOW_CHECKED_MOD((base0), (m));
int32_t ee = e;
while (ee > 0) {
if ((ee & 1) != 0) {
r = m_mul_i128_i128(r, b);
}
b = m_mul_i128_i128(b, b);
ee = FLOW_CHECKED_SHR((ee), (1));
}
return r;
}
int64_t mod_add_i64_i64(int64_t a, int64_t b) {
int64_t s = (a + b);
if (s >= MOD) {
return (s - MOD);
}
if (s < 0) {
return (s + MOD);
}
return s;
}
int32_t vp_factorial_i32_i32(int32_t p, int32_t n) {
int32_t e = 0;
int32_t pk = p;
while (pk <= n) {
e = (e + FLOW_CHECKED_DIV((n), (pk)));
pk = (pk * p);
}
return e;
}
int32_t main(void) {
__int128 m = ((__int128)0x1ULL << 64 | (__int128)0x1854D0F9CEE40000ULL);
int8_t* sieve = (int8_t*)(calloc(((int64_t)((N + 1))), 1));
int32_t i = 2;
while (i <= N) {
sieve[i] = 1;
i = (i + 1);
}
int32_t p = 2;
while ((p * p) <= N) {
if (sieve[p] != 0) {
int32_t mm = (p * p);
while (mm <= N) {
sieve[mm] = 0;
mm = (mm + p);
}
}
p = (p + 1);
}
int32_t* primes = (int32_t*)(calloc(400, 4));
int32_t nprimes = 0;
int32_t i2 = 2;
while (i2 <= N) {
if (sieve[i2] != 0) {
primes[nprimes] = i2;
nprimes = (nprimes + 1);
}
i2 = (i2 + 1);
}
int32_t* lens = (int32_t*)(calloc((10 * MAXP), 4));
int32_t** digs = (int32_t**)(calloc((10 * MAXP), 8));
int32_t* counts = (int32_t*)(calloc(10, 4));
counts[0] = 1;
lens[0] = 0;
digs[0] = calloc(20, 4);
int32_t opt = 1;
while (opt <= 9) {
int32_t i = 0;
while (i <= (9 - opt)) {
int32_t pc = counts[i];
int32_t pp = 0;
while (pp < pc) {
int32_t dst_idx = counts[(i + opt)];
int32_t* dst_d = (int32_t*)(calloc(20, 4));
int32_t* src_d = (int32_t*)(digs[((i * MAXP) + pp)]);
int32_t src_len = lens[((i * MAXP) + pp)];
int32_t j = src_len;
while (j > 0) {
dst_d[j] = src_d[(j - 1)];
j = (j - 1);
}
dst_d[0] = opt;
digs[(((i + opt) * MAXP) + dst_idx)] = dst_d;
lens[(((i + opt) * MAXP) + dst_idx)] = (src_len + 1);
counts[(i + opt)] = (dst_idx + 1);
pp = (pp + 1);
}
i = (i + 1);
}
opt = (opt + 1);
}
__int128 rep = 0;
int32_t i3 = 0;
while (i3 < N) {
rep = m_mul_i128_i128(rep, 10);
rep = m_add_i128_i128(rep, 1);
i3 = (i3 + 1);
}
int64_t total = 0;
int32_t k = 0;
while (k <= 9) {
int32_t pi = 0;
while (pi < counts[k]) {
int32_t p_len = lens[((k * MAXP) + pi)];
int32_t* p_d = (int32_t*)(digs[((k * MAXP) + pi)]);
int32_t* d = (int32_t*)(calloc(10, 4));
int32_t sumd = 0;
int32_t j = 0;
while (j < p_len) {
d[p_d[j]] = (d[p_d[j]] + 1);
j = (j + 1);
}
d[k] = (d[k] + 1);
int32_t j2 = 1;
while (j2 <= 9) {
sumd = (sumd + d[j2]);
j2 = (j2 + 1);
}
d[0] = (N - sumd);
if (d[0] >= 0) {
if (p_len < N) {
__int128 comb = 1;
int32_t pi2 = 0;
while (pi2 < nprimes) {
int32_t pr = primes[pi2];
int32_t e = vp_factorial_i32_i32(pr, N);
int32_t v = 0;
while (v < 10) {
e = (e - vp_factorial_i32_i32(pr, d[v]));
v = (v + 1);
}
if (e > 0) {
comb = m_mul_i128_i128(comb, m_pow_i128_i32(((__int128)(pr)), e));
}
pi2 = (pi2 + 1);
}
__int128 val = m_mul_i128_i128(rep, comb);
val = m_mul_i128_i128(val, ((__int128)((2 * k))));
val = FLOW_CHECKED_DIV((val), (((__int128)(N))));
int64_t vmod = ((int64_t)(FLOW_CHECKED_MOD((val), (((__int128)(MOD))))));
total = mod_add_i64_i64(total, vmod);
}
}
free(d);
pi = (pi + 1);
}
k = (k + 1);
}
printf("%lld\n", total);
int32_t i4 = 0;
while (i4 < 10) {
int32_t j4 = 0;
while (j4 < counts[i4]) {
free(digs[((i4 * MAXP) + j4)]);
j4 = (j4 + 1);
}
i4 = (i4 + 1);
}
free(lens);
free(digs);
free(counts);
free(sieve);
free(primes);
return 0;
}