# Project Euler 465
# Polar Polygons — P(7^13) mod 10^9+7 via Du Jiao totient sums (i128).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1000000007
const PRE: i64 = 20000000
const CAP: i64 = 4194304
let mut G_pref: ptr<i64> = null
let mut G_keys: ptr<i64> = null
let mut G_vals: ptr<i128> = null
let mut G_used: ptr<i8> = null
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 {
r = ((r as i128) * (b as i128) % (mod as i128)) as i64
}
b = ((b as i128) * (b as i128) % (mod as i128)) as i64
e = e / 2
}
return r
}
function hslot(key: i64) -> i64 {
let mut h: i64 = key % CAP
if h < 0 { h = h + CAP }
while G_used[h] == 1 && G_keys[h] != key {
h = h + 1
if h == CAP { h = 0 }
}
return h
}
function tot_sum(n: i64) -> i128 {
if n <= 0 { return 0 as i128 }
if n <= PRE { return G_pref[n] as i128 }
let s: i64 = hslot(n)
if G_used[s] == 1 { return G_vals[s] }
let nn: i128 = n as i128
let mut res: i128 = nn * (nn + 1) / 2
let mut lo: i64 = 2
while lo <= n {
let q: i64 = n / lo
let hi: i64 = n / q
res = res - ((hi - lo + 1) as i128) * tot_sum(q)
lo = hi + 1
}
G_used[s] = 1
G_keys[s] = n
G_vals[s] = res
return res
}
function i128_mod(x0: i128, m: i64) -> i64 {
let mm: i128 = m as i128
let mut r: i128 = x0 % mm
if r < 0 { r = r + mm }
return r as i64
}
function polar_polygons_mod(n: i64) -> i64 {
let mut B: i64 = 1
let mut S1: i64 = 0
let mut S2: i64 = 0
let mut lo: i64 = 1
let mut prev_phi: i128 = 0
let modm1: i64 = MOD - 1
let mut B_zero: i64 = 0
while lo <= n {
let q: i64 = n / lo
let hi: i64 = n / q
let curr: i128 = tot_sum(hi)
let sum_phi: i128 = curr - prev_phi
prev_phi = curr
let sum_phi_mod: i64 = i128_mod(sum_phi, MOD)
let c_mod: i64 = (sum_phi_mod * 4) % MOD
let q_mod: i64 = q % MOD
S1 = (S1 + c_mod * q_mod) % MOD
let q2: i64 = (q_mod * q_mod) % MOD
S2 = (S2 + c_mod * q2) % MOD
if B_zero == 0 {
let base: i64 = (q + 1) % MOD
if base == 0 {
B = 0
B_zero = 1
} else {
let expv: i64 = (i128_mod(sum_phi, modm1) * 4) % modm1
B = (B * modpow(base, expv, MOD)) % MOD
}
}
lo = hi + 1
}
let mut ans: i64 = (B * B) % MOD
ans = (ans - (2 * B % MOD) * S1 % MOD) % MOD
ans = (ans + S2) % MOD
ans = (ans - 1) % MOD
if ans < 0 { ans = ans + MOD }
return ans
}
function main() -> i32 {
G_pref = calloc(PRE + 1, 8)
let phi: ptr<i32> = calloc(PRE + 1, 4)
let primes: ptr<i32> = calloc(PRE / 5 + 16, 4)
G_keys = calloc(CAP, 8)
G_vals = calloc(CAP, 16)
G_used = calloc(CAP, 1)
if G_pref == null || phi == null || primes == null || G_keys == null || G_vals == null || G_used == null {
return 1
}
phi[1] = 1
let mut pc: i64 = 0
let mut i: i64 = 2
while i <= PRE {
if phi[i] == 0 {
phi[i] = (i - 1) as i32
primes[pc] = i as i32
pc = pc + 1
}
let mut j: i64 = 0
while j < pc {
let p: i64 = primes[j] as i64
let ip: i64 = i * p
if ip > PRE { break }
if i % p == 0 {
phi[ip] = ((phi[i] as i64) * p) as i32
break
} else {
phi[ip] = ((phi[i] as i64) * (p - 1)) as i32
}
j = j + 1
}
i = i + 1
}
let mut s: i64 = 0
i = 1
while i <= PRE {
s = s + (phi[i] as i64)
G_pref[i] = s
i = i + 1
}
free(primes)
free(phi)
let mut n: i64 = 1
let mut t: i64 = 0
while t < 13 {
n = n * 7
t = t + 1
}
printf("%lld\n", polar_polygons_mod(n))
free(G_used)
free(G_vals)
free(G_keys)
free(G_pref)
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 hslot_i64(int64_t key);
__int128 tot_sum_i64(int64_t n);
int64_t i128_mod_i128_i64(__int128 x0, int64_t m);
int64_t polar_polygons_mod_i64(int64_t n);
int32_t main(void);
static const int64_t MOD = 1000000007;
static const int64_t PRE = 20000000;
static const int64_t CAP = 4194304;
/* Module statics */
static int64_t* G_pref = NULL;
static int64_t* G_keys = NULL;
static __int128* G_vals = NULL;
static int8_t* G_used = NULL;
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) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
}
b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t hslot_i64(int64_t key) {
int64_t h = FLOW_CHECKED_MOD((key), (CAP));
if (h < 0) {
h = (h + CAP);
}
while ((G_used[h] == 1 && G_keys[h] != key)) {
h = (h + 1);
if (h == CAP) {
h = 0;
}
}
return h;
}
__int128 tot_sum_i64(int64_t n) {
if (n <= 0) {
return ((__int128)(0));
}
if (n <= PRE) {
return ((__int128)(G_pref[n]));
}
int64_t s = hslot_i64(n);
if (G_used[s] == 1) {
return G_vals[s];
}
__int128 nn = ((__int128)(n));
__int128 res = FLOW_CHECKED_DIV(((nn * (nn + 1))), (2));
int64_t lo = 2;
while (lo <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (lo));
int64_t hi = FLOW_CHECKED_DIV((n), (q));
res = (res - (((__int128)(((hi - lo) + 1))) * tot_sum_i64(q)));
lo = (hi + 1);
}
G_used[s] = 1;
G_keys[s] = n;
G_vals[s] = res;
return res;
}
int64_t i128_mod_i128_i64(__int128 x0, int64_t m) {
__int128 mm = ((__int128)(m));
__int128 r = FLOW_CHECKED_MOD((x0), (mm));
if (r < 0) {
r = (r + mm);
}
return ((int64_t)(r));
}
int64_t polar_polygons_mod_i64(int64_t n) {
int64_t B = 1;
int64_t S1 = 0;
int64_t S2 = 0;
int64_t lo = 1;
__int128 prev_phi = 0;
int64_t modm1 = (MOD - 1);
int64_t B_zero = 0;
while (lo <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (lo));
int64_t hi = FLOW_CHECKED_DIV((n), (q));
__int128 curr = tot_sum_i64(hi);
__int128 sum_phi = (curr - prev_phi);
prev_phi = curr;
int64_t sum_phi_mod = i128_mod_i128_i64(sum_phi, MOD);
int64_t c_mod = FLOW_CHECKED_MOD(((sum_phi_mod * 4)), (MOD));
int64_t q_mod = FLOW_CHECKED_MOD((q), (MOD));
S1 = FLOW_CHECKED_MOD(((S1 + (c_mod * q_mod))), (MOD));
int64_t q2 = FLOW_CHECKED_MOD(((q_mod * q_mod)), (MOD));
S2 = FLOW_CHECKED_MOD(((S2 + (c_mod * q2))), (MOD));
if (B_zero == 0) {
int64_t base = FLOW_CHECKED_MOD(((q + 1)), (MOD));
if (base == 0) {
B = 0;
B_zero = 1;
} else {
int64_t expv = FLOW_CHECKED_MOD(((i128_mod_i128_i64(sum_phi, modm1) * 4)), (modm1));
B = FLOW_CHECKED_MOD(((B * modpow_i64_i64_i64(base, expv, MOD))), (MOD));
}
}
lo = (hi + 1);
}
int64_t ans = FLOW_CHECKED_MOD(((B * B)), (MOD));
ans = FLOW_CHECKED_MOD(((ans - FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((2 * B)), (MOD)) * S1)), (MOD)))), (MOD));
ans = FLOW_CHECKED_MOD(((ans + S2)), (MOD));
ans = FLOW_CHECKED_MOD(((ans - 1)), (MOD));
if (ans < 0) {
ans = (ans + MOD);
}
return ans;
}
int32_t main(void) {
G_pref = calloc((PRE + 1), 8);
int32_t* phi = (int32_t*)(calloc((PRE + 1), 4));
int32_t* primes = (int32_t*)(calloc((FLOW_CHECKED_DIV((PRE), (5)) + 16), 4));
G_keys = calloc(CAP, 8);
G_vals = calloc(CAP, 16);
G_used = calloc(CAP, 1);
if ((((((G_pref == NULL || phi == NULL) || primes == NULL) || G_keys == NULL) || G_vals == NULL) || G_used == NULL)) {
return 1;
}
phi[1] = 1;
int64_t pc = 0;
int64_t i = 2;
while (i <= PRE) {
if (phi[i] == 0) {
phi[i] = ((int32_t)((i - 1)));
primes[pc] = ((int32_t)(i));
pc = (pc + 1);
}
int64_t j = 0;
while (j < pc) {
int64_t p = ((int64_t)(primes[j]));
int64_t ip = (i * p);
if (ip > PRE) {
break;
}
if (FLOW_CHECKED_MOD((i), (p)) == 0) {
phi[ip] = ((int32_t)((((int64_t)(phi[i])) * p)));
break;
} else {
phi[ip] = ((int32_t)((((int64_t)(phi[i])) * (p - 1))));
}
j = (j + 1);
}
i = (i + 1);
}
int64_t s = 0;
i = 1;
while (i <= PRE) {
s = (s + ((int64_t)(phi[i])));
G_pref[i] = s;
i = (i + 1);
}
free(primes);
free(phi);
int64_t n = 1;
int64_t t = 0;
while (t < 13) {
n = (n * 7);
t = (t + 1);
}
printf("%lld\n", polar_polygons_mod_i64(n));
free(G_used);
free(G_vals);
free(G_keys);
free(G_pref);
return 0;
}