/* 2kwas: exact autoconvolution maximum by direct O(N^2) summation in unsigned __int128. No FFT, NTT or Kronecker substitution. Input: binary file of N uint64 heights. */ #include #include #include #include typedef unsigned __int128 u128; static void pr(u128 x, char *buf) { char t[64]; int n = 0; if (!x) { strcpy(buf, "0"); return; } while (x) { t[n++] = '0' + (int)(x % 10); x /= 10; } for (int i = 0; i < n; i++) buf[i] = t[n-1-i]; buf[n] = 0; } int main(int argc, char **argv) { FILE *f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); long N = sz / 8; uint64_t *a = malloc(sz); if (fread(a, 8, N, f) != (size_t)N) return 1; fclose(f); long K = 2 * N - 1; u128 *c = calloc(K, sizeof(u128)); #pragma omp parallel for schedule(dynamic, 256) for (long k = 0; k < K; k++) { long lo = k - (N - 1); if (lo < 0) lo = 0; long hi = k / 2; /* i <= j, i + j = k */ u128 s = 0; for (long i = lo; i <= hi; i++) { long j = k - i; u128 p = (u128)a[i] * a[j]; s += (i == j) ? p : 2 * p; } c[k] = s; } u128 mx = 0, S = 0; long arg = -1, zeros = 0; uint64_t amax = 0; for (long k = 0; k < K; k++) if (c[k] > mx) { mx = c[k]; arg = k; } for (long i = 0; i < N; i++) { S += a[i]; if (!a[i]) zeros++; if (a[i] > amax) amax = a[i]; } long ties = 0; for (long k = 0; k < K; k++) if (c[k] == mx) ties++; char b1[64], b2[64]; pr(mx, b1); pr(S, b2); printf("N %ld\nmaxc %s\nargmax %ld\nties %ld\nS %s\nzeros %ld\namax %llu\n", N, b1, arg, ties, b2, zeros, (unsigned long long)amax); return 0; }