Problem 862

Larger Digit Permutation — S(12) via digit-multiset enumeration.

Answer6111397420935766740
Output6111397420935766740
StatusPASS
Native helperno
Runtime0 ms
Peak memory1104 KB
Time complexityO(1) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(1)O(n!)
Space complexityO(n)O(n)
ApproachFlow solutionPermutation enumeration or constraint search
VerdictOptimal

Flow source

# Project Euler 862
# Larger Digit Permutation — S(12) via digit-multiset enumeration.

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

let mut FACT: ptr<i64> = null
let mut TOTAL: i64 = 0
let mut D0: i64 = 0
let mut R: i64 = 0
let mut FK1: i64 = 0

function rec(digit: i64, rem: i64, denom_prod: i64) -> void {
    if digit == 10 {
        if rem != 0 { return }
        let denom: i64 = FACT[D0] * denom_prod
        let M: i64 = (R * FK1) / denom
        TOTAL = TOTAL + M * (M - 1) / 2
        return
    }
    if digit == 9 {
        rec(10, 0, denom_prod * FACT[rem])
        return
    }
    let mut c: i64 = 0
    while c <= rem {
        rec(digit + 1, rem - c, denom_prod * FACT[c])
        c = c + 1
    }
}

function S(k: i64) -> i64 {
    if k <= 1 { return 0 }
    FACT = calloc(k + 1, 8)
    FACT[0] = 1
    let mut i: i64 = 1
    while i <= k {
        FACT[i] = FACT[i - 1] * i
        i = i + 1
    }
    FK1 = FACT[k - 1]
    TOTAL = 0
    D0 = 0
    while D0 <= k {
        R = k - D0
        if R > 0 {
            rec(1, R, 1)
        }
        D0 = D0 + 1
    }
    let ans: i64 = TOTAL
    free(FACT)
    return ans
}

function main() -> i32 {
    printf("%lld\n", S(12))
    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; }

void rec_i64_i64_i64(int64_t digit, int64_t rem, int64_t denom_prod);
int64_t S_i64(int64_t k);
int32_t main(void);

/* Module statics */
static int64_t* FACT = NULL;
static int64_t TOTAL = 0;
static int64_t D0 = 0;
static int64_t R = 0;
static int64_t FK1 = 0;



void rec_i64_i64_i64(int64_t digit, int64_t rem, int64_t denom_prod) {
    if (digit == 10) {
        if (rem != 0) {
            return;
        }
        int64_t denom = (FACT[D0] * denom_prod);
        int64_t M = FLOW_CHECKED_DIV(((R * FK1)), (denom));
        TOTAL = (TOTAL + FLOW_CHECKED_DIV(((M * (M - 1))), (2)));
        return;
    }
    if (digit == 9) {
        rec_i64_i64_i64(10, 0, (denom_prod * FACT[rem]));
        return;
    }
    int64_t c = 0;
    while (c <= rem) {
        rec_i64_i64_i64((digit + 1), (rem - c), (denom_prod * FACT[c]));
        c = (c + 1);
    }
}

int64_t S_i64(int64_t k) {
    if (k <= 1) {
        return 0;
    }
    FACT = calloc((k + 1), 8);
    FACT[0] = 1;
    int64_t i = 1;
    while (i <= k) {
        FACT[i] = (FACT[(i - 1)] * i);
        i = (i + 1);
    }
    FK1 = FACT[(k - 1)];
    TOTAL = 0;
    D0 = 0;
    while (D0 <= k) {
        R = (k - D0);
        if (R > 0) {
            rec_i64_i64_i64(1, R, 1);
        }
        D0 = (D0 + 1);
    }
    int64_t ans = TOTAL;
    free(FACT);
    return ans;
}

int32_t main(void) {
    printf("%lld\n", S_i64(12));
    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) -> ()
  // Module static: FACT
  llvm.mlir.global internal @FACT() {addr_space = 0 : i32} : !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    llvm.return %0 : !llvm.ptr
  }
  // Module static: TOTAL
  llvm.mlir.global internal @TOTAL(0 : i64) : i64
  // Module static: D0
  llvm.mlir.global internal @D0(0 : i64) : i64
  // Module static: R
  llvm.mlir.global internal @R(0 : i64) : i64
  // Module static: FK1
  llvm.mlir.global internal @FK1(0 : i64) : i64
  func.func @rec(%arg0: i64, %arg1: i64, %arg2: i64) -> () {
    %1 = arith.constant 10 : i32
    %3 = arith.extsi %1 : i32 to i64
    %2 = arith.cmpi eq, %arg0, %3 : i64
    cf.cond_br %2, ^bb0, ^bb1
    ^bb0:
      %4 = arith.constant 0 : i32
      %6 = arith.extsi %4 : i32 to i64
      %5 = arith.cmpi ne, %arg1, %6 : i64
      cf.cond_br %5, ^bb3, ^bb4
      ^bb3:
        func.return
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %8 = llvm.mlir.addressof @FACT : !llvm.ptr
      %9 = llvm.load %8 : !llvm.ptr -> !llvm.ptr
      %10 = llvm.mlir.addressof @D0 : !llvm.ptr
      %11 = llvm.load %10 : !llvm.ptr -> i64
      %12 = llvm.getelementptr %9[%11] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %7 = llvm.load %12 : !llvm.ptr -> i64
      %13 = arith.muli %7, %arg2 : i64
      %14 = llvm.mlir.addressof @R : !llvm.ptr
      %15 = llvm.load %14 : !llvm.ptr -> i64
      %16 = llvm.mlir.addressof @FK1 : !llvm.ptr
      %17 = llvm.load %16 : !llvm.ptr -> i64
      %18 = arith.muli %15, %17 : i64
      %19 = arith.divsi %18, %13 : i64
      %20 = llvm.mlir.addressof @TOTAL : !llvm.ptr
      %21 = llvm.load %20 : !llvm.ptr -> i64
      %22 = arith.constant 1 : i32
      %24 = arith.extsi %22 : i32 to i64
      %23 = arith.subi %19, %24 : i64
      %25 = arith.muli %19, %23 : i64
      %26 = arith.constant 2 : i32
      %28 = arith.extsi %26 : i32 to i64
      %27 = arith.divsi %25, %28 : i64
      %29 = arith.addi %21, %27 : i64
      %30 = llvm.mlir.addressof @TOTAL : !llvm.ptr
      llvm.store %29, %30 : i64, !llvm.ptr
      func.return
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %31 = arith.constant 9 : i32
    %33 = arith.extsi %31 : i32 to i64
    %32 = arith.cmpi eq, %arg0, %33 : i64
    cf.cond_br %32, ^bb6, ^bb7
    ^bb6:
      %35 = arith.constant 10 : i32
      %36 = arith.constant 0 : i32
      %38 = llvm.mlir.addressof @FACT : !llvm.ptr
      %39 = llvm.load %38 : !llvm.ptr -> !llvm.ptr
      %40 = llvm.getelementptr %39[%arg1] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %37 = llvm.load %40 : !llvm.ptr -> i64
      %41 = arith.muli %arg2, %37 : i64
      %42 = arith.extsi %35 : i32 to i64
      %43 = arith.extsi %36 : i32 to i64
      func.call @rec(%42, %43, %41) : (i64, i64, i64) -> ()
      func.return
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %44 = arith.constant 0 : i32
    %45 = arith.extsi %44 : i32 to i64
    %46 = llvm.mlir.constant(1 : i64) : i64
    %47 = llvm.alloca %46 x i64 : (i64) -> !llvm.ptr
    llvm.store %45, %47 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %48 = llvm.load %47 : !llvm.ptr -> i64
    %49 = arith.cmpi sle, %48, %arg1 : i64
    cf.cond_br %49, ^bb10, ^bb11
    ^bb10:
      %51 = arith.constant 1 : i32
      %53 = arith.extsi %51 : i32 to i64
      %52 = arith.addi %arg0, %53 : i64
      %54 = llvm.load %47 : !llvm.ptr -> i64
      %55 = arith.subi %arg1, %54 : i64
      %57 = llvm.mlir.addressof @FACT : !llvm.ptr
      %58 = llvm.load %57 : !llvm.ptr -> !llvm.ptr
      %59 = llvm.load %47 : !llvm.ptr -> i64
      %60 = llvm.getelementptr %58[%59] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %56 = llvm.load %60 : !llvm.ptr -> i64
      %61 = arith.muli %arg2, %56 : i64
      func.call @rec(%52, %55, %61) : (i64, i64, i64) -> ()
      %62 = llvm.load %47 : !llvm.ptr -> i64
      %63 = arith.constant 1 : i32
      %65 = arith.extsi %63 : i32 to i64
      %64 = arith.addi %62, %65 : i64
      llvm.store %64, %47 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    func.return
  }
  func.func @S(%arg0: i64) -> i64 {
    %66 = arith.constant 1 : i32
    %68 = arith.extsi %66 : i32 to i64
    %67 = arith.cmpi sle, %arg0, %68 : i64
    cf.cond_br %67, ^bb12, ^bb13
    ^bb12:
      %69 = arith.constant 0 : i32
      %70 = arith.extsi %69 : i32 to i64
      func.return %70 : i64
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %72 = arith.constant 1 : i32
    %74 = arith.extsi %72 : i32 to i64
    %73 = arith.addi %arg0, %74 : i64
    %75 = arith.constant 8 : i32
    %76 = arith.extsi %75 : i32 to i64
    %71 = func.call @calloc(%73, %76) : (i64, i64) -> !llvm.ptr
    %77 = llvm.mlir.addressof @FACT : !llvm.ptr
    llvm.store %71, %77 : !llvm.ptr, !llvm.ptr
    %78 = arith.constant 1 : i32
    %79 = llvm.mlir.addressof @FACT : !llvm.ptr
    %80 = llvm.load %79 : !llvm.ptr -> !llvm.ptr
    %81 = arith.constant 0 : i32
    %82 = arith.extsi %78 : i32 to i64
    %83 = arith.extsi %81 : i32 to i64
    %84 = llvm.getelementptr %80[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %82, %84 : i64, !llvm.ptr
    %85 = arith.constant 1 : i32
    %86 = arith.extsi %85 : i32 to i64
    %87 = llvm.mlir.constant(1 : i64) : i64
    %88 = llvm.alloca %87 x i64 : (i64) -> !llvm.ptr
    llvm.store %86, %88 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %89 = llvm.load %88 : !llvm.ptr -> i64
    %90 = arith.cmpi sle, %89, %arg0 : i64
    cf.cond_br %90, ^bb16, ^bb17
    ^bb16:
      %92 = llvm.mlir.addressof @FACT : !llvm.ptr
      %93 = llvm.load %92 : !llvm.ptr -> !llvm.ptr
      %94 = llvm.load %88 : !llvm.ptr -> i64
      %95 = arith.constant 1 : i32
      %97 = arith.extsi %95 : i32 to i64
      %96 = arith.subi %94, %97 : i64
      %98 = llvm.getelementptr %93[%96] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %91 = llvm.load %98 : !llvm.ptr -> i64
      %99 = llvm.load %88 : !llvm.ptr -> i64
      %100 = arith.muli %91, %99 : i64
      %101 = llvm.mlir.addressof @FACT : !llvm.ptr
      %102 = llvm.load %101 : !llvm.ptr -> !llvm.ptr
      %103 = llvm.load %88 : !llvm.ptr -> i64
      %104 = llvm.getelementptr %102[%103] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %100, %104 : i64, !llvm.ptr
      %105 = llvm.load %88 : !llvm.ptr -> i64
      %106 = arith.constant 1 : i32
      %108 = arith.extsi %106 : i32 to i64
      %107 = arith.addi %105, %108 : i64
      llvm.store %107, %88 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %110 = llvm.mlir.addressof @FACT : !llvm.ptr
    %111 = llvm.load %110 : !llvm.ptr -> !llvm.ptr
    %112 = arith.constant 1 : i32
    %114 = arith.extsi %112 : i32 to i64
    %113 = arith.subi %arg0, %114 : i64
    %115 = llvm.getelementptr %111[%113] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %109 = llvm.load %115 : !llvm.ptr -> i64
    %116 = llvm.mlir.addressof @FK1 : !llvm.ptr
    llvm.store %109, %116 : i64, !llvm.ptr
    %117 = arith.constant 0 : i32
    %118 = arith.extsi %117 : i32 to i64
    %119 = llvm.mlir.addressof @TOTAL : !llvm.ptr
    llvm.store %118, %119 : i64, !llvm.ptr
    %120 = arith.constant 0 : i32
    %121 = arith.extsi %120 : i32 to i64
    %122 = llvm.mlir.addressof @D0 : !llvm.ptr
    llvm.store %121, %122 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %123 = llvm.mlir.addressof @D0 : !llvm.ptr
    %124 = llvm.load %123 : !llvm.ptr -> i64
    %125 = arith.cmpi sle, %124, %arg0 : i64
    cf.cond_br %125, ^bb19, ^bb20
    ^bb19:
      %126 = llvm.mlir.addressof @D0 : !llvm.ptr
      %127 = llvm.load %126 : !llvm.ptr -> i64
      %128 = arith.subi %arg0, %127 : i64
      %129 = llvm.mlir.addressof @R : !llvm.ptr
      llvm.store %128, %129 : i64, !llvm.ptr
      %130 = llvm.mlir.addressof @R : !llvm.ptr
      %131 = llvm.load %130 : !llvm.ptr -> i64
      %132 = arith.constant 0 : i32
      %134 = arith.extsi %132 : i32 to i64
      %133 = arith.cmpi sgt, %131, %134 : i64
      cf.cond_br %133, ^bb21, ^bb22
      ^bb21:
        %136 = arith.constant 1 : i32
        %137 = llvm.mlir.addressof @R : !llvm.ptr
        %138 = llvm.load %137 : !llvm.ptr -> i64
        %139 = arith.constant 1 : i32
        %140 = arith.extsi %136 : i32 to i64
        %141 = arith.extsi %139 : i32 to i64
        func.call @rec(%140, %138, %141) : (i64, i64, i64) -> ()
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %142 = llvm.mlir.addressof @D0 : !llvm.ptr
      %143 = llvm.load %142 : !llvm.ptr -> i64
      %144 = arith.constant 1 : i32
      %146 = arith.extsi %144 : i32 to i64
      %145 = arith.addi %143, %146 : i64
      %147 = llvm.mlir.addressof @D0 : !llvm.ptr
      llvm.store %145, %147 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %148 = llvm.mlir.addressof @TOTAL : !llvm.ptr
    %149 = llvm.load %148 : !llvm.ptr -> i64
    %151 = llvm.mlir.addressof @FACT : !llvm.ptr
    %152 = llvm.load %151 : !llvm.ptr -> !llvm.ptr
    func.call @free(%152) : (!llvm.ptr) -> ()
    func.return %149 : i64
  }
  func.func @main() -> i32 {
    %153 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %155 = arith.constant 12 : i32
    %156 = arith.extsi %155 : i32 to i64
    %154 = func.call @S(%156) : (i64) -> i64
    %157 = llvm.call @printf(%153, %154) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %158 = arith.constant 0 : i32
    func.return %158 : i32
  }
}