← All problems
Problem 340
Crazy Function S(21^7, 7^21, 12^7) mod 10^9.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n log n)
Space complexity O(1)O(n)
Approach Flow solution Modular DP or matrix exponentiation
Verdict Optimal
Flow source
# Project Euler 340
# Crazy Function S(21^7, 7^21, 12^7) mod 10^9.
function ipow(base: i64, exp: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base
let mut e: i64 = exp
while e > 0 {
if e % 2 == 1 { r = r * b }
b = b * b
e = e / 2
}
return r
}
function mulmod(a0: i64, b0: i64, mod: i64) -> i64 {
let mut a: i64 = ((a0 % mod) + mod) % mod
let mut b: i64 = ((b0 % mod) + mod) % mod
let mut r: i64 = 0
while b > 0 {
if b % 2 == 1 {
r = r + a
if r >= mod { r = r - mod }
}
a = a * 2
if a >= mod { a = a - mod }
b = b / 2
}
return r
}
function half_prod(x: i64, y: i64, mod: i64) -> i64 {
# (x*y/2) % mod with x*y even
if x % 2 == 0 {
return mulmod(x / 2, y, mod)
}
return mulmod(x, y / 2, mod)
}
function main() -> i32 {
let mod: i64 = 1000000000
let a0: i64 = ipow(21, 7)
let b0: i64 = ipow(7, 21)
let c0: i64 = ipow(12, 7)
let k: i64 = b0 / a0
let a: i64 = a0 % mod
let b: i64 = b0 % mod
let c: i64 = c0 % mod
# sum n from 0..b = b*(b+1)/2
let mut total: i64 = half_prod(b, b + 1, mod)
# sum_{k=0..K} a*(4(k+1)a - (3k+4)c) with K=b/a
# = ((K-1)K/2)*(4a^2 - 3ac) + 4a*K*(a-c)
let term_k: i64 = half_prod(k - 1, k, mod)
let coef: i64 = (mulmod(4, mulmod(a, a, mod), mod) - mulmod(3, mulmod(a, c, mod), mod) + mod) % mod
total = (total + mulmod(term_k, coef, mod)) % mod
total = (total + mulmod(mulmod(4, a, mod), mulmod(k, (a - c + mod) % mod, mod), mod)) % mod
# remaining partial bucket for k=K: length (b - K*a + 1)
let rem_len: i64 = (b0 - k * a0 + 1) % mod
let fconst: i64 = (mulmod(4, mulmod(k + 1, a, mod), mod) - mulmod(3 * k + 4, c, mod) + mod * 4) % mod
total = (total + mulmod(rem_len, fconst, mod)) % mod
printf("%lld\n", total)
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 ipow_i64_i64(int64_t base, int64_t exp);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t half_prod_i64_i64_i64(int64_t x, int64_t y, int64_t mod);
int32_t main(void);
int64_t ipow_i64_i64(int64_t base, int64_t exp) {
int64_t r = 1;
int64_t b = base;
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = (r * b);
}
b = (b * b);
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((a0), (mod)) + mod)), (mod));
int64_t b = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((b0), (mod)) + mod)), (mod));
int64_t r = 0;
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
r = (r + a);
if (r >= mod) {
r = (r - mod);
}
}
a = (a * 2);
if (a >= mod) {
a = (a - mod);
}
b = FLOW_CHECKED_DIV((b), (2));
}
return r;
}
int64_t half_prod_i64_i64_i64(int64_t x, int64_t y, int64_t mod) {
if (FLOW_CHECKED_MOD((x), (2)) == 0) {
return mulmod_i64_i64_i64(FLOW_CHECKED_DIV((x), (2)), y, mod);
}
return mulmod_i64_i64_i64(x, FLOW_CHECKED_DIV((y), (2)), mod);
}
int32_t main(void) {
int64_t mod = 1000000000;
int64_t a0 = ipow_i64_i64(21, 7);
int64_t b0 = ipow_i64_i64(7, 21);
int64_t c0 = ipow_i64_i64(12, 7);
int64_t k = FLOW_CHECKED_DIV((b0), (a0));
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
int64_t c = FLOW_CHECKED_MOD((c0), (mod));
int64_t total = half_prod_i64_i64_i64(b, (b + 1), mod);
int64_t term_k = half_prod_i64_i64_i64((k - 1), k, mod);
int64_t coef = FLOW_CHECKED_MOD((((mulmod_i64_i64_i64(4, mulmod_i64_i64_i64(a, a, mod), mod) - mulmod_i64_i64_i64(3, mulmod_i64_i64_i64(a, c, mod), mod)) + mod)), (mod));
total = FLOW_CHECKED_MOD(((total + mulmod_i64_i64_i64(term_k, coef, mod))), (mod));
total = FLOW_CHECKED_MOD(((total + mulmod_i64_i64_i64(mulmod_i64_i64_i64(4, a, mod), mulmod_i64_i64_i64(k, FLOW_CHECKED_MOD((((a - c) + mod)), (mod)), mod), mod))), (mod));
int64_t rem_len = FLOW_CHECKED_MOD((((b0 - (k * a0)) + 1)), (mod));
int64_t fconst = FLOW_CHECKED_MOD((((mulmod_i64_i64_i64(4, mulmod_i64_i64_i64((k + 1), a, mod), mod) - mulmod_i64_i64_i64(((3 * k) + 4), c, mod)) + (mod * 4))), (mod));
total = FLOW_CHECKED_MOD(((total + mulmod_i64_i64_i64(rem_len, fconst, mod))), (mod));
printf("%lld\n", total);
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 @ipow(%arg0: i64, %arg1: 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 = llvm.mlir.constant(1 : i64) : i64
%5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %5 : i64, !llvm.ptr
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %7 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%8 = llvm.load %7 : !llvm.ptr -> i64
%9 = arith.constant 0 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.cmpi sgt, %8, %11 : i64
cf.cond_br %10, ^bb1, ^bb2
^bb1:
%12 = llvm.load %7 : !llvm.ptr -> i64
%13 = arith.constant 2 : i32
%15 = arith.extsi %13 : i32 to i64
%14 = arith.remsi %12, %15 : i64
%16 = arith.constant 1 : i32
%18 = arith.extsi %16 : i32 to i64
%17 = arith.cmpi eq, %14, %18 : i64
cf.cond_br %17, ^bb3, ^bb4
^bb3:
%19 = llvm.load %3 : !llvm.ptr -> i64
%20 = llvm.load %5 : !llvm.ptr -> i64
%21 = arith.muli %19, %20 : i64
llvm.store %21, %3 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%22 = llvm.load %5 : !llvm.ptr -> i64
%23 = llvm.load %5 : !llvm.ptr -> i64
%24 = arith.muli %22, %23 : i64
llvm.store %24, %5 : i64, !llvm.ptr
%25 = llvm.load %7 : !llvm.ptr -> i64
%26 = arith.constant 2 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.divsi %25, %28 : i64
llvm.store %27, %7 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%29 = llvm.load %3 : !llvm.ptr -> i64
func.return %29 : i64
}
func.func @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%30 = arith.remsi %arg0, %arg2 : i64
%31 = arith.addi %30, %arg2 : i64
%32 = arith.remsi %31, %arg2 : i64
%33 = llvm.mlir.constant(1 : i64) : i64
%34 = llvm.alloca %33 x i64 : (i64) -> !llvm.ptr
llvm.store %32, %34 : i64, !llvm.ptr
%35 = arith.remsi %arg1, %arg2 : i64
%36 = arith.addi %35, %arg2 : i64
%37 = arith.remsi %36, %arg2 : i64
%38 = llvm.mlir.constant(1 : i64) : i64
%39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
llvm.store %37, %39 : i64, !llvm.ptr
%40 = arith.constant 0 : i32
%41 = arith.extsi %40 : i32 to i64
%42 = llvm.mlir.constant(1 : i64) : i64
%43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
llvm.store %41, %43 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%44 = llvm.load %39 : !llvm.ptr -> i64
%45 = arith.constant 0 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.cmpi sgt, %44, %47 : i64
cf.cond_br %46, ^bb7, ^bb8
^bb7:
%48 = llvm.load %39 : !llvm.ptr -> i64
%49 = arith.constant 2 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.remsi %48, %51 : i64
%52 = arith.constant 1 : i32
%54 = arith.extsi %52 : i32 to i64
%53 = arith.cmpi eq, %50, %54 : i64
cf.cond_br %53, ^bb9, ^bb10
^bb9:
%55 = llvm.load %43 : !llvm.ptr -> i64
%56 = llvm.load %34 : !llvm.ptr -> i64
%57 = arith.addi %55, %56 : i64
llvm.store %57, %43 : i64, !llvm.ptr
%58 = llvm.load %43 : !llvm.ptr -> i64
%59 = arith.cmpi sge, %58, %arg2 : i64
cf.cond_br %59, ^bb12, ^bb13
^bb12:
%60 = llvm.load %43 : !llvm.ptr -> i64
%61 = arith.subi %60, %arg2 : i64
llvm.store %61, %43 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%62 = llvm.load %34 : !llvm.ptr -> i64
%63 = arith.constant 2 : i32
%65 = arith.extsi %63 : i32 to i64
%64 = arith.muli %62, %65 : i64
llvm.store %64, %34 : i64, !llvm.ptr
%66 = llvm.load %34 : !llvm.ptr -> i64
%67 = arith.cmpi sge, %66, %arg2 : i64
cf.cond_br %67, ^bb15, ^bb16
^bb15:
%68 = llvm.load %34 : !llvm.ptr -> i64
%69 = arith.subi %68, %arg2 : i64
llvm.store %69, %34 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%70 = llvm.load %39 : !llvm.ptr -> i64
%71 = arith.constant 2 : i32
%73 = arith.extsi %71 : i32 to i64
%72 = arith.divsi %70, %73 : i64
llvm.store %72, %39 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%74 = llvm.load %43 : !llvm.ptr -> i64
func.return %74 : i64
}
func.func @half_prod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%75 = arith.constant 2 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.remsi %arg0, %77 : i64
%78 = arith.constant 0 : i32
%80 = arith.extsi %78 : i32 to i64
%79 = arith.cmpi eq, %76, %80 : i64
cf.cond_br %79, ^bb18, ^bb19
^bb18:
%82 = arith.constant 2 : i32
%84 = arith.extsi %82 : i32 to i64
%83 = arith.divsi %arg0, %84 : i64
%81 = func.call @mulmod(%83, %arg1, %arg2) : (i64, i64, i64) -> i64
func.return %81 : i64
^bb19:
cf.br ^bb20
^bb20:
%86 = arith.constant 2 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.divsi %arg1, %88 : i64
%85 = func.call @mulmod(%arg0, %87, %arg2) : (i64, i64, i64) -> i64
func.return %85 : i64
}
func.func @main() -> i32 {
%89 = arith.constant 1000000000 : i32
%90 = arith.extsi %89 : i32 to i64
%92 = arith.constant 21 : i32
%93 = arith.constant 7 : i32
%94 = arith.extsi %92 : i32 to i64
%95 = arith.extsi %93 : i32 to i64
%91 = func.call @ipow(%94, %95) : (i64, i64) -> i64
%97 = arith.constant 7 : i32
%98 = arith.constant 21 : i32
%99 = arith.extsi %97 : i32 to i64
%100 = arith.extsi %98 : i32 to i64
%96 = func.call @ipow(%99, %100) : (i64, i64) -> i64
%102 = arith.constant 12 : i32
%103 = arith.constant 7 : i32
%104 = arith.extsi %102 : i32 to i64
%105 = arith.extsi %103 : i32 to i64
%101 = func.call @ipow(%104, %105) : (i64, i64) -> i64
%106 = arith.divsi %96, %91 : i64
%107 = arith.remsi %91, %90 : i64
%108 = arith.remsi %96, %90 : i64
%109 = arith.remsi %101, %90 : i64
%111 = arith.constant 1 : i32
%113 = arith.extsi %111 : i32 to i64
%112 = arith.addi %108, %113 : i64
%110 = func.call @half_prod(%108, %112, %90) : (i64, i64, i64) -> i64
%114 = llvm.mlir.constant(1 : i64) : i64
%115 = llvm.alloca %114 x i64 : (i64) -> !llvm.ptr
llvm.store %110, %115 : i64, !llvm.ptr
%117 = arith.constant 1 : i32
%119 = arith.extsi %117 : i32 to i64
%118 = arith.subi %106, %119 : i64
%116 = func.call @half_prod(%118, %106, %90) : (i64, i64, i64) -> i64
%121 = arith.constant 4 : i32
%122 = func.call @mulmod(%107, %107, %90) : (i64, i64, i64) -> i64
%123 = arith.extsi %121 : i32 to i64
%120 = func.call @mulmod(%123, %122, %90) : (i64, i64, i64) -> i64
%125 = arith.constant 3 : i32
%126 = func.call @mulmod(%107, %109, %90) : (i64, i64, i64) -> i64
%127 = arith.extsi %125 : i32 to i64
%124 = func.call @mulmod(%127, %126, %90) : (i64, i64, i64) -> i64
%128 = arith.subi %120, %124 : i64
%129 = arith.addi %128, %90 : i64
%130 = arith.remsi %129, %90 : i64
%131 = llvm.load %115 : !llvm.ptr -> i64
%132 = func.call @mulmod(%116, %130, %90) : (i64, i64, i64) -> i64
%133 = arith.addi %131, %132 : i64
%134 = arith.remsi %133, %90 : i64
llvm.store %134, %115 : i64, !llvm.ptr
%135 = llvm.load %115 : !llvm.ptr -> i64
%138 = arith.constant 4 : i32
%139 = arith.extsi %138 : i32 to i64
%137 = func.call @mulmod(%139, %107, %90) : (i64, i64, i64) -> i64
%141 = arith.subi %107, %109 : i64
%142 = arith.addi %141, %90 : i64
%143 = arith.remsi %142, %90 : i64
%140 = func.call @mulmod(%106, %143, %90) : (i64, i64, i64) -> i64
%136 = func.call @mulmod(%137, %140, %90) : (i64, i64, i64) -> i64
%144 = arith.addi %135, %136 : i64
%145 = arith.remsi %144, %90 : i64
llvm.store %145, %115 : i64, !llvm.ptr
%146 = arith.muli %106, %91 : i64
%147 = arith.subi %96, %146 : i64
%148 = arith.constant 1 : i32
%150 = arith.extsi %148 : i32 to i64
%149 = arith.addi %147, %150 : i64
%151 = arith.remsi %149, %90 : i64
%153 = arith.constant 4 : i32
%155 = arith.constant 1 : i32
%157 = arith.extsi %155 : i32 to i64
%156 = arith.addi %106, %157 : i64
%154 = func.call @mulmod(%156, %107, %90) : (i64, i64, i64) -> i64
%158 = arith.extsi %153 : i32 to i64
%152 = func.call @mulmod(%158, %154, %90) : (i64, i64, i64) -> i64
%160 = arith.constant 3 : i32
%162 = arith.extsi %160 : i32 to i64
%161 = arith.muli %162, %106 : i64
%163 = arith.constant 4 : i32
%165 = arith.extsi %163 : i32 to i64
%164 = arith.addi %161, %165 : i64
%159 = func.call @mulmod(%164, %109, %90) : (i64, i64, i64) -> i64
%166 = arith.subi %152, %159 : i64
%167 = arith.constant 4 : i32
%169 = arith.extsi %167 : i32 to i64
%168 = arith.muli %90, %169 : i64
%170 = arith.addi %166, %168 : i64
%171 = arith.remsi %170, %90 : i64
%172 = llvm.load %115 : !llvm.ptr -> i64
%173 = func.call @mulmod(%151, %171, %90) : (i64, i64, i64) -> i64
%174 = arith.addi %172, %173 : i64
%175 = arith.remsi %174, %90 : i64
llvm.store %175, %115 : i64, !llvm.ptr
%176 = llvm.mlir.addressof @str_0 : !llvm.ptr
%177 = llvm.load %115 : !llvm.ptr -> i64
%178 = llvm.call @printf(%176, %177) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%179 = arith.constant 0 : i32
func.return %179 : i32
}
}