Problem 194

Last 8 digits of N(25,75,1984).

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

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n)
Space complexityO(1)O(n)
ApproachFlow solutionBig-integer arithmetic
VerdictOptimal

Flow source

# Project Euler 194
# Last 8 digits of N(25,75,1984).

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 SA(c: i64) -> i64 {
    return c*c*c*c*c - 9*c*c*c*c + 34*c*c*c - 69*c*c + 77*c - 38
}

function SB(c: i64) -> i64 {
    return c*c*c*c*c - 8*c*c*c*c + 27*c*c*c - 50*c*c + 52*c - 24
}

function comb_mod(n: i64, k: i64, mod: i64) -> i64 {
    # n! / (k! (n-k)!) mod mod; mod=1e8 not prime, so compute exactly with i128 then mod
    let mut num: i128 = 1
    let mut i: i64 = 0
    while i < k {
        num = num * ((n - i) as i128)
        i = i + 1
        num = num / (i as i128)
    }
    return (num % (mod as i128)) as i64
}

function main() -> i32 {
    let MOD: i64 = 100000000
    let a: i64 = 25
    let b: i64 = 75
    let c: i64 = 1984
    let n: i64 = a + b
    let comb: i64 = comb_mod(n, a, MOD)
    let sa: i64 = ((SA(c) % MOD) + MOD) % MOD
    let sb: i64 = ((SB(c) % MOD) + MOD) % MOD
    let mut val: i64 = comb
    val = (val * c) % MOD
    val = (val * (c - 1)) % MOD
    val = (val * modpow(sa, a, MOD)) % MOD
    val = (val * modpow(sb, b, MOD)) % MOD
    printf("%lld\n", val)
    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);
int64_t SA_i64(int64_t c);
int64_t SB_i64(int64_t c);
int64_t comb_mod_i64_i64_i64(int64_t n, int64_t k, 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;
}

int64_t SA_i64(int64_t c) {
    return (((((((((c * c) * c) * c) * c) - ((((9 * c) * c) * c) * c)) + (((34 * c) * c) * c)) - ((69 * c) * c)) + (77 * c)) - 38);
}

int64_t SB_i64(int64_t c) {
    return (((((((((c * c) * c) * c) * c) - ((((8 * c) * c) * c) * c)) + (((27 * c) * c) * c)) - ((50 * c) * c)) + (52 * c)) - 24);
}

int64_t comb_mod_i64_i64_i64(int64_t n, int64_t k, int64_t mod) {
    __int128 num = 1;
    int64_t i = 0;
    while (i < k) {
        num = (num * ((__int128)((n - i))));
        i = (i + 1);
        num = FLOW_CHECKED_DIV((num), (((__int128)(i))));
    }
    return ((int64_t)(FLOW_CHECKED_MOD((num), (((__int128)(mod))))));
}

int32_t main(void) {
    int64_t MOD = 100000000;
    int64_t a = 25;
    int64_t b = 75;
    int64_t c = 1984;
    int64_t n = (a + b);
    int64_t comb = comb_mod_i64_i64_i64(n, a, MOD);
    int64_t sa = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((SA_i64(c)), (MOD)) + MOD)), (MOD));
    int64_t sb = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((SB_i64(c)), (MOD)) + MOD)), (MOD));
    int64_t val = comb;
    val = FLOW_CHECKED_MOD(((val * c)), (MOD));
    val = FLOW_CHECKED_MOD(((val * (c - 1))), (MOD));
    val = FLOW_CHECKED_MOD(((val * modpow_i64_i64_i64(sa, a, MOD))), (MOD));
    val = FLOW_CHECKED_MOD(((val * modpow_i64_i64_i64(sb, b, MOD))), (MOD));
    printf("%lld\n", val);
    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 @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 @SA(%arg0: i64) -> i64 {
    %33 = arith.muli %arg0, %arg0 : i64
    %34 = arith.muli %33, %arg0 : i64
    %35 = arith.muli %34, %arg0 : i64
    %36 = arith.muli %35, %arg0 : i64
    %37 = arith.constant 9 : i32
    %39 = arith.extsi %37 : i32 to i64
    %38 = arith.muli %39, %arg0 : i64
    %40 = arith.muli %38, %arg0 : i64
    %41 = arith.muli %40, %arg0 : i64
    %42 = arith.muli %41, %arg0 : i64
    %43 = arith.subi %36, %42 : i64
    %44 = arith.constant 34 : i32
    %46 = arith.extsi %44 : i32 to i64
    %45 = arith.muli %46, %arg0 : i64
    %47 = arith.muli %45, %arg0 : i64
    %48 = arith.muli %47, %arg0 : i64
    %49 = arith.addi %43, %48 : i64
    %50 = arith.constant 69 : i32
    %52 = arith.extsi %50 : i32 to i64
    %51 = arith.muli %52, %arg0 : i64
    %53 = arith.muli %51, %arg0 : i64
    %54 = arith.subi %49, %53 : i64
    %55 = arith.constant 77 : i32
    %57 = arith.extsi %55 : i32 to i64
    %56 = arith.muli %57, %arg0 : i64
    %58 = arith.addi %54, %56 : i64
    %59 = arith.constant 38 : i32
    %61 = arith.extsi %59 : i32 to i64
    %60 = arith.subi %58, %61 : i64
    func.return %60 : i64
  }
  func.func @SB(%arg0: i64) -> i64 {
    %62 = arith.muli %arg0, %arg0 : i64
    %63 = arith.muli %62, %arg0 : i64
    %64 = arith.muli %63, %arg0 : i64
    %65 = arith.muli %64, %arg0 : i64
    %66 = arith.constant 8 : i32
    %68 = arith.extsi %66 : i32 to i64
    %67 = arith.muli %68, %arg0 : i64
    %69 = arith.muli %67, %arg0 : i64
    %70 = arith.muli %69, %arg0 : i64
    %71 = arith.muli %70, %arg0 : i64
    %72 = arith.subi %65, %71 : i64
    %73 = arith.constant 27 : i32
    %75 = arith.extsi %73 : i32 to i64
    %74 = arith.muli %75, %arg0 : i64
    %76 = arith.muli %74, %arg0 : i64
    %77 = arith.muli %76, %arg0 : i64
    %78 = arith.addi %72, %77 : i64
    %79 = arith.constant 50 : i32
    %81 = arith.extsi %79 : i32 to i64
    %80 = arith.muli %81, %arg0 : i64
    %82 = arith.muli %80, %arg0 : i64
    %83 = arith.subi %78, %82 : i64
    %84 = arith.constant 52 : i32
    %86 = arith.extsi %84 : i32 to i64
    %85 = arith.muli %86, %arg0 : i64
    %87 = arith.addi %83, %85 : i64
    %88 = arith.constant 24 : i32
    %90 = arith.extsi %88 : i32 to i64
    %89 = arith.subi %87, %90 : i64
    func.return %89 : i64
  }
  func.func @comb_mod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %91 = arith.constant 1 : i32
    %92 = arith.extsi %91 : i32 to i128
    %93 = llvm.mlir.constant(1 : i64) : i64
    %94 = llvm.alloca %93 x i128 : (i64) -> !llvm.ptr
    llvm.store %92, %94 : i128, !llvm.ptr
    %95 = arith.constant 0 : i32
    %96 = arith.extsi %95 : i32 to i64
    %97 = llvm.mlir.constant(1 : i64) : i64
    %98 = llvm.alloca %97 x i64 : (i64) -> !llvm.ptr
    llvm.store %96, %98 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %99 = llvm.load %98 : !llvm.ptr -> i64
    %100 = arith.cmpi slt, %99, %arg1 : i64
    cf.cond_br %100, ^bb7, ^bb8
    ^bb7:
      %101 = llvm.load %94 : !llvm.ptr -> i128
      %102 = llvm.load %98 : !llvm.ptr -> i64
      %103 = arith.subi %arg0, %102 : i64
      %104 = arith.extsi %103 : i64 to i128
      %106 = arith.trunci %101 : i128 to i64
      %107 = arith.trunci %104 : i128 to i64
      %105 = arith.muli %106, %107 : i64
      %108 = arith.extsi %105 : i64 to i128
      llvm.store %108, %94 : i128, !llvm.ptr
      %109 = llvm.load %98 : !llvm.ptr -> i64
      %110 = arith.constant 1 : i32
      %112 = arith.extsi %110 : i32 to i64
      %111 = arith.addi %109, %112 : i64
      llvm.store %111, %98 : i64, !llvm.ptr
      %113 = llvm.load %94 : !llvm.ptr -> i128
      %114 = llvm.load %98 : !llvm.ptr -> i64
      %115 = arith.extsi %114 : i64 to i128
      %117 = arith.trunci %113 : i128 to i64
      %118 = arith.trunci %115 : i128 to i64
      %116 = arith.divsi %117, %118 : i64
      %119 = arith.extsi %116 : i64 to i128
      llvm.store %119, %94 : i128, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %120 = llvm.load %94 : !llvm.ptr -> i128
    %121 = arith.extsi %arg2 : i64 to i128
    %123 = arith.trunci %120 : i128 to i64
    %124 = arith.trunci %121 : i128 to i64
    %122 = arith.remsi %123, %124 : i64
    func.return %122 : i64
  }
  func.func @main() -> i32 {
    %125 = arith.constant 100000000 : i32
    %126 = arith.extsi %125 : i32 to i64
    %127 = arith.constant 25 : i32
    %128 = arith.extsi %127 : i32 to i64
    %129 = arith.constant 75 : i32
    %130 = arith.extsi %129 : i32 to i64
    %131 = arith.constant 1984 : i32
    %132 = arith.extsi %131 : i32 to i64
    %133 = arith.addi %128, %130 : i64
    %134 = func.call @comb_mod(%133, %128, %126) : (i64, i64, i64) -> i64
    %135 = func.call @SA(%132) : (i64) -> i64
    %136 = arith.remsi %135, %126 : i64
    %137 = arith.addi %136, %126 : i64
    %138 = arith.remsi %137, %126 : i64
    %139 = func.call @SB(%132) : (i64) -> i64
    %140 = arith.remsi %139, %126 : i64
    %141 = arith.addi %140, %126 : i64
    %142 = arith.remsi %141, %126 : i64
    %143 = llvm.mlir.constant(1 : i64) : i64
    %144 = llvm.alloca %143 x i64 : (i64) -> !llvm.ptr
    llvm.store %134, %144 : i64, !llvm.ptr
    %145 = llvm.load %144 : !llvm.ptr -> i64
    %146 = arith.muli %145, %132 : i64
    %147 = arith.remsi %146, %126 : i64
    llvm.store %147, %144 : i64, !llvm.ptr
    %148 = llvm.load %144 : !llvm.ptr -> i64
    %149 = arith.constant 1 : i32
    %151 = arith.extsi %149 : i32 to i64
    %150 = arith.subi %132, %151 : i64
    %152 = arith.muli %148, %150 : i64
    %153 = arith.remsi %152, %126 : i64
    llvm.store %153, %144 : i64, !llvm.ptr
    %154 = llvm.load %144 : !llvm.ptr -> i64
    %155 = func.call @modpow(%138, %128, %126) : (i64, i64, i64) -> i64
    %156 = arith.muli %154, %155 : i64
    %157 = arith.remsi %156, %126 : i64
    llvm.store %157, %144 : i64, !llvm.ptr
    %158 = llvm.load %144 : !llvm.ptr -> i64
    %159 = func.call @modpow(%142, %130, %126) : (i64, i64, i64) -> i64
    %160 = arith.muli %158, %159 : i64
    %161 = arith.remsi %160, %126 : i64
    llvm.store %161, %144 : i64, !llvm.ptr
    %162 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %163 = llvm.load %144 : !llvm.ptr -> i64
    %164 = llvm.call @printf(%162, %163) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %165 = arith.constant 0 : i32
    func.return %165 : i32
  }
}