← All problems
Problem 605
Pairwise coin-tossing: last 8 digits of M_n(k) for n=10^8+7, k=10^4+7.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n)
Space complexity O(1)O(n)
Approach Flow solution Big-integer arithmetic
Verdict Optimal
Flow source
# Project Euler 605
# Pairwise coin-tossing: last 8 digits of M_n(k) for n=10^8+7, k=10^4+7.
const MOD: i64 = 100000000
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 & 1) == 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 main() -> i32 {
let n: i64 = 100000007
let k: i64 = 10007
let two_n: i64 = modpow(2, n, MOD)
let A_mod: i64 = (two_n - 1) % MOD
let N0_mod: i64 = ((k - 1) * A_mod + (n % MOD)) % MOD
let part1: i64 = modpow(2, n - k, MOD)
let part2: i64 = (A_mod * A_mod) % MOD
let ans: i64 = (part1 * N0_mod % MOD) * part2 % MOD
printf("%08lld\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);
int32_t main(void);
static const int64_t MOD = 100000000;
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 ((e & 1) == 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;
}
int32_t main(void) {
int64_t n = 100000007;
int64_t k = 10007;
int64_t two_n = modpow_i64_i64_i64(2, n, MOD);
int64_t A_mod = FLOW_CHECKED_MOD(((two_n - 1)), (MOD));
int64_t N0_mod = FLOW_CHECKED_MOD(((((k - 1) * A_mod) + FLOW_CHECKED_MOD((n), (MOD)))), (MOD));
int64_t part1 = modpow_i64_i64_i64(2, (n - k), MOD);
int64_t part2 = FLOW_CHECKED_MOD(((A_mod * A_mod)), (MOD));
int64_t ans = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((part1 * N0_mod)), (MOD)) * part2)), (MOD));
printf("%08lld\n", ans);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%08lld\n\00") {addr_space = 0 : i32} : !llvm.array<8 x i8>
// Constant: MOD
llvm.mlir.global internal constant @MOD(100000000 : 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.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 1 : i32
%22 = arith.extsi %20 : i32 to i64
%21 = arith.andi %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 @main() -> i32 {
%55 = arith.constant 100000007 : i32
%56 = arith.extsi %55 : i32 to i64
%57 = arith.constant 10007 : i32
%58 = arith.extsi %57 : i32 to i64
%60 = arith.constant 2 : i32
%61 = llvm.mlir.addressof @MOD : !llvm.ptr
%62 = llvm.load %61 : !llvm.ptr -> i64
%63 = arith.extsi %60 : i32 to i64
%59 = func.call @modpow(%63, %56, %62) : (i64, i64, i64) -> i64
%64 = arith.constant 1 : i32
%66 = arith.extsi %64 : i32 to i64
%65 = arith.subi %59, %66 : i64
%67 = llvm.mlir.addressof @MOD : !llvm.ptr
%68 = llvm.load %67 : !llvm.ptr -> i64
%69 = arith.remsi %65, %68 : i64
%70 = arith.constant 1 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.subi %58, %72 : i64
%73 = arith.muli %71, %69 : i64
%74 = llvm.mlir.addressof @MOD : !llvm.ptr
%75 = llvm.load %74 : !llvm.ptr -> i64
%76 = arith.remsi %56, %75 : i64
%77 = arith.addi %73, %76 : i64
%78 = llvm.mlir.addressof @MOD : !llvm.ptr
%79 = llvm.load %78 : !llvm.ptr -> i64
%80 = arith.remsi %77, %79 : i64
%82 = arith.constant 2 : i32
%83 = arith.subi %56, %58 : i64
%84 = llvm.mlir.addressof @MOD : !llvm.ptr
%85 = llvm.load %84 : !llvm.ptr -> i64
%86 = arith.extsi %82 : i32 to i64
%81 = func.call @modpow(%86, %83, %85) : (i64, i64, i64) -> i64
%87 = arith.muli %69, %69 : i64
%88 = llvm.mlir.addressof @MOD : !llvm.ptr
%89 = llvm.load %88 : !llvm.ptr -> i64
%90 = arith.remsi %87, %89 : i64
%91 = arith.muli %81, %80 : i64
%92 = llvm.mlir.addressof @MOD : !llvm.ptr
%93 = llvm.load %92 : !llvm.ptr -> i64
%94 = arith.remsi %91, %93 : i64
%95 = arith.muli %94, %90 : i64
%96 = llvm.mlir.addressof @MOD : !llvm.ptr
%97 = llvm.load %96 : !llvm.ptr -> i64
%98 = arith.remsi %95, %97 : i64
%99 = llvm.mlir.addressof @str_0 : !llvm.ptr
%100 = llvm.call @printf(%99, %98) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%101 = arith.constant 0 : i32
func.return %101 : i32
}
}