# Project Euler 738
# Counting Ordered Factorisations: D(10^10, 10^10) mod 1e9+7.
# Pure Flow port of the native C solver.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memset(s: ptr<void>, c: i64, n: i64) -> ptr<void>
function sqrt(x: f64) -> f64
function cbrt(x: f64) -> f64
}
const MOD: i64 = 1000000007
const HASH_CAP: i64 = 4194304
# Hash table: parallel arrays for (m, a, c, l)
let mut htab_m: ptr<i64> = null
let mut htab_a: ptr<i64> = null
let mut htab_c: ptr<i64> = null
let mut htab_l: ptr<i64> = null
function icbrt_floor(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut r: i64 = cbrt(n as f64) as i64
while (r + 1) * (r + 1) * (r + 1) <= n {
r = r + 1
}
while r * r * r > n {
r = r - 1
}
return r
}
function sum_floor_range(m: i64, l: i64, r: i64) -> i64 {
let mut res: i64 = 0
let mut i: i64 = l
while i <= r {
let q: i64 = m / i
let mut j: i64 = m / q
if j > r { j = r }
res = res + q * (j - i + 1)
i = j + 1
}
return res
}
function sum_arith(l: i64, r: i64) -> i64 {
let n: i64 = r - l + 1
return (l + r) * n / 2
}
function htab_init() -> void {
htab_m = calloc(HASH_CAP, 8) as ptr<i64>
htab_a = calloc(HASH_CAP, 8) as ptr<i64>
htab_c = calloc(HASH_CAP, 8) as ptr<i64>
htab_l = calloc(HASH_CAP, 8) as ptr<i64>
}
function htab_get(m: i64, a: i64, cp: ptr<i64>, lp: ptr<i64>) -> i32 {
let mut h: i64 = m * 1000003 + a
h = h ^ (h >> 16)
let mut idx: i64 = h & (HASH_CAP - 1)
while true {
if htab_m[idx] == 0 && htab_a[idx] == 0 {
return 0
}
if htab_m[idx] == m && htab_a[idx] == a {
cp[0] = htab_c[idx]
lp[0] = htab_l[idx]
return 1
}
idx = (idx + 1) & (HASH_CAP - 1)
}
return 0
}
function htab_put(m: i64, a: i64, c: i64, l: i64) -> void {
let mut h: i64 = m * 1000003 + a
h = h ^ (h >> 16)
let mut idx: i64 = h & (HASH_CAP - 1)
while true {
if htab_m[idx] == 0 && htab_a[idx] == 0 {
htab_m[idx] = m
htab_a[idx] = a
htab_c[idx] = c
htab_l[idx] = l
return
}
if htab_m[idx] == m && htab_a[idx] == a {
htab_c[idx] = c
htab_l[idx] = l
return
}
idx = (idx + 1) & (HASH_CAP - 1)
}
}
function count_and_length(m: i64, a: i64, Cp: ptr<i64>, Lp: ptr<i64>) -> void {
if m < a {
Cp[0] = 0
Lp[0] = 0
return
}
let cc_buf: array<i64, 1> = [0]
let ll_buf: array<i64, 1> = [0]
if htab_get(m, a, &cc_buf[0], &ll_buf[0]) == 1 {
Cp[0] = cc_buf[0]
Lp[0] = ll_buf[0]
return
}
let aa: i64 = a * a
let mut C: i64 = 0
let mut L: i64 = 0
if aa > m {
let cnt: i64 = (m - a + 1) % MOD
C = cnt
L = cnt
} else {
C = m - a + 1
L = C
let mut s: i64 = sqrt(m as f64) as i64
while (s + 1) * (s + 1) <= m {
s = s + 1
}
while s * s > m {
s = s - 1
}
if aa * a > m {
let l: i64 = a
if l <= s {
let sf: i64 = sum_floor_range(m, l, s)
let sa: i64 = sum_arith(l, s)
let baseC: i64 = sf - sa + (s - l + 1)
C = C + baseC
L = L + 2 * baseC
}
} else {
let t: i64 = icbrt_floor(m)
let mut upto: i64 = t
if s < t { upto = s }
if upto >= a {
let mut f: i64 = a
while f <= upto {
let subC_buf: array<i64, 1> = [0]
let subL_buf: array<i64, 1> = [0]
count_and_length(m / f, f, &subC_buf[0], &subL_buf[0])
let subC: i64 = subC_buf[0]
let subL: i64 = subL_buf[0]
C = C + subC
L = L + subL + subC
f = f + 1
}
}
let mut l2: i64 = upto + 1
if a > upto + 1 { l2 = a }
if l2 <= s {
let sf: i64 = sum_floor_range(m, l2, s)
let sa: i64 = sum_arith(l2, s)
let baseC: i64 = sf - sa + (s - l2 + 1)
C = C + baseC
L = L + 2 * baseC
}
}
}
C = C % MOD
L = L % MOD
htab_put(m, a, C, L)
Cp[0] = C
Lp[0] = L
}
function main() -> i32 {
htab_init()
let N: i64 = 10000000000
let K: i64 = 10000000000
let C_buf: array<i64, 1> = [0]
let L_buf: array<i64, 1> = [0]
count_and_length(N, 2, &C_buf[0], &L_buf[0])
let C: i64 = C_buf[0]
let L: i64 = L_buf[0]
let mut ans: i64 = (K % MOD) + (K + 1) % MOD * C % MOD - L
ans = ans % MOD
if ans < 0 { ans = ans + MOD }
printf("%lld\n", ans)
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; }
double cbrt(double x);
int64_t icbrt_floor_i64(int64_t n);
int64_t sum_floor_range_i64_i64_i64(int64_t m, int64_t l, int64_t r);
int64_t sum_arith_i64_i64(int64_t l, int64_t r);
void htab_init(void);
int32_t htab_get_i64_i64_ptr_i64_ptr_i64(int64_t m, int64_t a, int64_t* cp, int64_t* lp);
void htab_put_i64_i64_i64_i64(int64_t m, int64_t a, int64_t c, int64_t l);
void count_and_length_i64_i64_ptr_i64_ptr_i64(int64_t m, int64_t a, int64_t* Cp, int64_t* Lp);
int32_t main(void);
static const int64_t MOD = 1000000007;
static const int64_t HASH_CAP = 4194304;
/* Module statics */
static int64_t* htab_m = NULL;
static int64_t* htab_a = NULL;
static int64_t* htab_c = NULL;
static int64_t* htab_l = NULL;
int64_t icbrt_floor_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t r = ((int64_t)(cbrt(((double)(n)))));
while ((((r + 1) * (r + 1)) * (r + 1)) <= n) {
r = (r + 1);
}
while (((r * r) * r) > n) {
r = (r - 1);
}
return r;
}
int64_t sum_floor_range_i64_i64_i64(int64_t m, int64_t l, int64_t r) {
int64_t res = 0;
int64_t i = l;
while (i <= r) {
int64_t q = FLOW_CHECKED_DIV((m), (i));
int64_t j = FLOW_CHECKED_DIV((m), (q));
if (j > r) {
j = r;
}
res = (res + (q * ((j - i) + 1)));
i = (j + 1);
}
return res;
}
int64_t sum_arith_i64_i64(int64_t l, int64_t r) {
int64_t n = ((r - l) + 1);
return FLOW_CHECKED_DIV((((l + r) * n)), (2));
}
void htab_init(void) {
htab_m = ((int64_t*)(calloc(HASH_CAP, 8)));
htab_a = ((int64_t*)(calloc(HASH_CAP, 8)));
htab_c = ((int64_t*)(calloc(HASH_CAP, 8)));
htab_l = ((int64_t*)(calloc(HASH_CAP, 8)));
}
int32_t htab_get_i64_i64_ptr_i64_ptr_i64(int64_t m, int64_t a, int64_t* cp, int64_t* lp) {
int64_t h = ((m * 1000003) + a);
h = (h ^ FLOW_CHECKED_SHR((h), (16)));
int64_t idx = (h & (HASH_CAP - 1));
while (1) {
if ((htab_m[idx] == 0 && htab_a[idx] == 0)) {
return 0;
}
if ((htab_m[idx] == m && htab_a[idx] == a)) {
cp[0] = htab_c[idx];
lp[0] = htab_l[idx];
return 1;
}
idx = ((idx + 1) & (HASH_CAP - 1));
}
return 0;
}
void htab_put_i64_i64_i64_i64(int64_t m, int64_t a, int64_t c, int64_t l) {
int64_t h = ((m * 1000003) + a);
h = (h ^ FLOW_CHECKED_SHR((h), (16)));
int64_t idx = (h & (HASH_CAP - 1));
while (1) {
if ((htab_m[idx] == 0 && htab_a[idx] == 0)) {
htab_m[idx] = m;
htab_a[idx] = a;
htab_c[idx] = c;
htab_l[idx] = l;
return;
}
if ((htab_m[idx] == m && htab_a[idx] == a)) {
htab_c[idx] = c;
htab_l[idx] = l;
return;
}
idx = ((idx + 1) & (HASH_CAP - 1));
}
}
void count_and_length_i64_i64_ptr_i64_ptr_i64(int64_t m, int64_t a, int64_t* Cp, int64_t* Lp) {
if (m < a) {
Cp[0] = 0;
Lp[0] = 0;
return;
}
int64_t cc_buf[1] = { 0 };
int64_t ll_buf[1] = { 0 };
if (htab_get_i64_i64_ptr_i64_ptr_i64(m, a, (&(cc_buf[0])), (&(ll_buf[0]))) == 1) {
Cp[0] = (((unsigned)(0) < 1) ? cc_buf[0] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(0), 1), flow_fault_handler("array index out of bounds"), cc_buf[0]));
Lp[0] = (((unsigned)(0) < 1) ? ll_buf[0] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(0), 1), flow_fault_handler("array index out of bounds"), ll_buf[0]));
return;
}
int64_t aa = (a * a);
int64_t C = 0;
int64_t L = 0;
if (aa > m) {
int64_t cnt = FLOW_CHECKED_MOD((((m - a) + 1)), (MOD));
C = cnt;
L = cnt;
} else {
C = ((m - a) + 1);
L = C;
int64_t s = ((int64_t)(sqrt(((double)(m)))));
while (((s + 1) * (s + 1)) <= m) {
s = (s + 1);
}
while ((s * s) > m) {
s = (s - 1);
}
if ((aa * a) > m) {
int64_t l = a;
if (l <= s) {
int64_t sf = sum_floor_range_i64_i64_i64(m, l, s);
int64_t sa = sum_arith_i64_i64(l, s);
int64_t baseC = ((sf - sa) + ((s - l) + 1));
C = (C + baseC);
L = (L + (2 * baseC));
}
} else {
int64_t t = icbrt_floor_i64(m);
int64_t upto = t;
if (s < t) {
upto = s;
}
if (upto >= a) {
int64_t f = a;
while (f <= upto) {
int64_t subC_buf[1] = { 0 };
int64_t subL_buf[1] = { 0 };
count_and_length_i64_i64_ptr_i64_ptr_i64(FLOW_CHECKED_DIV((m), (f)), f, (&(subC_buf[0])), (&(subL_buf[0])));
int64_t subC = (((unsigned)(0) < 1) ? subC_buf[0] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(0), 1), flow_fault_handler("array index out of bounds"), subC_buf[0]));
int64_t subL = (((unsigned)(0) < 1) ? subL_buf[0] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(0), 1), flow_fault_handler("array index out of bounds"), subL_buf[0]));
C = (C + subC);
L = ((L + subL) + subC);
f = (f + 1);
}
}
int64_t l2 = (upto + 1);
if (a > (upto + 1)) {
l2 = a;
}
if (l2 <= s) {
int64_t sf = sum_floor_range_i64_i64_i64(m, l2, s);
int64_t sa = sum_arith_i64_i64(l2, s);
int64_t baseC = ((sf - sa) + ((s - l2) + 1));
C = (C + baseC);
L = (L + (2 * baseC));
}
}
}
C = FLOW_CHECKED_MOD((C), (MOD));
L = FLOW_CHECKED_MOD((L), (MOD));
htab_put_i64_i64_i64_i64(m, a, C, L);
Cp[0] = C;
Lp[0] = L;
}
int32_t main(void) {
htab_init();
int64_t N = 10000000000;
int64_t K = 10000000000;
int64_t C_buf[1] = { 0 };
int64_t L_buf[1] = { 0 };
count_and_length_i64_i64_ptr_i64_ptr_i64(N, 2, (&(C_buf[0])), (&(L_buf[0])));
int64_t C = (((unsigned)(0) < 1) ? C_buf[0] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(0), 1), flow_fault_handler("array index out of bounds"), C_buf[0]));
int64_t L = (((unsigned)(0) < 1) ? L_buf[0] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(0), 1), flow_fault_handler("array index out of bounds"), L_buf[0]));
int64_t ans = ((FLOW_CHECKED_MOD((K), (MOD)) + FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((K + 1)), (MOD)) * C)), (MOD))) - L);
ans = FLOW_CHECKED_MOD((ans), (MOD));
if (ans < 0) {
ans = (ans + MOD);
}
printf("%lld\n", ans);
return 0;
}