← All problems
Problem 899
DistribuNim I — count losing ordered pairs (a,b) with 1<=a,b<=7^17.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n^3)
Space complexity O(1)O(n^2)
Approach Flow solution Game theory DP
Verdict Optimal
Flow source
# Project Euler 899
# DistribuNim I — count losing ordered pairs (a,b) with 1<=a,b<=7^17.
function L(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut U: i64 = 0
let mut lo: i64 = 1
while lo <= n {
let mut hi: i64 = 2 * lo - 1
if hi > n { hi = n }
if hi >= lo {
let A_k: i64 = hi - lo + 1
let M: i64 = 2 * lo
let mut C_k: i64 = 0
if n >= M - 1 {
C_k = (n - (M - 1)) / M + 1
}
U = U + A_k * C_k
}
lo = lo * 2
}
# Diagonal losing positions: a=b=2^k-1. Count = floor(log2(n+1)).
let mut D: i64 = 0
let mut t: i64 = n + 1
while t > 1 {
t = t >> 1
D = D + 1
}
return 2 * U - D
}
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 & 1) != 0 { r = r * b }
b = b * b
e = e >> 1
}
return r
}
function main() -> i32 {
printf("%lld\n", L(ipow(7, 17)))
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 L_i64(int64_t n);
int64_t ipow_i64_i64(int64_t base, int64_t exp);
int32_t main(void);
int64_t L_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t U = 0;
int64_t lo = 1;
while (lo <= n) {
int64_t hi = ((2 * lo) - 1);
if (hi > n) {
hi = n;
}
if (hi >= lo) {
int64_t A_k = ((hi - lo) + 1);
int64_t M = (2 * lo);
int64_t C_k = 0;
if (n >= (M - 1)) {
C_k = (FLOW_CHECKED_DIV(((n - (M - 1))), (M)) + 1);
}
U = (U + (A_k * C_k));
}
lo = (lo * 2);
}
int64_t D = 0;
int64_t t = (n + 1);
while (t > 1) {
t = FLOW_CHECKED_SHR((t), (1));
D = (D + 1);
}
return ((2 * U) - D);
}
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 ((e & 1) != 0) {
r = (r * b);
}
b = (b * b);
e = FLOW_CHECKED_SHR((e), (1));
}
return r;
}
int32_t main(void) {
printf("%lld\n", L_i64(ipow_i64_i64(7, 17)));
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 @L(%arg0: i64) -> i64 {
%0 = arith.constant 0 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi sle, %arg0, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%3 = arith.constant 0 : i32
%4 = arith.extsi %3 : i32 to i64
func.return %4 : i64
^bb1:
cf.br ^bb2
^bb2:
%5 = arith.constant 0 : i32
%6 = arith.extsi %5 : i32 to i64
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
llvm.store %6, %8 : i64, !llvm.ptr
%9 = arith.constant 1 : i32
%10 = arith.extsi %9 : i32 to i64
%11 = llvm.mlir.constant(1 : i64) : i64
%12 = llvm.alloca %11 x i64 : (i64) -> !llvm.ptr
llvm.store %10, %12 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%13 = llvm.load %12 : !llvm.ptr -> i64
%14 = arith.cmpi sle, %13, %arg0 : i64
cf.cond_br %14, ^bb4, ^bb5
^bb4:
%15 = arith.constant 2 : i32
%16 = llvm.load %12 : !llvm.ptr -> i64
%18 = arith.extsi %15 : i32 to i64
%17 = arith.muli %18, %16 : i64
%19 = arith.constant 1 : i32
%21 = arith.extsi %19 : i32 to i64
%20 = arith.subi %17, %21 : i64
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
llvm.store %20, %23 : i64, !llvm.ptr
%24 = llvm.load %23 : !llvm.ptr -> i64
%25 = arith.cmpi sgt, %24, %arg0 : i64
cf.cond_br %25, ^bb6, ^bb7
^bb6:
llvm.store %arg0, %23 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%26 = llvm.load %23 : !llvm.ptr -> i64
%27 = llvm.load %12 : !llvm.ptr -> i64
%28 = arith.cmpi sge, %26, %27 : i64
cf.cond_br %28, ^bb9, ^bb10
^bb9:
%29 = llvm.load %23 : !llvm.ptr -> i64
%30 = llvm.load %12 : !llvm.ptr -> i64
%31 = arith.subi %29, %30 : i64
%32 = arith.constant 1 : i32
%34 = arith.extsi %32 : i32 to i64
%33 = arith.addi %31, %34 : i64
%35 = arith.constant 2 : i32
%36 = llvm.load %12 : !llvm.ptr -> i64
%38 = arith.extsi %35 : i32 to i64
%37 = arith.muli %38, %36 : i64
%39 = arith.constant 0 : i32
%40 = arith.extsi %39 : i32 to i64
%41 = llvm.mlir.constant(1 : i64) : i64
%42 = llvm.alloca %41 x i64 : (i64) -> !llvm.ptr
llvm.store %40, %42 : i64, !llvm.ptr
%43 = arith.constant 1 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.subi %37, %45 : i64
%46 = arith.cmpi sge, %arg0, %44 : i64
cf.cond_br %46, ^bb12, ^bb13
^bb12:
%47 = arith.constant 1 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.subi %37, %49 : i64
%50 = arith.subi %arg0, %48 : i64
%51 = arith.divsi %50, %37 : i64
%52 = arith.constant 1 : i32
%54 = arith.extsi %52 : i32 to i64
%53 = arith.addi %51, %54 : i64
llvm.store %53, %42 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%55 = llvm.load %8 : !llvm.ptr -> i64
%56 = llvm.load %42 : !llvm.ptr -> i64
%57 = arith.muli %33, %56 : i64
%58 = arith.addi %55, %57 : i64
llvm.store %58, %8 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%59 = llvm.load %12 : !llvm.ptr -> i64
%60 = arith.constant 2 : i32
%62 = arith.extsi %60 : i32 to i64
%61 = arith.muli %59, %62 : i64
llvm.store %61, %12 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%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 1 : i32
%69 = arith.extsi %67 : i32 to i64
%68 = arith.addi %arg0, %69 : i64
%70 = llvm.mlir.constant(1 : i64) : i64
%71 = llvm.alloca %70 x i64 : (i64) -> !llvm.ptr
llvm.store %68, %71 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%72 = llvm.load %71 : !llvm.ptr -> i64
%73 = arith.constant 1 : i32
%75 = arith.extsi %73 : i32 to i64
%74 = arith.cmpi sgt, %72, %75 : i64
cf.cond_br %74, ^bb16, ^bb17
^bb16:
%76 = llvm.load %71 : !llvm.ptr -> i64
%77 = arith.constant 1 : i32
%79 = arith.extsi %77 : i32 to i64
%78 = arith.shrsi %76, %79 : i64
llvm.store %78, %71 : i64, !llvm.ptr
%80 = llvm.load %66 : !llvm.ptr -> i64
%81 = arith.constant 1 : i32
%83 = arith.extsi %81 : i32 to i64
%82 = arith.addi %80, %83 : i64
llvm.store %82, %66 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%84 = arith.constant 2 : i32
%85 = llvm.load %8 : !llvm.ptr -> i64
%87 = arith.extsi %84 : i32 to i64
%86 = arith.muli %87, %85 : i64
%88 = llvm.load %66 : !llvm.ptr -> i64
%89 = arith.subi %86, %88 : i64
func.return %89 : i64
}
func.func @ipow(%arg0: i64, %arg1: i64) -> i64 {
%90 = arith.constant 1 : i32
%91 = arith.extsi %90 : i32 to i64
%92 = llvm.mlir.constant(1 : i64) : i64
%93 = llvm.alloca %92 x i64 : (i64) -> !llvm.ptr
llvm.store %91, %93 : i64, !llvm.ptr
%94 = llvm.mlir.constant(1 : i64) : i64
%95 = llvm.alloca %94 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %95 : i64, !llvm.ptr
%96 = llvm.mlir.constant(1 : i64) : i64
%97 = llvm.alloca %96 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %97 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%98 = llvm.load %97 : !llvm.ptr -> i64
%99 = arith.constant 0 : i32
%101 = arith.extsi %99 : i32 to i64
%100 = arith.cmpi sgt, %98, %101 : i64
cf.cond_br %100, ^bb19, ^bb20
^bb19:
%102 = llvm.load %97 : !llvm.ptr -> i64
%103 = arith.constant 1 : i32
%105 = arith.extsi %103 : i32 to i64
%104 = arith.andi %102, %105 : i64
%106 = arith.constant 0 : i32
%108 = arith.extsi %106 : i32 to i64
%107 = arith.cmpi ne, %104, %108 : i64
cf.cond_br %107, ^bb21, ^bb22
^bb21:
%109 = llvm.load %93 : !llvm.ptr -> i64
%110 = llvm.load %95 : !llvm.ptr -> i64
%111 = arith.muli %109, %110 : i64
llvm.store %111, %93 : i64, !llvm.ptr
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%112 = llvm.load %95 : !llvm.ptr -> i64
%113 = llvm.load %95 : !llvm.ptr -> i64
%114 = arith.muli %112, %113 : i64
llvm.store %114, %95 : i64, !llvm.ptr
%115 = llvm.load %97 : !llvm.ptr -> i64
%116 = arith.constant 1 : i32
%118 = arith.extsi %116 : i32 to i64
%117 = arith.shrsi %115, %118 : i64
llvm.store %117, %97 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%119 = llvm.load %93 : !llvm.ptr -> i64
func.return %119 : i64
}
func.func @main() -> i32 {
%120 = llvm.mlir.addressof @str_0 : !llvm.ptr
%123 = arith.constant 7 : i32
%124 = arith.constant 17 : i32
%125 = arith.extsi %123 : i32 to i64
%126 = arith.extsi %124 : i32 to i64
%122 = func.call @ipow(%125, %126) : (i64, i64) -> i64
%121 = func.call @L(%122) : (i64) -> i64
%127 = llvm.call @printf(%120, %121) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%128 = arith.constant 0 : i32
func.return %128 : i32
}
}