#include #include #include #include #include #include #define MAX_N 84 #define RANGE (4 * MAX_N + 1) typedef struct { int a, n, budget; } Row; typedef struct { uint64_t lo, hi; } Mask; static const int EDGE[6][2] = {{0,1},{0,2},{0,3},{1,2},{1,3},{2,3}}; static const int TRI[4][3] = {{0,1,2},{0,1,3},{0,2,3},{1,2,3}}; static int gcd_int(int a, int b) { while (b) { int r=a%b; a=b; b=r; } return a; } static int row_compare(const void *pa, const void *pb) { const Row *a=pa, *b=pb; return a->budget != b->budget ? a->budget-b->budget : a->a-b->a; } static int pop(Mask a) { return __builtin_popcountll(a.lo)+__builtin_popcountll(a.hi); } static Mask both(Mask a, Mask b) { return (Mask){a.lo&b.lo,a.hi&b.hi}; } static Mask join(Mask a, Mask b) { return (Mask){a.lo|b.lo,a.hi|b.hi}; } static int root(int p[4], int v) { while(p[v]!=v) v=p[v]; return v; } static int tree_weight(const int pair[6]) { int order[6]={0,1,2,3,4,5}, parent[4]={0,1,2,3}, sum=0; for(int i=1;i<6;i++) { int e=order[i], j=i; while(j>0 && pair[order[j-1]]0 && W<=510510 && L>0 && keep>0 && keep<=16); unsigned char *slot=calloc((size_t)W,1); Row *rows=malloc((size_t)W*sizeof(Row)); assert(slot && rows); for(int r=0;r2 && q[i]<=37); for(int s=1;s<=L;s++) if(slot[s%W]) { n++; for(int i=0;i<4;i++) counts[i][s%q[i]]++; } for(int a=0;aworst) worst=value; } budget-=worst; } rows[a]=(Row){a,n,budget}; int gone=a+1, added=a+L+1; if(slot[gone%W]) { n--; for(int i=0;i<4;i++) counts[i][gone%q[i]]--; } if(slot[added%W]) { n++; for(int i=0;i<4;i++) counts[i][added%q[i]]++; } } qsort(rows,(size_t)W,sizeof(Row),row_compare); int selected[16][MAX_N], sizes[16], kept=0; printf("{\"W\":%d,\"L\":%d,\"ranked_starts\":%d,\"minimum_first_order\":%d,\"selected\":[",W,L,W,rows[0].budget); for(int rank=0;rank0); int duplicate=0; for(int j=0;j0 && N<=MAX_N); Mask masks[4][37]={0}; for(int v=0;v=1 && offsets[v]<=L); } for(int i=0;i<4;i++) { assert(q[i]>2 && q[i]<=37); for(int t=0;ttwo) two=budget; } assert(first<=tree && tree<=two && two<=exact); int values[4]={first,tree,two,exact}; for(int k=0;k<4;k++) { assert(values[k]>=-3*N && values[k]<=N); hist[k][values[k]+3*N]++; if(values[k]tree) improved++; phases++; } assert(phases==(uint64_t)q[0]*q[1]*q[2]*q[3]); printf("{\"L\":%d,\"N\":%d,\"phase_count\":%llu,\"pointwise_triangle_improvements\":%llu,\"minima\":[",L,N,(unsigned long long)phases,(unsigned long long)improved); for(int k=0;k<4;k++) printf("%s%d",k?",":"",minima[k]); printf("],\"worst_phases\":["); for(int k=0;k<4;k++) printf("%s[%d,%d,%d,%d]",k?",":"",worst[k][0],worst[k][1],worst[k][2],worst[k][3]); printf("],\"histograms\":["); for(int k=0;k<4;k++) { if(k) printf(","); histogram(hist[k],-3*N,N); } printf("],\"tree_loss_histogram\":"); histogram(loss_tree,0,2*N); printf(",\"triangle_loss_histogram\":"); histogram(loss_two,0,N); printf("}\n"); } int main(int argc,char **argv) { assert(argc==2); if(!strcmp(argv[1],"select")) select_supports(); else { assert(!strcmp(argv[1],"phase")); sweep_phases(); } return 0; }