Problem 250

Subsets of {1^1..250250^250250} with sum divisible by 250; last 16 digits.

Answer1425480602091519
Output1425480602091519
StatusPASS
Native helperno
Runtime120 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n * sum)
Space complexityO(1)O(sum)
ApproachFlow solutionSubset sum DP
VerdictUnknown

Flow source

# Project Euler 250
# Subsets of {1^1..250250^250250} with sum divisible by 250; last 16 digits.

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

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

function main() -> i32 {
    let LIMIT: i64 = 250250
    let MOD: i64 = 10000000000000000
    let arr: ptr<i64> = calloc(250, 8)
    let nxt: ptr<i64> = calloc(250, 8)
    if arr == null || nxt == null { return 1 }
    arr[0] = 1

    let mut i: i64 = 1
    while i <= LIMIT {
        let x: i64 = modpow(i, i, 250)
        let mut y: i64 = 0
        while y < 250 {
            nxt[y] = 0
            y = y + 1
        }
        y = 0
        while y < 250 {
            let a: i64 = arr[y]
            if a != 0 {
                nxt[y] = (nxt[y] + a) % MOD
                let z: i64 = (x + y) % 250
                nxt[z] = (nxt[z] + a) % MOD
            }
            y = y + 1
        }
        y = 0
        while y < 250 {
            arr[y] = nxt[y]
            y = y + 1
        }
        i = i + 1
    }
    let mut ans: i64 = arr[0] - 1
    if ans < 0 { ans = ans + MOD }
    printf("%lld\n", ans)
    free(arr); free(nxt)
    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 base, int64_t exp, int64_t mod);
int32_t main(void);



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

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