← All problems
Problem 479
Roots on the Rise — S(10^6) mod 10^9+7.
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 479
# Roots on the Rise — S(10^6) mod 10^9+7.
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(a0: i64, mod: i64) -> i64 {
return modpow(a0, mod - 2, mod)
}
function main() -> i32 {
let n: i64 = 1000000
let mod: i64 = 1000000007
let mut total: i64 = 0
let mut k: i64 = 2
while k <= n {
let t: i64 = 1 - k * k
let mut tmod: i64 = t % mod
if tmod < 0 { tmod = tmod + mod }
let mut denom: i64 = (1 - tmod) % mod
if denom < 0 { denom = denom + mod }
let term: i64 = (1 - modpow(tmod, n, mod) + mod) % mod
let num: i64 = (term * tmod) % mod
total = (total + num * modinv(denom, mod)) % mod
k = k + 1
}
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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t modinv_i64_i64(int64_t a0, int64_t mod);
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(int64_t a0, int64_t mod) {
return modpow_i64_i64_i64(a0, (mod - 2), mod);
}
int32_t main(void) {
int64_t n = 1000000;
int64_t mod = 1000000007;
int64_t total = 0;
int64_t k = 2;
while (k <= n) {
int64_t t = (1 - (k * k));
int64_t tmod = FLOW_CHECKED_MOD((t), (mod));
if (tmod < 0) {
tmod = (tmod + mod);
}
int64_t denom = FLOW_CHECKED_MOD(((1 - tmod)), (mod));
if (denom < 0) {
denom = (denom + mod);
}
int64_t term = FLOW_CHECKED_MOD((((1 - modpow_i64_i64_i64(tmod, n, mod)) + mod)), (mod));
int64_t num = FLOW_CHECKED_MOD(((term * tmod)), (mod));
total = FLOW_CHECKED_MOD(((total + (num * modinv_i64_i64(denom, mod)))), (mod));
k = (k + 1);
}
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 @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) -> i64 {
%56 = arith.constant 2 : i32
%58 = arith.extsi %56 : i32 to i64
%57 = arith.subi %arg1, %58 : i64
%55 = func.call @modpow(%arg0, %57, %arg1) : (i64, i64, i64) -> i64
func.return %55 : i64
}
func.func @main() -> i32 {
%59 = arith.constant 1000000 : i32
%60 = arith.extsi %59 : i32 to i64
%61 = arith.constant 1000000007 : i32
%62 = arith.extsi %61 : i32 to i64
%63 = arith.constant 0 : i32
%64 = arith.extsi %63 : i32 to i64
%65 = llvm.mlir.constant(1 : i64) : i64
%66 = llvm.alloca %65 x i64 : (i64) -> !llvm.ptr
llvm.store %64, %66 : i64, !llvm.ptr
%67 = arith.constant 2 : i32
%68 = arith.extsi %67 : i32 to i64
%69 = llvm.mlir.constant(1 : i64) : i64
%70 = llvm.alloca %69 x i64 : (i64) -> !llvm.ptr
llvm.store %68, %70 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%71 = llvm.load %70 : !llvm.ptr -> i64
%72 = arith.cmpi sle, %71, %60 : i64
cf.cond_br %72, ^bb10, ^bb11
^bb10:
%73 = arith.constant 1 : i32
%74 = llvm.load %70 : !llvm.ptr -> i64
%75 = llvm.load %70 : !llvm.ptr -> i64
%76 = arith.muli %74, %75 : i64
%78 = arith.extsi %73 : i32 to i64
%77 = arith.subi %78, %76 : i64
%79 = arith.remsi %77, %62 : i64
%80 = llvm.mlir.constant(1 : i64) : i64
%81 = llvm.alloca %80 x i64 : (i64) -> !llvm.ptr
llvm.store %79, %81 : i64, !llvm.ptr
%82 = llvm.load %81 : !llvm.ptr -> i64
%83 = arith.constant 0 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.cmpi slt, %82, %85 : i64
cf.cond_br %84, ^bb12, ^bb13
^bb12:
%86 = llvm.load %81 : !llvm.ptr -> i64
%87 = arith.addi %86, %62 : i64
llvm.store %87, %81 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%88 = arith.constant 1 : i32
%89 = llvm.load %81 : !llvm.ptr -> i64
%91 = arith.extsi %88 : i32 to i64
%90 = arith.subi %91, %89 : i64
%92 = arith.remsi %90, %62 : i64
%93 = llvm.mlir.constant(1 : i64) : i64
%94 = llvm.alloca %93 x i64 : (i64) -> !llvm.ptr
llvm.store %92, %94 : i64, !llvm.ptr
%95 = llvm.load %94 : !llvm.ptr -> i64
%96 = arith.constant 0 : i32
%98 = arith.extsi %96 : i32 to i64
%97 = arith.cmpi slt, %95, %98 : i64
cf.cond_br %97, ^bb15, ^bb16
^bb15:
%99 = llvm.load %94 : !llvm.ptr -> i64
%100 = arith.addi %99, %62 : i64
llvm.store %100, %94 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%101 = arith.constant 1 : i32
%103 = llvm.load %81 : !llvm.ptr -> i64
%102 = func.call @modpow(%103, %60, %62) : (i64, i64, i64) -> i64
%105 = arith.extsi %101 : i32 to i64
%104 = arith.subi %105, %102 : i64
%106 = arith.addi %104, %62 : i64
%107 = arith.remsi %106, %62 : i64
%108 = llvm.load %81 : !llvm.ptr -> i64
%109 = arith.muli %107, %108 : i64
%110 = arith.remsi %109, %62 : i64
%111 = llvm.load %66 : !llvm.ptr -> i64
%113 = llvm.load %94 : !llvm.ptr -> i64
%112 = func.call @modinv(%113, %62) : (i64, i64) -> i64
%114 = arith.muli %110, %112 : i64
%115 = arith.addi %111, %114 : i64
%116 = arith.remsi %115, %62 : i64
llvm.store %116, %66 : i64, !llvm.ptr
%117 = llvm.load %70 : !llvm.ptr -> i64
%118 = arith.constant 1 : i32
%120 = arith.extsi %118 : i32 to i64
%119 = arith.addi %117, %120 : i64
llvm.store %119, %70 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%121 = llvm.mlir.addressof @str_0 : !llvm.ptr
%122 = llvm.load %66 : !llvm.ptr -> i64
%123 = llvm.call @printf(%121, %122) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%124 = arith.constant 0 : i32
func.return %124 : i32
}
}