1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
| // validator.c - ULTRA-FAST with hash table and CORRECT degree calculation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
#define MAX_DEGREE 30
#define MAX_LINE 65536
#define MAX_ROOTS 100
#define TOLERANCE 1e-3
#define PROGRESS_INTERVAL 10000
#define MAX_POLYS 2000000
#define HASH_SIZE 2097152
typedef struct {
int id;
double coeffs[MAX_DEGREE + 1];
int degree; // actual degree (count - 1)
int coeff_count; // number of coefficients
} InputPoly;
typedef struct {
int id;
int root_count;
double roots_left[MAX_ROOTS];
double roots_right[MAX_ROOTS];
} OutputPoly;
typedef struct HashNode {
int id;
int index;
struct HashNode *next;
} HashNode;
HashNode *hash_table[HASH_SIZE] = {NULL};
int hash_function(int id) {
return ((unsigned int)id * 2654435761u) % HASH_SIZE;
}
void hash_insert(int id, int index) {
int slot = hash_function(id);
HashNode *node = malloc(sizeof(HashNode));
node->id = id;
node->index = index;
node->next = hash_table[slot];
hash_table[slot] = node;
}
int hash_lookup(int id) {
int slot = hash_function(id);
HashNode *node = hash_table[slot];
while (node != NULL) {
if (node->id == id) return node->index;
node = node->next;
}
return -1;
}
void hash_free() {
for (int i = 0; i < HASH_SIZE; i++) {
HashNode *node = hash_table[i];
while (node != NULL) {
HashNode *next = node->next;
free(node);
node = next;
}
hash_table[i] = NULL;
}
}
double eval_poly(double *coeffs, int count, double x) {
double result = coeffs[0];
for (int i = 1; i < count; i++) {
result = result * x + coeffs[i];
}
return result;
}
int parse_line_with_id(const char *line, int *id, double *coeffs) {
const char *p = line;
while (isspace(*p)) p++;
if (strncmp(p, "ID=", 3) == 0) {
p += 3;
char *endptr;
*id = (int)strtol(p, &endptr, 10);
if (p == endptr) {
*id = -1;
return 0;
}
p = endptr;
} else {
*id = -1;
return 0;
}
int count = 0;
while (*p && count <= MAX_DEGREE) {
while (isspace(*p)) p++;
if (!*p || *p == '\n') break;
char *endptr;
coeffs[count] = strtod(p, &endptr);
if (p == endptr) break;
count++;
p = endptr;
}
return count;
}
int parse_output_line(const char *line, int *id, int *root_count,
double *roots_left, double *roots_right) {
const char *p = line;
while (isspace(*p)) p++;
if (strncmp(p, "ID=", 3) == 0) {
p += 3;
char *endptr;
*id = (int)strtol(p, &endptr, 10);
if (p == endptr) {
*id = -1;
return -1;
}
p = endptr;
} else {
*id = -1;
return -1;
}
while (isspace(*p)) p++;
char *endptr;
*root_count = (int)strtol(p, &endptr, 10);
if (p == endptr) return -1;
p = endptr;
int parsed = 0;
for (int i = 0; i < *root_count && i < MAX_ROOTS; i++) {
while (isspace(*p)) p++;
if (!*p) break;
roots_left[i] = strtod(p, &endptr);
if (p == endptr) break;
p = endptr;
while (isspace(*p)) p++;
roots_right[i] = strtod(p, &endptr);
if (p == endptr) break;
p = endptr;
parsed++;
}
if (parsed != *root_count) {
fprintf(stderr, "ID=%d claims %d roots but only %d pairs found in line\n",
*id, *root_count, parsed);
return -1;
}
return parsed;
}
void print_progress(int current, int total, double elapsed) {
if (total == 0) return;
int percent = (int)(100.0 * current / total);
double rate = current / elapsed;
double eta = (total - current) / rate;
printf("\rProgress: %d/%d (%d%%) | %.2f poly/sec | ETA: %.1fs",
current, total, percent, rate, eta);
fflush(stdout);
}
int main(int argc, char **argv) {
const char *input_path = (argc >= 2) ? argv[1] : "input.txt";
const char *output_path = (argc >= 3) ? argv[2] : "output.txt";
int precision = (argc >= 4) ? atoi(argv[3]) : 0;
printf("[OK] Loading input polynomials...\n");
if (precision > 0) {
printf("[PRECISION] Truncating to %d decimal places\n", precision);
} else {
printf("[PRECISION] Full (no truncation)\n");
}
FILE *in = fopen(input_path, "r");
if (!in) {
fprintf(stderr, "ERROR: cannot open %s\n", input_path);
return 1;
}
InputPoly *input_polys = malloc(MAX_POLYS * sizeof(InputPoly));
if (!input_polys) {
fprintf(stderr, "ERROR: cannot allocate memory\n");
fclose(in);
return 1;
}
char line[MAX_LINE];
int input_count = 0;
while (fgets(line, sizeof(line), in) && input_count < MAX_POLYS) {
const char *p = line;
while (isspace(*p)) p++;
if (!*p || *p == '\n') continue;
int id;
int coeff_count = parse_line_with_id(line, &id, input_polys[input_count].coeffs);
if (coeff_count == 0) {
fprintf(stderr, "WARNING: Skipping invalid input line (ID=%d)\n", id);
continue;
}
// CRITICAL FIX: degree = coefficient_count - 1
int degree = coeff_count - 1;
if (degree > MAX_DEGREE) {
fprintf(stderr, "ERROR: Polynomial ID=%d has degree %d > MAX_DEGREE %d\n",
id, degree, MAX_DEGREE);
fprintf(stderr, "\n%s\n", line);
free(input_polys);
fclose(in);
return 1;
}
input_polys[input_count].id = id;
input_polys[input_count].degree = degree;
input_polys[input_count].coeff_count = coeff_count;
input_count++;
}
fclose(in);
printf("[OK] Loaded %d input polynomials\n", input_count);
printf("[OK] Loading output results and building hash table...\n");
FILE *out = fopen(output_path, "r");
if (!out) {
fprintf(stderr, "ERROR: cannot open %s\n", output_path);
free(input_polys);
return 2;
}
OutputPoly *output_polys = malloc(MAX_POLYS * sizeof(OutputPoly));
if (!output_polys) {
fprintf(stderr, "ERROR: cannot allocate memory\n");
fclose(out);
free(input_polys);
return 1;
}
int output_count = 0;
long output_line_num = 0;
while (fgets(line, sizeof(line), out) && output_count < MAX_POLYS) {
output_line_num++;
const char *p = line;
while (isspace(*p)) p++;
if (!*p || *p == '\n') continue;
int id, root_count;
int parsed = parse_output_line(line, &id, &root_count,
output_polys[output_count].roots_left,
output_polys[output_count].roots_right);
if (parsed < 0) {
fprintf(stderr, "ERROR: Failed to parse output line %ld\n", output_line_num);
free(input_polys);
free(output_polys);
fclose(out);
return 1;
}
output_polys[output_count].id = id;
output_polys[output_count].root_count = parsed;
hash_insert(id, output_count);
output_count++;
}
fclose(out);
printf("[OK] Loaded %d output results\n", output_count);
printf("\n=== STURM ROOT VALIDATOR (Hash-based, Ultra-fast) ===\n");
printf("[OK] Validating...\n");
int total_polys = 0;
int total_roots = 0;
int valid_roots = 0;
int invalid_roots = 0;
int polys_with_errors = 0;
int missing_ids = 0;
double start_time = (double)clock() / CLOCKS_PER_SEC;
double last_update = start_time;
for (int i = 0; i < input_count; i++) {
int input_id = input_polys[i].id;
int out_idx = hash_lookup(input_id);
if (out_idx == -1) {
printf("\nNo output found for input ID=%d\n", input_id);
missing_ids++;
continue;
}
int poly_has_errors = 0;
int poly_error_count = 0;
for (int j = 0; j < output_polys[out_idx].root_count; j++) {
double mid = (output_polys[out_idx].roots_left[j] +
output_polys[out_idx].roots_right[j]) / 2.0;
// Use coeff_count for eval_poly
double f_val = eval_poly(input_polys[i].coeffs,
input_polys[i].coeff_count, mid);
total_roots++;
if (fabs(f_val) < TOLERANCE) {
valid_roots++;
} else {
invalid_roots++;
if (!poly_has_errors) {
printf("\nID=%d (degree %d): ERRORS FOUND\n",
input_id, input_polys[i].degree);
poly_has_errors = 1;
}
printf(" Root %d: x ∈ [%.6f, %.6f], f(%.6f) = %.2e [ERROR]\n",
j, output_polys[out_idx].roots_left[j],
output_polys[out_idx].roots_right[j], mid, f_val);
poly_error_count++;
}
}
if (poly_has_errors) {
printf(" → %d invalid roots in this polynomial\n", poly_error_count);
polys_with_errors++;
}
total_polys++;
double now = (double)clock() / CLOCKS_PER_SEC;
if ((total_polys % PROGRESS_INTERVAL == 0) || (now - last_update >= 1.0)) {
double elapsed = now - start_time;
print_progress(total_polys, input_count, elapsed);
last_update = now;
}
}
double end_time = (double)clock() / CLOCKS_PER_SEC;
double elapsed = end_time - start_time;
hash_free();
free(input_polys);
free(output_polys);
printf("\n[OK] Done. Validated %d polynomials\n", total_polys);
printf("[PERFORMANCE] Total time: %.2f seconds (%.2f poly/sec)\n",
elapsed, total_polys / elapsed);
printf("\n");
printf("==============================================\n");
printf(" VALIDATION SUMMARY \n");
printf("==============================================\n");
printf("Polynomials tested: %d\n", total_polys);
printf("Total roots tested: %d\n", total_roots);
printf("Valid roots: %d (%.1f%%)\n",
valid_roots, total_roots > 0 ? 100.0 * valid_roots / total_roots : 0.0);
printf("Invalid roots: %d (%.1f%%)\n",
invalid_roots, total_roots > 0 ? 100.0 * invalid_roots / total_roots : 0.0);
printf("Polynomials with errors: %d\n", polys_with_errors);
if (missing_ids > 0) {
printf("Missing output IDs: %d\n", missing_ids);
}
printf("==============================================\n");
if (invalid_roots == 0 && missing_ids == 0) {
printf("✓ VALIDATION PASSED\n");
return 0;
} else {
if (invalid_roots > 0) {
printf("✗ VALIDATION FAILED: %d invalid roots found\n", invalid_roots);
}
if (missing_ids > 0) {
printf("✗ VALIDATION FAILED: %d missing output IDs\n", missing_ids);
}
return 1;
}
} |