← All problems
Problem 596
Lattice Points in a Hyperball T(r)=1+8*(S(r^2)-4*S(floor(r^2/4))) mod 10^9+7, S(n)=sum d*floor(n/d).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(1)O(n^2)
Space complexity O(1)O(n^2)
Approach Flow solution Combinatorial or DP counting
Verdict Optimal
Flow source
# Project Euler 596
# Lattice Points in a Hyperball
# T(r)=1+8*(S(r^2)-4*S(floor(r^2/4))) mod 10^9+7, S(n)=sum d*floor(n/d).
const MOD: i64 = 1000000007
const INV2: i64 = 500000004
function sum_range(l: i64, r: i64) -> i64 {
if l > r { return 0 }
let cnt: i64 = r - l + 1
return ((l + r) % MOD) * (cnt % MOD) % MOD * INV2 % MOD
}
function sum_sigma_prefix(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut total: i64 = 0
let mut i: i64 = 1
while i <= n {
let q: i64 = n / i
let j: i64 = n / q
let s: i64 = sum_range(i, j)
total = (total + (q % MOD) * s % MOD) % MOD
i = j + 1
}
return total
}
function T(r: i64) -> i64 {
let n: i64 = r * r
let s: i64 = sum_sigma_prefix(n)
let s4: i64 = sum_sigma_prefix(n / 4)
let mut inner: i64 = (s - 4 * s4) % MOD
if inner < 0 { inner = inner + MOD }
return (1 + 8 * inner) % MOD
}
function main() -> i32 {
printf("%lld\n", T(100000000))
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 sum_range_i64_i64(int64_t l, int64_t r);
int64_t sum_sigma_prefix_i64(int64_t n);
int64_t T_i64(int64_t r);
int32_t main(void);
static const int64_t MOD = 1000000007;
static const int64_t INV2 = 500000004;
int64_t sum_range_i64_i64(int64_t l, int64_t r) {
if (l > r) {
return 0;
}
int64_t cnt = ((r - l) + 1);
return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((l + r)), (MOD)) * FLOW_CHECKED_MOD((cnt), (MOD)))), (MOD)) * INV2)), (MOD));
}
int64_t sum_sigma_prefix_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t total = 0;
int64_t i = 1;
while (i <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (i));
int64_t j = FLOW_CHECKED_DIV((n), (q));
int64_t s = sum_range_i64_i64(i, j);
total = FLOW_CHECKED_MOD(((total + FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((q), (MOD)) * s)), (MOD)))), (MOD));
i = (j + 1);
}
return total;
}
int64_t T_i64(int64_t r) {
int64_t n = (r * r);
int64_t s = sum_sigma_prefix_i64(n);
int64_t s4 = sum_sigma_prefix_i64(FLOW_CHECKED_DIV((n), (4)));
int64_t inner = FLOW_CHECKED_MOD(((s - (4 * s4))), (MOD));
if (inner < 0) {
inner = (inner + MOD);
}
return FLOW_CHECKED_MOD(((1 + (8 * inner))), (MOD));
}
int32_t main(void) {
printf("%lld\n", T_i64(100000000));
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>
// Constant: MOD
llvm.mlir.global internal constant @MOD(1000000007 : i64) : i64
// Constant: INV2
llvm.mlir.global internal constant @INV2(500000004 : i64) : i64
func.func @sum_range(%arg0: i64, %arg1: i64) -> i64 {
%0 = arith.cmpi sgt, %arg0, %arg1 : i64
cf.cond_br %0, ^bb0, ^bb1
^bb0:
%1 = arith.constant 0 : i32
%2 = arith.extsi %1 : i32 to i64
func.return %2 : i64
^bb1:
cf.br ^bb2
^bb2:
%3 = arith.subi %arg1, %arg0 : i64
%4 = arith.constant 1 : i32
%6 = arith.extsi %4 : i32 to i64
%5 = arith.addi %3, %6 : i64
%7 = arith.addi %arg0, %arg1 : i64
%8 = llvm.mlir.addressof @MOD : !llvm.ptr
%9 = llvm.load %8 : !llvm.ptr -> i64
%10 = arith.remsi %7, %9 : i64
%11 = llvm.mlir.addressof @MOD : !llvm.ptr
%12 = llvm.load %11 : !llvm.ptr -> i64
%13 = arith.remsi %5, %12 : i64
%14 = arith.muli %10, %13 : i64
%15 = llvm.mlir.addressof @MOD : !llvm.ptr
%16 = llvm.load %15 : !llvm.ptr -> i64
%17 = arith.remsi %14, %16 : i64
%18 = llvm.mlir.addressof @INV2 : !llvm.ptr
%19 = llvm.load %18 : !llvm.ptr -> i64
%20 = arith.muli %17, %19 : i64
%21 = llvm.mlir.addressof @MOD : !llvm.ptr
%22 = llvm.load %21 : !llvm.ptr -> i64
%23 = arith.remsi %20, %22 : i64
func.return %23 : i64
}
func.func @sum_sigma_prefix(%arg0: i64) -> i64 {
%24 = arith.constant 0 : i32
%26 = arith.extsi %24 : i32 to i64
%25 = arith.cmpi sle, %arg0, %26 : i64
cf.cond_br %25, ^bb3, ^bb4
^bb3:
%27 = arith.constant 0 : i32
%28 = arith.extsi %27 : i32 to i64
func.return %28 : i64
^bb4:
cf.br ^bb5
^bb5:
%29 = arith.constant 0 : i32
%30 = arith.extsi %29 : i32 to i64
%31 = llvm.mlir.constant(1 : i64) : i64
%32 = llvm.alloca %31 x i64 : (i64) -> !llvm.ptr
llvm.store %30, %32 : i64, !llvm.ptr
%33 = arith.constant 1 : i32
%34 = arith.extsi %33 : i32 to i64
%35 = llvm.mlir.constant(1 : i64) : i64
%36 = llvm.alloca %35 x i64 : (i64) -> !llvm.ptr
llvm.store %34, %36 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%37 = llvm.load %36 : !llvm.ptr -> i64
%38 = arith.cmpi sle, %37, %arg0 : i64
cf.cond_br %38, ^bb7, ^bb8
^bb7:
%39 = llvm.load %36 : !llvm.ptr -> i64
%40 = arith.divsi %arg0, %39 : i64
%41 = arith.divsi %arg0, %40 : i64
%43 = llvm.load %36 : !llvm.ptr -> i64
%42 = func.call @sum_range(%43, %41) : (i64, i64) -> i64
%44 = llvm.load %32 : !llvm.ptr -> i64
%45 = llvm.mlir.addressof @MOD : !llvm.ptr
%46 = llvm.load %45 : !llvm.ptr -> i64
%47 = arith.remsi %40, %46 : i64
%48 = arith.muli %47, %42 : i64
%49 = llvm.mlir.addressof @MOD : !llvm.ptr
%50 = llvm.load %49 : !llvm.ptr -> i64
%51 = arith.remsi %48, %50 : i64
%52 = arith.addi %44, %51 : i64
%53 = llvm.mlir.addressof @MOD : !llvm.ptr
%54 = llvm.load %53 : !llvm.ptr -> i64
%55 = arith.remsi %52, %54 : i64
llvm.store %55, %32 : i64, !llvm.ptr
%56 = arith.constant 1 : i32
%58 = arith.extsi %56 : i32 to i64
%57 = arith.addi %41, %58 : i64
llvm.store %57, %36 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%59 = llvm.load %32 : !llvm.ptr -> i64
func.return %59 : i64
}
func.func @T(%arg0: i64) -> i64 {
%60 = arith.muli %arg0, %arg0 : i64
%61 = func.call @sum_sigma_prefix(%60) : (i64) -> i64
%63 = arith.constant 4 : i32
%65 = arith.extsi %63 : i32 to i64
%64 = arith.divsi %60, %65 : i64
%62 = func.call @sum_sigma_prefix(%64) : (i64) -> i64
%66 = arith.constant 4 : i32
%68 = arith.extsi %66 : i32 to i64
%67 = arith.muli %68, %62 : i64
%69 = arith.subi %61, %67 : i64
%70 = llvm.mlir.addressof @MOD : !llvm.ptr
%71 = llvm.load %70 : !llvm.ptr -> i64
%72 = arith.remsi %69, %71 : i64
%73 = llvm.mlir.constant(1 : i64) : i64
%74 = llvm.alloca %73 x i64 : (i64) -> !llvm.ptr
llvm.store %72, %74 : i64, !llvm.ptr
%75 = llvm.load %74 : !llvm.ptr -> i64
%76 = arith.constant 0 : i32
%78 = arith.extsi %76 : i32 to i64
%77 = arith.cmpi slt, %75, %78 : i64
cf.cond_br %77, ^bb9, ^bb10
^bb9:
%79 = llvm.load %74 : !llvm.ptr -> i64
%80 = llvm.mlir.addressof @MOD : !llvm.ptr
%81 = llvm.load %80 : !llvm.ptr -> i64
%82 = arith.addi %79, %81 : i64
llvm.store %82, %74 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%83 = arith.constant 1 : i32
%84 = arith.constant 8 : i32
%85 = llvm.load %74 : !llvm.ptr -> i64
%87 = arith.extsi %84 : i32 to i64
%86 = arith.muli %87, %85 : i64
%89 = arith.extsi %83 : i32 to i64
%88 = arith.addi %89, %86 : i64
%90 = llvm.mlir.addressof @MOD : !llvm.ptr
%91 = llvm.load %90 : !llvm.ptr -> i64
%92 = arith.remsi %88, %91 : i64
func.return %92 : i64
}
func.func @main() -> i32 {
%93 = llvm.mlir.addressof @str_0 : !llvm.ptr
%95 = arith.constant 100000000 : i32
%96 = arith.extsi %95 : i32 to i64
%94 = func.call @T(%96) : (i64) -> i64
%97 = llvm.call @printf(%93, %94) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%98 = arith.constant 0 : i32
func.return %98 : i32
}
}