// lossscan762.c -- job #762 (route 2): exhaustive loss-directed scan of ALL starts a in [0,W). // // Conventions are those of return #350's frozen selector/phase code (selector350.c, adaptive-triage753.c, // check-adaptive-triage753.py): support = offsets s in [1,L] with gcd(a+s,W)=gcd(a+s+2,W)=1; killing prime q_i // with phase t_i in [0,q_i) kills offset s iff (s+t_i) mod q_i in {0, q_i-2}; exact = N - |union|; // loss l = exact - max(0, BC, B3) = min(exact, quadruple, smallest pair-only atom) (identity, #350). // // Since l <= quadruple, l > 0 needs a support position s0 killed by all four primes, which pins each phase to // one of two values, t_i in {(-s0) mod q_i, (q_i-2-s0) mod q_i}. So max over ALL q0 q1 q2 q3 phases of l(a,.) // equals the max over the <= 16 N candidate phases enumerated here. That makes an all-start exhaustive scan cheap. // // Usage: lossscan762 W L keep q0 q1 q2 q3 -> JSON on stdout (timing on stderr) #include #include #include #include #include #define MAX_N 84 typedef struct { uint64_t lo, hi; } Mask; static int pop(Mask a){ return __builtin_popcountll(a.lo)+__builtin_popcountll(a.hi); } static Mask band(Mask a, Mask b){ return (Mask){a.lo&b.lo,a.hi&b.hi}; } static Mask bor(Mask a, Mask b){ return (Mask){a.lo|b.lo,a.hi|b.hi}; } static Mask bnot(Mask a){ return (Mask){~a.lo,~a.hi}; } static const int EDGE[6][2]={{0,1},{0,2},{0,3},{1,2},{1,3},{2,3}}; static const int COMP[6][2]={{2,3},{1,3},{1,2},{0,3},{0,2},{0,1}}; static int gcd_int(int a,int b){ while(b){int r=a%b;a=b;b=r;} return a; } typedef struct { int a, N, F, maxloss, phase[4], exact, quad, minatom, atoms[6]; } Res; static int cmp_loss(const void*pa,const void*pb){ const Res*a=pa,*b=pb; if(a->maxloss!=b->maxloss) return b->maxloss-a->maxloss; return a->a-b->a; } int main(int argc,char**argv){ if(argc<8){ fprintf(stderr,"usage: W L keep q0 q1 q2 q3\n"); return 2; } int W=atoi(argv[1]), L=atoi(argv[2]), keep=atoi(argv[3]), q[4]; for(int i=0;i<4;i++) q[i]=atoi(argv[4+i]); int chordal=(argc>8 && !strcmp(argv[8],"chordal")); clock_t c0=clock(); unsigned char*slot=malloc(W); for(int r=0;r=MAX_N){fprintf(stderr,"N overflow at a=%d\n",a); return 3;} offsets[N++]=s; } // first-order F(a) = N - sum_i max_t |A_i(t)| (selector350.c convention) Mask masks[4][37]; int F=N; for(int i=0;i<4;i++){ int worst=0; for(int t=0;tworst) worst=cnt; } F-=worst; } if(F 0 needs the (0,1) and (2,3) // pair-only atoms nonempty, so a position s01 killed by primes 0,1 and s23 by primes 2,3 pin all four phases // (16 N^2 phases); the max over all phases of loss_c is the max over these. int NV = chordal ? N*N : N; for(int vv=0;vv>i)&1 ? q[i]-2 : 0; // (s+t) mod q == target int sref = (i<2) ? s0 : s1; t[i]=((target - sref)%q[i]+q[i])%q[i]; m[i]=masks[i][t[i]]; } Mask un=bor(bor(m[0],m[1]),bor(m[2],m[3])); int exact=N-pop(un); int quad=pop(band(band(m[0],m[1]),band(m[2],m[3]))); int atoms[6], minatom=1<<30; for(int e=0;e<6;e++){ atoms[e]=pop(band(band(m[EDGE[e][0]],m[EDGE[e][1]]),bnot(bor(m[COMP[e][0]],m[COMP[e][1]])))); if(atoms[e]R.maxloss || (loss==R.maxloss && 0)){ R.maxloss=loss; memcpy(R.phase,t,sizeof t); R.exact=exact; R.quad=quad; R.minatom=minatom; memcpy(R.atoms,atoms,sizeof atoms); } } } if(R.maxloss<0) R.maxloss=0; hist_loss[R.maxloss]++; if(R.maxloss>0) nstarts_pos++; if(R.maxloss>globalmax) globalmax=R.maxloss; res[a]=R; } // rank by max loss desc then a; keep first `keep` distinct complete shapes qsort(res,(size_t)W,sizeof(Res),cmp_loss); printf("{\"mode\":\"%s\",\"W\":%d,\"L\":%d,\"q\":[%d,%d,%d,%d],\"ranked_starts\":%d,\"global_max_loss\":%d,\"starts_with_positive_loss\":%lld,\"minimum_first_order\":%d,\"loss_histogram\":[",chordal?"chordal":"envelope",W,L,q[0],q[1],q[2],q[3],W,globalmax,(long long)nstarts_pos,minF); int comma=0; for(int v=0;v<=MAX_N;v++) if(hist_loss[v]){ printf("%s[%d,%llu]",comma?",":"",v,(unsigned long long)hist_loss[v]); comma=1; } printf("],\"selected\":["); int kept=0; int (*shapes)[MAX_N]=malloc(sizeof(int[MAX_N])*(size_t)keep); int sizes[64]; for(int rank=0;rank