Problem 429

Sum of squares of unitary divisors of 10^8!, mod 10^9+9.

Answer98792821
Output98792821
StatusPASS
Native helperno
Runtime550 ms
Peak memory49952 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(n)O(n)
ApproachFlow solutionSieve-based divisor sums
VerdictSuboptimal

Flow source

# Project Euler 429
# Sum of squares of unitary divisors of 10^8!, mod 10^9+9.

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

function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base0 % mod
    let mut e: i64 = exp0
    while e > 0 {
        if e % 2 == 1 {
            r = ((r as i128) * (b as i128) % (mod as i128)) as i64
        }
        b = ((b as i128) * (b as i128) % (mod as i128)) as i64
        e = e / 2
    }
    return r
}

function count_factors(n: i64, p: i64) -> i64 {
    let mut c: i64 = 0
    let mut pp: i64 = p
    while pp <= n {
        c = c + n / pp
        if pp > n / p { break }
        pp = pp * p
    }
    return c
}

function main() -> i32 {
    let LIMIT: i64 = 100000000
    let MOD: i64 = 1000000009
    let half: i64 = (LIMIT >> 1) + 1
    let sieve: ptr<i8> = calloc(half, 1)
    if sieve == null { return 1 }
    sieve[0] = 1
    let mut i: i64 = 1
    while 2 * i * i < half {
        if sieve[i] == 0 {
            let mut current: i64 = 3 * i + 1
            let step: i64 = 2 * i + 1
            while current < half {
                sieve[current] = 1
                current = current + step
            }
        }
        i = i + 1
    }
    let mut ans: i64 = 1
    let c2: i64 = count_factors(LIMIT, 2)
    ans = (ans * (1 + modpow(2, 2 * c2, MOD))) % MOD
    let mut p: i64 = 3
    while p <= LIMIT {
        if sieve[p >> 1] == 0 {
            let c: i64 = count_factors(LIMIT, p)
            ans = (ans * (1 + modpow(p, 2 * c, MOD))) % MOD
        }
        p = p + 2
    }
    printf("%lld\n", ans)
    free(sieve)
    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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t count_factors_i64_i64(int64_t n, int64_t p);
int32_t main(void);



int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((base0), (mod));
    int64_t e = exp0;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
        }
        b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t count_factors_i64_i64(int64_t n, int64_t p) {
    int64_t c = 0;
    int64_t pp = p;
    while (pp <= n) {
        c = (c + FLOW_CHECKED_DIV((n), (pp)));
        if (pp > FLOW_CHECKED_DIV((n), (p))) {
            break;
        }
        pp = (pp * p);
    }
    return c;
}

int32_t main(void) {
    int64_t LIMIT = 100000000;
    int64_t MOD = 1000000009;
    int64_t half = (FLOW_CHECKED_SHR((LIMIT), (1)) + 1);
    int8_t* sieve = (int8_t*)(calloc(half, 1));
    if (sieve == NULL) {
        return 1;
    }
    sieve[0] = 1;
    int64_t i = 1;
    while (((2 * i) * i) < half) {
        if (sieve[i] == 0) {
            int64_t current = ((3 * i) + 1);
            int64_t step = ((2 * i) + 1);
            while (current < half) {
                sieve[current] = 1;
                current = (current + step);
            }
        }
        i = (i + 1);
    }
    int64_t ans = 1;
    int64_t c2 = count_factors_i64_i64(LIMIT, 2);
    ans = FLOW_CHECKED_MOD(((ans * (1 + modpow_i64_i64_i64(2, (2 * c2), MOD)))), (MOD));
    int64_t p = 3;
    while (p <= LIMIT) {
        if (sieve[FLOW_CHECKED_SHR((p), (1))] == 0) {
            int64_t c = count_factors_i64_i64(LIMIT, p);
            ans = FLOW_CHECKED_MOD(((ans * (1 + modpow_i64_i64_i64(p, (2 * c), MOD)))), (MOD));
        }
        p = (p + 2);
    }
    printf("%lld\n", ans);
    free(sieve);
    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 @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i64, !llvm.ptr
    %4 = arith.remsi %arg0, %arg2 : i64
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %4, %6 : i64, !llvm.ptr
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %8 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %9 = llvm.load %8 : !llvm.ptr -> i64
    %10 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %11 = arith.cmpi sgt, %9, %12 : i64
    cf.cond_br %11, ^bb1, ^bb2
    ^bb1:
      %13 = llvm.load %8 : !llvm.ptr -> i64
      %14 = arith.constant 2 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.remsi %13, %16 : i64
      %17 = arith.constant 1 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.cmpi eq, %15, %19 : i64
      cf.cond_br %18, ^bb3, ^bb4
      ^bb3:
        %20 = llvm.load %3 : !llvm.ptr -> i64
        %21 = arith.extsi %20 : i64 to i128
        %22 = llvm.load %6 : !llvm.ptr -> i64
        %23 = arith.extsi %22 : i64 to i128
        %25 = arith.trunci %21 : i128 to i64
        %26 = arith.trunci %23 : i128 to i64
        %24 = arith.muli %25, %26 : i64
        %27 = arith.extsi %arg2 : i64 to i128
        %29 = arith.trunci %27 : i128 to i64
        %28 = arith.remsi %24, %29 : i64
        llvm.store %28, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %30 = llvm.load %6 : !llvm.ptr -> i64
      %31 = arith.extsi %30 : i64 to i128
      %32 = llvm.load %6 : !llvm.ptr -> i64
      %33 = arith.extsi %32 : i64 to i128
      %35 = arith.trunci %31 : i128 to i64
      %36 = arith.trunci %33 : i128 to i64
      %34 = arith.muli %35, %36 : i64
      %37 = arith.extsi %arg2 : i64 to i128
      %39 = arith.trunci %37 : i128 to i64
      %38 = arith.remsi %34, %39 : i64
      llvm.store %38, %6 : i64, !llvm.ptr
      %40 = llvm.load %8 : !llvm.ptr -> i64
      %41 = arith.constant 2 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.divsi %40, %43 : i64
      llvm.store %42, %8 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %44 = llvm.load %3 : !llvm.ptr -> i64
    func.return %44 : i64
  }
  func.func @count_factors(%arg0: i64, %arg1: i64) -> i64 {
    %45 = arith.constant 0 : i32
    %46 = arith.extsi %45 : i32 to i64
    %47 = llvm.mlir.constant(1 : i64) : i64
    %48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
    llvm.store %46, %48 : i64, !llvm.ptr
    %49 = llvm.mlir.constant(1 : i64) : i64
    %50 = llvm.alloca %49 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %50 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %51 = llvm.load %50 : !llvm.ptr -> i64
    %52 = arith.cmpi sle, %51, %arg0 : i64
    cf.cond_br %52, ^bb7, ^bb8
    ^bb7:
      %53 = llvm.load %48 : !llvm.ptr -> i64
      %54 = llvm.load %50 : !llvm.ptr -> i64
      %55 = arith.divsi %arg0, %54 : i64
      %56 = arith.addi %53, %55 : i64
      llvm.store %56, %48 : i64, !llvm.ptr
      %57 = llvm.load %50 : !llvm.ptr -> i64
      %58 = arith.divsi %arg0, %arg1 : i64
      %59 = arith.cmpi sgt, %57, %58 : i64
      cf.cond_br %59, ^bb9, ^bb10
      ^bb9:
        cf.br ^bb8
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %60 = llvm.load %50 : !llvm.ptr -> i64
      %61 = arith.muli %60, %arg1 : i64
      llvm.store %61, %50 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %62 = llvm.load %48 : !llvm.ptr -> i64
    func.return %62 : i64
  }
  func.func @main() -> i32 {
    %63 = arith.constant 100000000 : i32
    %64 = arith.extsi %63 : i32 to i64
    %65 = arith.constant 1000000009 : i32
    %66 = arith.extsi %65 : i32 to i64
    %67 = arith.constant 1 : i32
    %69 = arith.extsi %67 : i32 to i64
    %68 = arith.shrsi %64, %69 : i64
    %70 = arith.constant 1 : i32
    %72 = arith.extsi %70 : i32 to i64
    %71 = arith.addi %68, %72 : i64
    %74 = arith.constant 1 : i32
    %75 = arith.extsi %74 : i32 to i64
    %73 = func.call @calloc(%71, %75) : (i64, i64) -> !llvm.ptr
    %76 = llvm.mlir.zero : !llvm.ptr
    %77 = llvm.icmp "eq" %73, %76 : !llvm.ptr
    cf.cond_br %77, ^bb12, ^bb13
    ^bb12:
      %78 = arith.constant 1 : i32
      func.return %78 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %79 = arith.constant 1 : i32
    %80 = arith.constant 0 : i32
    %81 = arith.trunci %79 : i32 to i8
    %82 = arith.extsi %80 : i32 to i64
    %83 = llvm.getelementptr %73[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %81, %83 : i8, !llvm.ptr
    %84 = arith.constant 1 : i32
    %85 = arith.extsi %84 : i32 to i64
    %86 = llvm.mlir.constant(1 : i64) : i64
    %87 = llvm.alloca %86 x i64 : (i64) -> !llvm.ptr
    llvm.store %85, %87 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %88 = arith.constant 2 : i32
    %89 = llvm.load %87 : !llvm.ptr -> i64
    %91 = arith.extsi %88 : i32 to i64
    %90 = arith.muli %91, %89 : i64
    %92 = llvm.load %87 : !llvm.ptr -> i64
    %93 = arith.muli %90, %92 : i64
    %94 = arith.cmpi slt, %93, %71 : i64
    cf.cond_br %94, ^bb16, ^bb17
    ^bb16:
      %96 = llvm.load %87 : !llvm.ptr -> i64
      %97 = llvm.getelementptr %73[%96] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %95 = llvm.load %97 : !llvm.ptr -> i8
      %98 = arith.constant 0 : i32
      %100 = arith.extsi %95 : i8 to i32
      %99 = arith.cmpi eq, %100, %98 : i32
      cf.cond_br %99, ^bb18, ^bb19
      ^bb18:
        %101 = arith.constant 3 : i32
        %102 = llvm.load %87 : !llvm.ptr -> i64
        %104 = arith.extsi %101 : i32 to i64
        %103 = arith.muli %104, %102 : i64
        %105 = arith.constant 1 : i32
        %107 = arith.extsi %105 : i32 to i64
        %106 = arith.addi %103, %107 : i64
        %108 = llvm.mlir.constant(1 : i64) : i64
        %109 = llvm.alloca %108 x i64 : (i64) -> !llvm.ptr
        llvm.store %106, %109 : i64, !llvm.ptr
        %110 = arith.constant 2 : i32
        %111 = llvm.load %87 : !llvm.ptr -> i64
        %113 = arith.extsi %110 : i32 to i64
        %112 = arith.muli %113, %111 : i64
        %114 = arith.constant 1 : i32
        %116 = arith.extsi %114 : i32 to i64
        %115 = arith.addi %112, %116 : i64
        cf.br ^bb21
        ^bb21:
        %117 = llvm.load %109 : !llvm.ptr -> i64
        %118 = arith.cmpi slt, %117, %71 : i64
        cf.cond_br %118, ^bb22, ^bb23
        ^bb22:
          %119 = arith.constant 1 : i32
          %120 = llvm.load %109 : !llvm.ptr -> i64
          %121 = arith.trunci %119 : i32 to i8
          %122 = llvm.getelementptr %73[%120] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %121, %122 : i8, !llvm.ptr
          %123 = llvm.load %109 : !llvm.ptr -> i64
          %124 = arith.addi %123, %115 : i64
          llvm.store %124, %109 : i64, !llvm.ptr
          cf.br ^bb21
        ^bb23:
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %125 = llvm.load %87 : !llvm.ptr -> i64
      %126 = arith.constant 1 : i32
      %128 = arith.extsi %126 : i32 to i64
      %127 = arith.addi %125, %128 : i64
      llvm.store %127, %87 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %129 = arith.constant 1 : i32
    %130 = arith.extsi %129 : i32 to i64
    %131 = llvm.mlir.constant(1 : i64) : i64
    %132 = llvm.alloca %131 x i64 : (i64) -> !llvm.ptr
    llvm.store %130, %132 : i64, !llvm.ptr
    %134 = arith.constant 2 : i32
    %135 = arith.extsi %134 : i32 to i64
    %133 = func.call @count_factors(%64, %135) : (i64, i64) -> i64
    %136 = llvm.load %132 : !llvm.ptr -> i64
    %137 = arith.constant 1 : i32
    %139 = arith.constant 2 : i32
    %140 = arith.constant 2 : i32
    %142 = arith.extsi %140 : i32 to i64
    %141 = arith.muli %142, %133 : i64
    %143 = arith.extsi %139 : i32 to i64
    %138 = func.call @modpow(%143, %141, %66) : (i64, i64, i64) -> i64
    %145 = arith.extsi %137 : i32 to i64
    %144 = arith.addi %145, %138 : i64
    %146 = arith.muli %136, %144 : i64
    %147 = arith.remsi %146, %66 : i64
    llvm.store %147, %132 : i64, !llvm.ptr
    %148 = arith.constant 3 : i32
    %149 = arith.extsi %148 : i32 to i64
    %150 = llvm.mlir.constant(1 : i64) : i64
    %151 = llvm.alloca %150 x i64 : (i64) -> !llvm.ptr
    llvm.store %149, %151 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %152 = llvm.load %151 : !llvm.ptr -> i64
    %153 = arith.cmpi sle, %152, %64 : i64
    cf.cond_br %153, ^bb25, ^bb26
    ^bb25:
      %155 = llvm.load %151 : !llvm.ptr -> i64
      %156 = arith.constant 1 : i32
      %158 = arith.extsi %156 : i32 to i64
      %157 = arith.shrsi %155, %158 : i64
      %159 = llvm.getelementptr %73[%157] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %154 = llvm.load %159 : !llvm.ptr -> i8
      %160 = arith.constant 0 : i32
      %162 = arith.extsi %154 : i8 to i32
      %161 = arith.cmpi eq, %162, %160 : i32
      cf.cond_br %161, ^bb27, ^bb28
      ^bb27:
        %164 = llvm.load %151 : !llvm.ptr -> i64
        %163 = func.call @count_factors(%64, %164) : (i64, i64) -> i64
        %165 = llvm.load %132 : !llvm.ptr -> i64
        %166 = arith.constant 1 : i32
        %168 = llvm.load %151 : !llvm.ptr -> i64
        %169 = arith.constant 2 : i32
        %171 = arith.extsi %169 : i32 to i64
        %170 = arith.muli %171, %163 : i64
        %167 = func.call @modpow(%168, %170, %66) : (i64, i64, i64) -> i64
        %173 = arith.extsi %166 : i32 to i64
        %172 = arith.addi %173, %167 : i64
        %174 = arith.muli %165, %172 : i64
        %175 = arith.remsi %174, %66 : i64
        llvm.store %175, %132 : i64, !llvm.ptr
        cf.br ^bb29
      ^bb28:
        cf.br ^bb29
      ^bb29:
      %176 = llvm.load %151 : !llvm.ptr -> i64
      %177 = arith.constant 2 : i32
      %179 = arith.extsi %177 : i32 to i64
      %178 = arith.addi %176, %179 : i64
      llvm.store %178, %151 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %180 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %181 = llvm.load %132 : !llvm.ptr -> i64
    %182 = llvm.call @printf(%180, %181) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%73) : (!llvm.ptr) -> ()
    %184 = arith.constant 0 : i32
    func.return %184 : i32
  }
}