Problem 405

f(10^(10^18)) mod 17^7 via closed form + Euler totient.

Answer237696125
Output237696125
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 log log n)
Space complexityO(1)O(n)
ApproachFlow solutionSieve-based totient computation
VerdictOptimal

Flow source

# Project Euler 405
# f(10^(10^18)) mod 17^7 via closed form + Euler totient.

function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base0 % mod
    if b < 0 { b = b + mod }
    let mut e: i64 = exp0
    while e > 0 {
        if e % 2 == 1 {
            let t: i128 = (r as i128) * (b as i128) % (mod as i128)
            r = t as i64
        }
        let t2: i128 = (b as i128) * (b as i128) % (mod as i128)
        b = t2 as i64
        e = e / 2
    }
    return r
}

function modinv(x: i64, mod: i64, phi: i64) -> i64 {
    return modpow(x, phi - 1, mod)
}

function main() -> i32 {
    # MOD = 17^7
    let MOD: i64 = 410338673
    let PHI: i64 = MOD - MOD / 17
    # e = 10^(10^18) mod PHI
    let e: i64 = modpow(10, 1000000000000000000, PHI)
    let inv15: i64 = modinv(MOD - 15, MOD, PHI)
    let inv3: i64 = modinv(MOD - 3, MOD, PHI)
    let inv5: i64 = modinv(5, MOD, PHI)
    let t1: i64 = (modpow(MOD - 1, e, MOD) * inv15) % MOD
    let t2: i64 = (((modpow(2, e, MOD) * 4) % MOD) * inv3) % MOD
    let t3: i64 = (((2 * inv5) % MOD) * modpow(4, e, MOD)) % MOD
    let ans: i64 = (t1 + t2 + t3 + 1) % MOD
    printf("%lld\n", ans)
    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 modinv_i64_i64_i64(int64_t x, int64_t mod, int64_t phi);
int32_t main(void);

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));
    if (b < 0) {
        b = (b + mod);
    }
    int64_t e = exp0;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            __int128 t = FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))));
            r = ((int64_t)(t));
        }
        __int128 t2 = FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))));
        b = ((int64_t)(t2));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t modinv_i64_i64_i64(int64_t x, int64_t mod, int64_t phi) {
    return modpow_i64_i64_i64(x, (phi - 1), mod);
}

int32_t main(void) {
    int64_t MOD = 410338673;
    int64_t PHI = (MOD - FLOW_CHECKED_DIV((MOD), (17)));
    int64_t e = modpow_i64_i64_i64(10, 1000000000000000000, PHI);
    int64_t inv15 = modinv_i64_i64_i64((MOD - 15), MOD, PHI);
    int64_t inv3 = modinv_i64_i64_i64((MOD - 3), MOD, PHI);
    int64_t inv5 = modinv_i64_i64_i64(5, MOD, PHI);
    int64_t t1 = FLOW_CHECKED_MOD(((modpow_i64_i64_i64((MOD - 1), e, MOD) * inv15)), (MOD));
    int64_t t2 = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((modpow_i64_i64_i64(2, e, MOD) * 4)), (MOD)) * inv3)), (MOD));
    int64_t t3 = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((2 * inv5)), (MOD)) * modpow_i64_i64_i64(4, e, MOD))), (MOD));
    int64_t ans = FLOW_CHECKED_MOD(((((t1 + t2) + t3) + 1)), (MOD));
    printf("%lld\n", ans);
    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.load %6 : !llvm.ptr -> i64
    %8 = arith.constant 0 : i32
    %10 = arith.extsi %8 : i32 to i64
    %9 = arith.cmpi slt, %7, %10 : i64
    cf.cond_br %9, ^bb0, ^bb1
    ^bb0:
      %11 = llvm.load %6 : !llvm.ptr -> i64
      %12 = arith.addi %11, %arg2 : i64
      llvm.store %12, %6 : i64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %14 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %14 : !llvm.ptr -> i64
    %16 = arith.constant 0 : i32
    %18 = arith.extsi %16 : i32 to i64
    %17 = arith.cmpi sgt, %15, %18 : i64
    cf.cond_br %17, ^bb4, ^bb5
    ^bb4:
      %19 = llvm.load %14 : !llvm.ptr -> i64
      %20 = arith.constant 2 : i32
      %22 = arith.extsi %20 : i32 to i64
      %21 = arith.remsi %19, %22 : i64
      %23 = arith.constant 1 : i32
      %25 = arith.extsi %23 : i32 to i64
      %24 = arith.cmpi eq, %21, %25 : i64
      cf.cond_br %24, ^bb6, ^bb7
      ^bb6:
        %26 = llvm.load %3 : !llvm.ptr -> i64
        %27 = arith.extsi %26 : i64 to i128
        %28 = llvm.load %6 : !llvm.ptr -> i64
        %29 = arith.extsi %28 : i64 to i128
        %31 = arith.trunci %27 : i128 to i64
        %32 = arith.trunci %29 : i128 to i64
        %30 = arith.muli %31, %32 : i64
        %33 = arith.extsi %arg2 : i64 to i128
        %35 = arith.trunci %33 : i128 to i64
        %34 = arith.remsi %30, %35 : i64
        %36 = arith.extsi %34 : i64 to i128
        %37 = arith.trunci %36 : i128 to i64
        llvm.store %37, %3 : i64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %38 = llvm.load %6 : !llvm.ptr -> i64
      %39 = arith.extsi %38 : i64 to i128
      %40 = llvm.load %6 : !llvm.ptr -> i64
      %41 = arith.extsi %40 : i64 to i128
      %43 = arith.trunci %39 : i128 to i64
      %44 = arith.trunci %41 : i128 to i64
      %42 = arith.muli %43, %44 : i64
      %45 = arith.extsi %arg2 : i64 to i128
      %47 = arith.trunci %45 : i128 to i64
      %46 = arith.remsi %42, %47 : i64
      %48 = arith.extsi %46 : i64 to i128
      %49 = arith.trunci %48 : i128 to i64
      llvm.store %49, %6 : i64, !llvm.ptr
      %50 = llvm.load %14 : !llvm.ptr -> i64
      %51 = arith.constant 2 : i32
      %53 = arith.extsi %51 : i32 to i64
      %52 = arith.divsi %50, %53 : i64
      llvm.store %52, %14 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %54 = llvm.load %3 : !llvm.ptr -> i64
    func.return %54 : i64
  }
  func.func @modinv(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %56 = arith.constant 1 : i32
    %58 = arith.extsi %56 : i32 to i64
    %57 = arith.subi %arg2, %58 : i64
    %55 = func.call @modpow(%arg0, %57, %arg1) : (i64, i64, i64) -> i64
    func.return %55 : i64
  }
  func.func @main() -> i32 {
    %59 = arith.constant 410338673 : i32
    %60 = arith.extsi %59 : i32 to i64
    %61 = arith.constant 17 : i32
    %63 = arith.extsi %61 : i32 to i64
    %62 = arith.divsi %60, %63 : i64
    %64 = arith.subi %60, %62 : i64
    %66 = arith.constant 10 : i32
    %67 = arith.constant 999999995705032704 : i32
    %68 = arith.extsi %66 : i32 to i64
    %69 = arith.extsi %67 : i32 to i64
    %65 = func.call @modpow(%68, %69, %64) : (i64, i64, i64) -> i64
    %71 = arith.constant 15 : i32
    %73 = arith.extsi %71 : i32 to i64
    %72 = arith.subi %60, %73 : i64
    %70 = func.call @modinv(%72, %60, %64) : (i64, i64, i64) -> i64
    %75 = arith.constant 3 : i32
    %77 = arith.extsi %75 : i32 to i64
    %76 = arith.subi %60, %77 : i64
    %74 = func.call @modinv(%76, %60, %64) : (i64, i64, i64) -> i64
    %79 = arith.constant 5 : i32
    %80 = arith.extsi %79 : i32 to i64
    %78 = func.call @modinv(%80, %60, %64) : (i64, i64, i64) -> i64
    %82 = arith.constant 1 : i32
    %84 = arith.extsi %82 : i32 to i64
    %83 = arith.subi %60, %84 : i64
    %81 = func.call @modpow(%83, %65, %60) : (i64, i64, i64) -> i64
    %85 = arith.muli %81, %70 : i64
    %86 = arith.remsi %85, %60 : i64
    %88 = arith.constant 2 : i32
    %89 = arith.extsi %88 : i32 to i64
    %87 = func.call @modpow(%89, %65, %60) : (i64, i64, i64) -> i64
    %90 = arith.constant 4 : i32
    %92 = arith.extsi %90 : i32 to i64
    %91 = arith.muli %87, %92 : i64
    %93 = arith.remsi %91, %60 : i64
    %94 = arith.muli %93, %74 : i64
    %95 = arith.remsi %94, %60 : i64
    %96 = arith.constant 2 : i32
    %98 = arith.extsi %96 : i32 to i64
    %97 = arith.muli %98, %78 : i64
    %99 = arith.remsi %97, %60 : i64
    %101 = arith.constant 4 : i32
    %102 = arith.extsi %101 : i32 to i64
    %100 = func.call @modpow(%102, %65, %60) : (i64, i64, i64) -> i64
    %103 = arith.muli %99, %100 : i64
    %104 = arith.remsi %103, %60 : i64
    %105 = arith.addi %86, %95 : i64
    %106 = arith.addi %105, %104 : i64
    %107 = arith.constant 1 : i32
    %109 = arith.extsi %107 : i32 to i64
    %108 = arith.addi %106, %109 : i64
    %110 = arith.remsi %108, %60 : i64
    %111 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %112 = llvm.call @printf(%111, %110) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %113 = arith.constant 0 : i32
    func.return %113 : i32
  }
}