Problem 745

S(10^14)=sum g(n) mod 10^9+7 via squareful Dirichlet inversion.

Answer94586478
Output94586478
StatusPASS
Native helperno
Runtime830 ms
Peak memory79248 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log n)
Space complexityO(n)O(n)
ApproachFlow solutionModular DP or matrix exponentiation
VerdictSuboptimal

Flow source

# Project Euler 745
# S(10^14)=sum g(n) mod 10^9+7 via squareful Dirichlet inversion.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function isqrt(n: i64) -> i64 {
    if n <= 0 { return 0 }
    let mut x: i64 = n
    let mut y: i64 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

function main() -> i32 {
    let N: i64 = 100000000000000
    let mod: i64 = 1000000007
    let sqrtN: i64 = isqrt(N)
    let a: ptr<i64> = calloc(sqrtN + 1, 8)
    if a == null { return 1 }
    let mut i: i64 = sqrtN
    while i >= 1 {
        let mut s: i64 = N / (i * i)
        let mut j: i64 = 2
        while i * j <= sqrtN {
            s = s - a[i * j]
            j = j + 1
        }
        a[i] = s
        i = i - 1
    }
    let mut total: i64 = 0
    i = 1
    while i <= sqrtN {
        let term: i64 = ((i * i) % mod) * (a[i] % mod) % mod
        total = (total + term) % mod
        i = i + 1
    }
    printf("%lld\n", total)
    free(a)
    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; }

int64_t isqrt_i64(int64_t n);
int32_t main(void);



int64_t isqrt_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    int64_t x = n;
    int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

int32_t main(void) {
    int64_t N = 100000000000000;
    int64_t mod = 1000000007;
    int64_t sqrtN = isqrt_i64(N);
    int64_t* a = (int64_t*)(calloc((sqrtN + 1), 8));
    if (a == NULL) {
        return 1;
    }
    int64_t i = sqrtN;
    while (i >= 1) {
        int64_t s = FLOW_CHECKED_DIV((N), ((i * i)));
        int64_t j = 2;
        while ((i * j) <= sqrtN) {
            s = (s - a[(i * j)]);
            j = (j + 1);
        }
        a[i] = s;
        i = (i - 1);
    }
    int64_t total = 0;
    i = 1;
    while (i <= sqrtN) {
        int64_t term = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((i * i)), (mod)) * FLOW_CHECKED_MOD((a[i]), (mod)))), (mod));
        total = FLOW_CHECKED_MOD(((total + term)), (mod));
        i = (i + 1);
    }
    printf("%lld\n", total);
    free(a);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @isqrt(%arg0: i64) -> i64 {
    %0 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi sle, %arg0, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 0 : i32
      %4 = arith.extsi %3 : i32 to i64
      func.return %4 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %6 : i64, !llvm.ptr
    %7 = llvm.load %6 : !llvm.ptr -> i64
    %8 = arith.constant 1 : i32
    %10 = arith.extsi %8 : i32 to i64
    %9 = arith.addi %7, %10 : i64
    %11 = arith.constant 2 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.divsi %9, %13 : i64
    %14 = llvm.mlir.constant(1 : i64) : i64
    %15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %15 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %16 = llvm.load %15 : !llvm.ptr -> i64
    %17 = llvm.load %6 : !llvm.ptr -> i64
    %18 = arith.cmpi slt, %16, %17 : i64
    cf.cond_br %18, ^bb4, ^bb5
    ^bb4:
      %19 = llvm.load %15 : !llvm.ptr -> i64
      llvm.store %19, %6 : i64, !llvm.ptr
      %20 = llvm.load %6 : !llvm.ptr -> i64
      %21 = llvm.load %6 : !llvm.ptr -> i64
      %22 = arith.divsi %arg0, %21 : i64
      %23 = arith.addi %20, %22 : i64
      %24 = arith.constant 2 : i32
      %26 = arith.extsi %24 : i32 to i64
      %25 = arith.divsi %23, %26 : i64
      llvm.store %25, %15 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %27 = llvm.load %6 : !llvm.ptr -> i64
    func.return %27 : i64
  }
  func.func @main() -> i32 {
    %28 = arith.constant 99995705032704 : i32
    %29 = arith.extsi %28 : i32 to i64
    %30 = arith.constant 1000000007 : i32
    %31 = arith.extsi %30 : i32 to i64
    %32 = func.call @isqrt(%29) : (i64) -> i64
    %34 = arith.constant 1 : i32
    %36 = arith.extsi %34 : i32 to i64
    %35 = arith.addi %32, %36 : i64
    %37 = arith.constant 8 : i32
    %38 = arith.extsi %37 : i32 to i64
    %33 = func.call @calloc(%35, %38) : (i64, i64) -> !llvm.ptr
    %39 = llvm.mlir.zero : !llvm.ptr
    %40 = llvm.icmp "eq" %33, %39 : !llvm.ptr
    cf.cond_br %40, ^bb6, ^bb7
    ^bb6:
      %41 = arith.constant 1 : i32
      func.return %41 : i32
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %42 = llvm.mlir.constant(1 : i64) : i64
    %43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
    llvm.store %32, %43 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %44 = llvm.load %43 : !llvm.ptr -> i64
    %45 = arith.constant 1 : i32
    %47 = arith.extsi %45 : i32 to i64
    %46 = arith.cmpi sge, %44, %47 : i64
    cf.cond_br %46, ^bb10, ^bb11
    ^bb10:
      %48 = llvm.load %43 : !llvm.ptr -> i64
      %49 = llvm.load %43 : !llvm.ptr -> i64
      %50 = arith.muli %48, %49 : i64
      %51 = arith.divsi %29, %50 : i64
      %52 = llvm.mlir.constant(1 : i64) : i64
      %53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
      llvm.store %51, %53 : i64, !llvm.ptr
      %54 = arith.constant 2 : i32
      %55 = arith.extsi %54 : i32 to i64
      %56 = llvm.mlir.constant(1 : i64) : i64
      %57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
      llvm.store %55, %57 : i64, !llvm.ptr
      cf.br ^bb12
      ^bb12:
      %58 = llvm.load %43 : !llvm.ptr -> i64
      %59 = llvm.load %57 : !llvm.ptr -> i64
      %60 = arith.muli %58, %59 : i64
      %61 = arith.cmpi sle, %60, %32 : i64
      cf.cond_br %61, ^bb13, ^bb14
      ^bb13:
        %62 = llvm.load %53 : !llvm.ptr -> i64
        %64 = llvm.load %43 : !llvm.ptr -> i64
        %65 = llvm.load %57 : !llvm.ptr -> i64
        %66 = arith.muli %64, %65 : i64
        %67 = llvm.getelementptr %33[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %63 = llvm.load %67 : !llvm.ptr -> i64
        %68 = arith.subi %62, %63 : i64
        llvm.store %68, %53 : i64, !llvm.ptr
        %69 = llvm.load %57 : !llvm.ptr -> i64
        %70 = arith.constant 1 : i32
        %72 = arith.extsi %70 : i32 to i64
        %71 = arith.addi %69, %72 : i64
        llvm.store %71, %57 : i64, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      %73 = llvm.load %53 : !llvm.ptr -> i64
      %74 = llvm.load %43 : !llvm.ptr -> i64
      %75 = llvm.getelementptr %33[%74] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %73, %75 : i64, !llvm.ptr
      %76 = llvm.load %43 : !llvm.ptr -> i64
      %77 = arith.constant 1 : i32
      %79 = arith.extsi %77 : i32 to i64
      %78 = arith.subi %76, %79 : i64
      llvm.store %78, %43 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %80 = arith.constant 0 : i32
    %81 = arith.extsi %80 : i32 to i64
    %82 = llvm.mlir.constant(1 : i64) : i64
    %83 = llvm.alloca %82 x i64 : (i64) -> !llvm.ptr
    llvm.store %81, %83 : i64, !llvm.ptr
    %84 = arith.constant 1 : i32
    %85 = arith.extsi %84 : i32 to i64
    llvm.store %85, %43 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %86 = llvm.load %43 : !llvm.ptr -> i64
    %87 = arith.cmpi sle, %86, %32 : i64
    cf.cond_br %87, ^bb16, ^bb17
    ^bb16:
      %88 = llvm.load %43 : !llvm.ptr -> i64
      %89 = llvm.load %43 : !llvm.ptr -> i64
      %90 = arith.muli %88, %89 : i64
      %91 = arith.remsi %90, %31 : i64
      %93 = llvm.load %43 : !llvm.ptr -> i64
      %94 = llvm.getelementptr %33[%93] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %92 = llvm.load %94 : !llvm.ptr -> i64
      %95 = arith.remsi %92, %31 : i64
      %96 = arith.muli %91, %95 : i64
      %97 = arith.remsi %96, %31 : i64
      %98 = llvm.load %83 : !llvm.ptr -> i64
      %99 = arith.addi %98, %97 : i64
      %100 = arith.remsi %99, %31 : i64
      llvm.store %100, %83 : i64, !llvm.ptr
      %101 = llvm.load %43 : !llvm.ptr -> i64
      %102 = arith.constant 1 : i32
      %104 = arith.extsi %102 : i32 to i64
      %103 = arith.addi %101, %104 : i64
      llvm.store %103, %43 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %105 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %106 = llvm.load %83 : !llvm.ptr -> i64
    %107 = llvm.call @printf(%105, %106) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%33) : (!llvm.ptr) -> ()
    %109 = arith.constant 0 : i32
    func.return %109 : i32
  }
}