Problem 326

f(10^12, 10^6): count subarray sums ≡ 0 mod M via period 6M.

Answer1966666166408794329
Output1966666166408794329
StatusPASS
Native helperno
Runtime20 ms
Peak memory8960 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 326
# f(10^12, 10^6): count subarray sums ≡ 0 mod M via period 6M.

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

function a_n(n: i64) -> i64 {
    let r: i64 = n % 6
    if r == 0 || r == 2 { return n / 2 }
    if r == 1 { return (2 * n + 1) / 3 }
    if r == 3 { return (n - 3) / 6 }
    if r == 4 { return n - 1 }
    return (n - 5) / 6
}

function main() -> i32 {
    let N: i64 = 1000000000000
    let M: i64 = 1000000
    let P: i64 = 6 * M
    let q: i64 = N / P
    let r: i64 = N % P
    let freq: ptr<i64> = calloc(M, 8)
    if freq == null { return 1 }
    freq[0] = q + 1
    let mut s: i64 = 0
    if q == 0 {
        let mut i: i64 = 1
        while i <= r {
            s = (s + a_n(i)) % M
            freq[s] = freq[s] + 1
            i = i + 1
        }
    } else {
        let mut i: i64 = 1
        while i < P {
            s = (s + a_n(i)) % M
            if i <= r {
                freq[s] = freq[s] + (q + 1)
            } else {
                freq[s] = freq[s] + q
            }
            i = i + 1
        }
    }
    let mut ans: i64 = 0
    let mut i: i64 = 0
    while i < M {
        let c: i64 = freq[i]
        if c > 1 {
            ans = ans + c * (c - 1) / 2
        }
        i = i + 1
    }
    printf("%lld\n", ans)
    free(freq)
    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 a_n_i64(int64_t n);
int32_t main(void);



int64_t a_n_i64(int64_t n) {
    int64_t r = FLOW_CHECKED_MOD((n), (6));
    if ((r == 0 || r == 2)) {
        return FLOW_CHECKED_DIV((n), (2));
    }
    if (r == 1) {
        return FLOW_CHECKED_DIV((((2 * n) + 1)), (3));
    }
    if (r == 3) {
        return FLOW_CHECKED_DIV(((n - 3)), (6));
    }
    if (r == 4) {
        return (n - 1);
    }
    return FLOW_CHECKED_DIV(((n - 5)), (6));
}

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