Problem 885

Sorted Digits — S(n)=(1/9)*sum_k (10+9k)^n - 10^n, mod 1123455689.

Answer827850196
Output827850196
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 885
# Sorted Digits — S(n)=(1/9)*sum_k (10+9k)^n - 10^n, mod 1123455689.

const MOD: i64 = 1123455689

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 & 1) != 0 {
            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 >> 1
    }
    return r
}

function egcd(a0: i64, b0: i64, x_out: ptr<i64>, y_out: ptr<i64>) -> i64 {
    let mut a: i64 = a0
    let mut b: i64 = b0
    let mut x0: i64 = 1
    let mut y0: i64 = 0
    let mut x1: i64 = 0
    let mut y1: i64 = 1
    while b != 0 {
        let q: i64 = a / b
        let nb: i64 = a - q * b
        a = b
        b = nb
        let nx: i64 = x0 - q * x1
        x0 = x1
        x1 = nx
        let ny: i64 = y0 - q * y1
        y0 = y1
        y1 = ny
    }
    x_out[0] = x0
    y_out[0] = y0
    return a
}

function modinv(a: i64, m: i64) -> i64 {
    let mut x: i64 = 0
    let mut y: i64 = 0
    egcd(a % m, m, &x, &y)
    let r: i64 = x % m
    if r < 0 { return r + m }
    return r
}

function S(n: i64) -> i64 {
    let inv9: i64 = modinv(9, MOD)
    let mut total: i64 = 0
    let mut k: i64 = 1
    while k <= 9 {
        total = (total + modpow(10 + 9 * k, n, MOD)) % MOD
        k = k + 1
    }
    total = (total - (9 * modpow(10, n, MOD)) % MOD + MOD) % MOD
    return ((total as i128) * (inv9 as i128) % (MOD as i128)) as i64
}

function main() -> i32 {
    printf("%lld\n", S(18))
    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 egcd_i64_i64_ptr_i64_ptr_i64(int64_t a0, int64_t b0, int64_t* x_out, int64_t* y_out);
int64_t modinv_i64_i64(int64_t a, int64_t m);
int64_t S_i64(int64_t n);
int32_t main(void);

static const int64_t MOD = 1123455689;

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 ((e & 1) != 0) {
            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_SHR((e), (1));
    }
    return r;
}

int64_t egcd_i64_i64_ptr_i64_ptr_i64(int64_t a0, int64_t b0, int64_t* x_out, int64_t* y_out) {
    int64_t a = a0;
    int64_t b = b0;
    int64_t x0 = 1;
    int64_t y0 = 0;
    int64_t x1 = 0;
    int64_t y1 = 1;
    while (b != 0) {
        int64_t q = FLOW_CHECKED_DIV((a), (b));
        int64_t nb = (a - (q * b));
        a = b;
        b = nb;
        int64_t nx = (x0 - (q * x1));
        x0 = x1;
        x1 = nx;
        int64_t ny = (y0 - (q * y1));
        y0 = y1;
        y1 = ny;
    }
    x_out[0] = x0;
    y_out[0] = y0;
    return a;
}

int64_t modinv_i64_i64(int64_t a, int64_t m) {
    int64_t x = 0;
    int64_t y = 0;
    egcd_i64_i64_ptr_i64_ptr_i64(FLOW_CHECKED_MOD((a), (m)), m, (&(x)), (&(y)));
    int64_t r = FLOW_CHECKED_MOD((x), (m));
    if (r < 0) {
        return (r + m);
    }
    return r;
}

int64_t S_i64(int64_t n) {
    int64_t inv9 = modinv_i64_i64(9, MOD);
    int64_t total = 0;
    int64_t k = 1;
    while (k <= 9) {
        total = FLOW_CHECKED_MOD(((total + modpow_i64_i64_i64((10 + (9 * k)), n, MOD))), (MOD));
        k = (k + 1);
    }
    total = FLOW_CHECKED_MOD((((total - FLOW_CHECKED_MOD(((9 * modpow_i64_i64_i64(10, n, MOD))), (MOD))) + MOD)), (MOD));
    return ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(total)) * ((__int128)(inv9)))), (((__int128)(MOD))))));
}

int32_t main(void) {
    printf("%lld\n", S_i64(18));
    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>
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1123455689 : i64) : i64
  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 1 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.andi %13, %16 : i64
      %17 = arith.constant 0 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.cmpi ne, %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 1 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.shrsi %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 @egcd(%arg0: i64, %arg1: i64, %arg2: !llvm.ptr, %arg3: !llvm.ptr) -> i64 {
    %45 = llvm.mlir.constant(1 : i64) : i64
    %46 = llvm.alloca %45 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %46 : i64, !llvm.ptr
    %47 = llvm.mlir.constant(1 : i64) : i64
    %48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %48 : i64, !llvm.ptr
    %49 = arith.constant 1 : i32
    %50 = arith.extsi %49 : i32 to i64
    %51 = llvm.mlir.constant(1 : i64) : i64
    %52 = llvm.alloca %51 x i64 : (i64) -> !llvm.ptr
    llvm.store %50, %52 : i64, !llvm.ptr
    %53 = arith.constant 0 : i32
    %54 = arith.extsi %53 : i32 to i64
    %55 = llvm.mlir.constant(1 : i64) : i64
    %56 = llvm.alloca %55 x i64 : (i64) -> !llvm.ptr
    llvm.store %54, %56 : i64, !llvm.ptr
    %57 = arith.constant 0 : i32
    %58 = arith.extsi %57 : i32 to i64
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x i64 : (i64) -> !llvm.ptr
    llvm.store %58, %60 : i64, !llvm.ptr
    %61 = arith.constant 1 : i32
    %62 = arith.extsi %61 : i32 to i64
    %63 = llvm.mlir.constant(1 : i64) : i64
    %64 = llvm.alloca %63 x i64 : (i64) -> !llvm.ptr
    llvm.store %62, %64 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %65 = llvm.load %48 : !llvm.ptr -> i64
    %66 = arith.constant 0 : i32
    %68 = arith.extsi %66 : i32 to i64
    %67 = arith.cmpi ne, %65, %68 : i64
    cf.cond_br %67, ^bb7, ^bb8
    ^bb7:
      %69 = llvm.load %46 : !llvm.ptr -> i64
      %70 = llvm.load %48 : !llvm.ptr -> i64
      %71 = arith.divsi %69, %70 : i64
      %72 = llvm.load %46 : !llvm.ptr -> i64
      %73 = llvm.load %48 : !llvm.ptr -> i64
      %74 = arith.muli %71, %73 : i64
      %75 = arith.subi %72, %74 : i64
      %76 = llvm.load %48 : !llvm.ptr -> i64
      llvm.store %76, %46 : i64, !llvm.ptr
      llvm.store %75, %48 : i64, !llvm.ptr
      %77 = llvm.load %52 : !llvm.ptr -> i64
      %78 = llvm.load %60 : !llvm.ptr -> i64
      %79 = arith.muli %71, %78 : i64
      %80 = arith.subi %77, %79 : i64
      %81 = llvm.load %60 : !llvm.ptr -> i64
      llvm.store %81, %52 : i64, !llvm.ptr
      llvm.store %80, %60 : i64, !llvm.ptr
      %82 = llvm.load %56 : !llvm.ptr -> i64
      %83 = llvm.load %64 : !llvm.ptr -> i64
      %84 = arith.muli %71, %83 : i64
      %85 = arith.subi %82, %84 : i64
      %86 = llvm.load %64 : !llvm.ptr -> i64
      llvm.store %86, %56 : i64, !llvm.ptr
      llvm.store %85, %64 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %87 = llvm.load %52 : !llvm.ptr -> i64
    %88 = arith.constant 0 : i32
    %89 = arith.extsi %88 : i32 to i64
    %90 = llvm.getelementptr %arg2[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %87, %90 : i64, !llvm.ptr
    %91 = llvm.load %56 : !llvm.ptr -> i64
    %92 = arith.constant 0 : i32
    %93 = arith.extsi %92 : i32 to i64
    %94 = llvm.getelementptr %arg3[%93] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %91, %94 : i64, !llvm.ptr
    %95 = llvm.load %46 : !llvm.ptr -> i64
    func.return %95 : i64
  }
  func.func @modinv(%arg0: i64, %arg1: i64) -> i64 {
    %96 = arith.constant 0 : i32
    %97 = arith.extsi %96 : i32 to i64
    %98 = llvm.mlir.constant(1 : i64) : i64
    %99 = llvm.alloca %98 x i64 : (i64) -> !llvm.ptr
    llvm.store %97, %99 : i64, !llvm.ptr
    %100 = arith.constant 0 : i32
    %101 = arith.extsi %100 : i32 to i64
    %102 = llvm.mlir.constant(1 : i64) : i64
    %103 = llvm.alloca %102 x i64 : (i64) -> !llvm.ptr
    llvm.store %101, %103 : i64, !llvm.ptr
    %105 = arith.remsi %arg0, %arg1 : i64
    %104 = func.call @egcd(%105, %arg1, %99, %103) : (i64, i64, !llvm.ptr, !llvm.ptr) -> i64
    %106 = llvm.load %99 : !llvm.ptr -> i64
    %107 = arith.remsi %106, %arg1 : i64
    %108 = arith.constant 0 : i32
    %110 = arith.extsi %108 : i32 to i64
    %109 = arith.cmpi slt, %107, %110 : i64
    cf.cond_br %109, ^bb9, ^bb10
    ^bb9:
      %111 = arith.addi %107, %arg1 : i64
      func.return %111 : i64
    ^bb10:
      cf.br ^bb11
    ^bb11:
    func.return %107 : i64
  }
  func.func @S(%arg0: i64) -> i64 {
    %113 = arith.constant 9 : i32
    %114 = llvm.mlir.addressof @MOD : !llvm.ptr
    %115 = llvm.load %114 : !llvm.ptr -> i64
    %116 = arith.extsi %113 : i32 to i64
    %112 = func.call @modinv(%116, %115) : (i64, i64) -> i64
    %117 = arith.constant 0 : i32
    %118 = arith.extsi %117 : i32 to i64
    %119 = llvm.mlir.constant(1 : i64) : i64
    %120 = llvm.alloca %119 x i64 : (i64) -> !llvm.ptr
    llvm.store %118, %120 : i64, !llvm.ptr
    %121 = arith.constant 1 : i32
    %122 = arith.extsi %121 : i32 to i64
    %123 = llvm.mlir.constant(1 : i64) : i64
    %124 = llvm.alloca %123 x i64 : (i64) -> !llvm.ptr
    llvm.store %122, %124 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %125 = llvm.load %124 : !llvm.ptr -> i64
    %126 = arith.constant 9 : i32
    %128 = arith.extsi %126 : i32 to i64
    %127 = arith.cmpi sle, %125, %128 : i64
    cf.cond_br %127, ^bb13, ^bb14
    ^bb13:
      %129 = llvm.load %120 : !llvm.ptr -> i64
      %131 = arith.constant 10 : i32
      %132 = arith.constant 9 : i32
      %133 = llvm.load %124 : !llvm.ptr -> i64
      %135 = arith.extsi %132 : i32 to i64
      %134 = arith.muli %135, %133 : i64
      %137 = arith.extsi %131 : i32 to i64
      %136 = arith.addi %137, %134 : i64
      %138 = llvm.mlir.addressof @MOD : !llvm.ptr
      %139 = llvm.load %138 : !llvm.ptr -> i64
      %130 = func.call @modpow(%136, %arg0, %139) : (i64, i64, i64) -> i64
      %140 = arith.addi %129, %130 : i64
      %141 = llvm.mlir.addressof @MOD : !llvm.ptr
      %142 = llvm.load %141 : !llvm.ptr -> i64
      %143 = arith.remsi %140, %142 : i64
      llvm.store %143, %120 : i64, !llvm.ptr
      %144 = llvm.load %124 : !llvm.ptr -> i64
      %145 = arith.constant 1 : i32
      %147 = arith.extsi %145 : i32 to i64
      %146 = arith.addi %144, %147 : i64
      llvm.store %146, %124 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %148 = llvm.load %120 : !llvm.ptr -> i64
    %149 = arith.constant 9 : i32
    %151 = arith.constant 10 : i32
    %152 = llvm.mlir.addressof @MOD : !llvm.ptr
    %153 = llvm.load %152 : !llvm.ptr -> i64
    %154 = arith.extsi %151 : i32 to i64
    %150 = func.call @modpow(%154, %arg0, %153) : (i64, i64, i64) -> i64
    %156 = arith.extsi %149 : i32 to i64
    %155 = arith.muli %156, %150 : i64
    %157 = llvm.mlir.addressof @MOD : !llvm.ptr
    %158 = llvm.load %157 : !llvm.ptr -> i64
    %159 = arith.remsi %155, %158 : i64
    %160 = arith.subi %148, %159 : i64
    %161 = llvm.mlir.addressof @MOD : !llvm.ptr
    %162 = llvm.load %161 : !llvm.ptr -> i64
    %163 = arith.addi %160, %162 : i64
    %164 = llvm.mlir.addressof @MOD : !llvm.ptr
    %165 = llvm.load %164 : !llvm.ptr -> i64
    %166 = arith.remsi %163, %165 : i64
    llvm.store %166, %120 : i64, !llvm.ptr
    %167 = llvm.load %120 : !llvm.ptr -> i64
    %168 = arith.extsi %167 : i64 to i128
    %169 = arith.extsi %112 : i64 to i128
    %171 = arith.trunci %168 : i128 to i64
    %172 = arith.trunci %169 : i128 to i64
    %170 = arith.muli %171, %172 : i64
    %173 = llvm.mlir.addressof @MOD : !llvm.ptr
    %174 = llvm.load %173 : !llvm.ptr -> i64
    %175 = arith.extsi %174 : i64 to i128
    %177 = arith.trunci %175 : i128 to i64
    %176 = arith.remsi %170, %177 : i64
    func.return %176 : i64
  }
  func.func @main() -> i32 {
    %178 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %180 = arith.constant 18 : i32
    %181 = arith.extsi %180 : i32 to i64
    %179 = func.call @S(%181) : (i64) -> i64
    %182 = llvm.call @printf(%178, %179) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %183 = arith.constant 0 : i32
    func.return %183 : i32
  }
}