@routilet/c-exam-kit 0.0.0-stage → 0.6.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +18 -0
- package/README.md +19 -2
- package/package.json +29 -4
- package/preset/CC-BY-4.0.txt +156 -0
- package/preset/manifest.json +3117 -0
- package/preset/reference.json +2644 -0
|
@@ -0,0 +1,3117 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 2,
|
|
3
|
+
"id": "exam-io-c99",
|
|
4
|
+
"language": "c",
|
|
5
|
+
"standard": "c99",
|
|
6
|
+
"headerFile": "exam.h",
|
|
7
|
+
"systemHeaders": [
|
|
8
|
+
"stdio.h",
|
|
9
|
+
"stdarg.h",
|
|
10
|
+
"stdlib.h",
|
|
11
|
+
"stddef.h",
|
|
12
|
+
"stdbool.h",
|
|
13
|
+
"ctype.h",
|
|
14
|
+
"errno.h",
|
|
15
|
+
"limits.h",
|
|
16
|
+
"math.h",
|
|
17
|
+
"string.h",
|
|
18
|
+
"stdint.h",
|
|
19
|
+
"float.h"
|
|
20
|
+
],
|
|
21
|
+
"languageId": "c99",
|
|
22
|
+
"components": [
|
|
23
|
+
{
|
|
24
|
+
"id": "exam_algo_fail",
|
|
25
|
+
"symbols": [],
|
|
26
|
+
"requires": [],
|
|
27
|
+
"code": "static inline void exam_algo_fail(const char *message) {\n fprintf(stderr, \"exam algorithm: %s\\n\", message);\n exit(EXIT_FAILURE);\n}\n"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"id": "exam_check_n",
|
|
31
|
+
"symbols": [],
|
|
32
|
+
"requires": [
|
|
33
|
+
"exam_algo_fail"
|
|
34
|
+
],
|
|
35
|
+
"code": "static inline void exam_check_n(int n) {\n if (n < 0) exam_algo_fail(\"negative array length\");\n}\n"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"id": "exam_add_checked_ll",
|
|
39
|
+
"symbols": [],
|
|
40
|
+
"requires": [
|
|
41
|
+
"exam_algo_fail"
|
|
42
|
+
],
|
|
43
|
+
"code": "static inline long long exam_add_checked_ll(long long a, long long b) {\n if ((b > 0 && a > LLONG_MAX - b) ||\n (b < 0 && a < LLONG_MIN - b))\n exam_algo_fail(\"long long addition overflow\");\n return a + b;\n}\n"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"id": "exam_sub_checked_ll",
|
|
47
|
+
"symbols": [],
|
|
48
|
+
"requires": [
|
|
49
|
+
"exam_algo_fail"
|
|
50
|
+
],
|
|
51
|
+
"code": "static inline long long exam_sub_checked_ll(long long a, long long b) {\n if ((b > 0 && a < LLONG_MIN + b) ||\n (b < 0 && a > LLONG_MAX + b))\n exam_algo_fail(\"long long subtraction overflow\");\n return a - b;\n}\n"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"id": "ExamIntIndexPair",
|
|
55
|
+
"symbols": [],
|
|
56
|
+
"requires": [],
|
|
57
|
+
"code": "typedef struct {\n int value;\n int index;\n} ExamIntIndexPair;\n"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"id": "exam_int_index_compare",
|
|
61
|
+
"symbols": [],
|
|
62
|
+
"requires": [
|
|
63
|
+
"ExamIntIndexPair"
|
|
64
|
+
],
|
|
65
|
+
"code": "static inline int exam_int_index_compare(const void *left, const void *right) {\n const ExamIntIndexPair *a = (const ExamIntIndexPair *)left;\n const ExamIntIndexPair *b = (const ExamIntIndexPair *)right;\n if (a->value != b->value)\n return (a->value > b->value) - (a->value < b->value);\n return (a->index > b->index) - (a->index < b->index);\n}\n"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"id": "argsort_ints_into",
|
|
69
|
+
"symbols": [
|
|
70
|
+
"argsort_ints_into"
|
|
71
|
+
],
|
|
72
|
+
"requires": [
|
|
73
|
+
"exam_algo_fail",
|
|
74
|
+
"ExamIntIndexPair",
|
|
75
|
+
"exam_int_index_compare"
|
|
76
|
+
],
|
|
77
|
+
"code": "static inline void argsort_ints_into(const int *a, int n, int *order,\n size_t capacity) {\n if (n < 0 || (size_t)n > capacity)\n exam_algo_fail(\"argsort_ints output capacity too small\");\n if (n == 0) return;\n if (!a || !order) exam_algo_fail(\"null argsort_ints array\");\n if ((size_t)n > SIZE_MAX / sizeof(ExamIntIndexPair))\n exam_algo_fail(\"argsort_ints allocation size overflow\");\n ExamIntIndexPair *pairs = (ExamIntIndexPair *)malloc(\n (size_t)n * sizeof(*pairs));\n if (!pairs) exam_algo_fail(\"argsort_ints allocation failed\");\n for (int i = 0; i < n; ++i) {\n pairs[i].value = a[i];\n pairs[i].index = i;\n }\n qsort(pairs, (size_t)n, sizeof(*pairs), exam_int_index_compare);\n for (int i = 0; i < n; ++i) order[i] = pairs[i].index;\n free(pairs);\n}\n"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"id": "argsort_ints",
|
|
81
|
+
"symbols": [
|
|
82
|
+
"argsort_ints"
|
|
83
|
+
],
|
|
84
|
+
"requires": [
|
|
85
|
+
"argsort_ints_into"
|
|
86
|
+
],
|
|
87
|
+
"code": "#define argsort_ints(a, n, order) \\\n argsort_ints_into((a), (n), (order), sizeof(order) / sizeof((order)[0]))\n"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
"id": "build_diff_ints_into",
|
|
91
|
+
"symbols": [
|
|
92
|
+
"build_diff_ints_into"
|
|
93
|
+
],
|
|
94
|
+
"requires": [
|
|
95
|
+
"exam_algo_fail"
|
|
96
|
+
],
|
|
97
|
+
"code": "static inline void build_diff_ints_into(const int *a, int n,\n long long *difference, size_t capacity) {\n if (n < 0 || (size_t)n >= capacity)\n exam_algo_fail(\"difference array capacity must be at least n+1\");\n for (int i = 0; i < n; ++i)\n difference[i] = (long long)a[i] - (i ? (long long)a[i-1] : 0);\n difference[n] = 0;\n}\n"
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
"id": "diff_range_add",
|
|
101
|
+
"symbols": [
|
|
102
|
+
"diff_range_add"
|
|
103
|
+
],
|
|
104
|
+
"requires": [
|
|
105
|
+
"exam_algo_fail",
|
|
106
|
+
"exam_add_checked_ll",
|
|
107
|
+
"exam_sub_checked_ll"
|
|
108
|
+
],
|
|
109
|
+
"code": "static inline void diff_range_add(long long *difference, int n,\n int begin, int end, long long delta) {\n if (n < 0 || begin < 0 || begin > end || end > n)\n exam_algo_fail(\"invalid difference range\");\n if (begin == end) return;\n difference[begin] = exam_add_checked_ll(difference[begin], delta);\n difference[end] = exam_sub_checked_ll(difference[end], delta);\n}\n"
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"id": "diff_to_lls_into",
|
|
113
|
+
"symbols": [
|
|
114
|
+
"diff_to_lls_into"
|
|
115
|
+
],
|
|
116
|
+
"requires": [
|
|
117
|
+
"exam_algo_fail",
|
|
118
|
+
"exam_add_checked_ll"
|
|
119
|
+
],
|
|
120
|
+
"code": "static inline void diff_to_lls_into(const long long *difference, int n,\n long long *out, size_t capacity) {\n if (n < 0 || (size_t)n > capacity)\n exam_algo_fail(\"difference output array too small\");\n long long current = 0;\n for (int i = 0; i < n; ++i) {\n current = exam_add_checked_ll(current, difference[i]);\n out[i] = current;\n }\n}\n"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"id": "build_diff_ints",
|
|
124
|
+
"symbols": [
|
|
125
|
+
"build_diff_ints"
|
|
126
|
+
],
|
|
127
|
+
"requires": [
|
|
128
|
+
"build_diff_ints_into"
|
|
129
|
+
],
|
|
130
|
+
"code": "#define build_diff_ints(a,n,difference) \\\n build_diff_ints_into((a),(n),(difference),sizeof(difference)/sizeof((difference)[0]))\n"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"id": "diff_to_lls",
|
|
134
|
+
"symbols": [
|
|
135
|
+
"diff_to_lls"
|
|
136
|
+
],
|
|
137
|
+
"requires": [
|
|
138
|
+
"diff_to_lls_into"
|
|
139
|
+
],
|
|
140
|
+
"code": "#define diff_to_lls(difference,n,out) \\\n diff_to_lls_into((difference),(n),(out),sizeof(out)/sizeof((out)[0]))\n"
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
"id": "exam_doubles_finite",
|
|
144
|
+
"symbols": [],
|
|
145
|
+
"requires": [
|
|
146
|
+
"exam_check_n",
|
|
147
|
+
"exam_algo_fail"
|
|
148
|
+
],
|
|
149
|
+
"code": "static inline void exam_doubles_finite(const double *a,int n) {\n exam_check_n(n);\n for(int i=0;i<n;++i)if(!isfinite(a[i]))\n exam_algo_fail(\"double array contains nonfinite value\");\n}\n"
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
"id": "fill_doubles_into",
|
|
153
|
+
"symbols": [
|
|
154
|
+
"fill_doubles_into"
|
|
155
|
+
],
|
|
156
|
+
"requires": [
|
|
157
|
+
"exam_algo_fail"
|
|
158
|
+
],
|
|
159
|
+
"code": "static inline void fill_doubles_into(double *a,int n,double value,\n size_t capacity) {\n if(n<0 || (size_t)n>capacity)\n exam_algo_fail(\"fill_doubles exceeds capacity\");\n for(int i=0;i<n;++i)a[i]=value;\n}\n"
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"id": "copy_doubles_into",
|
|
163
|
+
"symbols": [
|
|
164
|
+
"copy_doubles_into"
|
|
165
|
+
],
|
|
166
|
+
"requires": [
|
|
167
|
+
"exam_algo_fail"
|
|
168
|
+
],
|
|
169
|
+
"code": "static inline void copy_doubles_into(double *dst,const double *src,int n,\n size_t capacity) {\n if(n<0 || (size_t)n>capacity)\n exam_algo_fail(\"copy_doubles exceeds capacity\");\n if(n)memmove(dst,src,(size_t)n*sizeof(double));\n}\n"
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"id": "exam_cmp_double",
|
|
173
|
+
"symbols": [],
|
|
174
|
+
"requires": [],
|
|
175
|
+
"code": "static inline int exam_cmp_double(const void *pa,const void *pb) {\n double a=*(const double *)pa,b=*(const double *)pb;\n return (a>b)-(a<b);\n}\n"
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
"id": "sort_doubles",
|
|
179
|
+
"symbols": [
|
|
180
|
+
"sort_doubles"
|
|
181
|
+
],
|
|
182
|
+
"requires": [
|
|
183
|
+
"exam_doubles_finite",
|
|
184
|
+
"exam_cmp_double"
|
|
185
|
+
],
|
|
186
|
+
"code": "static inline void sort_doubles(double *a,int n) {\n exam_doubles_finite(a,n);\n if(n>1)qsort(a,(size_t)n,sizeof(*a),exam_cmp_double);\n}\n"
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
"id": "reverse_doubles",
|
|
190
|
+
"symbols": [
|
|
191
|
+
"reverse_doubles"
|
|
192
|
+
],
|
|
193
|
+
"requires": [
|
|
194
|
+
"exam_check_n"
|
|
195
|
+
],
|
|
196
|
+
"code": "static inline void reverse_doubles(double *a,int n) {\n exam_check_n(n);\n for(int i=0;i<n/2;++i) {\n double t=a[i];a[i]=a[n-1-i];a[n-1-i]=t;\n }\n}\n"
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
"id": "find_doubles",
|
|
200
|
+
"symbols": [
|
|
201
|
+
"find_doubles"
|
|
202
|
+
],
|
|
203
|
+
"requires": [
|
|
204
|
+
"exam_check_n"
|
|
205
|
+
],
|
|
206
|
+
"code": "static inline int find_doubles(const double *a,int n,double key) {\n exam_check_n(n);\n for(int i=0;i<n;++i)if(a[i]==key)return i;\n return -1;\n}\n"
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
"id": "lower_bound_doubles",
|
|
210
|
+
"symbols": [
|
|
211
|
+
"lower_bound_doubles"
|
|
212
|
+
],
|
|
213
|
+
"requires": [
|
|
214
|
+
"exam_check_n"
|
|
215
|
+
],
|
|
216
|
+
"code": "static inline int lower_bound_doubles(const double *a,int n,double key) {\n exam_check_n(n);\n int l=0,r=n;\n while(l<r) {\n int mid=l+(r-l)/2;\n if(a[mid]<key)l=mid+1;else r=mid;\n }\n return l;\n}\n"
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
"id": "upper_bound_doubles",
|
|
220
|
+
"symbols": [
|
|
221
|
+
"upper_bound_doubles"
|
|
222
|
+
],
|
|
223
|
+
"requires": [
|
|
224
|
+
"exam_check_n"
|
|
225
|
+
],
|
|
226
|
+
"code": "static inline int upper_bound_doubles(const double *a,int n,double key) {\n exam_check_n(n);\n int l=0,r=n;\n while(l<r) {\n int mid=l+(r-l)/2;\n if(a[mid]<=key)l=mid+1;else r=mid;\n }\n return l;\n}\n"
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
"id": "sum_doubles",
|
|
230
|
+
"symbols": [
|
|
231
|
+
"sum_doubles"
|
|
232
|
+
],
|
|
233
|
+
"requires": [
|
|
234
|
+
"exam_doubles_finite",
|
|
235
|
+
"exam_algo_fail"
|
|
236
|
+
],
|
|
237
|
+
"code": "static inline double sum_doubles(const double *a,int n) {\n exam_doubles_finite(a,n);\n long double total=0;\n for(int i=0;i<n;++i)total+=(long double)a[i];\n if(fabsl(total)>DBL_MAX)exam_algo_fail(\"sum_doubles overflow\");\n return (double)total;\n}\n"
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
"id": "mean_doubles",
|
|
241
|
+
"symbols": [
|
|
242
|
+
"mean_doubles"
|
|
243
|
+
],
|
|
244
|
+
"requires": [
|
|
245
|
+
"exam_algo_fail",
|
|
246
|
+
"exam_doubles_finite"
|
|
247
|
+
],
|
|
248
|
+
"code": "static inline double mean_doubles(const double *a,int n) {\n if(n<=0)exam_algo_fail(\"mean_doubles needs nonempty input\");\n exam_doubles_finite(a,n);\n long double total=0;\n for(int i=0;i<n;++i)total+=(long double)a[i];\n return (double)(total/n);\n}\n"
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
"id": "min_doubles",
|
|
252
|
+
"symbols": [
|
|
253
|
+
"min_doubles"
|
|
254
|
+
],
|
|
255
|
+
"requires": [
|
|
256
|
+
"exam_algo_fail",
|
|
257
|
+
"exam_doubles_finite"
|
|
258
|
+
],
|
|
259
|
+
"code": "static inline double min_doubles(const double *a,int n) {\n if(n<=0)exam_algo_fail(\"min_doubles needs nonempty input\");\n exam_doubles_finite(a,n);\n double value=a[0];\n for(int i=1;i<n;++i)if(a[i]<value)value=a[i];\n return value;\n}\n"
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
"id": "max_doubles",
|
|
263
|
+
"symbols": [
|
|
264
|
+
"max_doubles"
|
|
265
|
+
],
|
|
266
|
+
"requires": [
|
|
267
|
+
"exam_algo_fail",
|
|
268
|
+
"exam_doubles_finite"
|
|
269
|
+
],
|
|
270
|
+
"code": "static inline double max_doubles(const double *a,int n) {\n if(n<=0)exam_algo_fail(\"max_doubles needs nonempty input\");\n exam_doubles_finite(a,n);\n double value=a[0];\n for(int i=1;i<n;++i)if(a[i]>value)value=a[i];\n return value;\n}\n"
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
"id": "fill_doubles",
|
|
274
|
+
"symbols": [
|
|
275
|
+
"fill_doubles"
|
|
276
|
+
],
|
|
277
|
+
"requires": [
|
|
278
|
+
"fill_doubles_into"
|
|
279
|
+
],
|
|
280
|
+
"code": "#define fill_doubles(a,n,value) fill_doubles_into((a),(n),(value),sizeof(a)/sizeof((a)[0]))\n"
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
"id": "copy_doubles",
|
|
284
|
+
"symbols": [
|
|
285
|
+
"copy_doubles"
|
|
286
|
+
],
|
|
287
|
+
"requires": [
|
|
288
|
+
"copy_doubles_into"
|
|
289
|
+
],
|
|
290
|
+
"code": "#define copy_doubles(a,src,n) copy_doubles_into((a),(src),(n),sizeof(a)/sizeof((a)[0]))\n"
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
"id": "values_doubles_into",
|
|
294
|
+
"symbols": [
|
|
295
|
+
"values_doubles_into"
|
|
296
|
+
],
|
|
297
|
+
"requires": [
|
|
298
|
+
"exam_algo_fail"
|
|
299
|
+
],
|
|
300
|
+
"code": "static inline int values_doubles_into(double *destination,\n const double *items, size_t count,\n size_t capacity) {\n if (count > (size_t)INT_MAX || count > capacity ||\n (count && (!destination || !items)))\n exam_algo_fail(\"values_doubles output capacity too small\");\n if (count) memmove(destination, items, count * sizeof(double));\n return (int)count;\n}\n"
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
"id": "exam_cmp_double_desc",
|
|
304
|
+
"symbols": [],
|
|
305
|
+
"requires": [],
|
|
306
|
+
"code": "static inline int exam_cmp_double_desc(const void *pa, const void *pb) {\n double a = *(const double *)pa, b = *(const double *)pb;\n return (a < b) - (a > b);\n}\n"
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
"id": "sort_doubles_desc",
|
|
310
|
+
"symbols": [
|
|
311
|
+
"sort_doubles_desc"
|
|
312
|
+
],
|
|
313
|
+
"requires": [
|
|
314
|
+
"exam_doubles_finite",
|
|
315
|
+
"exam_cmp_double_desc"
|
|
316
|
+
],
|
|
317
|
+
"code": "static inline void sort_doubles_desc(double *a, int n) {\n exam_doubles_finite(a, n);\n if (n > 1) qsort(a, (size_t)n, sizeof(*a), exam_cmp_double_desc);\n}\n"
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
"id": "count_doubles",
|
|
321
|
+
"symbols": [
|
|
322
|
+
"count_doubles"
|
|
323
|
+
],
|
|
324
|
+
"requires": [
|
|
325
|
+
"exam_check_n"
|
|
326
|
+
],
|
|
327
|
+
"code": "static inline int count_doubles(const double *a, int n, double key) {\n exam_check_n(n);\n int count = 0;\n for (int i = 0; i < n; ++i) if (a[i] == key) ++count;\n return count;\n}\n"
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
"id": "binary_search_doubles",
|
|
331
|
+
"symbols": [
|
|
332
|
+
"binary_search_doubles"
|
|
333
|
+
],
|
|
334
|
+
"requires": [
|
|
335
|
+
"lower_bound_doubles"
|
|
336
|
+
],
|
|
337
|
+
"code": "static inline int binary_search_doubles(const double *a, int n, double key) {\n int pos = lower_bound_doubles(a, n, key);\n return pos < n && a[pos] == key ? pos : -1;\n}\n"
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
"id": "argmin_doubles",
|
|
341
|
+
"symbols": [
|
|
342
|
+
"argmin_doubles"
|
|
343
|
+
],
|
|
344
|
+
"requires": [
|
|
345
|
+
"exam_algo_fail",
|
|
346
|
+
"exam_doubles_finite"
|
|
347
|
+
],
|
|
348
|
+
"code": "static inline int argmin_doubles(const double *a, int n) {\n if (n <= 0) exam_algo_fail(\"argmin_doubles requires nonempty array\");\n exam_doubles_finite(a, n);\n int result = 0;\n for (int i = 1; i < n; ++i) if (a[i] < a[result]) result = i;\n return result;\n}\n"
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
"id": "argmax_doubles",
|
|
352
|
+
"symbols": [
|
|
353
|
+
"argmax_doubles"
|
|
354
|
+
],
|
|
355
|
+
"requires": [
|
|
356
|
+
"exam_algo_fail",
|
|
357
|
+
"exam_doubles_finite"
|
|
358
|
+
],
|
|
359
|
+
"code": "static inline int argmax_doubles(const double *a, int n) {\n if (n <= 0) exam_algo_fail(\"argmax_doubles requires nonempty array\");\n exam_doubles_finite(a, n);\n int result = 0;\n for (int i = 1; i < n; ++i) if (a[i] > a[result]) result = i;\n return result;\n}\n"
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
"id": "variance_doubles",
|
|
363
|
+
"symbols": [
|
|
364
|
+
"variance_doubles"
|
|
365
|
+
],
|
|
366
|
+
"requires": [
|
|
367
|
+
"exam_algo_fail",
|
|
368
|
+
"exam_doubles_finite"
|
|
369
|
+
],
|
|
370
|
+
"code": "static inline double variance_doubles(const double *a, int n) {\n if (n <= 0) exam_algo_fail(\"variance_doubles requires nonempty array\");\n exam_doubles_finite(a, n);\n long double total = 0;\n for (int i = 0; i < n; ++i) total += (long double)a[i];\n long double mean = total / n, squared = 0;\n for (int i = 0; i < n; ++i) {\n long double delta = (long double)a[i] - mean;\n squared += delta * delta;\n }\n double result = (double)(squared / n);\n if (!isfinite(result)) exam_algo_fail(\"variance_doubles overflow\");\n return result;\n}\n"
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
"id": "stddev_doubles",
|
|
374
|
+
"symbols": [
|
|
375
|
+
"stddev_doubles"
|
|
376
|
+
],
|
|
377
|
+
"requires": [
|
|
378
|
+
"variance_doubles"
|
|
379
|
+
],
|
|
380
|
+
"code": "static inline double stddev_doubles(const double *a, int n) {\n return sqrt(variance_doubles(a, n));\n}\n"
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
"id": "median_doubles",
|
|
384
|
+
"symbols": [
|
|
385
|
+
"median_doubles"
|
|
386
|
+
],
|
|
387
|
+
"requires": [
|
|
388
|
+
"exam_algo_fail",
|
|
389
|
+
"exam_doubles_finite",
|
|
390
|
+
"exam_cmp_double"
|
|
391
|
+
],
|
|
392
|
+
"code": "static inline double median_doubles(const double *a, int n) {\n if (n <= 0) exam_algo_fail(\"median_doubles requires nonempty array\");\n exam_doubles_finite(a, n);\n if ((size_t)n > SIZE_MAX / sizeof(double))\n exam_algo_fail(\"median_doubles allocation overflow\");\n double *temp = (double *)malloc((size_t)n * sizeof(*temp));\n if (!temp) exam_algo_fail(\"median_doubles allocation failed\");\n memcpy(temp, a, (size_t)n * sizeof(*temp));\n qsort(temp, (size_t)n, sizeof(*temp), exam_cmp_double);\n double result = (n & 1) ? temp[n / 2]\n : (double)(((long double)temp[n / 2 - 1] +\n (long double)temp[n / 2]) / 2);\n free(temp);\n return result;\n}\n"
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
"id": "unique_sorted_doubles",
|
|
396
|
+
"symbols": [
|
|
397
|
+
"unique_sorted_doubles"
|
|
398
|
+
],
|
|
399
|
+
"requires": [
|
|
400
|
+
"exam_doubles_finite"
|
|
401
|
+
],
|
|
402
|
+
"code": "static inline int unique_sorted_doubles(double *a, int n) {\n exam_doubles_finite(a, n);\n if (n == 0) return 0;\n int count = 1;\n for (int i = 1; i < n; ++i)\n if (a[i] != a[count - 1]) a[count++] = a[i];\n return count;\n}\n"
|
|
403
|
+
},
|
|
404
|
+
{
|
|
405
|
+
"id": "unique_doubles",
|
|
406
|
+
"symbols": [
|
|
407
|
+
"unique_doubles"
|
|
408
|
+
],
|
|
409
|
+
"requires": [
|
|
410
|
+
"exam_doubles_finite"
|
|
411
|
+
],
|
|
412
|
+
"code": "static inline int unique_doubles(double *a, int n) {\n exam_doubles_finite(a, n);\n int count = 0;\n for (int i = 0; i < n; ++i) {\n int j = 0;\n while (j < count && a[j] != a[i]) ++j;\n if (j == count) a[count++] = a[i];\n }\n return count;\n}\n"
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
"id": "values_doubles",
|
|
416
|
+
"symbols": [
|
|
417
|
+
"values_doubles"
|
|
418
|
+
],
|
|
419
|
+
"requires": [
|
|
420
|
+
"values_doubles_into"
|
|
421
|
+
],
|
|
422
|
+
"code": "#define values_doubles(array, ...) \\\n values_doubles_into((array), (const double[]){__VA_ARGS__}, \\\n sizeof((const double[]){__VA_ARGS__}) / sizeof(double), \\\n sizeof(array) / sizeof((array)[0]))\n"
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
"id": "frequency_ints_into",
|
|
426
|
+
"symbols": [
|
|
427
|
+
"frequency_ints_into"
|
|
428
|
+
],
|
|
429
|
+
"requires": [
|
|
430
|
+
"exam_algo_fail"
|
|
431
|
+
],
|
|
432
|
+
"code": "static inline void frequency_ints_into(const int *values, int n,\n int *frequency, int max_value,\n size_t capacity) {\n if(n<0 || max_value<0 || (size_t)max_value>=capacity)\n exam_algo_fail(\"frequency_ints exceeds destination capacity\");\n for(int x=0;x<=max_value;++x)frequency[x]=0;\n for(int i=0;i<n;++i) {\n int x=values[i];\n if(x<0 || x>max_value)\n exam_algo_fail(\"frequency_ints input outside nonnegative range\");\n if(frequency[x]==INT_MAX)\n exam_algo_fail(\"frequency_ints counter overflow\");\n ++frequency[x];\n }\n}\n"
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
"id": "frequency_ints",
|
|
436
|
+
"symbols": [
|
|
437
|
+
"frequency_ints"
|
|
438
|
+
],
|
|
439
|
+
"requires": [
|
|
440
|
+
"frequency_ints_into"
|
|
441
|
+
],
|
|
442
|
+
"code": "#define frequency_ints(values,n,frequency,max_value) \\\n frequency_ints_into((values),(n),(frequency),(max_value), \\\n sizeof(frequency)/sizeof((frequency)[0]))\n"
|
|
443
|
+
},
|
|
444
|
+
{
|
|
445
|
+
"id": "fill_ints_into",
|
|
446
|
+
"symbols": [
|
|
447
|
+
"fill_ints_into"
|
|
448
|
+
],
|
|
449
|
+
"requires": [
|
|
450
|
+
"exam_algo_fail"
|
|
451
|
+
],
|
|
452
|
+
"code": "static inline void fill_ints_into(int *array, int n, int value, size_t capacity) {\n if (n < 0 || (size_t)n > capacity || (n && !array))\n exam_algo_fail(\"fill_ints exceeds array capacity\");\n for (int i = 0; i < n; ++i) array[i] = value;\n}\n"
|
|
453
|
+
},
|
|
454
|
+
{
|
|
455
|
+
"id": "copy_ints_into",
|
|
456
|
+
"symbols": [
|
|
457
|
+
"copy_ints_into"
|
|
458
|
+
],
|
|
459
|
+
"requires": [
|
|
460
|
+
"exam_algo_fail"
|
|
461
|
+
],
|
|
462
|
+
"code": "static inline void copy_ints_into(int *destination, const int *source,\n int n, size_t capacity) {\n if (n < 0 || (size_t)n > capacity || (n && (!destination || !source)))\n exam_algo_fail(\"copy_ints exceeds destination capacity\");\n if (n) memmove(destination, source, (size_t)n * sizeof(int));\n}\n"
|
|
463
|
+
},
|
|
464
|
+
{
|
|
465
|
+
"id": "values_ints_into",
|
|
466
|
+
"symbols": [
|
|
467
|
+
"values_ints_into"
|
|
468
|
+
],
|
|
469
|
+
"requires": [
|
|
470
|
+
"exam_algo_fail"
|
|
471
|
+
],
|
|
472
|
+
"code": "static inline int values_ints_into(int *destination, const int *values,\n size_t count, size_t capacity) {\n if (count > capacity || count > INT_MAX)\n exam_algo_fail(\"values_ints exceeds destination capacity\");\n if (count) memmove(destination, values, count * sizeof(int));\n return (int)count;\n}\n"
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
"id": "range_ints_into",
|
|
476
|
+
"symbols": [
|
|
477
|
+
"range_ints_into"
|
|
478
|
+
],
|
|
479
|
+
"requires": [
|
|
480
|
+
"exam_algo_fail"
|
|
481
|
+
],
|
|
482
|
+
"code": "static inline int range_ints_into(int *destination, int first, int stop,\n int step, size_t capacity) {\n if (!step) exam_algo_fail(\"range_ints step must not be zero\");\n int count = 0;\n long long current = first;\n while ((step > 0 && current < stop) ||\n (step < 0 && current > stop)) {\n if ((size_t)count >= capacity || count == INT_MAX)\n exam_algo_fail(\"range_ints exceeds array capacity\");\n destination[count++] = (int)current;\n current += step;\n }\n return count;\n}\n"
|
|
483
|
+
},
|
|
484
|
+
{
|
|
485
|
+
"id": "reverse_ints",
|
|
486
|
+
"symbols": [
|
|
487
|
+
"reverse_ints"
|
|
488
|
+
],
|
|
489
|
+
"requires": [
|
|
490
|
+
"exam_check_n"
|
|
491
|
+
],
|
|
492
|
+
"code": "static inline void reverse_ints(int *array, int n) {\n exam_check_n(n);\n for (int i = 0; i < n / 2; ++i) {\n int temp = array[i];\n array[i] = array[n-1-i];\n array[n-1-i] = temp;\n }\n}\n"
|
|
493
|
+
},
|
|
494
|
+
{
|
|
495
|
+
"id": "swap_ints",
|
|
496
|
+
"symbols": [
|
|
497
|
+
"swap_ints"
|
|
498
|
+
],
|
|
499
|
+
"requires": [
|
|
500
|
+
"exam_algo_fail"
|
|
501
|
+
],
|
|
502
|
+
"code": "static inline void swap_ints(int *array, int i, int j) {\n if (i < 0 || j < 0) exam_algo_fail(\"negative array index\");\n int temp = array[i]; array[i] = array[j]; array[j] = temp;\n}\n"
|
|
503
|
+
},
|
|
504
|
+
{
|
|
505
|
+
"id": "fill_ints",
|
|
506
|
+
"symbols": [
|
|
507
|
+
"fill_ints"
|
|
508
|
+
],
|
|
509
|
+
"requires": [
|
|
510
|
+
"fill_ints_into"
|
|
511
|
+
],
|
|
512
|
+
"code": "#define fill_ints(array, n, value) \\\n fill_ints_into((array), (n), (value), sizeof(array)/sizeof((array)[0]))\n"
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
"id": "copy_ints",
|
|
516
|
+
"symbols": [
|
|
517
|
+
"copy_ints"
|
|
518
|
+
],
|
|
519
|
+
"requires": [
|
|
520
|
+
"copy_ints_into"
|
|
521
|
+
],
|
|
522
|
+
"code": "#define copy_ints(array, source, n) \\\n copy_ints_into((array), (source), (n), sizeof(array)/sizeof((array)[0]))\n"
|
|
523
|
+
},
|
|
524
|
+
{
|
|
525
|
+
"id": "values_ints",
|
|
526
|
+
"symbols": [
|
|
527
|
+
"values_ints"
|
|
528
|
+
],
|
|
529
|
+
"requires": [
|
|
530
|
+
"values_ints_into"
|
|
531
|
+
],
|
|
532
|
+
"code": "#define values_ints(array, ...) \\\n values_ints_into((array), (const int[]){__VA_ARGS__}, \\\n sizeof((const int[]){__VA_ARGS__})/sizeof(int), sizeof(array)/sizeof((array)[0]))\n"
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
"id": "range_ints",
|
|
536
|
+
"symbols": [
|
|
537
|
+
"range_ints"
|
|
538
|
+
],
|
|
539
|
+
"requires": [
|
|
540
|
+
"range_ints_into"
|
|
541
|
+
],
|
|
542
|
+
"code": "#define range_ints(array, first, stop, step) \\\n range_ints_into((array), (first), (stop), (step), sizeof(array)/sizeof((array)[0]))\n"
|
|
543
|
+
},
|
|
544
|
+
{
|
|
545
|
+
"id": "linear_search_ints",
|
|
546
|
+
"symbols": [
|
|
547
|
+
"linear_search_ints"
|
|
548
|
+
],
|
|
549
|
+
"requires": [
|
|
550
|
+
"exam_check_n"
|
|
551
|
+
],
|
|
552
|
+
"code": "static inline int linear_search_ints(const int *a, int n, int key) {\n exam_check_n(n);\n for (int i = 0; i < n; ++i) if (a[i] == key) return i;\n return -1;\n}\n"
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
"id": "find_ints",
|
|
556
|
+
"symbols": [
|
|
557
|
+
"find_ints"
|
|
558
|
+
],
|
|
559
|
+
"requires": [
|
|
560
|
+
"linear_search_ints"
|
|
561
|
+
],
|
|
562
|
+
"code": "static inline int find_ints(const int *a, int n, int key) {\n return linear_search_ints(a, n, key);\n}\n"
|
|
563
|
+
},
|
|
564
|
+
{
|
|
565
|
+
"id": "lower_bound_ints",
|
|
566
|
+
"symbols": [
|
|
567
|
+
"lower_bound_ints"
|
|
568
|
+
],
|
|
569
|
+
"requires": [
|
|
570
|
+
"exam_check_n"
|
|
571
|
+
],
|
|
572
|
+
"code": "static inline int lower_bound_ints(const int *a, int n, int key) {\n exam_check_n(n);\n int left = 0, right = n;\n while (left < right) {\n int mid = left + (right-left)/2;\n if (a[mid] < key) left = mid+1;\n else right = mid;\n }\n return left;\n}\n"
|
|
573
|
+
},
|
|
574
|
+
{
|
|
575
|
+
"id": "upper_bound_ints",
|
|
576
|
+
"symbols": [
|
|
577
|
+
"upper_bound_ints"
|
|
578
|
+
],
|
|
579
|
+
"requires": [
|
|
580
|
+
"exam_check_n"
|
|
581
|
+
],
|
|
582
|
+
"code": "static inline int upper_bound_ints(const int *a, int n, int key) {\n exam_check_n(n);\n int left = 0, right = n;\n while (left < right) {\n int mid = left + (right-left)/2;\n if (a[mid] <= key) left = mid+1;\n else right = mid;\n }\n return left;\n}\n"
|
|
583
|
+
},
|
|
584
|
+
{
|
|
585
|
+
"id": "binary_search_ints",
|
|
586
|
+
"symbols": [
|
|
587
|
+
"binary_search_ints"
|
|
588
|
+
],
|
|
589
|
+
"requires": [
|
|
590
|
+
"lower_bound_ints"
|
|
591
|
+
],
|
|
592
|
+
"code": "static inline int binary_search_ints(const int *a, int n, int key) {\n int i = lower_bound_ints(a, n, key);\n return i < n && a[i] == key ? i : -1;\n}\n"
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
"id": "count_ints",
|
|
596
|
+
"symbols": [
|
|
597
|
+
"count_ints"
|
|
598
|
+
],
|
|
599
|
+
"requires": [
|
|
600
|
+
"exam_check_n"
|
|
601
|
+
],
|
|
602
|
+
"code": "static inline int count_ints(const int *a, int n, int key) {\n exam_check_n(n);\n int count = 0;\n for (int i = 0; i < n; ++i) if (a[i] == key) ++count;\n return count;\n}\n"
|
|
603
|
+
},
|
|
604
|
+
{
|
|
605
|
+
"id": "max_subarray_sum_ints",
|
|
606
|
+
"symbols": [
|
|
607
|
+
"max_subarray_sum_ints"
|
|
608
|
+
],
|
|
609
|
+
"requires": [
|
|
610
|
+
"exam_algo_fail"
|
|
611
|
+
],
|
|
612
|
+
"code": "static inline long long max_subarray_sum_ints(const int *a, int n) {\n if (n <= 0) exam_algo_fail(\"max_subarray_sum_ints requires nonempty array\");\n long long best = a[0], running = a[0];\n for (int i = 1; i < n; ++i) {\n running = running > 0 ? running + a[i] : a[i];\n if (running > best) best = running;\n }\n return best;\n}\n"
|
|
613
|
+
},
|
|
614
|
+
{
|
|
615
|
+
"id": "longest_equal_run_ints",
|
|
616
|
+
"symbols": [
|
|
617
|
+
"longest_equal_run_ints"
|
|
618
|
+
],
|
|
619
|
+
"requires": [
|
|
620
|
+
"exam_check_n"
|
|
621
|
+
],
|
|
622
|
+
"code": "static inline int longest_equal_run_ints(const int *a, int n) {\n exam_check_n(n);\n if (n == 0) return 0;\n int best = 1, run = 1;\n for (int i = 1; i < n; ++i) {\n run = a[i] == a[i-1] ? run+1 : 1;\n if (run > best) best = run;\n }\n return best;\n}\n"
|
|
623
|
+
},
|
|
624
|
+
{
|
|
625
|
+
"id": "longest_plus_one_run_ints",
|
|
626
|
+
"symbols": [
|
|
627
|
+
"longest_plus_one_run_ints"
|
|
628
|
+
],
|
|
629
|
+
"requires": [
|
|
630
|
+
"exam_check_n"
|
|
631
|
+
],
|
|
632
|
+
"code": "static inline int longest_plus_one_run_ints(const int *a, int n) {\n exam_check_n(n);\n if (n == 0) return 0;\n int best = 1, run = 1;\n for (int i = 1; i < n; ++i) {\n run = (long long)a[i] == (long long)a[i-1] + 1 ? run+1 : 1;\n if (run > best) best = run;\n }\n return best;\n}\n"
|
|
633
|
+
},
|
|
634
|
+
{
|
|
635
|
+
"id": "max_forward_diff_ints",
|
|
636
|
+
"symbols": [
|
|
637
|
+
"max_forward_diff_ints"
|
|
638
|
+
],
|
|
639
|
+
"requires": [
|
|
640
|
+
"exam_algo_fail"
|
|
641
|
+
],
|
|
642
|
+
"code": "static inline long long max_forward_diff_ints(const int *a, int n) {\n if (n < 2) exam_algo_fail(\"max_forward_diff_ints requires at least 2 values\");\n int max_before = a[0];\n long long best = (long long)a[0] - a[1];\n for (int i = 1; i < n; ++i) {\n long long diff = (long long)max_before - a[i];\n if (diff > best) best = diff;\n if (a[i] > max_before) max_before = a[i];\n }\n return best;\n}\n"
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
"id": "exam_cmp_int_asc",
|
|
646
|
+
"symbols": [],
|
|
647
|
+
"requires": [],
|
|
648
|
+
"code": "static inline int exam_cmp_int_asc(const void *a, const void *b) {\n int x = *(const int *)a, y = *(const int *)b;\n return (x > y) - (x < y);\n}\n"
|
|
649
|
+
},
|
|
650
|
+
{
|
|
651
|
+
"id": "exam_cmp_int_desc",
|
|
652
|
+
"symbols": [],
|
|
653
|
+
"requires": [],
|
|
654
|
+
"code": "static inline int exam_cmp_int_desc(const void *a, const void *b) {\n int x = *(const int *)a, y = *(const int *)b;\n return (x < y) - (x > y);\n}\n"
|
|
655
|
+
},
|
|
656
|
+
{
|
|
657
|
+
"id": "sort_ints",
|
|
658
|
+
"symbols": [
|
|
659
|
+
"sort_ints"
|
|
660
|
+
],
|
|
661
|
+
"requires": [
|
|
662
|
+
"exam_check_n",
|
|
663
|
+
"exam_cmp_int_asc"
|
|
664
|
+
],
|
|
665
|
+
"code": "static inline void sort_ints(int *a, int n) {\n exam_check_n(n);\n if (n > 1) qsort(a, (size_t)n, sizeof(*a), exam_cmp_int_asc);\n}\n"
|
|
666
|
+
},
|
|
667
|
+
{
|
|
668
|
+
"id": "sort_ints_desc",
|
|
669
|
+
"symbols": [
|
|
670
|
+
"sort_ints_desc"
|
|
671
|
+
],
|
|
672
|
+
"requires": [
|
|
673
|
+
"exam_check_n",
|
|
674
|
+
"exam_cmp_int_desc"
|
|
675
|
+
],
|
|
676
|
+
"code": "static inline void sort_ints_desc(int *a, int n) {\n exam_check_n(n);\n if (n > 1) qsort(a, (size_t)n, sizeof(*a), exam_cmp_int_desc);\n}\n"
|
|
677
|
+
},
|
|
678
|
+
{
|
|
679
|
+
"id": "bubble_sort_ints",
|
|
680
|
+
"symbols": [
|
|
681
|
+
"bubble_sort_ints"
|
|
682
|
+
],
|
|
683
|
+
"requires": [
|
|
684
|
+
"exam_check_n"
|
|
685
|
+
],
|
|
686
|
+
"code": "static inline void bubble_sort_ints(int *a, int n) {\n exam_check_n(n);\n for (int end = n; end > 1; --end) {\n bool changed = false;\n for (int j = 1; j < end; ++j) if (a[j-1] > a[j]) {\n int temp = a[j-1]; a[j-1] = a[j]; a[j] = temp;\n changed = true;\n }\n if (!changed) break;\n }\n}\n"
|
|
687
|
+
},
|
|
688
|
+
{
|
|
689
|
+
"id": "unique_sorted_ints",
|
|
690
|
+
"symbols": [
|
|
691
|
+
"unique_sorted_ints"
|
|
692
|
+
],
|
|
693
|
+
"requires": [
|
|
694
|
+
"exam_check_n"
|
|
695
|
+
],
|
|
696
|
+
"code": "static inline int unique_sorted_ints(int *a, int n) {\n exam_check_n(n);\n if (!n) return 0;\n int write = 1;\n for (int i = 1; i < n; ++i)\n if (a[i] != a[write-1]) a[write++] = a[i];\n return write;\n}\n"
|
|
697
|
+
},
|
|
698
|
+
{
|
|
699
|
+
"id": "unique_ints",
|
|
700
|
+
"symbols": [
|
|
701
|
+
"unique_ints"
|
|
702
|
+
],
|
|
703
|
+
"requires": [
|
|
704
|
+
"exam_check_n"
|
|
705
|
+
],
|
|
706
|
+
"code": "static inline int unique_ints(int *a, int n) {\n exam_check_n(n);\n int write = 0;\n for (int i = 0; i < n; ++i) {\n bool found = false;\n for (int j = 0; j < write; ++j)\n if (a[j] == a[i]) { found = true; break; }\n if (!found) a[write++] = a[i];\n }\n return write;\n}\n"
|
|
707
|
+
},
|
|
708
|
+
{
|
|
709
|
+
"id": "sum_ints",
|
|
710
|
+
"symbols": [
|
|
711
|
+
"sum_ints"
|
|
712
|
+
],
|
|
713
|
+
"requires": [
|
|
714
|
+
"exam_check_n"
|
|
715
|
+
],
|
|
716
|
+
"code": "static inline long long sum_ints(const int *a, int n) {\n exam_check_n(n);\n long long total = 0;\n for (int i = 0; i < n; ++i) total += a[i];\n return total;\n}\n"
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
"id": "min_ints",
|
|
720
|
+
"symbols": [
|
|
721
|
+
"min_ints"
|
|
722
|
+
],
|
|
723
|
+
"requires": [
|
|
724
|
+
"exam_algo_fail"
|
|
725
|
+
],
|
|
726
|
+
"code": "static inline int min_ints(const int *a, int n) {\n if (n <= 0) exam_algo_fail(\"min_ints requires nonempty array\");\n int value = a[0];\n for (int i = 1; i < n; ++i) if (a[i] < value) value = a[i];\n return value;\n}\n"
|
|
727
|
+
},
|
|
728
|
+
{
|
|
729
|
+
"id": "max_ints",
|
|
730
|
+
"symbols": [
|
|
731
|
+
"max_ints"
|
|
732
|
+
],
|
|
733
|
+
"requires": [
|
|
734
|
+
"exam_algo_fail"
|
|
735
|
+
],
|
|
736
|
+
"code": "static inline int max_ints(const int *a, int n) {\n if (n <= 0) exam_algo_fail(\"max_ints requires nonempty array\");\n int value = a[0];\n for (int i = 1; i < n; ++i) if (a[i] > value) value = a[i];\n return value;\n}\n"
|
|
737
|
+
},
|
|
738
|
+
{
|
|
739
|
+
"id": "argmin_ints",
|
|
740
|
+
"symbols": [
|
|
741
|
+
"argmin_ints"
|
|
742
|
+
],
|
|
743
|
+
"requires": [
|
|
744
|
+
"exam_algo_fail"
|
|
745
|
+
],
|
|
746
|
+
"code": "static inline int argmin_ints(const int *a, int n) {\n if (n <= 0) exam_algo_fail(\"argmin_ints requires nonempty array\");\n int pos = 0;\n for (int i = 1; i < n; ++i) if (a[i] < a[pos]) pos = i;\n return pos;\n}\n"
|
|
747
|
+
},
|
|
748
|
+
{
|
|
749
|
+
"id": "argmax_ints",
|
|
750
|
+
"symbols": [
|
|
751
|
+
"argmax_ints"
|
|
752
|
+
],
|
|
753
|
+
"requires": [
|
|
754
|
+
"exam_algo_fail"
|
|
755
|
+
],
|
|
756
|
+
"code": "static inline int argmax_ints(const int *a, int n) {\n if (n <= 0) exam_algo_fail(\"argmax_ints requires nonempty array\");\n int pos = 0;\n for (int i = 1; i < n; ++i) if (a[i] > a[pos]) pos = i;\n return pos;\n}\n"
|
|
757
|
+
},
|
|
758
|
+
{
|
|
759
|
+
"id": "mean_ints",
|
|
760
|
+
"symbols": [
|
|
761
|
+
"mean_ints"
|
|
762
|
+
],
|
|
763
|
+
"requires": [
|
|
764
|
+
"exam_algo_fail",
|
|
765
|
+
"sum_ints"
|
|
766
|
+
],
|
|
767
|
+
"code": "static inline double mean_ints(const int *a, int n) {\n if (n <= 0) exam_algo_fail(\"mean_ints requires nonempty array\");\n return (double)sum_ints(a, n) / n;\n}\n"
|
|
768
|
+
},
|
|
769
|
+
{
|
|
770
|
+
"id": "variance_ints",
|
|
771
|
+
"symbols": [
|
|
772
|
+
"variance_ints"
|
|
773
|
+
],
|
|
774
|
+
"requires": [
|
|
775
|
+
"exam_algo_fail",
|
|
776
|
+
"sum_ints"
|
|
777
|
+
],
|
|
778
|
+
"code": "static inline double variance_ints(const int *a, int n) {\n if (n <= 0) exam_algo_fail(\"variance_ints requires nonempty array\");\n long double mean = (long double)sum_ints(a, n)/n;\n long double sum = 0;\n for (int i = 0; i < n; ++i) {\n long double d = (long double)a[i] - mean;\n sum += d*d;\n }\n return (double)(sum/n);\n}\n"
|
|
779
|
+
},
|
|
780
|
+
{
|
|
781
|
+
"id": "stddev_ints",
|
|
782
|
+
"symbols": [
|
|
783
|
+
"stddev_ints"
|
|
784
|
+
],
|
|
785
|
+
"requires": [
|
|
786
|
+
"variance_ints"
|
|
787
|
+
],
|
|
788
|
+
"code": "static inline double stddev_ints(const int *a, int n) {\n return sqrt(variance_ints(a, n));\n}\n"
|
|
789
|
+
},
|
|
790
|
+
{
|
|
791
|
+
"id": "median_ints",
|
|
792
|
+
"symbols": [
|
|
793
|
+
"median_ints"
|
|
794
|
+
],
|
|
795
|
+
"requires": [
|
|
796
|
+
"exam_algo_fail",
|
|
797
|
+
"exam_cmp_int_asc"
|
|
798
|
+
],
|
|
799
|
+
"code": "static inline double median_ints(const int *a, int n) {\n if (n <= 0) exam_algo_fail(\"median_ints requires nonempty array\");\n if ((size_t)n > SIZE_MAX / sizeof(int))\n exam_algo_fail(\"median_ints capacity overflow\");\n int *copy = (int *)malloc((size_t)n*sizeof(int));\n if (!copy) exam_algo_fail(\"median_ints allocation failed\");\n memcpy(copy, a, (size_t)n*sizeof(int));\n qsort(copy, (size_t)n, sizeof(int), exam_cmp_int_asc);\n double result;\n if (n & 1) result = copy[n/2];\n else result = ((double)copy[n/2-1] + (double)copy[n/2])/2.0;\n free(copy);\n return result;\n}\n"
|
|
800
|
+
},
|
|
801
|
+
{
|
|
802
|
+
"id": "fill_lls_into",
|
|
803
|
+
"symbols": [
|
|
804
|
+
"fill_lls_into"
|
|
805
|
+
],
|
|
806
|
+
"requires": [
|
|
807
|
+
"exam_algo_fail"
|
|
808
|
+
],
|
|
809
|
+
"code": "static inline void fill_lls_into(long long *a, int n, long long value,\n size_t capacity) {\n if (n<0 || (size_t)n>capacity) exam_algo_fail(\"fill_lls exceeds capacity\");\n for(int i=0;i<n;++i)a[i]=value;\n}\n"
|
|
810
|
+
},
|
|
811
|
+
{
|
|
812
|
+
"id": "copy_lls_into",
|
|
813
|
+
"symbols": [
|
|
814
|
+
"copy_lls_into"
|
|
815
|
+
],
|
|
816
|
+
"requires": [
|
|
817
|
+
"exam_algo_fail"
|
|
818
|
+
],
|
|
819
|
+
"code": "static inline void copy_lls_into(long long *dest,const long long *src,int n,\n size_t capacity) {\n if(n<0 || (size_t)n>capacity)exam_algo_fail(\"copy_lls exceeds capacity\");\n if(n)memmove(dest,src,(size_t)n*sizeof(long long));\n}\n"
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
"id": "exam_cmp_ll",
|
|
823
|
+
"symbols": [],
|
|
824
|
+
"requires": [],
|
|
825
|
+
"code": "static inline int exam_cmp_ll(const void *pa,const void *pb) {\n long long a=*(const long long *)pa,b=*(const long long *)pb;\n return (a>b)-(a<b);\n}\n"
|
|
826
|
+
},
|
|
827
|
+
{
|
|
828
|
+
"id": "sort_lls",
|
|
829
|
+
"symbols": [
|
|
830
|
+
"sort_lls"
|
|
831
|
+
],
|
|
832
|
+
"requires": [
|
|
833
|
+
"exam_check_n",
|
|
834
|
+
"exam_cmp_ll"
|
|
835
|
+
],
|
|
836
|
+
"code": "static inline void sort_lls(long long *a,int n) {\n exam_check_n(n);\n if(n>1)qsort(a,(size_t)n,sizeof(*a),exam_cmp_ll);\n}\n"
|
|
837
|
+
},
|
|
838
|
+
{
|
|
839
|
+
"id": "reverse_lls",
|
|
840
|
+
"symbols": [
|
|
841
|
+
"reverse_lls"
|
|
842
|
+
],
|
|
843
|
+
"requires": [
|
|
844
|
+
"exam_check_n"
|
|
845
|
+
],
|
|
846
|
+
"code": "static inline void reverse_lls(long long *a,int n) {\n exam_check_n(n);\n for(int i=0;i<n/2;++i) {\n long long t=a[i];a[i]=a[n-1-i];a[n-1-i]=t;\n }\n}\n"
|
|
847
|
+
},
|
|
848
|
+
{
|
|
849
|
+
"id": "find_lls",
|
|
850
|
+
"symbols": [
|
|
851
|
+
"find_lls"
|
|
852
|
+
],
|
|
853
|
+
"requires": [
|
|
854
|
+
"exam_check_n"
|
|
855
|
+
],
|
|
856
|
+
"code": "static inline int find_lls(const long long *a,int n,long long key) {\n exam_check_n(n);\n for(int i=0;i<n;++i)if(a[i]==key)return i;\n return -1;\n}\n"
|
|
857
|
+
},
|
|
858
|
+
{
|
|
859
|
+
"id": "lower_bound_lls",
|
|
860
|
+
"symbols": [
|
|
861
|
+
"lower_bound_lls"
|
|
862
|
+
],
|
|
863
|
+
"requires": [
|
|
864
|
+
"exam_check_n"
|
|
865
|
+
],
|
|
866
|
+
"code": "static inline int lower_bound_lls(const long long *a,int n,long long key) {\n exam_check_n(n);\n int l=0,r=n;\n while(l<r) {\n int mid=l+(r-l)/2;\n if(a[mid]<key)l=mid+1;else r=mid;\n }\n return l;\n}\n"
|
|
867
|
+
},
|
|
868
|
+
{
|
|
869
|
+
"id": "upper_bound_lls",
|
|
870
|
+
"symbols": [
|
|
871
|
+
"upper_bound_lls"
|
|
872
|
+
],
|
|
873
|
+
"requires": [
|
|
874
|
+
"exam_check_n"
|
|
875
|
+
],
|
|
876
|
+
"code": "static inline int upper_bound_lls(const long long *a,int n,long long key) {\n exam_check_n(n);\n int l=0,r=n;\n while(l<r) {\n int mid=l+(r-l)/2;\n if(a[mid]<=key)l=mid+1;else r=mid;\n }\n return l;\n}\n"
|
|
877
|
+
},
|
|
878
|
+
{
|
|
879
|
+
"id": "sum_lls",
|
|
880
|
+
"symbols": [
|
|
881
|
+
"sum_lls"
|
|
882
|
+
],
|
|
883
|
+
"requires": [
|
|
884
|
+
"exam_check_n",
|
|
885
|
+
"exam_algo_fail"
|
|
886
|
+
],
|
|
887
|
+
"code": "static inline long long sum_lls(const long long *a,int n) {\n exam_check_n(n);\n long long total=0;\n for(int i=0;i<n;++i) {\n if((a[i]>0 && total>LLONG_MAX-a[i]) ||\n (a[i]<0 && total<LLONG_MIN-a[i]))\n exam_algo_fail(\"sum_lls overflow\");\n total+=a[i];\n }\n return total;\n}\n"
|
|
888
|
+
},
|
|
889
|
+
{
|
|
890
|
+
"id": "min_lls",
|
|
891
|
+
"symbols": [
|
|
892
|
+
"min_lls"
|
|
893
|
+
],
|
|
894
|
+
"requires": [
|
|
895
|
+
"exam_algo_fail"
|
|
896
|
+
],
|
|
897
|
+
"code": "static inline long long min_lls(const long long *a,int n) {\n if(n<=0)exam_algo_fail(\"min_lls needs nonempty input\");\n long long value=a[0];\n for(int i=1;i<n;++i)if(a[i]<value)value=a[i];\n return value;\n}\n"
|
|
898
|
+
},
|
|
899
|
+
{
|
|
900
|
+
"id": "max_lls",
|
|
901
|
+
"symbols": [
|
|
902
|
+
"max_lls"
|
|
903
|
+
],
|
|
904
|
+
"requires": [
|
|
905
|
+
"exam_algo_fail"
|
|
906
|
+
],
|
|
907
|
+
"code": "static inline long long max_lls(const long long *a,int n) {\n if(n<=0)exam_algo_fail(\"max_lls needs nonempty input\");\n long long value=a[0];\n for(int i=1;i<n;++i)if(a[i]>value)value=a[i];\n return value;\n}\n"
|
|
908
|
+
},
|
|
909
|
+
{
|
|
910
|
+
"id": "fill_lls",
|
|
911
|
+
"symbols": [
|
|
912
|
+
"fill_lls"
|
|
913
|
+
],
|
|
914
|
+
"requires": [
|
|
915
|
+
"fill_lls_into"
|
|
916
|
+
],
|
|
917
|
+
"code": "#define fill_lls(a,n,value) fill_lls_into((a),(n),(value),sizeof(a)/sizeof((a)[0]))\n"
|
|
918
|
+
},
|
|
919
|
+
{
|
|
920
|
+
"id": "copy_lls",
|
|
921
|
+
"symbols": [
|
|
922
|
+
"copy_lls"
|
|
923
|
+
],
|
|
924
|
+
"requires": [
|
|
925
|
+
"copy_lls_into"
|
|
926
|
+
],
|
|
927
|
+
"code": "#define copy_lls(a,src,n) copy_lls_into((a),(src),(n),sizeof(a)/sizeof((a)[0]))\n"
|
|
928
|
+
},
|
|
929
|
+
{
|
|
930
|
+
"id": "values_lls_into",
|
|
931
|
+
"symbols": [
|
|
932
|
+
"values_lls_into"
|
|
933
|
+
],
|
|
934
|
+
"requires": [
|
|
935
|
+
"exam_algo_fail"
|
|
936
|
+
],
|
|
937
|
+
"code": "static inline int values_lls_into(long long *destination,\n const long long *items, size_t count,\n size_t capacity) {\n if (count > (size_t)INT_MAX || count > capacity ||\n (count && (!destination || !items)))\n exam_algo_fail(\"values_lls output capacity too small\");\n if (count) memmove(destination, items, count * sizeof(long long));\n return (int)count;\n}\n"
|
|
938
|
+
},
|
|
939
|
+
{
|
|
940
|
+
"id": "range_lls_into",
|
|
941
|
+
"symbols": [
|
|
942
|
+
"range_lls_into"
|
|
943
|
+
],
|
|
944
|
+
"requires": [
|
|
945
|
+
"exam_algo_fail"
|
|
946
|
+
],
|
|
947
|
+
"code": "static inline int range_lls_into(long long *destination,\n long long first, long long stop,\n long long step, size_t capacity) {\n if (!step) exam_algo_fail(\"range_lls step must not be zero\");\n int n = 0;\n long long current = first;\n while ((step > 0 && current < stop) ||\n (step < 0 && current > stop)) {\n if ((size_t)n >= capacity || n == INT_MAX || !destination)\n exam_algo_fail(\"range_lls output capacity too small\");\n destination[n++] = current;\n if (step > 0 && current > LLONG_MAX - step) break;\n if (step < 0 && current < LLONG_MIN - step) break;\n current += step;\n }\n return n;\n}\n"
|
|
948
|
+
},
|
|
949
|
+
{
|
|
950
|
+
"id": "exam_cmp_ll_desc",
|
|
951
|
+
"symbols": [],
|
|
952
|
+
"requires": [],
|
|
953
|
+
"code": "static inline int exam_cmp_ll_desc(const void *pa, const void *pb) {\n long long a = *(const long long *)pa, b = *(const long long *)pb;\n return (a < b) - (a > b);\n}\n"
|
|
954
|
+
},
|
|
955
|
+
{
|
|
956
|
+
"id": "sort_lls_desc",
|
|
957
|
+
"symbols": [
|
|
958
|
+
"sort_lls_desc"
|
|
959
|
+
],
|
|
960
|
+
"requires": [
|
|
961
|
+
"exam_check_n",
|
|
962
|
+
"exam_cmp_ll_desc"
|
|
963
|
+
],
|
|
964
|
+
"code": "static inline void sort_lls_desc(long long *a, int n) {\n exam_check_n(n);\n if (n < 2) return;\n /* qsort comparator in descending order, without signed subtraction. */\n qsort(a, (size_t)n, sizeof(*a), exam_cmp_ll_desc);\n}\n"
|
|
965
|
+
},
|
|
966
|
+
{
|
|
967
|
+
"id": "count_lls",
|
|
968
|
+
"symbols": [
|
|
969
|
+
"count_lls"
|
|
970
|
+
],
|
|
971
|
+
"requires": [
|
|
972
|
+
"exam_check_n"
|
|
973
|
+
],
|
|
974
|
+
"code": "static inline int count_lls(const long long *a, int n, long long key) {\n exam_check_n(n);\n int count = 0;\n for (int i = 0; i < n; ++i) if (a[i] == key) ++count;\n return count;\n}\n"
|
|
975
|
+
},
|
|
976
|
+
{
|
|
977
|
+
"id": "binary_search_lls",
|
|
978
|
+
"symbols": [
|
|
979
|
+
"binary_search_lls"
|
|
980
|
+
],
|
|
981
|
+
"requires": [
|
|
982
|
+
"lower_bound_lls"
|
|
983
|
+
],
|
|
984
|
+
"code": "static inline int binary_search_lls(const long long *a, int n,\n long long key) {\n int pos = lower_bound_lls(a, n, key);\n return pos < n && a[pos] == key ? pos : -1;\n}\n"
|
|
985
|
+
},
|
|
986
|
+
{
|
|
987
|
+
"id": "argmin_lls",
|
|
988
|
+
"symbols": [
|
|
989
|
+
"argmin_lls"
|
|
990
|
+
],
|
|
991
|
+
"requires": [
|
|
992
|
+
"exam_algo_fail"
|
|
993
|
+
],
|
|
994
|
+
"code": "static inline int argmin_lls(const long long *a, int n) {\n if (n <= 0) exam_algo_fail(\"argmin_lls requires nonempty array\");\n int result = 0;\n for (int i = 1; i < n; ++i) if (a[i] < a[result]) result = i;\n return result;\n}\n"
|
|
995
|
+
},
|
|
996
|
+
{
|
|
997
|
+
"id": "argmax_lls",
|
|
998
|
+
"symbols": [
|
|
999
|
+
"argmax_lls"
|
|
1000
|
+
],
|
|
1001
|
+
"requires": [
|
|
1002
|
+
"exam_algo_fail"
|
|
1003
|
+
],
|
|
1004
|
+
"code": "static inline int argmax_lls(const long long *a, int n) {\n if (n <= 0) exam_algo_fail(\"argmax_lls requires nonempty array\");\n int result = 0;\n for (int i = 1; i < n; ++i) if (a[i] > a[result]) result = i;\n return result;\n}\n"
|
|
1005
|
+
},
|
|
1006
|
+
{
|
|
1007
|
+
"id": "mean_lls",
|
|
1008
|
+
"symbols": [
|
|
1009
|
+
"mean_lls"
|
|
1010
|
+
],
|
|
1011
|
+
"requires": [
|
|
1012
|
+
"exam_algo_fail"
|
|
1013
|
+
],
|
|
1014
|
+
"code": "static inline double mean_lls(const long long *a, int n) {\n if (n <= 0) exam_algo_fail(\"mean_lls requires nonempty array\");\n long double total = 0;\n for (int i = 0; i < n; ++i) total += (long double)a[i];\n return (double)(total / n);\n}\n"
|
|
1015
|
+
},
|
|
1016
|
+
{
|
|
1017
|
+
"id": "variance_lls",
|
|
1018
|
+
"symbols": [
|
|
1019
|
+
"variance_lls"
|
|
1020
|
+
],
|
|
1021
|
+
"requires": [
|
|
1022
|
+
"exam_algo_fail"
|
|
1023
|
+
],
|
|
1024
|
+
"code": "static inline double variance_lls(const long long *a, int n) {\n if (n <= 0) exam_algo_fail(\"variance_lls requires nonempty array\");\n long double total = 0;\n for (int i = 0; i < n; ++i) total += (long double)a[i];\n long double mean = total / n, squared = 0;\n for (int i = 0; i < n; ++i) {\n long double d = (long double)a[i] - mean;\n squared += d * d;\n }\n return (double)(squared / n);\n}\n"
|
|
1025
|
+
},
|
|
1026
|
+
{
|
|
1027
|
+
"id": "stddev_lls",
|
|
1028
|
+
"symbols": [
|
|
1029
|
+
"stddev_lls"
|
|
1030
|
+
],
|
|
1031
|
+
"requires": [
|
|
1032
|
+
"variance_lls"
|
|
1033
|
+
],
|
|
1034
|
+
"code": "static inline double stddev_lls(const long long *a, int n) {\n return sqrt(variance_lls(a, n));\n}\n"
|
|
1035
|
+
},
|
|
1036
|
+
{
|
|
1037
|
+
"id": "median_lls",
|
|
1038
|
+
"symbols": [
|
|
1039
|
+
"median_lls"
|
|
1040
|
+
],
|
|
1041
|
+
"requires": [
|
|
1042
|
+
"exam_algo_fail",
|
|
1043
|
+
"exam_cmp_ll"
|
|
1044
|
+
],
|
|
1045
|
+
"code": "static inline double median_lls(const long long *a, int n) {\n if (n <= 0) exam_algo_fail(\"median_lls requires nonempty array\");\n if ((size_t)n > SIZE_MAX / sizeof(long long))\n exam_algo_fail(\"median_lls allocation overflow\");\n long long *temp = (long long *)malloc((size_t)n * sizeof(*temp));\n if (!temp) exam_algo_fail(\"median_lls allocation failed\");\n memcpy(temp, a, (size_t)n * sizeof(*temp));\n qsort(temp, (size_t)n, sizeof(*temp), exam_cmp_ll);\n double result = (n & 1) ? (double)temp[n / 2]\n : (double)(((long double)temp[n / 2 - 1] +\n (long double)temp[n / 2]) / 2);\n free(temp);\n return result;\n}\n"
|
|
1046
|
+
},
|
|
1047
|
+
{
|
|
1048
|
+
"id": "unique_sorted_lls",
|
|
1049
|
+
"symbols": [
|
|
1050
|
+
"unique_sorted_lls"
|
|
1051
|
+
],
|
|
1052
|
+
"requires": [
|
|
1053
|
+
"exam_check_n"
|
|
1054
|
+
],
|
|
1055
|
+
"code": "static inline int unique_sorted_lls(long long *a, int n) {\n exam_check_n(n);\n if (n == 0) return 0;\n int count = 1;\n for (int i = 1; i < n; ++i)\n if (a[i] != a[count - 1]) a[count++] = a[i];\n return count;\n}\n"
|
|
1056
|
+
},
|
|
1057
|
+
{
|
|
1058
|
+
"id": "unique_lls",
|
|
1059
|
+
"symbols": [
|
|
1060
|
+
"unique_lls"
|
|
1061
|
+
],
|
|
1062
|
+
"requires": [
|
|
1063
|
+
"exam_check_n"
|
|
1064
|
+
],
|
|
1065
|
+
"code": "static inline int unique_lls(long long *a, int n) {\n exam_check_n(n);\n int count = 0;\n for (int i = 0; i < n; ++i) {\n int j = 0;\n while (j < count && a[j] != a[i]) ++j;\n if (j == count) a[count++] = a[i];\n }\n return count;\n}\n"
|
|
1066
|
+
},
|
|
1067
|
+
{
|
|
1068
|
+
"id": "values_lls",
|
|
1069
|
+
"symbols": [
|
|
1070
|
+
"values_lls"
|
|
1071
|
+
],
|
|
1072
|
+
"requires": [
|
|
1073
|
+
"values_lls_into"
|
|
1074
|
+
],
|
|
1075
|
+
"code": "#define values_lls(array, ...) \\\n values_lls_into((array), (const long long[]){__VA_ARGS__}, \\\n sizeof((const long long[]){__VA_ARGS__}) / sizeof(long long), \\\n sizeof(array) / sizeof((array)[0]))\n"
|
|
1076
|
+
},
|
|
1077
|
+
{
|
|
1078
|
+
"id": "range_lls",
|
|
1079
|
+
"symbols": [
|
|
1080
|
+
"range_lls"
|
|
1081
|
+
],
|
|
1082
|
+
"requires": [
|
|
1083
|
+
"range_lls_into"
|
|
1084
|
+
],
|
|
1085
|
+
"code": "#define range_lls(array, first, stop, step) \\\n range_lls_into((array), (first), (stop), (step), \\\n sizeof(array) / sizeof((array)[0]))\n"
|
|
1086
|
+
},
|
|
1087
|
+
{
|
|
1088
|
+
"id": "exam_failf",
|
|
1089
|
+
"symbols": [],
|
|
1090
|
+
"requires": [],
|
|
1091
|
+
"code": "static inline void exam_failf(const char *group, const char *api,\n const char *format, ...) {\n va_list args;\n fprintf(stderr, \"exam %s: %s: \", group, api);\n va_start(args, format);\n vfprintf(stderr, format, args);\n va_end(args);\n fputc('\\n', stderr);\n exit(EXIT_FAILURE);\n}\n"
|
|
1092
|
+
},
|
|
1093
|
+
{
|
|
1094
|
+
"id": "build_prefix_ints_into",
|
|
1095
|
+
"symbols": [
|
|
1096
|
+
"build_prefix_ints_into"
|
|
1097
|
+
],
|
|
1098
|
+
"requires": [
|
|
1099
|
+
"exam_failf"
|
|
1100
|
+
],
|
|
1101
|
+
"code": "static inline void build_prefix_ints_into(const int *a, int n, long long *prefix,\n size_t capacity) {\n if (n < 0)\n exam_failf(\"algorithm\", \"build_prefix_ints\", \"n=%d out of range (expected n >= 0)\", n);\n if (!prefix || (n && !a))\n exam_failf(\"algorithm\", \"build_prefix_ints\", \"%s must not be NULL\",\n prefix ? \"input\" : \"destination\");\n if ((unsigned long long)n >= capacity)\n exam_failf(\"algorithm\", \"build_prefix_ints\",\n \"capacity=%zu too small (need %llu elements)\", capacity, (unsigned long long)n + 1);\n prefix[0] = 0;\n for (int i = 0; i < n; ++i) prefix[i+1] = prefix[i] + (long long)a[i];\n}\n"
|
|
1102
|
+
},
|
|
1103
|
+
{
|
|
1104
|
+
"id": "prefix_range_sum",
|
|
1105
|
+
"symbols": [
|
|
1106
|
+
"prefix_range_sum"
|
|
1107
|
+
],
|
|
1108
|
+
"requires": [
|
|
1109
|
+
"exam_algo_fail"
|
|
1110
|
+
],
|
|
1111
|
+
"code": "static inline long long prefix_range_sum(const long long *prefix, int begin,\n int end) {\n if (begin < 0 || end < begin) exam_algo_fail(\"invalid prefix range\");\n return prefix[end] - prefix[begin];\n}\n"
|
|
1112
|
+
},
|
|
1113
|
+
{
|
|
1114
|
+
"id": "build_prefix_ints",
|
|
1115
|
+
"symbols": [
|
|
1116
|
+
"build_prefix_ints"
|
|
1117
|
+
],
|
|
1118
|
+
"requires": [
|
|
1119
|
+
"build_prefix_ints_into"
|
|
1120
|
+
],
|
|
1121
|
+
"code": "#define build_prefix_ints(a, n, prefix) \\\n build_prefix_ints_into((a), (n), (prefix), sizeof(prefix)/sizeof((prefix)[0]))\n"
|
|
1122
|
+
},
|
|
1123
|
+
{
|
|
1124
|
+
"id": "EXAM_BIGINT_CAP",
|
|
1125
|
+
"symbols": [],
|
|
1126
|
+
"requires": [],
|
|
1127
|
+
"code": "#ifndef EXAM_BIGINT_CAP\n#define EXAM_BIGINT_CAP 2048\n#endif\n"
|
|
1128
|
+
},
|
|
1129
|
+
{
|
|
1130
|
+
"id": "BigInt",
|
|
1131
|
+
"symbols": [
|
|
1132
|
+
"BigInt"
|
|
1133
|
+
],
|
|
1134
|
+
"requires": [
|
|
1135
|
+
"EXAM_BIGINT_CAP"
|
|
1136
|
+
],
|
|
1137
|
+
"code": "typedef struct {\n int sign; /* -1, 0, +1; 0 is represented by sign=0,len=1,d[0]=0 */\n int len;\n unsigned char d[EXAM_BIGINT_CAP]; /* least significant digit first */\n} BigInt;\n"
|
|
1138
|
+
},
|
|
1139
|
+
{
|
|
1140
|
+
"id": "bigint_zero",
|
|
1141
|
+
"symbols": [
|
|
1142
|
+
"bigint_zero"
|
|
1143
|
+
],
|
|
1144
|
+
"requires": [
|
|
1145
|
+
"BigInt",
|
|
1146
|
+
"exam_algo_fail"
|
|
1147
|
+
],
|
|
1148
|
+
"code": "static inline void bigint_zero(BigInt *out) {\n if (!out) exam_algo_fail(\"null BigInt destination\");\n memset(out, 0, sizeof(*out));\n out->len = 1;\n}\n"
|
|
1149
|
+
},
|
|
1150
|
+
{
|
|
1151
|
+
"id": "exam_bigint_normalize",
|
|
1152
|
+
"symbols": [],
|
|
1153
|
+
"requires": [
|
|
1154
|
+
"BigInt"
|
|
1155
|
+
],
|
|
1156
|
+
"code": "static inline void exam_bigint_normalize(BigInt *a) {\n while (a->len > 1 && !a->d[a->len-1]) --a->len;\n if (a->len == 1 && !a->d[0]) a->sign = 0;\n else if (!a->sign) a->sign = 1;\n}\n"
|
|
1157
|
+
},
|
|
1158
|
+
{
|
|
1159
|
+
"id": "exam_bigint_add_abs",
|
|
1160
|
+
"symbols": [],
|
|
1161
|
+
"requires": [
|
|
1162
|
+
"BigInt",
|
|
1163
|
+
"bigint_zero",
|
|
1164
|
+
"EXAM_BIGINT_CAP",
|
|
1165
|
+
"exam_algo_fail",
|
|
1166
|
+
"exam_bigint_normalize"
|
|
1167
|
+
],
|
|
1168
|
+
"code": "static inline void exam_bigint_add_abs(BigInt *out, const BigInt *a,\n const BigInt *b) {\n bigint_zero(out);\n int n = a->len > b->len ? a->len : b->len;\n int carry = 0;\n for (int i = 0; i < n; ++i) {\n int value = (i < a->len ? a->d[i] : 0) +\n (i < b->len ? b->d[i] : 0) + carry;\n out->d[i] = (unsigned char)(value % 10);\n carry = value / 10;\n }\n out->len = n;\n if (carry) {\n if (n >= EXAM_BIGINT_CAP) exam_algo_fail(\"BigInt addition overflow\");\n out->d[out->len++] = (unsigned char)carry;\n }\n out->sign = 1;\n exam_bigint_normalize(out);\n}\n"
|
|
1169
|
+
},
|
|
1170
|
+
{
|
|
1171
|
+
"id": "exam_bigint_cmp_abs",
|
|
1172
|
+
"symbols": [],
|
|
1173
|
+
"requires": [
|
|
1174
|
+
"BigInt"
|
|
1175
|
+
],
|
|
1176
|
+
"code": "static inline int exam_bigint_cmp_abs(const BigInt *a, const BigInt *b) {\n if (a->len != b->len) return (a->len > b->len) ? 1 : -1;\n for (int i = a->len - 1; i >= 0; --i)\n if (a->d[i] != b->d[i]) return (a->d[i] > b->d[i]) ? 1 : -1;\n return 0;\n}\n"
|
|
1177
|
+
},
|
|
1178
|
+
{
|
|
1179
|
+
"id": "exam_bigint_sub_abs",
|
|
1180
|
+
"symbols": [],
|
|
1181
|
+
"requires": [
|
|
1182
|
+
"BigInt",
|
|
1183
|
+
"bigint_zero",
|
|
1184
|
+
"exam_bigint_normalize"
|
|
1185
|
+
],
|
|
1186
|
+
"code": "static inline void exam_bigint_sub_abs(BigInt *out, const BigInt *a,\n const BigInt *b) {\n bigint_zero(out);\n out->len = a->len;\n int borrow = 0;\n for (int i = 0; i < a->len; ++i) {\n int value = a->d[i] - (i < b->len ? b->d[i] : 0) - borrow;\n borrow = value < 0;\n if (borrow) value += 10;\n out->d[i] = (unsigned char)value;\n }\n out->sign = 1;\n exam_bigint_normalize(out);\n}\n"
|
|
1187
|
+
},
|
|
1188
|
+
{
|
|
1189
|
+
"id": "bigint_add",
|
|
1190
|
+
"symbols": [
|
|
1191
|
+
"bigint_add"
|
|
1192
|
+
],
|
|
1193
|
+
"requires": [
|
|
1194
|
+
"BigInt",
|
|
1195
|
+
"exam_algo_fail",
|
|
1196
|
+
"exam_bigint_add_abs",
|
|
1197
|
+
"exam_bigint_cmp_abs",
|
|
1198
|
+
"bigint_zero",
|
|
1199
|
+
"exam_bigint_sub_abs"
|
|
1200
|
+
],
|
|
1201
|
+
"code": "static inline void bigint_add(BigInt *out, const BigInt *a,\n const BigInt *b) {\n if (!out || !a || !b) exam_algo_fail(\"null BigInt addition operand\");\n BigInt result;\n if (!a->sign) result = *b;\n else if (!b->sign) result = *a;\n else if (a->sign == b->sign) {\n exam_bigint_add_abs(&result, a, b);\n if (result.sign) result.sign = a->sign;\n } else {\n int cmp = exam_bigint_cmp_abs(a, b);\n if (cmp == 0) bigint_zero(&result);\n else if (cmp > 0) {\n exam_bigint_sub_abs(&result, a, b);\n result.sign = a->sign;\n } else {\n exam_bigint_sub_abs(&result, b, a);\n result.sign = b->sign;\n }\n }\n *out = result;\n}\n"
|
|
1202
|
+
},
|
|
1203
|
+
{
|
|
1204
|
+
"id": "bigint_sub",
|
|
1205
|
+
"symbols": [
|
|
1206
|
+
"bigint_sub"
|
|
1207
|
+
],
|
|
1208
|
+
"requires": [
|
|
1209
|
+
"BigInt",
|
|
1210
|
+
"exam_algo_fail",
|
|
1211
|
+
"bigint_add"
|
|
1212
|
+
],
|
|
1213
|
+
"code": "static inline void bigint_sub(BigInt *out, const BigInt *a,\n const BigInt *b) {\n if (!out || !a || !b) exam_algo_fail(\"null BigInt subtraction operand\");\n BigInt inverted = *b;\n inverted.sign = -inverted.sign;\n bigint_add(out, a, &inverted);\n}\n"
|
|
1214
|
+
},
|
|
1215
|
+
{
|
|
1216
|
+
"id": "bigint_compare",
|
|
1217
|
+
"symbols": [
|
|
1218
|
+
"bigint_compare"
|
|
1219
|
+
],
|
|
1220
|
+
"requires": [
|
|
1221
|
+
"BigInt",
|
|
1222
|
+
"exam_bigint_cmp_abs"
|
|
1223
|
+
],
|
|
1224
|
+
"code": "static inline int bigint_compare(const BigInt *a, const BigInt *b) {\n if (a->sign != b->sign) return (a->sign > b->sign) ? 1 : -1;\n if (!a->sign) return 0;\n int cmp = exam_bigint_cmp_abs(a, b);\n return a->sign > 0 ? cmp : -cmp;\n}\n"
|
|
1225
|
+
},
|
|
1226
|
+
{
|
|
1227
|
+
"id": "exam_bigint_sub_abs_inplace",
|
|
1228
|
+
"symbols": [],
|
|
1229
|
+
"requires": [
|
|
1230
|
+
"BigInt",
|
|
1231
|
+
"exam_bigint_normalize"
|
|
1232
|
+
],
|
|
1233
|
+
"code": "static inline void exam_bigint_sub_abs_inplace(BigInt *a, const BigInt *b) {\n int borrow = 0;\n for (int i = 0; i < a->len; ++i) {\n int value = a->d[i] - (i < b->len ? b->d[i] : 0) - borrow;\n borrow = value < 0;\n if (borrow) value += 10;\n a->d[i] = (unsigned char)value;\n }\n exam_bigint_normalize(a);\n}\n"
|
|
1234
|
+
},
|
|
1235
|
+
{
|
|
1236
|
+
"id": "bigint_divmod",
|
|
1237
|
+
"symbols": [
|
|
1238
|
+
"bigint_divmod"
|
|
1239
|
+
],
|
|
1240
|
+
"requires": [
|
|
1241
|
+
"BigInt",
|
|
1242
|
+
"exam_algo_fail",
|
|
1243
|
+
"bigint_zero",
|
|
1244
|
+
"EXAM_BIGINT_CAP",
|
|
1245
|
+
"exam_bigint_normalize",
|
|
1246
|
+
"exam_bigint_cmp_abs",
|
|
1247
|
+
"exam_bigint_sub_abs_inplace"
|
|
1248
|
+
],
|
|
1249
|
+
"code": "static inline void bigint_divmod(BigInt *quotient, BigInt *remainder,\n const BigInt *a, const BigInt *b) {\n if (!quotient || !remainder || !a || !b || quotient == remainder)\n exam_algo_fail(\"invalid BigInt division arguments\");\n if (!b->sign) exam_algo_fail(\"BigInt division by zero\");\n BigInt q, r;\n bigint_zero(&q);\n bigint_zero(&r);\n q.len = a->len;\n for (int i = a->len-1; i >= 0; --i) {\n if (r.len == EXAM_BIGINT_CAP && r.sign)\n exam_algo_fail(\"BigInt division work buffer overflow\");\n if (r.sign) {\n for (int j = r.len; j > 0; --j) r.d[j] = r.d[j-1];\n ++r.len;\n }\n r.d[0] = a->d[i];\n r.sign = 1;\n exam_bigint_normalize(&r);\n int digit = 0;\n while (exam_bigint_cmp_abs(&r, b) >= 0) {\n exam_bigint_sub_abs_inplace(&r, b);\n ++digit;\n if (digit > 9) exam_algo_fail(\"BigInt division internal error\");\n }\n q.d[i] = (unsigned char)digit;\n }\n q.sign = a->sign * b->sign;\n r.sign = a->sign;\n exam_bigint_normalize(&q);\n exam_bigint_normalize(&r);\n *quotient = q;\n *remainder = r;\n}\n"
|
|
1250
|
+
},
|
|
1251
|
+
{
|
|
1252
|
+
"id": "bigint_mul_small",
|
|
1253
|
+
"symbols": [
|
|
1254
|
+
"bigint_mul_small"
|
|
1255
|
+
],
|
|
1256
|
+
"requires": [
|
|
1257
|
+
"BigInt",
|
|
1258
|
+
"exam_algo_fail",
|
|
1259
|
+
"bigint_zero",
|
|
1260
|
+
"EXAM_BIGINT_CAP",
|
|
1261
|
+
"exam_bigint_normalize"
|
|
1262
|
+
],
|
|
1263
|
+
"code": "static inline void bigint_mul_small(BigInt *out, const BigInt *a, int factor) {\n if (!out || !a) exam_algo_fail(\"null BigInt multiplication operand\");\n if (factor == 0 || !a->sign) { bigint_zero(out); return; }\n unsigned long long x = factor < 0 ? 0ULL-(unsigned)factor : (unsigned)factor;\n BigInt result;\n bigint_zero(&result);\n unsigned long long carry = 0;\n int i = 0;\n for (; i < a->len || carry; ++i) {\n if (i >= EXAM_BIGINT_CAP) exam_algo_fail(\"BigInt multiplication overflow\");\n unsigned long long value = carry +\n (i < a->len ? (unsigned long long)a->d[i]*x : 0ULL);\n result.d[i] = (unsigned char)(value%10ULL);\n carry = value/10ULL;\n }\n result.len = i;\n result.sign = a->sign * (factor < 0 ? -1 : 1);\n exam_bigint_normalize(&result);\n *out = result;\n}\n"
|
|
1264
|
+
},
|
|
1265
|
+
{
|
|
1266
|
+
"id": "bigint_factorial",
|
|
1267
|
+
"symbols": [
|
|
1268
|
+
"bigint_factorial"
|
|
1269
|
+
],
|
|
1270
|
+
"requires": [
|
|
1271
|
+
"BigInt",
|
|
1272
|
+
"exam_algo_fail",
|
|
1273
|
+
"bigint_zero",
|
|
1274
|
+
"bigint_mul_small"
|
|
1275
|
+
],
|
|
1276
|
+
"code": "static inline void bigint_factorial(BigInt *out, int n) {\n if (!out || n < 0) exam_algo_fail(\"invalid BigInt factorial\");\n BigInt result;\n bigint_zero(&result);\n result.d[0] = 1;\n result.sign = 1;\n for (int i = 2; i <= n; ++i)\n bigint_mul_small(&result, &result, i);\n *out = result;\n}\n"
|
|
1277
|
+
},
|
|
1278
|
+
{
|
|
1279
|
+
"id": "exam_io_fail",
|
|
1280
|
+
"symbols": [],
|
|
1281
|
+
"requires": [],
|
|
1282
|
+
"code": "static inline void exam_io_fail(const char *message) {\n fprintf(stderr, \"exam io: %s\\n\", message);\n exit(EXIT_FAILURE);\n}\n"
|
|
1283
|
+
},
|
|
1284
|
+
{
|
|
1285
|
+
"id": "exam_io_get",
|
|
1286
|
+
"symbols": [],
|
|
1287
|
+
"requires": [
|
|
1288
|
+
"exam_io_fail"
|
|
1289
|
+
],
|
|
1290
|
+
"code": "static inline int exam_io_get(void) {\n int c = fgetc(stdin);\n if (c == EOF && ferror(stdin)) exam_io_fail(\"input stream error\");\n return c;\n}\n"
|
|
1291
|
+
},
|
|
1292
|
+
{
|
|
1293
|
+
"id": "exam_io_space",
|
|
1294
|
+
"symbols": [],
|
|
1295
|
+
"requires": [],
|
|
1296
|
+
"code": "static inline int exam_io_space(int c) {\n return c != EOF && isspace((unsigned char)c);\n}\n"
|
|
1297
|
+
},
|
|
1298
|
+
{
|
|
1299
|
+
"id": "exam_io_unget",
|
|
1300
|
+
"symbols": [],
|
|
1301
|
+
"requires": [
|
|
1302
|
+
"exam_io_fail"
|
|
1303
|
+
],
|
|
1304
|
+
"code": "static inline void exam_io_unget(int c) {\n if (c != EOF && ungetc(c, stdin) == EOF)\n exam_io_fail(\"cannot restore input delimiter\");\n}\n"
|
|
1305
|
+
},
|
|
1306
|
+
{
|
|
1307
|
+
"id": "exam_io_partial_line",
|
|
1308
|
+
"symbols": [],
|
|
1309
|
+
"requires": [],
|
|
1310
|
+
"code": "static inline int *exam_io_partial_line(void) {\n static int partial = 0;\n return &partial;\n}\n"
|
|
1311
|
+
},
|
|
1312
|
+
{
|
|
1313
|
+
"id": "exam_io_token",
|
|
1314
|
+
"symbols": [],
|
|
1315
|
+
"requires": [
|
|
1316
|
+
"exam_failf",
|
|
1317
|
+
"exam_io_get",
|
|
1318
|
+
"exam_io_space",
|
|
1319
|
+
"exam_io_unget",
|
|
1320
|
+
"exam_io_partial_line"
|
|
1321
|
+
],
|
|
1322
|
+
"code": "static inline int exam_io_token(const char *api, char *out, size_t capacity) {\n if (!capacity) exam_failf(\"io\", api, \"buffer capacity is 0\");\n int c;\n do { c = exam_io_get(); } while (exam_io_space(c));\n if (c == EOF) return 0;\n size_t len = 0;\n do {\n if (len >= capacity - 1) exam_failf(\"io\", api, \"token exceeds capacity=%zu (maximum content: %zu bytes)\", capacity, capacity - 1);\n if (c == 0) exam_failf(\"io\", api, \"NUL byte in token at byte %zu\", len);\n out[len++] = (char)c;\n c = exam_io_get();\n } while (c != EOF && !exam_io_space(c));\n exam_io_unget(c);\n out[len] = '\\0';\n *exam_io_partial_line() = 1;\n return 1;\n}\n"
|
|
1323
|
+
},
|
|
1324
|
+
{
|
|
1325
|
+
"id": "bigint_from_str",
|
|
1326
|
+
"symbols": [
|
|
1327
|
+
"bigint_from_str"
|
|
1328
|
+
],
|
|
1329
|
+
"requires": [
|
|
1330
|
+
"BigInt",
|
|
1331
|
+
"exam_algo_fail",
|
|
1332
|
+
"EXAM_BIGINT_CAP",
|
|
1333
|
+
"bigint_zero",
|
|
1334
|
+
"exam_bigint_normalize"
|
|
1335
|
+
],
|
|
1336
|
+
"code": "static inline void bigint_from_str(BigInt *out, const char *text) {\n if (!text || !out) exam_algo_fail(\"null BigInt string/destination\");\n bool negative = false;\n if (*text == '+' || *text == '-') { negative = *text == '-'; ++text; }\n if (!*text) exam_algo_fail(\"BigInt has no digits\");\n size_t len = strlen(text);\n if (len > EXAM_BIGINT_CAP) exam_algo_fail(\"BigInt input too long\");\n BigInt temp;\n bigint_zero(&temp);\n temp.len = (int)len;\n temp.sign = negative ? -1 : 1;\n for (size_t i = 0; i < len; ++i) {\n char c = text[len-1-i];\n if (c < '0' || c > '9') exam_algo_fail(\"invalid BigInt digit\");\n temp.d[i] = (unsigned char)(c-'0');\n }\n exam_bigint_normalize(&temp);\n *out = temp;\n}\n"
|
|
1337
|
+
},
|
|
1338
|
+
{
|
|
1339
|
+
"id": "try_read_bigint",
|
|
1340
|
+
"symbols": [
|
|
1341
|
+
"try_read_bigint"
|
|
1342
|
+
],
|
|
1343
|
+
"requires": [
|
|
1344
|
+
"BigInt",
|
|
1345
|
+
"exam_algo_fail",
|
|
1346
|
+
"EXAM_BIGINT_CAP",
|
|
1347
|
+
"exam_io_token",
|
|
1348
|
+
"bigint_from_str"
|
|
1349
|
+
],
|
|
1350
|
+
"code": "static inline bool try_read_bigint(BigInt *out) {\n if (!out) exam_algo_fail(\"null BigInt destination\");\n char buffer[EXAM_BIGINT_CAP+2];\n if (!exam_io_token(\"try_read_bigint\", buffer, sizeof buffer)) return false;\n bigint_from_str(out, buffer);\n return true;\n}\n"
|
|
1351
|
+
},
|
|
1352
|
+
{
|
|
1353
|
+
"id": "read_bigint",
|
|
1354
|
+
"symbols": [
|
|
1355
|
+
"read_bigint"
|
|
1356
|
+
],
|
|
1357
|
+
"requires": [
|
|
1358
|
+
"BigInt",
|
|
1359
|
+
"try_read_bigint",
|
|
1360
|
+
"exam_algo_fail"
|
|
1361
|
+
],
|
|
1362
|
+
"code": "static inline void read_bigint(BigInt *out) {\n if (!try_read_bigint(out)) exam_algo_fail(\"unexpected EOF reading BigInt\");\n}\n"
|
|
1363
|
+
},
|
|
1364
|
+
{
|
|
1365
|
+
"id": "bigint_mul",
|
|
1366
|
+
"symbols": [
|
|
1367
|
+
"bigint_mul"
|
|
1368
|
+
],
|
|
1369
|
+
"requires": [
|
|
1370
|
+
"BigInt",
|
|
1371
|
+
"exam_algo_fail",
|
|
1372
|
+
"bigint_zero",
|
|
1373
|
+
"EXAM_BIGINT_CAP",
|
|
1374
|
+
"exam_bigint_normalize"
|
|
1375
|
+
],
|
|
1376
|
+
"code": "static inline void bigint_mul(BigInt *out, const BigInt *a,\n const BigInt *b) {\n if (!out || !a || !b) exam_algo_fail(\"null BigInt multiplication operand\");\n BigInt result;\n bigint_zero(&result);\n if (!a->sign || !b->sign) { *out = result; return; }\n if (a->len + b->len - 1 > EXAM_BIGINT_CAP)\n exam_algo_fail(\"BigInt multiplication overflow\");\n for (int i = 0; i < a->len; ++i) {\n int carry = 0, j = 0;\n for (; j < b->len || carry; ++j) {\n int position = i+j;\n if (position >= EXAM_BIGINT_CAP) exam_algo_fail(\"BigInt multiplication overflow\");\n int value = result.d[position] + carry +\n (j < b->len ? a->d[i] * b->d[j] : 0);\n result.d[position] = (unsigned char)(value % 10);\n carry = value / 10;\n }\n }\n int size = a->len+b->len;\n result.len = size <= EXAM_BIGINT_CAP ? size : EXAM_BIGINT_CAP;\n result.sign = a->sign*b->sign;\n exam_bigint_normalize(&result);\n *out = result;\n}\n"
|
|
1377
|
+
},
|
|
1378
|
+
{
|
|
1379
|
+
"id": "bigint_from_ll",
|
|
1380
|
+
"symbols": [
|
|
1381
|
+
"bigint_from_ll"
|
|
1382
|
+
],
|
|
1383
|
+
"requires": [
|
|
1384
|
+
"BigInt",
|
|
1385
|
+
"exam_algo_fail",
|
|
1386
|
+
"bigint_zero",
|
|
1387
|
+
"EXAM_BIGINT_CAP"
|
|
1388
|
+
],
|
|
1389
|
+
"code": "static inline void bigint_from_ll(BigInt *out, long long value) {\n if (!out) exam_algo_fail(\"null BigInt destination\");\n BigInt temp;\n bigint_zero(&temp);\n unsigned long long x = value < 0 ? 0ULL-(unsigned long long)value :\n (unsigned long long)value;\n int n = 0;\n do {\n if (n >= EXAM_BIGINT_CAP) exam_algo_fail(\"BigInt capacity too small\");\n temp.d[n++] = (unsigned char)(x % 10ULL);\n x /= 10ULL;\n } while (x);\n temp.len = n;\n temp.sign = value == 0 ? 0 : (value < 0 ? -1 : 1);\n *out = temp;\n}\n"
|
|
1390
|
+
},
|
|
1391
|
+
{
|
|
1392
|
+
"id": "bigint_to_str_into",
|
|
1393
|
+
"symbols": [
|
|
1394
|
+
"bigint_to_str_into"
|
|
1395
|
+
],
|
|
1396
|
+
"requires": [
|
|
1397
|
+
"BigInt",
|
|
1398
|
+
"exam_algo_fail"
|
|
1399
|
+
],
|
|
1400
|
+
"code": "static inline void bigint_to_str_into(const BigInt *a, char *out,\n size_t capacity) {\n if (!a || !out) exam_algo_fail(\"null BigInt string/destination\");\n size_t required = (size_t)a->len + (a->sign < 0 ? 1U : 0U) + 1U;\n if (required > capacity) exam_algo_fail(\"BigInt string buffer too small\");\n size_t pos = 0;\n if (a->sign < 0) out[pos++] = '-';\n for (int i = a->len - 1; i >= 0; --i) out[pos++] = (char)('0'+a->d[i]);\n out[pos] = '\\0';\n}\n"
|
|
1401
|
+
},
|
|
1402
|
+
{
|
|
1403
|
+
"id": "bigint_print",
|
|
1404
|
+
"symbols": [
|
|
1405
|
+
"bigint_print"
|
|
1406
|
+
],
|
|
1407
|
+
"requires": [
|
|
1408
|
+
"BigInt",
|
|
1409
|
+
"exam_algo_fail"
|
|
1410
|
+
],
|
|
1411
|
+
"code": "static inline void bigint_print(const BigInt *a, const char *end) {\n if (!a || !end) exam_algo_fail(\"null BigInt/terminator\");\n if (a->sign < 0 && putchar('-') == EOF) exam_algo_fail(\"BigInt output error\");\n for (int i = a->len - 1; i >= 0; --i)\n if (putchar('0' + a->d[i]) == EOF) exam_algo_fail(\"BigInt output error\");\n if (fputs(end, stdout) == EOF) exam_algo_fail(\"BigInt output error\");\n}\n"
|
|
1412
|
+
},
|
|
1413
|
+
{
|
|
1414
|
+
"id": "bigint_to_str",
|
|
1415
|
+
"symbols": [
|
|
1416
|
+
"bigint_to_str"
|
|
1417
|
+
],
|
|
1418
|
+
"requires": [
|
|
1419
|
+
"bigint_to_str_into"
|
|
1420
|
+
],
|
|
1421
|
+
"code": "#define bigint_to_str(a, array) \\\n bigint_to_str_into((a), (array), sizeof(array))\n"
|
|
1422
|
+
},
|
|
1423
|
+
{
|
|
1424
|
+
"id": "bigint_div_small",
|
|
1425
|
+
"symbols": [
|
|
1426
|
+
"bigint_div_small"
|
|
1427
|
+
],
|
|
1428
|
+
"requires": [
|
|
1429
|
+
"BigInt",
|
|
1430
|
+
"exam_algo_fail",
|
|
1431
|
+
"bigint_zero",
|
|
1432
|
+
"exam_bigint_normalize"
|
|
1433
|
+
],
|
|
1434
|
+
"code": "static inline void bigint_div_small(BigInt *quotient, int *remainder,\n const BigInt *a, int divisor) {\n if (!quotient || !remainder || !a || divisor == 0)\n exam_algo_fail(\"invalid BigInt small division arguments\");\n unsigned long long x = divisor < 0 ? 0ULL-(unsigned)divisor : (unsigned)divisor;\n BigInt result;\n bigint_zero(&result);\n result.len = a->len;\n unsigned long long carry = 0;\n for (int i = a->len-1; i >= 0; --i) {\n unsigned long long current = carry*10ULL + a->d[i];\n result.d[i] = (unsigned char)(current/x);\n carry = current%x;\n }\n result.sign = a->sign*(divisor < 0 ? -1 : 1);\n exam_bigint_normalize(&result);\n *quotient = result;\n *remainder = (int)carry*(a->sign < 0 ? -1 : 1);\n}\n"
|
|
1435
|
+
},
|
|
1436
|
+
{
|
|
1437
|
+
"id": "bisect_root",
|
|
1438
|
+
"symbols": [
|
|
1439
|
+
"bisect_root"
|
|
1440
|
+
],
|
|
1441
|
+
"requires": [
|
|
1442
|
+
"exam_algo_fail"
|
|
1443
|
+
],
|
|
1444
|
+
"code": "static inline double bisect_root(double (*f)(double), double left,\n double right, double tolerance, int iterations) {\n if(!f || !isfinite(left) || !isfinite(right) || left>=right ||\n !isfinite(tolerance) || tolerance<=0 || iterations<=0)\n exam_algo_fail(\"invalid bisect_root arguments\");\n double a=f(left), b=f(right);\n if(!isfinite(a) || !isfinite(b))\n exam_algo_fail(\"nonfinite endpoint function value\");\n if(a==0) return left;\n if(b==0) return right;\n if((a<0)==(b<0))\n exam_algo_fail(\"bisect_root requires opposite endpoint signs\");\n for(int i=0;i<iterations && right-left>tolerance;++i) {\n double mid=left+(right-left)/2;\n if(mid==left || mid==right)break;\n double value=f(mid);\n if(!isfinite(value))exam_algo_fail(\"nonfinite function value\");\n if(value==0)return mid;\n if((value<0)==(a<0)) { left=mid; a=value; }\n else { right=mid; b=value; }\n }\n return left+(right-left)/2;\n}\n"
|
|
1445
|
+
},
|
|
1446
|
+
{
|
|
1447
|
+
"id": "exam_bit_check",
|
|
1448
|
+
"symbols": [],
|
|
1449
|
+
"requires": [
|
|
1450
|
+
"exam_algo_fail"
|
|
1451
|
+
],
|
|
1452
|
+
"code": "static inline int exam_bit_check(int bit) {\n if (bit < 0 || bit >= 64) exam_algo_fail(\"bit index must be 0..63\");\n return bit;\n}\n"
|
|
1453
|
+
},
|
|
1454
|
+
{
|
|
1455
|
+
"id": "bit_get",
|
|
1456
|
+
"symbols": [
|
|
1457
|
+
"bit_get"
|
|
1458
|
+
],
|
|
1459
|
+
"requires": [
|
|
1460
|
+
"exam_bit_check"
|
|
1461
|
+
],
|
|
1462
|
+
"code": "static inline int bit_get(unsigned long long x, int bit) {\n return (int)((x >> exam_bit_check(bit)) & 1ULL);\n}\n"
|
|
1463
|
+
},
|
|
1464
|
+
{
|
|
1465
|
+
"id": "bit_set",
|
|
1466
|
+
"symbols": [
|
|
1467
|
+
"bit_set"
|
|
1468
|
+
],
|
|
1469
|
+
"requires": [
|
|
1470
|
+
"exam_bit_check"
|
|
1471
|
+
],
|
|
1472
|
+
"code": "static inline unsigned long long bit_set(unsigned long long x, int bit) {\n return x | (1ULL << exam_bit_check(bit));\n}\n"
|
|
1473
|
+
},
|
|
1474
|
+
{
|
|
1475
|
+
"id": "bit_clear",
|
|
1476
|
+
"symbols": [
|
|
1477
|
+
"bit_clear"
|
|
1478
|
+
],
|
|
1479
|
+
"requires": [
|
|
1480
|
+
"exam_bit_check"
|
|
1481
|
+
],
|
|
1482
|
+
"code": "static inline unsigned long long bit_clear(unsigned long long x, int bit) {\n return x & ~(1ULL << exam_bit_check(bit));\n}\n"
|
|
1483
|
+
},
|
|
1484
|
+
{
|
|
1485
|
+
"id": "bit_toggle",
|
|
1486
|
+
"symbols": [
|
|
1487
|
+
"bit_toggle"
|
|
1488
|
+
],
|
|
1489
|
+
"requires": [
|
|
1490
|
+
"exam_bit_check"
|
|
1491
|
+
],
|
|
1492
|
+
"code": "static inline unsigned long long bit_toggle(unsigned long long x, int bit) {\n return x ^ (1ULL << exam_bit_check(bit));\n}\n"
|
|
1493
|
+
},
|
|
1494
|
+
{
|
|
1495
|
+
"id": "bit_count",
|
|
1496
|
+
"symbols": [
|
|
1497
|
+
"bit_count"
|
|
1498
|
+
],
|
|
1499
|
+
"requires": [],
|
|
1500
|
+
"code": "static inline int bit_count(unsigned long long x) {\n int count = 0;\n while (x) { x &= x - 1ULL; ++count; }\n return count;\n}\n"
|
|
1501
|
+
},
|
|
1502
|
+
{
|
|
1503
|
+
"id": "is_power_of_two",
|
|
1504
|
+
"symbols": [
|
|
1505
|
+
"is_power_of_two"
|
|
1506
|
+
],
|
|
1507
|
+
"requires": [],
|
|
1508
|
+
"code": "static inline bool is_power_of_two(unsigned long long x) {\n return x && !(x & (x - 1ULL));\n}\n"
|
|
1509
|
+
},
|
|
1510
|
+
{
|
|
1511
|
+
"id": "bit_lowest_set_index",
|
|
1512
|
+
"symbols": [
|
|
1513
|
+
"bit_lowest_set_index"
|
|
1514
|
+
],
|
|
1515
|
+
"requires": [],
|
|
1516
|
+
"code": "static inline int bit_lowest_set_index(unsigned long long x) {\n if (!x) return -1;\n int k = 0;\n while (!(x & 1ULL)) { x >>= 1U; ++k; }\n return k;\n}\n"
|
|
1517
|
+
},
|
|
1518
|
+
{
|
|
1519
|
+
"id": "bit_highest_set_index",
|
|
1520
|
+
"symbols": [
|
|
1521
|
+
"bit_highest_set_index"
|
|
1522
|
+
],
|
|
1523
|
+
"requires": [],
|
|
1524
|
+
"code": "static inline int bit_highest_set_index(unsigned long long x) {\n if (!x) return -1;\n int k = 0;\n while (x >>= 1U) ++k;\n return k;\n}\n"
|
|
1525
|
+
},
|
|
1526
|
+
{
|
|
1527
|
+
"id": "bit_rotate_left",
|
|
1528
|
+
"symbols": [
|
|
1529
|
+
"bit_rotate_left"
|
|
1530
|
+
],
|
|
1531
|
+
"requires": [],
|
|
1532
|
+
"code": "static inline unsigned long long bit_rotate_left(unsigned long long x, int count) {\n unsigned int n = (unsigned int)((count % 64 + 64) % 64);\n if (n == 0) return x;\n return (x << n) | (x >> (64U - n));\n}\n"
|
|
1533
|
+
},
|
|
1534
|
+
{
|
|
1535
|
+
"id": "exam_calendar_print",
|
|
1536
|
+
"symbols": [],
|
|
1537
|
+
"requires": [
|
|
1538
|
+
"exam_failf"
|
|
1539
|
+
],
|
|
1540
|
+
"code": "static inline void exam_calendar_print(const char *api, const char *format,\n const char *end, const char *fields,\n const int values[3]) {\n if (!format || !end)\n exam_failf(\"algorithm\", api, \"%s must not be NULL\", format ? \"end\" : \"format\");\n\n for (size_t i = 0; format[i]; ++i) {\n unsigned char c = (unsigned char)format[i];\n if (c != '%') {\n if (putchar(c) == EOF)\n exam_failf(\"algorithm\", api, \"output stream error\");\n continue;\n }\n\n size_t start = i;\n c = (unsigned char)format[++i];\n if (c == '%') {\n if (putchar('%') == EOF)\n exam_failf(\"algorithm\", api, \"output stream error\");\n continue;\n }\n bool unpadded = c == '-';\n if (unpadded) c = (unsigned char)format[++i];\n if (!c)\n exam_failf(\"algorithm\", api, \"incomplete format specifier at byte %zu\", start);\n const char *field = strchr(fields, c);\n if (!field)\n exam_failf(\"algorithm\", api, \"unsupported format specifier at byte %zu (code=0x%02X)\",\n start, (unsigned)c);\n int width = unpadded ? 0 : (c == 'Y' ? 4 : 2);\n if (printf(\"%0*d\", width, values[field - fields]) < 0)\n exam_failf(\"algorithm\", api, \"output stream error\");\n }\n if (fputs(end, stdout) == EOF)\n exam_failf(\"algorithm\", api, \"output stream error\");\n}\n"
|
|
1541
|
+
},
|
|
1542
|
+
{
|
|
1543
|
+
"id": "EXAM_ENUM_CAP",
|
|
1544
|
+
"symbols": [],
|
|
1545
|
+
"requires": [],
|
|
1546
|
+
"code": "#ifndef EXAM_ENUM_CAP\n#define EXAM_ENUM_CAP 64\n#endif\n"
|
|
1547
|
+
},
|
|
1548
|
+
{
|
|
1549
|
+
"id": "Combination",
|
|
1550
|
+
"symbols": [
|
|
1551
|
+
"Combination"
|
|
1552
|
+
],
|
|
1553
|
+
"requires": [
|
|
1554
|
+
"EXAM_ENUM_CAP"
|
|
1555
|
+
],
|
|
1556
|
+
"code": "typedef struct {\n int n, r, started, finished;\n int indices[EXAM_ENUM_CAP];\n} Combination;\n"
|
|
1557
|
+
},
|
|
1558
|
+
{
|
|
1559
|
+
"id": "combination_new",
|
|
1560
|
+
"symbols": [
|
|
1561
|
+
"combination_new"
|
|
1562
|
+
],
|
|
1563
|
+
"requires": [
|
|
1564
|
+
"Combination",
|
|
1565
|
+
"EXAM_ENUM_CAP",
|
|
1566
|
+
"exam_algo_fail"
|
|
1567
|
+
],
|
|
1568
|
+
"code": "static inline Combination combination_new(int n, int r) {\n if (n < 0 || n > EXAM_ENUM_CAP || r < 0 || r > n)\n exam_algo_fail(\"invalid combination dimensions\");\n Combination it = {0};\n it.n = n; it.r = r;\n for (int i = 0; i < r; ++i) it.indices[i] = i;\n return it;\n}\n"
|
|
1569
|
+
},
|
|
1570
|
+
{
|
|
1571
|
+
"id": "combination_next",
|
|
1572
|
+
"symbols": [
|
|
1573
|
+
"combination_next"
|
|
1574
|
+
],
|
|
1575
|
+
"requires": [
|
|
1576
|
+
"Combination",
|
|
1577
|
+
"exam_algo_fail"
|
|
1578
|
+
],
|
|
1579
|
+
"code": "static inline bool combination_next(Combination *it) {\n if (!it) exam_algo_fail(\"null combination iterator\");\n if (it->finished) return false;\n if (!it->started) { it->started = 1; return true; }\n int i = it->r - 1;\n while (i >= 0 && it->indices[i] == it->n - it->r + i) --i;\n if (i < 0) { it->finished = 1; return false; }\n ++it->indices[i];\n for (int j = i + 1; j < it->r; ++j)\n it->indices[j] = it->indices[j-1] + 1;\n return true;\n}\n"
|
|
1580
|
+
},
|
|
1581
|
+
{
|
|
1582
|
+
"id": "Complex",
|
|
1583
|
+
"symbols": [
|
|
1584
|
+
"Complex"
|
|
1585
|
+
],
|
|
1586
|
+
"requires": [],
|
|
1587
|
+
"code": "typedef struct {\n double re, im;\n} Complex;\n"
|
|
1588
|
+
},
|
|
1589
|
+
{
|
|
1590
|
+
"id": "complex_new",
|
|
1591
|
+
"symbols": [
|
|
1592
|
+
"complex_new"
|
|
1593
|
+
],
|
|
1594
|
+
"requires": [
|
|
1595
|
+
"Complex"
|
|
1596
|
+
],
|
|
1597
|
+
"code": "static inline Complex complex_new(double real, double imaginary) {\n Complex z = {real, imaginary};\n return z;\n}\n"
|
|
1598
|
+
},
|
|
1599
|
+
{
|
|
1600
|
+
"id": "complex_add",
|
|
1601
|
+
"symbols": [
|
|
1602
|
+
"complex_add"
|
|
1603
|
+
],
|
|
1604
|
+
"requires": [
|
|
1605
|
+
"Complex",
|
|
1606
|
+
"complex_new"
|
|
1607
|
+
],
|
|
1608
|
+
"code": "static inline Complex complex_add(Complex a, Complex b) {\n return complex_new(a.re + b.re, a.im + b.im);\n}\n"
|
|
1609
|
+
},
|
|
1610
|
+
{
|
|
1611
|
+
"id": "complex_sub",
|
|
1612
|
+
"symbols": [
|
|
1613
|
+
"complex_sub"
|
|
1614
|
+
],
|
|
1615
|
+
"requires": [
|
|
1616
|
+
"Complex",
|
|
1617
|
+
"complex_new"
|
|
1618
|
+
],
|
|
1619
|
+
"code": "static inline Complex complex_sub(Complex a, Complex b) {\n return complex_new(a.re - b.re, a.im - b.im);\n}\n"
|
|
1620
|
+
},
|
|
1621
|
+
{
|
|
1622
|
+
"id": "complex_mul",
|
|
1623
|
+
"symbols": [
|
|
1624
|
+
"complex_mul"
|
|
1625
|
+
],
|
|
1626
|
+
"requires": [
|
|
1627
|
+
"Complex",
|
|
1628
|
+
"complex_new"
|
|
1629
|
+
],
|
|
1630
|
+
"code": "static inline Complex complex_mul(Complex a, Complex b) {\n return complex_new(a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re);\n}\n"
|
|
1631
|
+
},
|
|
1632
|
+
{
|
|
1633
|
+
"id": "complex_div",
|
|
1634
|
+
"symbols": [
|
|
1635
|
+
"complex_div"
|
|
1636
|
+
],
|
|
1637
|
+
"requires": [
|
|
1638
|
+
"Complex",
|
|
1639
|
+
"exam_algo_fail",
|
|
1640
|
+
"complex_new"
|
|
1641
|
+
],
|
|
1642
|
+
"code": "static inline Complex complex_div(Complex a, Complex b) {\n if (b.re == 0 && b.im == 0) exam_algo_fail(\"complex division by zero\");\n if (fabs(b.re) >= fabs(b.im)) {\n double r = b.im / b.re;\n double den = b.re + b.im * r;\n return complex_new((a.re + a.im*r)/den, (a.im - a.re*r)/den);\n }\n double r = b.re / b.im;\n double den = b.im + b.re * r;\n return complex_new((a.re*r + a.im)/den, (a.im*r - a.re)/den);\n}\n"
|
|
1643
|
+
},
|
|
1644
|
+
{
|
|
1645
|
+
"id": "complex_abs",
|
|
1646
|
+
"symbols": [
|
|
1647
|
+
"complex_abs"
|
|
1648
|
+
],
|
|
1649
|
+
"requires": [
|
|
1650
|
+
"Complex"
|
|
1651
|
+
],
|
|
1652
|
+
"code": "static inline double complex_abs(Complex z) {\n return hypot(z.re, z.im);\n}\n"
|
|
1653
|
+
},
|
|
1654
|
+
{
|
|
1655
|
+
"id": "complex_print",
|
|
1656
|
+
"symbols": [
|
|
1657
|
+
"complex_print"
|
|
1658
|
+
],
|
|
1659
|
+
"requires": [
|
|
1660
|
+
"Complex",
|
|
1661
|
+
"exam_algo_fail"
|
|
1662
|
+
],
|
|
1663
|
+
"code": "static inline void complex_print(Complex z, int digits, const char *end) {\n if (digits < 0 || digits > 100) exam_algo_fail(\"invalid precision\");\n if (printf(\"%.*f %.*f%s\", digits, z.re, digits, z.im, end) < 0)\n exam_algo_fail(\"complex output failed\");\n}\n"
|
|
1664
|
+
},
|
|
1665
|
+
{
|
|
1666
|
+
"id": "Date",
|
|
1667
|
+
"symbols": [
|
|
1668
|
+
"Date"
|
|
1669
|
+
],
|
|
1670
|
+
"requires": [],
|
|
1671
|
+
"code": "typedef struct { int year, month, day; } Date;\n"
|
|
1672
|
+
},
|
|
1673
|
+
{
|
|
1674
|
+
"id": "date_is_leap",
|
|
1675
|
+
"symbols": [
|
|
1676
|
+
"date_is_leap"
|
|
1677
|
+
],
|
|
1678
|
+
"requires": [],
|
|
1679
|
+
"code": "static inline bool date_is_leap(int year) {\n return year%4==0 && (year%100!=0 || year%400==0);\n}\n"
|
|
1680
|
+
},
|
|
1681
|
+
{
|
|
1682
|
+
"id": "date_days_in_month",
|
|
1683
|
+
"symbols": [
|
|
1684
|
+
"date_days_in_month"
|
|
1685
|
+
],
|
|
1686
|
+
"requires": [
|
|
1687
|
+
"date_is_leap"
|
|
1688
|
+
],
|
|
1689
|
+
"code": "static inline int date_days_in_month(int year, int month) {\n static const int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};\n if (year<1 || year>9999 || month<1 || month>12) return 0;\n return days[month]+(month==2 && date_is_leap(year));\n}\n"
|
|
1690
|
+
},
|
|
1691
|
+
{
|
|
1692
|
+
"id": "date_valid",
|
|
1693
|
+
"symbols": [
|
|
1694
|
+
"date_valid"
|
|
1695
|
+
],
|
|
1696
|
+
"requires": [
|
|
1697
|
+
"Date",
|
|
1698
|
+
"date_days_in_month"
|
|
1699
|
+
],
|
|
1700
|
+
"code": "static inline bool date_valid(Date x) {\n int days=date_days_in_month(x.year,x.month);\n return days && x.day>=1 && x.day<=days;\n}\n"
|
|
1701
|
+
},
|
|
1702
|
+
{
|
|
1703
|
+
"id": "exam_date_check",
|
|
1704
|
+
"symbols": [],
|
|
1705
|
+
"requires": [
|
|
1706
|
+
"Date",
|
|
1707
|
+
"date_days_in_month",
|
|
1708
|
+
"exam_failf"
|
|
1709
|
+
],
|
|
1710
|
+
"code": "static inline void exam_date_check(const char *api, const char *argument, Date x) {\n if (x.year < 1 || x.year > 9999)\n exam_failf(\"algorithm\", api, \"%s.year=%d out of range (expected 1..9999; month=%d, day=%d)\",\n argument, x.year, x.month, x.day);\n if (x.month < 1 || x.month > 12)\n exam_failf(\"algorithm\", api, \"%s.month=%d out of range (expected 1..12; year=%d, day=%d)\",\n argument, x.month, x.year, x.day);\n int max_day = date_days_in_month(x.year, x.month);\n if (x.day < 1 || x.day > max_day)\n exam_failf(\"algorithm\", api, \"%s.day=%d out of range for %04d-%02d (expected 1..%d)\",\n argument, x.day, x.year, x.month, max_day);\n}\n"
|
|
1711
|
+
},
|
|
1712
|
+
{
|
|
1713
|
+
"id": "date_new",
|
|
1714
|
+
"symbols": [
|
|
1715
|
+
"date_new"
|
|
1716
|
+
],
|
|
1717
|
+
"requires": [
|
|
1718
|
+
"Date",
|
|
1719
|
+
"exam_date_check"
|
|
1720
|
+
],
|
|
1721
|
+
"code": "static inline Date date_new(int year, int month, int day) {\n Date x = {year,month,day};\n exam_date_check(\"date_new\", \"date\", x);\n return x;\n}\n"
|
|
1722
|
+
},
|
|
1723
|
+
{
|
|
1724
|
+
"id": "exam_date_day_of_year",
|
|
1725
|
+
"symbols": [],
|
|
1726
|
+
"requires": [
|
|
1727
|
+
"Date",
|
|
1728
|
+
"date_days_in_month"
|
|
1729
|
+
],
|
|
1730
|
+
"code": "static inline int exam_date_day_of_year(Date x) {\n int count=x.day;\n for (int m=1;m<x.month;++m) count+=date_days_in_month(x.year,m);\n return count;\n}\n"
|
|
1731
|
+
},
|
|
1732
|
+
{
|
|
1733
|
+
"id": "exam_number_error",
|
|
1734
|
+
"symbols": [],
|
|
1735
|
+
"requires": [
|
|
1736
|
+
"exam_failf"
|
|
1737
|
+
],
|
|
1738
|
+
"code": "static inline void exam_number_error(const char *api, const char *field,\n const char *reason, const char *token) {\n char preview[32 * 4 + 4];\n size_t j = 0;\n for (size_t i = 0; token[i]; ++i) {\n if (i == 32) {\n preview[j++] = '.';\n preview[j++] = '.';\n preview[j++] = '.';\n break;\n }\n unsigned char c = (unsigned char)token[i];\n if (c >= 32 && c <= 126 && c != '\"' && c != '\\\\') {\n preview[j++] = (char)c;\n } else {\n const char *hex = \"0123456789ABCDEF\";\n preview[j++] = '\\\\';\n preview[j++] = 'x';\n preview[j++] = hex[c >> 4];\n preview[j++] = hex[c & 15];\n }\n }\n preview[j] = '\\0';\n exam_failf(\"io\", api, \"%s%s%s (token=\\\"%s\\\")\",\n field ? field : \"\", field ? \": \" : \"\", reason, preview);\n}\n"
|
|
1739
|
+
},
|
|
1740
|
+
{
|
|
1741
|
+
"id": "exam_try_read_int",
|
|
1742
|
+
"symbols": [],
|
|
1743
|
+
"requires": [
|
|
1744
|
+
"exam_failf",
|
|
1745
|
+
"exam_io_token",
|
|
1746
|
+
"exam_number_error"
|
|
1747
|
+
],
|
|
1748
|
+
"code": "static inline bool exam_try_read_int(const char *api, const char *field, int *value) {\n if (!value) exam_failf(\"io\", api, \"destination must not be NULL\");\n char token[128];\n if (!exam_io_token(api, token, sizeof token)) return false;\n char *end = NULL;\n errno = 0;\n long long result = strtoll(token, &end, 10);\n if (end == token || *end != '\\0') exam_number_error(api, field, \"invalid integer\", token);\n if (errno == ERANGE || result < INT_MIN || result > INT_MAX)\n exam_number_error(api, field, \"integer out of range\", token);\n *value = (int)result;\n return true;\n}\n"
|
|
1749
|
+
},
|
|
1750
|
+
{
|
|
1751
|
+
"id": "exam_read_int_for",
|
|
1752
|
+
"symbols": [],
|
|
1753
|
+
"requires": [
|
|
1754
|
+
"exam_try_read_int",
|
|
1755
|
+
"exam_failf"
|
|
1756
|
+
],
|
|
1757
|
+
"code": "static inline int exam_read_int_for(const char *api, const char *field) {\n int value;\n if (!exam_try_read_int(api, field, &value))\n exam_failf(\"io\", api, \"unexpected EOF while reading %s\", field ? field : \"int\");\n return value;\n}\n"
|
|
1758
|
+
},
|
|
1759
|
+
{
|
|
1760
|
+
"id": "date_read",
|
|
1761
|
+
"symbols": [
|
|
1762
|
+
"date_read"
|
|
1763
|
+
],
|
|
1764
|
+
"requires": [
|
|
1765
|
+
"Date",
|
|
1766
|
+
"exam_date_check",
|
|
1767
|
+
"exam_read_int_for"
|
|
1768
|
+
],
|
|
1769
|
+
"code": "static inline Date date_read(void) {\n Date x;\n x.year = exam_read_int_for(\"date_read\", \"year\");\n x.month = exam_read_int_for(\"date_read\", \"month\");\n x.day = exam_read_int_for(\"date_read\", \"day\");\n exam_date_check(\"date_read\", \"date\", x);\n return x;\n}\n"
|
|
1770
|
+
},
|
|
1771
|
+
{
|
|
1772
|
+
"id": "date_day_of_year",
|
|
1773
|
+
"symbols": [
|
|
1774
|
+
"date_day_of_year"
|
|
1775
|
+
],
|
|
1776
|
+
"requires": [
|
|
1777
|
+
"Date",
|
|
1778
|
+
"exam_date_check",
|
|
1779
|
+
"exam_date_day_of_year"
|
|
1780
|
+
],
|
|
1781
|
+
"code": "static inline int date_day_of_year(Date x) {\n exam_date_check(\"date_day_of_year\", \"date\", x);\n return exam_date_day_of_year(x);\n}\n"
|
|
1782
|
+
},
|
|
1783
|
+
{
|
|
1784
|
+
"id": "exam_date_start_of_year",
|
|
1785
|
+
"symbols": [],
|
|
1786
|
+
"requires": [],
|
|
1787
|
+
"code": "static inline int exam_date_start_of_year(int year) {\n int y=year-1;\n return 365*y+y/4-y/100+y/400;\n}\n"
|
|
1788
|
+
},
|
|
1789
|
+
{
|
|
1790
|
+
"id": "exam_date_serial",
|
|
1791
|
+
"symbols": [],
|
|
1792
|
+
"requires": [
|
|
1793
|
+
"Date",
|
|
1794
|
+
"exam_date_start_of_year",
|
|
1795
|
+
"exam_date_day_of_year"
|
|
1796
|
+
],
|
|
1797
|
+
"code": "static inline int exam_date_serial(Date x) {\n return exam_date_start_of_year(x.year) + exam_date_day_of_year(x) - 1;\n}\n"
|
|
1798
|
+
},
|
|
1799
|
+
{
|
|
1800
|
+
"id": "date_to_serial",
|
|
1801
|
+
"symbols": [
|
|
1802
|
+
"date_to_serial"
|
|
1803
|
+
],
|
|
1804
|
+
"requires": [
|
|
1805
|
+
"Date",
|
|
1806
|
+
"exam_date_check",
|
|
1807
|
+
"exam_date_serial"
|
|
1808
|
+
],
|
|
1809
|
+
"code": "static inline int date_to_serial(Date x) {\n exam_date_check(\"date_to_serial\", \"date\", x);\n return exam_date_serial(x);\n}\n"
|
|
1810
|
+
},
|
|
1811
|
+
{
|
|
1812
|
+
"id": "date_from_serial",
|
|
1813
|
+
"symbols": [
|
|
1814
|
+
"date_from_serial"
|
|
1815
|
+
],
|
|
1816
|
+
"requires": [
|
|
1817
|
+
"Date",
|
|
1818
|
+
"exam_date_start_of_year",
|
|
1819
|
+
"exam_failf",
|
|
1820
|
+
"date_days_in_month"
|
|
1821
|
+
],
|
|
1822
|
+
"code": "static inline Date date_from_serial(int serial) {\n if (serial<0 || serial>=exam_date_start_of_year(10000))\n exam_failf(\"algorithm\", \"date_from_serial\", \"serial=%d out of range (expected 0..%d)\",\n serial, exam_date_start_of_year(10000) - 1);\n int left=1, right=10000;\n while (left+1<right) {\n int mid=left+(right-left)/2;\n if (exam_date_start_of_year(mid)<=serial) left=mid;\n else right=mid;\n }\n int remaining=serial-exam_date_start_of_year(left);\n int month=1;\n while (remaining>=date_days_in_month(left,month)) {\n remaining-=date_days_in_month(left,month);++month;\n }\n return (Date){left, month, remaining + 1};\n}\n"
|
|
1823
|
+
},
|
|
1824
|
+
{
|
|
1825
|
+
"id": "date_add_days",
|
|
1826
|
+
"symbols": [
|
|
1827
|
+
"date_add_days"
|
|
1828
|
+
],
|
|
1829
|
+
"requires": [
|
|
1830
|
+
"Date",
|
|
1831
|
+
"date_valid",
|
|
1832
|
+
"exam_date_serial",
|
|
1833
|
+
"exam_date_start_of_year",
|
|
1834
|
+
"exam_failf",
|
|
1835
|
+
"date_from_serial"
|
|
1836
|
+
],
|
|
1837
|
+
"code": "static inline Date date_add_days(Date x, int delta) {\n if (!date_valid(x))\n exam_failf(\"algorithm\", \"date_add_days\",\n \"invalid base date (year=%d, month=%d, day=%d), delta=%d\",\n x.year, x.month, x.day, delta);\n long long serial = (long long)exam_date_serial(x) + delta;\n if (serial < 0 || serial >= exam_date_start_of_year(10000))\n exam_failf(\"algorithm\", \"date_add_days\",\n \"result outside 0001-01-01..9999-12-31 (base=%04d-%02d-%02d, delta=%d)\",\n x.year, x.month, x.day, delta);\n return date_from_serial((int)serial);\n}\n"
|
|
1838
|
+
},
|
|
1839
|
+
{
|
|
1840
|
+
"id": "date_diff",
|
|
1841
|
+
"symbols": [
|
|
1842
|
+
"date_diff"
|
|
1843
|
+
],
|
|
1844
|
+
"requires": [
|
|
1845
|
+
"Date",
|
|
1846
|
+
"exam_date_check",
|
|
1847
|
+
"exam_date_serial"
|
|
1848
|
+
],
|
|
1849
|
+
"code": "static inline int date_diff(Date a, Date b) {\n exam_date_check(\"date_diff\", \"a\", a);\n exam_date_check(\"date_diff\", \"b\", b);\n return exam_date_serial(b) - exam_date_serial(a);\n}\n"
|
|
1850
|
+
},
|
|
1851
|
+
{
|
|
1852
|
+
"id": "date_compare",
|
|
1853
|
+
"symbols": [
|
|
1854
|
+
"date_compare"
|
|
1855
|
+
],
|
|
1856
|
+
"requires": [
|
|
1857
|
+
"Date",
|
|
1858
|
+
"exam_date_check",
|
|
1859
|
+
"exam_date_serial"
|
|
1860
|
+
],
|
|
1861
|
+
"code": "static inline int date_compare(Date a, Date b) {\n exam_date_check(\"date_compare\", \"a\", a);\n exam_date_check(\"date_compare\", \"b\", b);\n int x = exam_date_serial(a), y = exam_date_serial(b);\n return (x>y)-(x<y);\n}\n"
|
|
1862
|
+
},
|
|
1863
|
+
{
|
|
1864
|
+
"id": "date_print",
|
|
1865
|
+
"symbols": [
|
|
1866
|
+
"date_print"
|
|
1867
|
+
],
|
|
1868
|
+
"requires": [
|
|
1869
|
+
"Date",
|
|
1870
|
+
"exam_date_check",
|
|
1871
|
+
"exam_calendar_print"
|
|
1872
|
+
],
|
|
1873
|
+
"code": "static inline void date_print(Date x, const char *format, const char *end) {\n exam_date_check(\"date_print\", \"date\", x);\n const int values[] = {x.year, x.month, x.day};\n exam_calendar_print(\"date_print\", format, end, \"Ymd\", values);\n}\n"
|
|
1874
|
+
},
|
|
1875
|
+
{
|
|
1876
|
+
"id": "first_true_int",
|
|
1877
|
+
"symbols": [
|
|
1878
|
+
"first_true_int"
|
|
1879
|
+
],
|
|
1880
|
+
"requires": [
|
|
1881
|
+
"exam_algo_fail"
|
|
1882
|
+
],
|
|
1883
|
+
"code": "static inline int first_true_int(int begin, int end,\n bool (*predicate)(int)) {\n if (begin > end || !predicate)\n exam_algo_fail(\"invalid first_true_int interval or predicate\");\n int left = begin, right = end;\n while (left < right) {\n int middle = (int)((long long)left +\n ((long long)right - left) / 2LL);\n if (predicate(middle)) right = middle;\n else left = middle + 1;\n }\n return left;\n}\n"
|
|
1884
|
+
},
|
|
1885
|
+
{
|
|
1886
|
+
"id": "exam_io_skip_current_line",
|
|
1887
|
+
"symbols": [],
|
|
1888
|
+
"requires": [
|
|
1889
|
+
"exam_io_get",
|
|
1890
|
+
"exam_io_unget",
|
|
1891
|
+
"exam_io_partial_line"
|
|
1892
|
+
],
|
|
1893
|
+
"code": "static inline void exam_io_skip_current_line(void) {\n int c;\n do { c = exam_io_get(); } while (c != EOF && c != '\\n' && c != '\\r');\n if (c == '\\r') {\n int next = exam_io_get();\n if (next != '\\n') exam_io_unget(next);\n }\n *exam_io_partial_line() = 0;\n}\n"
|
|
1894
|
+
},
|
|
1895
|
+
{
|
|
1896
|
+
"id": "exam_io_line_buffer",
|
|
1897
|
+
"symbols": [],
|
|
1898
|
+
"requires": [
|
|
1899
|
+
"exam_failf",
|
|
1900
|
+
"exam_io_get",
|
|
1901
|
+
"exam_io_unget",
|
|
1902
|
+
"exam_io_partial_line"
|
|
1903
|
+
],
|
|
1904
|
+
"code": "static inline int exam_io_line_buffer(const char *api, char *out, size_t capacity) {\n if (!capacity) exam_failf(\"io\", api, \"buffer capacity is 0\");\n int c = exam_io_get();\n if (c == EOF) return 0;\n size_t len = 0;\n while (c != EOF && c != '\\n' && c != '\\r') {\n if (len >= capacity - 1) exam_failf(\"io\", api, \"line exceeds capacity=%zu (maximum content: %zu bytes)\", capacity, capacity - 1);\n if (c == 0) exam_failf(\"io\", api, \"NUL byte in line at byte %zu\", len);\n out[len++] = (char)c;\n c = exam_io_get();\n }\n if (c == '\\r') {\n int next = exam_io_get();\n if (next != '\\n') exam_io_unget(next);\n }\n out[len] = '\\0';\n *exam_io_partial_line() = 0;\n return 1;\n}\n"
|
|
1905
|
+
},
|
|
1906
|
+
{
|
|
1907
|
+
"id": "exam_io_write",
|
|
1908
|
+
"symbols": [],
|
|
1909
|
+
"requires": [
|
|
1910
|
+
"exam_io_fail"
|
|
1911
|
+
],
|
|
1912
|
+
"code": "static inline void exam_io_write(const char *s) {\n if (fputs(s, stdout) == EOF) exam_io_fail(\"output stream error\");\n}\n"
|
|
1913
|
+
},
|
|
1914
|
+
{
|
|
1915
|
+
"id": "try_read_char_raw",
|
|
1916
|
+
"symbols": [
|
|
1917
|
+
"try_read_char_raw"
|
|
1918
|
+
],
|
|
1919
|
+
"requires": [
|
|
1920
|
+
"exam_io_fail",
|
|
1921
|
+
"exam_io_get",
|
|
1922
|
+
"exam_io_partial_line"
|
|
1923
|
+
],
|
|
1924
|
+
"code": "static inline bool try_read_char_raw(char *value) {\n if (!value) exam_io_fail(\"null char destination\");\n int c = exam_io_get();\n if (c == EOF) return false;\n *value = (char)c;\n *exam_io_partial_line() = (c != '\\n' && c != '\\r');\n return true;\n}\n"
|
|
1925
|
+
},
|
|
1926
|
+
{
|
|
1927
|
+
"id": "read_char_raw",
|
|
1928
|
+
"symbols": [
|
|
1929
|
+
"read_char_raw"
|
|
1930
|
+
],
|
|
1931
|
+
"requires": [
|
|
1932
|
+
"try_read_char_raw",
|
|
1933
|
+
"exam_io_fail"
|
|
1934
|
+
],
|
|
1935
|
+
"code": "static inline char read_char_raw(void) {\n char value;\n if (!try_read_char_raw(&value)) exam_io_fail(\"unexpected EOF while reading char\");\n return value;\n}\n"
|
|
1936
|
+
},
|
|
1937
|
+
{
|
|
1938
|
+
"id": "try_read_char",
|
|
1939
|
+
"symbols": [
|
|
1940
|
+
"try_read_char"
|
|
1941
|
+
],
|
|
1942
|
+
"requires": [
|
|
1943
|
+
"exam_io_fail",
|
|
1944
|
+
"exam_io_get",
|
|
1945
|
+
"exam_io_space",
|
|
1946
|
+
"exam_io_partial_line"
|
|
1947
|
+
],
|
|
1948
|
+
"code": "static inline bool try_read_char(char *value) {\n if (!value) exam_io_fail(\"null char destination\");\n int c;\n do { c = exam_io_get(); } while (exam_io_space(c));\n if (c == EOF) return false;\n *value = (char)c;\n *exam_io_partial_line() = 1;\n return true;\n}\n"
|
|
1949
|
+
},
|
|
1950
|
+
{
|
|
1951
|
+
"id": "read_char",
|
|
1952
|
+
"symbols": [
|
|
1953
|
+
"read_char"
|
|
1954
|
+
],
|
|
1955
|
+
"requires": [
|
|
1956
|
+
"try_read_char",
|
|
1957
|
+
"exam_io_fail"
|
|
1958
|
+
],
|
|
1959
|
+
"code": "static inline char read_char(void) {\n char value;\n if (!try_read_char(&value)) exam_io_fail(\"unexpected EOF while reading char\");\n return value;\n}\n"
|
|
1960
|
+
},
|
|
1961
|
+
{
|
|
1962
|
+
"id": "exam_try_read_double",
|
|
1963
|
+
"symbols": [],
|
|
1964
|
+
"requires": [
|
|
1965
|
+
"exam_failf",
|
|
1966
|
+
"exam_io_token",
|
|
1967
|
+
"exam_number_error"
|
|
1968
|
+
],
|
|
1969
|
+
"code": "static inline bool exam_try_read_double(const char *api, const char *field, double *value) {\n if (!value) exam_failf(\"io\", api, \"destination must not be NULL\");\n char token[128];\n if (!exam_io_token(api, token, sizeof token)) return false;\n char *end = NULL;\n errno = 0;\n double result = strtod(token, &end);\n if (end == token || *end != '\\0') exam_number_error(api, field, \"invalid double\", token);\n if (errno == ERANGE || !isfinite(result)) exam_number_error(api, field, \"double out of range\", token);\n *value = result;\n return true;\n}\n"
|
|
1970
|
+
},
|
|
1971
|
+
{
|
|
1972
|
+
"id": "read_double",
|
|
1973
|
+
"symbols": [
|
|
1974
|
+
"read_double"
|
|
1975
|
+
],
|
|
1976
|
+
"requires": [
|
|
1977
|
+
"exam_try_read_double",
|
|
1978
|
+
"exam_failf"
|
|
1979
|
+
],
|
|
1980
|
+
"code": "static inline double read_double(void) {\n double value;\n if (!exam_try_read_double(\"read_double\", NULL, &value))\n exam_failf(\"io\", \"read_double\", \"unexpected EOF while reading double\");\n return value;\n}\n"
|
|
1981
|
+
},
|
|
1982
|
+
{
|
|
1983
|
+
"id": "read_doubles_into",
|
|
1984
|
+
"symbols": [
|
|
1985
|
+
"read_doubles_into"
|
|
1986
|
+
],
|
|
1987
|
+
"requires": [
|
|
1988
|
+
"exam_io_fail",
|
|
1989
|
+
"read_double"
|
|
1990
|
+
],
|
|
1991
|
+
"code": "static inline void read_doubles_into(double *array, int count, size_t capacity) {\n if (count < 0 || (size_t)count > capacity)\n exam_io_fail(\"double array length exceeds capacity\");\n if (count && !array) exam_io_fail(\"null double array\");\n for (int i = 0; i < count; ++i) array[i] = read_double();\n}\n"
|
|
1992
|
+
},
|
|
1993
|
+
{
|
|
1994
|
+
"id": "read_doubles",
|
|
1995
|
+
"symbols": [
|
|
1996
|
+
"read_doubles"
|
|
1997
|
+
],
|
|
1998
|
+
"requires": [
|
|
1999
|
+
"read_doubles_into"
|
|
2000
|
+
],
|
|
2001
|
+
"code": "#define read_doubles(array, count) \\\n read_doubles_into((array), (count), sizeof(array) / sizeof((array)[0]))\n"
|
|
2002
|
+
},
|
|
2003
|
+
{
|
|
2004
|
+
"id": "read_int",
|
|
2005
|
+
"symbols": [
|
|
2006
|
+
"read_int"
|
|
2007
|
+
],
|
|
2008
|
+
"requires": [
|
|
2009
|
+
"exam_read_int_for"
|
|
2010
|
+
],
|
|
2011
|
+
"code": "static inline int read_int(void) {\n return exam_read_int_for(\"read_int\", NULL);\n}\n"
|
|
2012
|
+
},
|
|
2013
|
+
{
|
|
2014
|
+
"id": "read_ints_into",
|
|
2015
|
+
"symbols": [
|
|
2016
|
+
"read_ints_into"
|
|
2017
|
+
],
|
|
2018
|
+
"requires": [
|
|
2019
|
+
"exam_io_fail",
|
|
2020
|
+
"read_int"
|
|
2021
|
+
],
|
|
2022
|
+
"code": "static inline void read_ints_into(int *array, int count, size_t capacity) {\n if (count < 0 || (size_t)count > capacity)\n exam_io_fail(\"int array length exceeds capacity\");\n if (count && !array) exam_io_fail(\"null int array\");\n for (int i = 0; i < count; ++i) array[i] = read_int();\n}\n"
|
|
2023
|
+
},
|
|
2024
|
+
{
|
|
2025
|
+
"id": "read_ints",
|
|
2026
|
+
"symbols": [
|
|
2027
|
+
"read_ints"
|
|
2028
|
+
],
|
|
2029
|
+
"requires": [
|
|
2030
|
+
"read_ints_into"
|
|
2031
|
+
],
|
|
2032
|
+
"code": "#define read_ints(array, count) \\\n read_ints_into((array), (count), sizeof(array) / sizeof((array)[0]))\n"
|
|
2033
|
+
},
|
|
2034
|
+
{
|
|
2035
|
+
"id": "exam_read_line_into",
|
|
2036
|
+
"symbols": [],
|
|
2037
|
+
"requires": [
|
|
2038
|
+
"exam_failf",
|
|
2039
|
+
"exam_io_line_buffer",
|
|
2040
|
+
"exam_io_partial_line",
|
|
2041
|
+
"exam_io_skip_current_line"
|
|
2042
|
+
],
|
|
2043
|
+
"code": "static inline bool exam_read_line_into(const char *api, char *out, size_t capacity) {\n if (!out) exam_failf(\"io\", api, \"destination must not be NULL\");\n if (*exam_io_partial_line()) exam_io_skip_current_line();\n return exam_io_line_buffer(api, out, capacity) != 0;\n}\n"
|
|
2044
|
+
},
|
|
2045
|
+
{
|
|
2046
|
+
"id": "try_read_line_into",
|
|
2047
|
+
"symbols": [
|
|
2048
|
+
"try_read_line_into"
|
|
2049
|
+
],
|
|
2050
|
+
"requires": [
|
|
2051
|
+
"exam_read_line_into"
|
|
2052
|
+
],
|
|
2053
|
+
"code": "static inline bool try_read_line_into(char *out, size_t capacity) {\n return exam_read_line_into(\"try_read_line\", out, capacity);\n}\n"
|
|
2054
|
+
},
|
|
2055
|
+
{
|
|
2056
|
+
"id": "read_line_into",
|
|
2057
|
+
"symbols": [
|
|
2058
|
+
"read_line_into"
|
|
2059
|
+
],
|
|
2060
|
+
"requires": [
|
|
2061
|
+
"exam_read_line_into",
|
|
2062
|
+
"exam_failf"
|
|
2063
|
+
],
|
|
2064
|
+
"code": "static inline void read_line_into(char *out, size_t capacity) {\n if (!exam_read_line_into(\"read_line\", out, capacity))\n exam_failf(\"io\", \"read_line\", \"unexpected EOF\");\n}\n"
|
|
2065
|
+
},
|
|
2066
|
+
{
|
|
2067
|
+
"id": "exam_read_rest_into",
|
|
2068
|
+
"symbols": [],
|
|
2069
|
+
"requires": [
|
|
2070
|
+
"exam_failf",
|
|
2071
|
+
"exam_io_line_buffer"
|
|
2072
|
+
],
|
|
2073
|
+
"code": "static inline bool exam_read_rest_into(const char *api, char *out, size_t capacity) {\n if (!out) exam_failf(\"io\", api, \"destination must not be NULL\");\n return exam_io_line_buffer(api, out, capacity) != 0;\n}\n"
|
|
2074
|
+
},
|
|
2075
|
+
{
|
|
2076
|
+
"id": "try_read_rest_into",
|
|
2077
|
+
"symbols": [
|
|
2078
|
+
"try_read_rest_into"
|
|
2079
|
+
],
|
|
2080
|
+
"requires": [
|
|
2081
|
+
"exam_read_rest_into"
|
|
2082
|
+
],
|
|
2083
|
+
"code": "static inline bool try_read_rest_into(char *out, size_t capacity) {\n return exam_read_rest_into(\"try_read_rest\", out, capacity);\n}\n"
|
|
2084
|
+
},
|
|
2085
|
+
{
|
|
2086
|
+
"id": "read_rest_into",
|
|
2087
|
+
"symbols": [
|
|
2088
|
+
"read_rest_into"
|
|
2089
|
+
],
|
|
2090
|
+
"requires": [
|
|
2091
|
+
"exam_read_rest_into",
|
|
2092
|
+
"exam_failf"
|
|
2093
|
+
],
|
|
2094
|
+
"code": "static inline void read_rest_into(char *out, size_t capacity) {\n if (!exam_read_rest_into(\"read_rest\", out, capacity))\n exam_failf(\"io\", \"read_rest\", \"unexpected EOF\");\n}\n"
|
|
2095
|
+
},
|
|
2096
|
+
{
|
|
2097
|
+
"id": "skip_line",
|
|
2098
|
+
"symbols": [
|
|
2099
|
+
"skip_line"
|
|
2100
|
+
],
|
|
2101
|
+
"requires": [
|
|
2102
|
+
"exam_io_skip_current_line"
|
|
2103
|
+
],
|
|
2104
|
+
"code": "static inline void skip_line(void) {\n exam_io_skip_current_line();\n}\n"
|
|
2105
|
+
},
|
|
2106
|
+
{
|
|
2107
|
+
"id": "try_read_line",
|
|
2108
|
+
"symbols": [
|
|
2109
|
+
"try_read_line"
|
|
2110
|
+
],
|
|
2111
|
+
"requires": [
|
|
2112
|
+
"try_read_line_into"
|
|
2113
|
+
],
|
|
2114
|
+
"code": "#define try_read_line(array) try_read_line_into((array), sizeof(array))\n"
|
|
2115
|
+
},
|
|
2116
|
+
{
|
|
2117
|
+
"id": "read_line",
|
|
2118
|
+
"symbols": [
|
|
2119
|
+
"read_line"
|
|
2120
|
+
],
|
|
2121
|
+
"requires": [
|
|
2122
|
+
"read_line_into"
|
|
2123
|
+
],
|
|
2124
|
+
"code": "#define read_line(array) read_line_into((array), sizeof(array))\n"
|
|
2125
|
+
},
|
|
2126
|
+
{
|
|
2127
|
+
"id": "try_read_rest",
|
|
2128
|
+
"symbols": [
|
|
2129
|
+
"try_read_rest"
|
|
2130
|
+
],
|
|
2131
|
+
"requires": [
|
|
2132
|
+
"try_read_rest_into"
|
|
2133
|
+
],
|
|
2134
|
+
"code": "#define try_read_rest(array) try_read_rest_into((array), sizeof(array))\n"
|
|
2135
|
+
},
|
|
2136
|
+
{
|
|
2137
|
+
"id": "read_rest",
|
|
2138
|
+
"symbols": [
|
|
2139
|
+
"read_rest"
|
|
2140
|
+
],
|
|
2141
|
+
"requires": [
|
|
2142
|
+
"read_rest_into"
|
|
2143
|
+
],
|
|
2144
|
+
"code": "#define read_rest(array) read_rest_into((array), sizeof(array))\n"
|
|
2145
|
+
},
|
|
2146
|
+
{
|
|
2147
|
+
"id": "exam_try_read_ll",
|
|
2148
|
+
"symbols": [],
|
|
2149
|
+
"requires": [
|
|
2150
|
+
"exam_failf",
|
|
2151
|
+
"exam_io_token",
|
|
2152
|
+
"exam_number_error"
|
|
2153
|
+
],
|
|
2154
|
+
"code": "static inline bool exam_try_read_ll(const char *api, const char *field, long long *value) {\n if (!value) exam_failf(\"io\", api, \"destination must not be NULL\");\n char token[128];\n if (!exam_io_token(api, token, sizeof token)) return false;\n char *end = NULL;\n errno = 0;\n long long result = strtoll(token, &end, 10);\n if (end == token || *end != '\\0') exam_number_error(api, field, \"invalid long long\", token);\n if (errno == ERANGE) exam_number_error(api, field, \"long long out of range\", token);\n *value = result;\n return true;\n}\n"
|
|
2155
|
+
},
|
|
2156
|
+
{
|
|
2157
|
+
"id": "read_ll",
|
|
2158
|
+
"symbols": [
|
|
2159
|
+
"read_ll"
|
|
2160
|
+
],
|
|
2161
|
+
"requires": [
|
|
2162
|
+
"exam_try_read_ll",
|
|
2163
|
+
"exam_failf"
|
|
2164
|
+
],
|
|
2165
|
+
"code": "static inline long long read_ll(void) {\n long long value;\n if (!exam_try_read_ll(\"read_ll\", NULL, &value))\n exam_failf(\"io\", \"read_ll\", \"unexpected EOF while reading long long\");\n return value;\n}\n"
|
|
2166
|
+
},
|
|
2167
|
+
{
|
|
2168
|
+
"id": "read_lls_into",
|
|
2169
|
+
"symbols": [
|
|
2170
|
+
"read_lls_into"
|
|
2171
|
+
],
|
|
2172
|
+
"requires": [
|
|
2173
|
+
"exam_io_fail",
|
|
2174
|
+
"read_ll"
|
|
2175
|
+
],
|
|
2176
|
+
"code": "static inline void read_lls_into(long long *array, int count, size_t capacity) {\n if (count < 0 || (size_t)count > capacity)\n exam_io_fail(\"long long array length exceeds capacity\");\n if (count && !array) exam_io_fail(\"null long long array\");\n for (int i = 0; i < count; ++i) array[i] = read_ll();\n}\n"
|
|
2177
|
+
},
|
|
2178
|
+
{
|
|
2179
|
+
"id": "read_lls",
|
|
2180
|
+
"symbols": [
|
|
2181
|
+
"read_lls"
|
|
2182
|
+
],
|
|
2183
|
+
"requires": [
|
|
2184
|
+
"read_lls_into"
|
|
2185
|
+
],
|
|
2186
|
+
"code": "#define read_lls(array, count) \\\n read_lls_into((array), (count), sizeof(array) / sizeof((array)[0]))\n"
|
|
2187
|
+
},
|
|
2188
|
+
{
|
|
2189
|
+
"id": "exam_read_word_into",
|
|
2190
|
+
"symbols": [],
|
|
2191
|
+
"requires": [
|
|
2192
|
+
"exam_failf",
|
|
2193
|
+
"exam_io_token"
|
|
2194
|
+
],
|
|
2195
|
+
"code": "static inline bool exam_read_word_into(const char *api, char *out, size_t capacity) {\n if (!out) exam_failf(\"io\", api, \"destination must not be NULL\");\n return exam_io_token(api, out, capacity) != 0;\n}\n"
|
|
2196
|
+
},
|
|
2197
|
+
{
|
|
2198
|
+
"id": "try_read_word_into",
|
|
2199
|
+
"symbols": [
|
|
2200
|
+
"try_read_word_into"
|
|
2201
|
+
],
|
|
2202
|
+
"requires": [
|
|
2203
|
+
"exam_read_word_into"
|
|
2204
|
+
],
|
|
2205
|
+
"code": "static inline bool try_read_word_into(char *out, size_t capacity) {\n return exam_read_word_into(\"try_read_word\", out, capacity);\n}\n"
|
|
2206
|
+
},
|
|
2207
|
+
{
|
|
2208
|
+
"id": "read_word_into",
|
|
2209
|
+
"symbols": [
|
|
2210
|
+
"read_word_into"
|
|
2211
|
+
],
|
|
2212
|
+
"requires": [
|
|
2213
|
+
"exam_read_word_into",
|
|
2214
|
+
"exam_failf"
|
|
2215
|
+
],
|
|
2216
|
+
"code": "static inline void read_word_into(char *out, size_t capacity) {\n if (!exam_read_word_into(\"read_word\", out, capacity))\n exam_failf(\"io\", \"read_word\", \"unexpected EOF\");\n}\n"
|
|
2217
|
+
},
|
|
2218
|
+
{
|
|
2219
|
+
"id": "try_read_word",
|
|
2220
|
+
"symbols": [
|
|
2221
|
+
"try_read_word"
|
|
2222
|
+
],
|
|
2223
|
+
"requires": [
|
|
2224
|
+
"try_read_word_into"
|
|
2225
|
+
],
|
|
2226
|
+
"code": "#define try_read_word(array) try_read_word_into((array), sizeof(array))\n"
|
|
2227
|
+
},
|
|
2228
|
+
{
|
|
2229
|
+
"id": "read_word",
|
|
2230
|
+
"symbols": [
|
|
2231
|
+
"read_word"
|
|
2232
|
+
],
|
|
2233
|
+
"requires": [
|
|
2234
|
+
"read_word_into"
|
|
2235
|
+
],
|
|
2236
|
+
"code": "#define read_word(array) read_word_into((array), sizeof(array))\n"
|
|
2237
|
+
},
|
|
2238
|
+
{
|
|
2239
|
+
"id": "exam_abs_ull",
|
|
2240
|
+
"symbols": [],
|
|
2241
|
+
"requires": [],
|
|
2242
|
+
"code": "static inline unsigned long long exam_abs_ull(long long a) {\n return a >= 0 ? (unsigned long long)a : 0ULL - (unsigned long long)a;\n}\n"
|
|
2243
|
+
},
|
|
2244
|
+
{
|
|
2245
|
+
"id": "gcd",
|
|
2246
|
+
"symbols": [
|
|
2247
|
+
"gcd"
|
|
2248
|
+
],
|
|
2249
|
+
"requires": [
|
|
2250
|
+
"exam_abs_ull"
|
|
2251
|
+
],
|
|
2252
|
+
"code": "static inline unsigned long long gcd(long long a, long long b) {\n unsigned long long x = exam_abs_ull(a), y = exam_abs_ull(b);\n while (y) { unsigned long long r = x % y; x = y; y = r; }\n return x;\n}\n"
|
|
2253
|
+
},
|
|
2254
|
+
{
|
|
2255
|
+
"id": "lcm",
|
|
2256
|
+
"symbols": [
|
|
2257
|
+
"lcm"
|
|
2258
|
+
],
|
|
2259
|
+
"requires": [
|
|
2260
|
+
"exam_abs_ull",
|
|
2261
|
+
"gcd",
|
|
2262
|
+
"exam_algo_fail"
|
|
2263
|
+
],
|
|
2264
|
+
"code": "static inline unsigned long long lcm(long long a, long long b) {\n unsigned long long x = exam_abs_ull(a), y = exam_abs_ull(b);\n if (!x || !y) return 0;\n unsigned long long k = x / gcd(a, b);\n if (k > ULLONG_MAX / y) exam_algo_fail(\"lcm overflow\");\n return k * y;\n}\n"
|
|
2265
|
+
},
|
|
2266
|
+
{
|
|
2267
|
+
"id": "exam_mul_ll",
|
|
2268
|
+
"symbols": [],
|
|
2269
|
+
"requires": [
|
|
2270
|
+
"exam_algo_fail"
|
|
2271
|
+
],
|
|
2272
|
+
"code": "static inline long long exam_mul_ll(long long a, long long b) {\n if ((a > 0 && b > 0 && a > LLONG_MAX / b) ||\n (a > 0 && b < 0 && b < LLONG_MIN / a) ||\n (a < 0 && b > 0 && a < LLONG_MIN / b) ||\n (a < 0 && b < 0 && a < LLONG_MAX / b))\n exam_algo_fail(\"signed integer multiplication overflow\");\n return a * b;\n}\n"
|
|
2273
|
+
},
|
|
2274
|
+
{
|
|
2275
|
+
"id": "exam_add_ll",
|
|
2276
|
+
"symbols": [],
|
|
2277
|
+
"requires": [
|
|
2278
|
+
"exam_algo_fail"
|
|
2279
|
+
],
|
|
2280
|
+
"code": "static inline long long exam_add_ll(long long a, long long b) {\n if ((b > 0 && a > LLONG_MAX - b) ||\n (b < 0 && a < LLONG_MIN - b))\n exam_algo_fail(\"signed integer addition overflow\");\n return a + b;\n}\n"
|
|
2281
|
+
},
|
|
2282
|
+
{
|
|
2283
|
+
"id": "int_pow",
|
|
2284
|
+
"symbols": [
|
|
2285
|
+
"int_pow"
|
|
2286
|
+
],
|
|
2287
|
+
"requires": [
|
|
2288
|
+
"exam_mul_ll"
|
|
2289
|
+
],
|
|
2290
|
+
"code": "static inline long long int_pow(long long base, unsigned int exponent) {\n long long result = 1;\n while (exponent) {\n if (exponent & 1U) result = exam_mul_ll(result, base);\n exponent >>= 1U;\n if (exponent) base = exam_mul_ll(base, base);\n }\n return result;\n}\n"
|
|
2291
|
+
},
|
|
2292
|
+
{
|
|
2293
|
+
"id": "factorial_basic",
|
|
2294
|
+
"symbols": [
|
|
2295
|
+
"factorial_basic"
|
|
2296
|
+
],
|
|
2297
|
+
"requires": [
|
|
2298
|
+
"exam_algo_fail"
|
|
2299
|
+
],
|
|
2300
|
+
"code": "static inline long long factorial_basic(int n) {\n if (n < 0 || n > 20) exam_algo_fail(\"factorial requires 0 <= n <= 20\");\n long long result = 1;\n for (int i = 2; i <= n; ++i) result *= i;\n return result;\n}\n"
|
|
2301
|
+
},
|
|
2302
|
+
{
|
|
2303
|
+
"id": "is_prime",
|
|
2304
|
+
"symbols": [
|
|
2305
|
+
"is_prime"
|
|
2306
|
+
],
|
|
2307
|
+
"requires": [],
|
|
2308
|
+
"code": "static inline bool is_prime(long long n) {\n if (n < 2) return false;\n if (n % 2 == 0) return n == 2;\n for (long long d = 3; d <= n / d; d += 2)\n if (n % d == 0) return false;\n return true;\n}\n"
|
|
2309
|
+
},
|
|
2310
|
+
{
|
|
2311
|
+
"id": "exam_mod_add",
|
|
2312
|
+
"symbols": [],
|
|
2313
|
+
"requires": [],
|
|
2314
|
+
"code": "static inline unsigned long long exam_mod_add(unsigned long long a,\n unsigned long long b,\n unsigned long long mod) {\n return a >= mod - b ? a - (mod - b) : a + b;\n}\n"
|
|
2315
|
+
},
|
|
2316
|
+
{
|
|
2317
|
+
"id": "exam_mod_mul",
|
|
2318
|
+
"symbols": [],
|
|
2319
|
+
"requires": [
|
|
2320
|
+
"exam_mod_add"
|
|
2321
|
+
],
|
|
2322
|
+
"code": "static inline unsigned long long exam_mod_mul(unsigned long long a,\n unsigned long long b,\n unsigned long long mod) {\n unsigned long long result = 0;\n while (b) {\n if (b & 1ULL) result = exam_mod_add(result, a, mod);\n b >>= 1U;\n if (b) a = exam_mod_add(a, a, mod);\n }\n return result;\n}\n"
|
|
2323
|
+
},
|
|
2324
|
+
{
|
|
2325
|
+
"id": "mod_pow",
|
|
2326
|
+
"symbols": [
|
|
2327
|
+
"mod_pow"
|
|
2328
|
+
],
|
|
2329
|
+
"requires": [
|
|
2330
|
+
"exam_algo_fail",
|
|
2331
|
+
"exam_mod_mul"
|
|
2332
|
+
],
|
|
2333
|
+
"code": "static inline long long mod_pow(long long base, unsigned long long exponent,\n long long modulus) {\n if (modulus <= 0) exam_algo_fail(\"modulus must be positive\");\n unsigned long long mod = (unsigned long long)modulus;\n long long reduced = base % modulus;\n if (reduced < 0) reduced += modulus;\n unsigned long long x = (unsigned long long)reduced, result = 1ULL % mod;\n while (exponent) {\n if (exponent & 1ULL) result = exam_mod_mul(result, x, mod);\n exponent >>= 1U;\n if (exponent) x = exam_mod_mul(x, x, mod);\n }\n return (long long)result;\n}\n"
|
|
2334
|
+
},
|
|
2335
|
+
{
|
|
2336
|
+
"id": "nearly_equal",
|
|
2337
|
+
"symbols": [
|
|
2338
|
+
"nearly_equal"
|
|
2339
|
+
],
|
|
2340
|
+
"requires": [
|
|
2341
|
+
"exam_algo_fail"
|
|
2342
|
+
],
|
|
2343
|
+
"code": "static inline bool nearly_equal(double a, double b,\n double absolute_tolerance,\n double relative_tolerance) {\n if (!isfinite(a) || !isfinite(b) || absolute_tolerance < 0 ||\n relative_tolerance < 0 || !isfinite(absolute_tolerance) ||\n !isfinite(relative_tolerance))\n exam_algo_fail(\"invalid floating comparison arguments\");\n double delta = fabs(a - b);\n double scale = fmax(fabs(a), fabs(b));\n return delta <= fmax(absolute_tolerance, relative_tolerance * scale);\n}\n"
|
|
2344
|
+
},
|
|
2345
|
+
{
|
|
2346
|
+
"id": "round_digits",
|
|
2347
|
+
"symbols": [
|
|
2348
|
+
"round_digits"
|
|
2349
|
+
],
|
|
2350
|
+
"requires": [
|
|
2351
|
+
"exam_algo_fail"
|
|
2352
|
+
],
|
|
2353
|
+
"code": "static inline double round_digits(double value, int digits) {\n if (!isfinite(value) || digits < -15 || digits > 15)\n exam_algo_fail(\"invalid round_digits argument\");\n double scale = pow(10.0, (double)digits);\n double scaled = value * scale;\n if (!isfinite(scaled)) exam_algo_fail(\"round_digits overflow\");\n return round(scaled) / scale;\n}\n"
|
|
2354
|
+
},
|
|
2355
|
+
{
|
|
2356
|
+
"id": "exam_digit_value",
|
|
2357
|
+
"symbols": [],
|
|
2358
|
+
"requires": [],
|
|
2359
|
+
"code": "static inline int exam_digit_value(int c) {\n if (c >= '0' && c <= '9') return c-'0';\n if (c >= 'a' && c <= 'z') return c-'a'+10;\n if (c >= 'A' && c <= 'Z') return c-'A'+10;\n return -1;\n}\n"
|
|
2360
|
+
},
|
|
2361
|
+
{
|
|
2362
|
+
"id": "base_to_int",
|
|
2363
|
+
"symbols": [
|
|
2364
|
+
"base_to_int"
|
|
2365
|
+
],
|
|
2366
|
+
"requires": [
|
|
2367
|
+
"exam_algo_fail",
|
|
2368
|
+
"exam_digit_value"
|
|
2369
|
+
],
|
|
2370
|
+
"code": "static inline long long base_to_int(const char *s, int base) {\n if (base < 2 || base > 36) exam_algo_fail(\"base must be 2..36\");\n bool negative = false;\n if (*s == '+' || *s == '-') { negative = (*s == '-'); ++s; }\n if (!*s) exam_algo_fail(\"missing digits\");\n unsigned long long magnitude = 0;\n unsigned long long limit = (unsigned long long)LLONG_MAX + (negative ? 1ULL : 0ULL);\n for (; *s; ++s) {\n int digit = exam_digit_value((unsigned char)*s);\n if (digit < 0 || digit >= base) exam_algo_fail(\"invalid base digit\");\n if (magnitude > (limit - (unsigned long long)digit)/(unsigned)base)\n exam_algo_fail(\"base conversion overflow\");\n magnitude = magnitude*(unsigned)base + (unsigned)digit;\n }\n if (negative) {\n if (magnitude == (unsigned long long)LLONG_MAX+1ULL) return LLONG_MIN;\n return -(long long)magnitude;\n }\n return (long long)magnitude;\n}\n"
|
|
2371
|
+
},
|
|
2372
|
+
{
|
|
2373
|
+
"id": "int_to_base_into",
|
|
2374
|
+
"symbols": [
|
|
2375
|
+
"int_to_base_into"
|
|
2376
|
+
],
|
|
2377
|
+
"requires": [
|
|
2378
|
+
"exam_algo_fail"
|
|
2379
|
+
],
|
|
2380
|
+
"code": "static inline void int_to_base_into(long long value, int base,\n char *out, size_t capacity) {\n static const char digits[] = \"0123456789abcdefghijklmnopqrstuvwxyz\";\n if (base < 2 || base > 36 || !out || !capacity)\n exam_algo_fail(\"invalid base conversion arguments\");\n char reversed[70];\n size_t len = 0;\n bool negative = value < 0;\n unsigned long long magnitude = negative\n ? 0ULL - (unsigned long long)value : (unsigned long long)value;\n do {\n reversed[len++] = digits[magnitude % (unsigned)base];\n magnitude /= (unsigned)base;\n } while (magnitude);\n if (negative) reversed[len++] = '-';\n if (len >= capacity) exam_algo_fail(\"base conversion output too small\");\n for (size_t i = 0; i < len; ++i) out[i] = reversed[len-1-i];\n out[len] = '\\0';\n}\n"
|
|
2381
|
+
},
|
|
2382
|
+
{
|
|
2383
|
+
"id": "int_digit_count",
|
|
2384
|
+
"symbols": [
|
|
2385
|
+
"int_digit_count"
|
|
2386
|
+
],
|
|
2387
|
+
"requires": [
|
|
2388
|
+
"exam_algo_fail"
|
|
2389
|
+
],
|
|
2390
|
+
"code": "static inline int int_digit_count(long long n, int base) {\n if (base < 2 || base > 36) exam_algo_fail(\"base must be 2..36\");\n unsigned long long x = n < 0 ? 0ULL-(unsigned long long)n : (unsigned long long)n;\n int count = 1;\n while (x >= (unsigned)base) { x /= (unsigned)base; ++count; }\n return count;\n}\n"
|
|
2391
|
+
},
|
|
2392
|
+
{
|
|
2393
|
+
"id": "int_digit_sum",
|
|
2394
|
+
"symbols": [
|
|
2395
|
+
"int_digit_sum"
|
|
2396
|
+
],
|
|
2397
|
+
"requires": [
|
|
2398
|
+
"exam_algo_fail"
|
|
2399
|
+
],
|
|
2400
|
+
"code": "static inline int int_digit_sum(long long n, int base) {\n if (base < 2 || base > 36) exam_algo_fail(\"base must be 2..36\");\n unsigned long long x = n < 0 ? 0ULL-(unsigned long long)n : (unsigned long long)n;\n int total = 0;\n do { total += (int)(x % (unsigned)base); x /= (unsigned)base; } while (x);\n return total;\n}\n"
|
|
2401
|
+
},
|
|
2402
|
+
{
|
|
2403
|
+
"id": "int_digit_at",
|
|
2404
|
+
"symbols": [
|
|
2405
|
+
"int_digit_at"
|
|
2406
|
+
],
|
|
2407
|
+
"requires": [
|
|
2408
|
+
"exam_algo_fail"
|
|
2409
|
+
],
|
|
2410
|
+
"code": "static inline int int_digit_at(long long n, int base, int position) {\n if (base < 2 || base > 36 || position < 0)\n exam_algo_fail(\"invalid digit position/base\");\n unsigned long long x = n < 0 ? 0ULL-(unsigned long long)n : (unsigned long long)n;\n while (position-- && x) x /= (unsigned)base;\n return (int)(x % (unsigned)base);\n}\n"
|
|
2411
|
+
},
|
|
2412
|
+
{
|
|
2413
|
+
"id": "int_to_base",
|
|
2414
|
+
"symbols": [
|
|
2415
|
+
"int_to_base"
|
|
2416
|
+
],
|
|
2417
|
+
"requires": [
|
|
2418
|
+
"int_to_base_into"
|
|
2419
|
+
],
|
|
2420
|
+
"code": "#define int_to_base(value, base, array) \\\n int_to_base_into((value),(base),(array),sizeof(array))\n"
|
|
2421
|
+
},
|
|
2422
|
+
{
|
|
2423
|
+
"id": "int_to_digits_into",
|
|
2424
|
+
"symbols": [
|
|
2425
|
+
"int_to_digits_into"
|
|
2426
|
+
],
|
|
2427
|
+
"requires": [
|
|
2428
|
+
"exam_algo_fail"
|
|
2429
|
+
],
|
|
2430
|
+
"code": "static inline int int_to_digits_into(long long value,int base,\n int *out,size_t capacity) {\n if(base<2 || base>36)exam_algo_fail(\"base must be 2..36\");\n unsigned long long x=value<0?0ULL-(unsigned long long)value:(unsigned long long)value;\n int reversed[64],n=0;\n do {\n reversed[n++]=(int)(x%(unsigned)base);\n x/=(unsigned)base;\n }while(x);\n if((size_t)n>capacity)exam_algo_fail(\"digit buffer too small\");\n for(int i=0;i<n;++i)out[i]=reversed[n-1-i];\n return n;\n}\n"
|
|
2431
|
+
},
|
|
2432
|
+
{
|
|
2433
|
+
"id": "digits_to_int",
|
|
2434
|
+
"symbols": [
|
|
2435
|
+
"digits_to_int"
|
|
2436
|
+
],
|
|
2437
|
+
"requires": [
|
|
2438
|
+
"exam_algo_fail"
|
|
2439
|
+
],
|
|
2440
|
+
"code": "static inline long long digits_to_int(const int *digits,int n,int base) {\n if(base<2 || base>36 || n<=0)exam_algo_fail(\"invalid digit input\");\n unsigned long long value=0;\n for(int i=0;i<n;++i) {\n if(digits[i]<0 || digits[i]>=base)\n exam_algo_fail(\"digit outside base\");\n if(value>((unsigned long long)LLONG_MAX-(unsigned)digits[i])/(unsigned)base)\n exam_algo_fail(\"digits_to_int overflow\");\n value=value*(unsigned)base+(unsigned)digits[i];\n }\n return (long long)value;\n}\n"
|
|
2441
|
+
},
|
|
2442
|
+
{
|
|
2443
|
+
"id": "int_reverse_digits",
|
|
2444
|
+
"symbols": [
|
|
2445
|
+
"int_reverse_digits"
|
|
2446
|
+
],
|
|
2447
|
+
"requires": [
|
|
2448
|
+
"exam_algo_fail"
|
|
2449
|
+
],
|
|
2450
|
+
"code": "static inline long long int_reverse_digits(long long n) {\n bool negative=n<0;\n unsigned long long x=negative?0ULL-(unsigned long long)n:(unsigned long long)n;\n unsigned long long limit=(unsigned long long)LLONG_MAX+(negative?1ULL:0ULL);\n unsigned long long result=0;\n do {\n unsigned long long digit=x%10ULL;\n if(result>(limit-digit)/10ULL)\n exam_algo_fail(\"reversed digits out of long long range\");\n result=result*10ULL+digit;\n x/=10ULL;\n }while(x);\n if(negative && result==(unsigned long long)LLONG_MAX+1ULL)return LLONG_MIN;\n return negative?-(long long)result:(long long)result;\n}\n"
|
|
2451
|
+
},
|
|
2452
|
+
{
|
|
2453
|
+
"id": "int_to_digits",
|
|
2454
|
+
"symbols": [
|
|
2455
|
+
"int_to_digits"
|
|
2456
|
+
],
|
|
2457
|
+
"requires": [
|
|
2458
|
+
"int_to_digits_into"
|
|
2459
|
+
],
|
|
2460
|
+
"code": "#define int_to_digits(value,base,array) \\\n int_to_digits_into((value),(base),(array),sizeof(array)/sizeof((array)[0]))\n"
|
|
2461
|
+
},
|
|
2462
|
+
{
|
|
2463
|
+
"id": "Permutation",
|
|
2464
|
+
"symbols": [
|
|
2465
|
+
"Permutation"
|
|
2466
|
+
],
|
|
2467
|
+
"requires": [
|
|
2468
|
+
"EXAM_ENUM_CAP"
|
|
2469
|
+
],
|
|
2470
|
+
"code": "typedef struct {\n int n, r, started, finished;\n int indices[EXAM_ENUM_CAP];\n int pool[EXAM_ENUM_CAP], cycles[EXAM_ENUM_CAP];\n} Permutation;\n"
|
|
2471
|
+
},
|
|
2472
|
+
{
|
|
2473
|
+
"id": "permutation_new",
|
|
2474
|
+
"symbols": [
|
|
2475
|
+
"permutation_new"
|
|
2476
|
+
],
|
|
2477
|
+
"requires": [
|
|
2478
|
+
"Permutation",
|
|
2479
|
+
"EXAM_ENUM_CAP",
|
|
2480
|
+
"exam_algo_fail"
|
|
2481
|
+
],
|
|
2482
|
+
"code": "static inline Permutation permutation_new(int n, int r) {\n if (n < 0 || n > EXAM_ENUM_CAP || r < 0 || r > n)\n exam_algo_fail(\"invalid permutation dimensions\");\n Permutation it = {0};\n it.n = n; it.r = r;\n for (int i = 0; i < n; ++i) it.pool[i] = i;\n for (int i = 0; i < r; ++i) it.cycles[i] = n - i;\n return it;\n}\n"
|
|
2483
|
+
},
|
|
2484
|
+
{
|
|
2485
|
+
"id": "permutation_next",
|
|
2486
|
+
"symbols": [
|
|
2487
|
+
"permutation_next"
|
|
2488
|
+
],
|
|
2489
|
+
"requires": [
|
|
2490
|
+
"Permutation",
|
|
2491
|
+
"exam_algo_fail"
|
|
2492
|
+
],
|
|
2493
|
+
"code": "static inline bool permutation_next(Permutation *it) {\n if (!it) exam_algo_fail(\"null permutation iterator\");\n if (it->finished) return false;\n if (!it->started) {\n it->started = 1;\n for (int i = 0; i < it->r; ++i) it->indices[i] = it->pool[i];\n return true;\n }\n for (int i = it->r - 1; i >= 0; --i) {\n --it->cycles[i];\n if (it->cycles[i] == 0) {\n int first = it->pool[i];\n for (int j = i; j < it->n - 1; ++j) it->pool[j] = it->pool[j+1];\n it->pool[it->n-1] = first;\n it->cycles[i] = it->n - i;\n } else {\n int j = it->n - it->cycles[i];\n int tmp = it->pool[i]; it->pool[i] = it->pool[j]; it->pool[j] = tmp;\n for (int k = 0; k < it->r; ++k) it->indices[k] = it->pool[k];\n return true;\n }\n }\n it->finished = 1;\n return false;\n}\n"
|
|
2494
|
+
},
|
|
2495
|
+
{
|
|
2496
|
+
"id": "poly_eval_lls",
|
|
2497
|
+
"symbols": [
|
|
2498
|
+
"poly_eval_lls"
|
|
2499
|
+
],
|
|
2500
|
+
"requires": [
|
|
2501
|
+
"exam_algo_fail",
|
|
2502
|
+
"exam_mul_ll",
|
|
2503
|
+
"exam_add_checked_ll"
|
|
2504
|
+
],
|
|
2505
|
+
"code": "static inline long long poly_eval_lls(const long long *coefficients,\n int count, long long x) {\n if(count<=0)exam_algo_fail(\"poly_eval_lls needs at least one coefficient\");\n long long result=coefficients[count-1];\n for(int i=count-2;i>=0;--i) {\n long long product=exam_mul_ll(result,x);\n result=exam_add_checked_ll(product,coefficients[i]);\n }\n return result;\n}\n"
|
|
2506
|
+
},
|
|
2507
|
+
{
|
|
2508
|
+
"id": "sieve_primes_into",
|
|
2509
|
+
"symbols": [
|
|
2510
|
+
"sieve_primes_into"
|
|
2511
|
+
],
|
|
2512
|
+
"requires": [
|
|
2513
|
+
"exam_algo_fail"
|
|
2514
|
+
],
|
|
2515
|
+
"code": "static inline void sieve_primes_into(unsigned char *flags, int limit,\n size_t capacity) {\n if (limit < 0 || (size_t)limit >= capacity || !flags)\n exam_algo_fail(\"prime sieve exceeds buffer capacity\");\n for (int i = 0; i <= limit; ++i) flags[i] = (i >= 2);\n for (int p = 2; p <= limit / p; ++p) if (flags[p]) {\n for (int j = p * p; j <= limit;) {\n flags[j] = 0;\n if (j > limit - p) break;\n j += p;\n }\n }\n}\n"
|
|
2516
|
+
},
|
|
2517
|
+
{
|
|
2518
|
+
"id": "sieve_primes",
|
|
2519
|
+
"symbols": [
|
|
2520
|
+
"sieve_primes"
|
|
2521
|
+
],
|
|
2522
|
+
"requires": [
|
|
2523
|
+
"sieve_primes_into"
|
|
2524
|
+
],
|
|
2525
|
+
"code": "#define sieve_primes(array, limit) \\\n sieve_primes_into((array), (limit), sizeof(array) / sizeof((array)[0]))\n"
|
|
2526
|
+
},
|
|
2527
|
+
{
|
|
2528
|
+
"id": "print_int",
|
|
2529
|
+
"symbols": [
|
|
2530
|
+
"print_int"
|
|
2531
|
+
],
|
|
2532
|
+
"requires": [
|
|
2533
|
+
"exam_io_fail",
|
|
2534
|
+
"exam_io_write"
|
|
2535
|
+
],
|
|
2536
|
+
"code": "static inline void print_int(int value, const char *end) {\n if (printf(\"%d\", value) < 0) exam_io_fail(\"output stream error\");\n exam_io_write(end);\n}\n"
|
|
2537
|
+
},
|
|
2538
|
+
{
|
|
2539
|
+
"id": "print_ints",
|
|
2540
|
+
"symbols": [
|
|
2541
|
+
"print_ints"
|
|
2542
|
+
],
|
|
2543
|
+
"requires": [
|
|
2544
|
+
"exam_io_fail",
|
|
2545
|
+
"exam_io_write",
|
|
2546
|
+
"print_int"
|
|
2547
|
+
],
|
|
2548
|
+
"code": "static inline void print_ints(const int *array, int count,\n const char *separator, const char *end) {\n if (count < 0 || (count && !array)) exam_io_fail(\"invalid int array\");\n for (int i = 0; i < count; ++i) {\n if (i) exam_io_write(separator);\n print_int(array[i], \"\");\n }\n exam_io_write(end);\n}\n"
|
|
2549
|
+
},
|
|
2550
|
+
{
|
|
2551
|
+
"id": "print_ll",
|
|
2552
|
+
"symbols": [
|
|
2553
|
+
"print_ll"
|
|
2554
|
+
],
|
|
2555
|
+
"requires": [
|
|
2556
|
+
"exam_io_fail",
|
|
2557
|
+
"exam_io_write"
|
|
2558
|
+
],
|
|
2559
|
+
"code": "static inline void print_ll(long long value, const char *end) {\n if (printf(\"%lld\", value) < 0) exam_io_fail(\"output stream error\");\n exam_io_write(end);\n}\n"
|
|
2560
|
+
},
|
|
2561
|
+
{
|
|
2562
|
+
"id": "print_lls",
|
|
2563
|
+
"symbols": [
|
|
2564
|
+
"print_lls"
|
|
2565
|
+
],
|
|
2566
|
+
"requires": [
|
|
2567
|
+
"exam_io_fail",
|
|
2568
|
+
"exam_io_write",
|
|
2569
|
+
"print_ll"
|
|
2570
|
+
],
|
|
2571
|
+
"code": "static inline void print_lls(const long long *array, int count,\n const char *separator, const char *end) {\n if (count < 0 || (count && !array)) exam_io_fail(\"invalid long long array\");\n for (int i = 0; i < count; ++i) {\n if (i) exam_io_write(separator);\n print_ll(array[i], \"\");\n }\n exam_io_write(end);\n}\n"
|
|
2572
|
+
},
|
|
2573
|
+
{
|
|
2574
|
+
"id": "print_double",
|
|
2575
|
+
"symbols": [
|
|
2576
|
+
"print_double"
|
|
2577
|
+
],
|
|
2578
|
+
"requires": [
|
|
2579
|
+
"exam_io_fail",
|
|
2580
|
+
"exam_io_write"
|
|
2581
|
+
],
|
|
2582
|
+
"code": "static inline void print_double(double value, int digits, const char *end) {\n if (digits < 0 || digits > 100) exam_io_fail(\"invalid double precision\");\n if (printf(\"%.*f\", digits, value) < 0) exam_io_fail(\"output stream error\");\n exam_io_write(end);\n}\n"
|
|
2583
|
+
},
|
|
2584
|
+
{
|
|
2585
|
+
"id": "print_doubles",
|
|
2586
|
+
"symbols": [
|
|
2587
|
+
"print_doubles"
|
|
2588
|
+
],
|
|
2589
|
+
"requires": [
|
|
2590
|
+
"exam_io_fail",
|
|
2591
|
+
"exam_io_write",
|
|
2592
|
+
"print_double"
|
|
2593
|
+
],
|
|
2594
|
+
"code": "static inline void print_doubles(const double *array, int count, int digits,\n const char *separator, const char *end) {\n if (count < 0 || (count && !array)) exam_io_fail(\"invalid double array\");\n for (int i = 0; i < count; ++i) {\n if (i) exam_io_write(separator);\n print_double(array[i], digits, \"\");\n }\n exam_io_write(end);\n}\n"
|
|
2595
|
+
},
|
|
2596
|
+
{
|
|
2597
|
+
"id": "print_char",
|
|
2598
|
+
"symbols": [
|
|
2599
|
+
"print_char"
|
|
2600
|
+
],
|
|
2601
|
+
"requires": [
|
|
2602
|
+
"exam_io_fail",
|
|
2603
|
+
"exam_io_write"
|
|
2604
|
+
],
|
|
2605
|
+
"code": "static inline void print_char(char value, const char *end) {\n if (putchar((unsigned char)value) == EOF) exam_io_fail(\"output stream error\");\n exam_io_write(end);\n}\n"
|
|
2606
|
+
},
|
|
2607
|
+
{
|
|
2608
|
+
"id": "print_text",
|
|
2609
|
+
"symbols": [
|
|
2610
|
+
"print_text"
|
|
2611
|
+
],
|
|
2612
|
+
"requires": [
|
|
2613
|
+
"exam_io_write"
|
|
2614
|
+
],
|
|
2615
|
+
"code": "static inline void print_text(const char *value, const char *end) {\n exam_io_write(value);\n exam_io_write(end);\n}\n"
|
|
2616
|
+
},
|
|
2617
|
+
{
|
|
2618
|
+
"id": "Record",
|
|
2619
|
+
"symbols": [
|
|
2620
|
+
"Record"
|
|
2621
|
+
],
|
|
2622
|
+
"requires": [],
|
|
2623
|
+
"code": "typedef struct Record Record;\n"
|
|
2624
|
+
},
|
|
2625
|
+
{
|
|
2626
|
+
"id": "exam_find_records_by",
|
|
2627
|
+
"symbols": [],
|
|
2628
|
+
"requires": [
|
|
2629
|
+
"Record",
|
|
2630
|
+
"exam_failf"
|
|
2631
|
+
],
|
|
2632
|
+
"code": "static inline int exam_find_records_by(const Record *records, int n,\n size_t element_size, const Record *key,\n int (*compare)(const Record *, const Record *)) {\n if (n < 0)\n exam_failf(\"algorithm\", \"find_records_by\", \"n=%d out of range (expected n >= 0)\", n);\n if (n && (!records || !key || !compare))\n exam_failf(\"algorithm\", \"find_records_by\", \"%s must not be NULL (n=%d)\",\n !records ? \"records\" : (!key ? \"key\" : \"compare\"), n);\n const unsigned char *bytes = (const unsigned char *)records;\n for (int i = 0; i < n; ++i) {\n const Record *record = (const Record *)(bytes + (size_t)i * element_size);\n if (compare(record, key) == 0) return i;\n }\n return -1;\n}\n"
|
|
2633
|
+
},
|
|
2634
|
+
{
|
|
2635
|
+
"id": "exam_binary_search_records_by",
|
|
2636
|
+
"symbols": [],
|
|
2637
|
+
"requires": [
|
|
2638
|
+
"Record",
|
|
2639
|
+
"exam_failf"
|
|
2640
|
+
],
|
|
2641
|
+
"code": "static inline int exam_binary_search_records_by(const Record *records, int n,\n size_t element_size, const Record *key,\n int (*compare)(const Record *, const Record *)) {\n if (n < 0)\n exam_failf(\"algorithm\", \"binary_search_records_by\", \"n=%d out of range (expected n >= 0)\", n);\n if (n && (!records || !key || !compare))\n exam_failf(\"algorithm\", \"binary_search_records_by\", \"%s must not be NULL (n=%d)\",\n !records ? \"records\" : (!key ? \"key\" : \"compare\"), n);\n const unsigned char *bytes = (const unsigned char *)records;\n int left = 0, right = n;\n while (left < right) {\n int mid = left + (right - left) / 2;\n const Record *record = (const Record *)(bytes + (size_t)mid * element_size);\n if (compare(record, key) < 0) left = mid + 1;\n else right = mid;\n }\n if (left == n) return -1;\n const Record *record = (const Record *)(bytes + (size_t)left * element_size);\n return compare(record, key) == 0 ? left : -1;\n}\n"
|
|
2642
|
+
},
|
|
2643
|
+
{
|
|
2644
|
+
"id": "find_records_by",
|
|
2645
|
+
"symbols": [
|
|
2646
|
+
"find_records_by"
|
|
2647
|
+
],
|
|
2648
|
+
"requires": [
|
|
2649
|
+
"exam_find_records_by"
|
|
2650
|
+
],
|
|
2651
|
+
"code": "#define find_records_by(array, n, key, compare) \\\n exam_find_records_by((array), (n), sizeof((array)[0]), (key), (compare))\n"
|
|
2652
|
+
},
|
|
2653
|
+
{
|
|
2654
|
+
"id": "binary_search_records_by",
|
|
2655
|
+
"symbols": [
|
|
2656
|
+
"binary_search_records_by"
|
|
2657
|
+
],
|
|
2658
|
+
"requires": [
|
|
2659
|
+
"exam_binary_search_records_by"
|
|
2660
|
+
],
|
|
2661
|
+
"code": "#define binary_search_records_by(array, n, key, compare) \\\n exam_binary_search_records_by((array), (n), sizeof((array)[0]), (key), (compare))\n"
|
|
2662
|
+
},
|
|
2663
|
+
{
|
|
2664
|
+
"id": "exam_swap_records",
|
|
2665
|
+
"symbols": [],
|
|
2666
|
+
"requires": [
|
|
2667
|
+
"Record"
|
|
2668
|
+
],
|
|
2669
|
+
"code": "static inline void exam_swap_records(Record *records, size_t a, size_t b, size_t size) {\n unsigned char *bytes = (unsigned char *)records;\n for (size_t i = 0; i < size; ++i) {\n unsigned char t = bytes[(size_t)a * size + i];\n bytes[(size_t)a * size + i] = bytes[(size_t)b * size + i];\n bytes[(size_t)b * size + i] = t;\n }\n}\n"
|
|
2670
|
+
},
|
|
2671
|
+
{
|
|
2672
|
+
"id": "exam_sift_records",
|
|
2673
|
+
"symbols": [],
|
|
2674
|
+
"requires": [
|
|
2675
|
+
"Record",
|
|
2676
|
+
"exam_swap_records"
|
|
2677
|
+
],
|
|
2678
|
+
"code": "static inline void exam_sift_records(Record *records, size_t root, size_t n, size_t size,\n int (*compare)(const Record *, const Record *)) {\n unsigned char *bytes = (unsigned char *)records;\n while (root < n / 2) {\n size_t child = root * 2 + 1;\n if (child + 1 < n &&\n compare((const Record *)(bytes + (size_t)child * size),\n (const Record *)(bytes + (size_t)(child + 1) * size)) < 0)\n ++child;\n if (compare((const Record *)(bytes + (size_t)root * size),\n (const Record *)(bytes + (size_t)child * size)) >= 0) break;\n exam_swap_records(records, root, child, size);\n root = child;\n }\n}\n"
|
|
2679
|
+
},
|
|
2680
|
+
{
|
|
2681
|
+
"id": "exam_sort_records",
|
|
2682
|
+
"symbols": [],
|
|
2683
|
+
"requires": [
|
|
2684
|
+
"Record",
|
|
2685
|
+
"exam_check_n",
|
|
2686
|
+
"exam_algo_fail",
|
|
2687
|
+
"exam_sift_records",
|
|
2688
|
+
"exam_swap_records"
|
|
2689
|
+
],
|
|
2690
|
+
"code": "static inline void exam_sort_records(Record *records, int n, size_t element_size,\n int (*compare)(const Record *, const Record *)) {\n exam_check_n(n);\n if (n < 2) return;\n if (!records || !element_size || !compare)\n exam_algo_fail(\"invalid record array or comparator\");\n size_t count = (size_t)n;\n for (size_t i = count / 2; i > 0; --i)\n exam_sift_records(records, i - 1, count, element_size, compare);\n for (size_t i = count - 1; i > 0; --i) {\n exam_swap_records(records, 0, i, element_size);\n exam_sift_records(records, 0, i, element_size, compare);\n }\n}\n"
|
|
2691
|
+
},
|
|
2692
|
+
{
|
|
2693
|
+
"id": "sort_records",
|
|
2694
|
+
"symbols": [
|
|
2695
|
+
"sort_records"
|
|
2696
|
+
],
|
|
2697
|
+
"requires": [
|
|
2698
|
+
"exam_sort_records"
|
|
2699
|
+
],
|
|
2700
|
+
"code": "#define sort_records(array, n, compare) \\\n exam_sort_records((array), (n), sizeof((array)[0]), (compare))\n"
|
|
2701
|
+
},
|
|
2702
|
+
{
|
|
2703
|
+
"id": "str_reverse",
|
|
2704
|
+
"symbols": [
|
|
2705
|
+
"str_reverse"
|
|
2706
|
+
],
|
|
2707
|
+
"requires": [],
|
|
2708
|
+
"code": "static inline void str_reverse(char *s) {\n size_t n = strlen(s);\n for (size_t i = 0; i < n/2; ++i) {\n char c = s[i]; s[i] = s[n-1-i]; s[n-1-i] = c;\n }\n}\n"
|
|
2709
|
+
},
|
|
2710
|
+
{
|
|
2711
|
+
"id": "str_lower",
|
|
2712
|
+
"symbols": [
|
|
2713
|
+
"str_lower"
|
|
2714
|
+
],
|
|
2715
|
+
"requires": [],
|
|
2716
|
+
"code": "static inline void str_lower(char *s) {\n for (size_t i = 0; s[i]; ++i)\n s[i] = (char)tolower((unsigned char)s[i]);\n}\n"
|
|
2717
|
+
},
|
|
2718
|
+
{
|
|
2719
|
+
"id": "str_upper",
|
|
2720
|
+
"symbols": [
|
|
2721
|
+
"str_upper"
|
|
2722
|
+
],
|
|
2723
|
+
"requires": [],
|
|
2724
|
+
"code": "static inline void str_upper(char *s) {\n for (size_t i = 0; s[i]; ++i)\n s[i] = (char)toupper((unsigned char)s[i]);\n}\n"
|
|
2725
|
+
},
|
|
2726
|
+
{
|
|
2727
|
+
"id": "str_trim",
|
|
2728
|
+
"symbols": [
|
|
2729
|
+
"str_trim"
|
|
2730
|
+
],
|
|
2731
|
+
"requires": [],
|
|
2732
|
+
"code": "static inline void str_trim(char *s) {\n size_t n = strlen(s), left = 0, right = n;\n while (left < n && isspace((unsigned char)s[left])) ++left;\n while (right > left && isspace((unsigned char)s[right-1])) --right;\n memmove(s, s+left, right-left);\n s[right-left] = '\\0';\n}\n"
|
|
2733
|
+
},
|
|
2734
|
+
{
|
|
2735
|
+
"id": "str_is_palindrome",
|
|
2736
|
+
"symbols": [
|
|
2737
|
+
"str_is_palindrome"
|
|
2738
|
+
],
|
|
2739
|
+
"requires": [],
|
|
2740
|
+
"code": "static inline bool str_is_palindrome(const char *s) {\n size_t n = strlen(s);\n for (size_t i = 0; i < n/2; ++i) if (s[i] != s[n-1-i]) return false;\n return true;\n}\n"
|
|
2741
|
+
},
|
|
2742
|
+
{
|
|
2743
|
+
"id": "str_count_char",
|
|
2744
|
+
"symbols": [
|
|
2745
|
+
"str_count_char"
|
|
2746
|
+
],
|
|
2747
|
+
"requires": [
|
|
2748
|
+
"exam_algo_fail"
|
|
2749
|
+
],
|
|
2750
|
+
"code": "static inline int str_count_char(const char *s, char key) {\n if (!key) exam_algo_fail(\"str_count_char does not accept NUL\");\n int count = 0;\n for (size_t i = 0; s[i]; ++i) if (s[i] == key) ++count;\n return count;\n}\n"
|
|
2751
|
+
},
|
|
2752
|
+
{
|
|
2753
|
+
"id": "str_count_substr",
|
|
2754
|
+
"symbols": [
|
|
2755
|
+
"str_count_substr"
|
|
2756
|
+
],
|
|
2757
|
+
"requires": [
|
|
2758
|
+
"exam_algo_fail"
|
|
2759
|
+
],
|
|
2760
|
+
"code": "static inline int str_count_substr(const char *s, const char *needle,\n bool overlapping) {\n if (!*needle) exam_algo_fail(\"str_count_substr empty pattern\");\n int count = 0;\n size_t advance = overlapping ? 1 : strlen(needle);\n const char *pos = s;\n while ((pos = strstr(pos, needle)) != NULL) {\n if (count == INT_MAX) exam_algo_fail(\"substring count overflow\");\n ++count;\n pos += advance;\n }\n return count;\n}\n"
|
|
2761
|
+
},
|
|
2762
|
+
{
|
|
2763
|
+
"id": "str_longest_char_run",
|
|
2764
|
+
"symbols": [
|
|
2765
|
+
"str_longest_char_run"
|
|
2766
|
+
],
|
|
2767
|
+
"requires": [
|
|
2768
|
+
"exam_algo_fail"
|
|
2769
|
+
],
|
|
2770
|
+
"code": "static inline int str_longest_char_run(const char *s, char target) {\n if (!target) exam_algo_fail(\"NUL cannot be a run target\");\n int best = 0, run = 0;\n for (size_t i = 0; s[i]; ++i) {\n run = s[i] == target ? run + 1 : 0;\n if (run > best) best = run;\n }\n return best;\n}\n"
|
|
2771
|
+
},
|
|
2772
|
+
{
|
|
2773
|
+
"id": "str_insert_into",
|
|
2774
|
+
"symbols": [
|
|
2775
|
+
"str_insert_into"
|
|
2776
|
+
],
|
|
2777
|
+
"requires": [
|
|
2778
|
+
"exam_failf"
|
|
2779
|
+
],
|
|
2780
|
+
"code": "static inline void str_insert_into(char *s, int pos, const char *text,\n size_t capacity) {\n if (!s || !text) {\n exam_failf(\"algorithm\", \"str_insert\", \"%s must not be NULL\", s ? \"text\" : \"destination\");\n return;\n }\n if (!capacity) {\n exam_failf(\"algorithm\", \"str_insert\", \"destination capacity is 0\");\n return;\n }\n if (!memchr(s, 0, capacity)) {\n exam_failf(\"algorithm\", \"str_insert\",\n \"destination is not NUL-terminated within capacity=%zu\", capacity);\n return;\n }\n size_t n = strlen(s), add = strlen(text);\n if (pos < 0 || (size_t)pos > n) {\n exam_failf(\"algorithm\", \"str_insert\", \"pos=%d out of range (expected 0..%zu)\", pos, n);\n return;\n }\n if (add > capacity - n - 1) {\n exam_failf(\"algorithm\", \"str_insert\",\n \"capacity=%zu too small (existing=%zu bytes, inserted=%zu bytes, plus NUL)\",\n capacity, n, add);\n return;\n }\n /* text must not alias s when insertion shifts the buffer. */\n memmove(s + pos + add, s + pos, n - (size_t)pos + 1);\n memcpy(s + pos, text, add);\n}\n"
|
|
2781
|
+
},
|
|
2782
|
+
{
|
|
2783
|
+
"id": "str_erase",
|
|
2784
|
+
"symbols": [
|
|
2785
|
+
"str_erase"
|
|
2786
|
+
],
|
|
2787
|
+
"requires": [
|
|
2788
|
+
"exam_algo_fail"
|
|
2789
|
+
],
|
|
2790
|
+
"code": "static inline void str_erase(char *s, int pos, int count) {\n size_t n = strlen(s);\n if (pos < 0 || count < 0 || (size_t)pos > n ||\n (size_t)count > n - (size_t)pos)\n exam_algo_fail(\"str_erase invalid range\");\n memmove(s + pos, s + pos + count, n - (size_t)pos - (size_t)count + 1);\n}\n"
|
|
2791
|
+
},
|
|
2792
|
+
{
|
|
2793
|
+
"id": "str_replace_char",
|
|
2794
|
+
"symbols": [
|
|
2795
|
+
"str_replace_char"
|
|
2796
|
+
],
|
|
2797
|
+
"requires": [
|
|
2798
|
+
"exam_algo_fail"
|
|
2799
|
+
],
|
|
2800
|
+
"code": "static inline int str_replace_char(char *s, char old_char, char new_char) {\n if (!old_char || !new_char) exam_algo_fail(\"NUL cannot be replaced\");\n int count = 0;\n for (size_t i = 0; s[i]; ++i) if (s[i] == old_char) {\n s[i] = new_char; ++count;\n }\n return count;\n}\n"
|
|
2801
|
+
},
|
|
2802
|
+
{
|
|
2803
|
+
"id": "str_remove_char",
|
|
2804
|
+
"symbols": [
|
|
2805
|
+
"str_remove_char"
|
|
2806
|
+
],
|
|
2807
|
+
"requires": [
|
|
2808
|
+
"exam_algo_fail"
|
|
2809
|
+
],
|
|
2810
|
+
"code": "static inline int str_remove_char(char *s, char key) {\n if (!key) exam_algo_fail(\"NUL cannot be removed\");\n size_t write = 0;\n int removed = 0;\n for (size_t read = 0; s[read]; ++read) {\n if (s[read] == key) ++removed;\n else s[write++] = s[read];\n }\n s[write] = '\\0';\n return removed;\n}\n"
|
|
2811
|
+
},
|
|
2812
|
+
{
|
|
2813
|
+
"id": "str_replace_all_into",
|
|
2814
|
+
"symbols": [
|
|
2815
|
+
"str_replace_all_into"
|
|
2816
|
+
],
|
|
2817
|
+
"requires": [
|
|
2818
|
+
"exam_algo_fail"
|
|
2819
|
+
],
|
|
2820
|
+
"code": "static inline int str_replace_all_into(char *s, const char *needle,\n const char *replacement,\n size_t capacity) {\n if (!s || !needle || !replacement || capacity == 0)\n exam_algo_fail(\"str_replace_all invalid arguments\");\n size_t old_len = strlen(needle), new_len = strlen(replacement);\n if (!old_len) exam_algo_fail(\"str_replace_all empty pattern\");\n char *out = (char *)malloc(capacity);\n if (!out) exam_algo_fail(\"str_replace_all allocation failed\");\n size_t written = 0;\n int count = 0;\n const char *src = s, *match;\n while ((match = strstr(src, needle)) != NULL) {\n size_t prefix = (size_t)(match - src);\n if (prefix > capacity - 1 - written ||\n new_len > capacity - 1 - written - prefix)\n { free(out); exam_algo_fail(\"str_replace_all exceeds capacity\"); }\n memcpy(out + written, src, prefix);\n written += prefix;\n memcpy(out + written, replacement, new_len);\n written += new_len;\n src = match + old_len;\n if (count == INT_MAX) { free(out); exam_algo_fail(\"replace count overflow\"); }\n ++count;\n }\n size_t tail = strlen(src);\n if (tail > capacity - 1 - written) {\n free(out); exam_algo_fail(\"str_replace_all exceeds capacity\");\n }\n memcpy(out+written, src, tail+1);\n memcpy(s, out, written+tail+1);\n free(out);\n return count;\n}\n"
|
|
2821
|
+
},
|
|
2822
|
+
{
|
|
2823
|
+
"id": "str_insert",
|
|
2824
|
+
"symbols": [
|
|
2825
|
+
"str_insert"
|
|
2826
|
+
],
|
|
2827
|
+
"requires": [
|
|
2828
|
+
"str_insert_into"
|
|
2829
|
+
],
|
|
2830
|
+
"code": "#define str_insert(array, pos, text) \\\n str_insert_into((array), (pos), (text), sizeof(array))\n"
|
|
2831
|
+
},
|
|
2832
|
+
{
|
|
2833
|
+
"id": "str_replace_all",
|
|
2834
|
+
"symbols": [
|
|
2835
|
+
"str_replace_all"
|
|
2836
|
+
],
|
|
2837
|
+
"requires": [
|
|
2838
|
+
"str_replace_all_into"
|
|
2839
|
+
],
|
|
2840
|
+
"code": "#define str_replace_all(array, needle, replacement) \\\n str_replace_all_into((array), (needle), (replacement), sizeof(array))\n"
|
|
2841
|
+
},
|
|
2842
|
+
{
|
|
2843
|
+
"id": "exam_cmp_unsigned_char",
|
|
2844
|
+
"symbols": [],
|
|
2845
|
+
"requires": [],
|
|
2846
|
+
"code": "static inline int exam_cmp_unsigned_char(const void *x, const void *y) {\n unsigned char a = *(const unsigned char *)x;\n unsigned char b = *(const unsigned char *)y;\n return (a > b) - (a < b);\n}\n"
|
|
2847
|
+
},
|
|
2848
|
+
{
|
|
2849
|
+
"id": "str_sort_chars",
|
|
2850
|
+
"symbols": [
|
|
2851
|
+
"str_sort_chars"
|
|
2852
|
+
],
|
|
2853
|
+
"requires": [
|
|
2854
|
+
"exam_cmp_unsigned_char"
|
|
2855
|
+
],
|
|
2856
|
+
"code": "static inline void str_sort_chars(char *s) {\n qsort(s, strlen(s), sizeof(char), exam_cmp_unsigned_char);\n}\n"
|
|
2857
|
+
},
|
|
2858
|
+
{
|
|
2859
|
+
"id": "str_unique_chars",
|
|
2860
|
+
"symbols": [
|
|
2861
|
+
"str_unique_chars"
|
|
2862
|
+
],
|
|
2863
|
+
"requires": [],
|
|
2864
|
+
"code": "static inline int str_unique_chars(char *s) {\n bool seen[256] = {false};\n size_t write = 0, length = strlen(s);\n for (size_t i = 0; i < length; ++i) {\n unsigned char c = (unsigned char)s[i];\n if (!seen[c]) { seen[c] = true; s[write++] = s[i]; }\n }\n s[write] = '\\0';\n return (int)write;\n}\n"
|
|
2865
|
+
},
|
|
2866
|
+
{
|
|
2867
|
+
"id": "exam_split_store",
|
|
2868
|
+
"symbols": [],
|
|
2869
|
+
"requires": [
|
|
2870
|
+
"exam_algo_fail"
|
|
2871
|
+
],
|
|
2872
|
+
"code": "static inline void exam_split_store(char *out_rows, size_t capacity_rows,\n size_t width, int row,\n const char *begin, size_t length) {\n if (row < 0 || (size_t)row >= capacity_rows || width == 0 ||\n length >= width || !out_rows)\n exam_algo_fail(\"str_split output capacity too small\");\n char *destination = out_rows + (size_t)row * width;\n memcpy(destination, begin, length);\n destination[length] = '\\0';\n}\n"
|
|
2873
|
+
},
|
|
2874
|
+
{
|
|
2875
|
+
"id": "str_split_into",
|
|
2876
|
+
"symbols": [
|
|
2877
|
+
"str_split_into"
|
|
2878
|
+
],
|
|
2879
|
+
"requires": [
|
|
2880
|
+
"exam_algo_fail",
|
|
2881
|
+
"exam_split_store"
|
|
2882
|
+
],
|
|
2883
|
+
"code": "static inline int str_split_into(const char *text, const char *delimiter,\n char *out_rows, size_t rows, size_t width) {\n if (!text || !delimiter || !delimiter[0])\n exam_algo_fail(\"str_split requires a nonempty delimiter\");\n size_t delimiter_length = strlen(delimiter);\n int count = 0;\n const char *begin = text;\n const char *match;\n while ((match = strstr(begin, delimiter)) != NULL) {\n if (count == INT_MAX) exam_algo_fail(\"too many split fields\");\n exam_split_store(out_rows, rows, width, count++,\n begin, (size_t)(match - begin));\n begin = match + delimiter_length;\n }\n if (count == INT_MAX) exam_algo_fail(\"too many split fields\");\n exam_split_store(out_rows, rows, width, count++, begin, strlen(begin));\n return count;\n}\n"
|
|
2884
|
+
},
|
|
2885
|
+
{
|
|
2886
|
+
"id": "str_split_whitespace_into",
|
|
2887
|
+
"symbols": [
|
|
2888
|
+
"str_split_whitespace_into"
|
|
2889
|
+
],
|
|
2890
|
+
"requires": [
|
|
2891
|
+
"exam_algo_fail",
|
|
2892
|
+
"exam_split_store"
|
|
2893
|
+
],
|
|
2894
|
+
"code": "static inline int str_split_whitespace_into(const char *text,\n char *out_rows,\n size_t rows, size_t width) {\n if (!text) exam_algo_fail(\"null str_split_whitespace input\");\n int count = 0;\n const char *p = text;\n while (*p) {\n while (*p && isspace((unsigned char)*p)) ++p;\n if (!*p) break;\n const char *begin = p;\n while (*p && !isspace((unsigned char)*p)) ++p;\n if (count == INT_MAX) exam_algo_fail(\"too many split fields\");\n exam_split_store(out_rows, rows, width, count++,\n begin, (size_t)(p - begin));\n }\n return count;\n}\n"
|
|
2895
|
+
},
|
|
2896
|
+
{
|
|
2897
|
+
"id": "str_join_into",
|
|
2898
|
+
"symbols": [
|
|
2899
|
+
"str_join_into"
|
|
2900
|
+
],
|
|
2901
|
+
"requires": [
|
|
2902
|
+
"exam_algo_fail"
|
|
2903
|
+
],
|
|
2904
|
+
"code": "static inline void str_join_into(const char *rows, int n,\n size_t row_capacity, size_t width,\n const char *delimiter,\n char *output, size_t capacity) {\n if (n < 0 || (size_t)n > row_capacity || !delimiter || !output ||\n capacity == 0 || width == 0 || (n > 0 && !rows))\n exam_algo_fail(\"invalid str_join arguments\");\n size_t separator_length = strlen(delimiter);\n size_t written = 0;\n for (int i = 0; i < n; ++i) {\n if (i) {\n if (separator_length > capacity - written - 1)\n exam_algo_fail(\"str_join output capacity too small\");\n memcpy(output + written, delimiter, separator_length);\n written += separator_length;\n }\n const char *word = rows + (size_t)i * width;\n const char *end = (const char *)memchr(word, '\\0', width);\n if (!end) exam_algo_fail(\"str_join input row is not NUL-terminated\");\n size_t length = (size_t)(end - word);\n if (length > capacity - written - 1)\n exam_algo_fail(\"str_join output capacity too small\");\n memcpy(output + written, word, length);\n written += length;\n }\n output[written] = '\\0';\n}\n"
|
|
2905
|
+
},
|
|
2906
|
+
{
|
|
2907
|
+
"id": "str_split",
|
|
2908
|
+
"symbols": [
|
|
2909
|
+
"str_split"
|
|
2910
|
+
],
|
|
2911
|
+
"requires": [
|
|
2912
|
+
"str_split_into"
|
|
2913
|
+
],
|
|
2914
|
+
"code": "#define str_split(text, delimiter, rows) \\\n str_split_into((text), (delimiter), (char *)(rows), \\\n sizeof(rows) / sizeof((rows)[0]), sizeof((rows)[0]))\n"
|
|
2915
|
+
},
|
|
2916
|
+
{
|
|
2917
|
+
"id": "str_split_whitespace",
|
|
2918
|
+
"symbols": [
|
|
2919
|
+
"str_split_whitespace"
|
|
2920
|
+
],
|
|
2921
|
+
"requires": [
|
|
2922
|
+
"str_split_whitespace_into"
|
|
2923
|
+
],
|
|
2924
|
+
"code": "#define str_split_whitespace(text, rows) \\\n str_split_whitespace_into((text), (char *)(rows), \\\n sizeof(rows) / sizeof((rows)[0]), \\\n sizeof((rows)[0]))\n"
|
|
2925
|
+
},
|
|
2926
|
+
{
|
|
2927
|
+
"id": "str_join",
|
|
2928
|
+
"symbols": [
|
|
2929
|
+
"str_join"
|
|
2930
|
+
],
|
|
2931
|
+
"requires": [
|
|
2932
|
+
"str_join_into"
|
|
2933
|
+
],
|
|
2934
|
+
"code": "#define str_join(rows, n, delimiter, output) \\\n str_join_into((const char *)(rows), (n), \\\n sizeof(rows) / sizeof((rows)[0]), sizeof((rows)[0]), \\\n (delimiter), (output), sizeof(output))\n"
|
|
2935
|
+
},
|
|
2936
|
+
{
|
|
2937
|
+
"id": "Time",
|
|
2938
|
+
"symbols": [
|
|
2939
|
+
"Time"
|
|
2940
|
+
],
|
|
2941
|
+
"requires": [],
|
|
2942
|
+
"code": "typedef struct { int hour, minute, second; } Time;\n"
|
|
2943
|
+
},
|
|
2944
|
+
{
|
|
2945
|
+
"id": "time_valid",
|
|
2946
|
+
"symbols": [
|
|
2947
|
+
"time_valid"
|
|
2948
|
+
],
|
|
2949
|
+
"requires": [
|
|
2950
|
+
"Time"
|
|
2951
|
+
],
|
|
2952
|
+
"code": "static inline bool time_valid(Time t) {\n return t.hour>=0 && t.hour<24 && t.minute>=0 && t.minute<60 &&\n t.second>=0 && t.second<60;\n}\n"
|
|
2953
|
+
},
|
|
2954
|
+
{
|
|
2955
|
+
"id": "exam_time_check",
|
|
2956
|
+
"symbols": [],
|
|
2957
|
+
"requires": [
|
|
2958
|
+
"Time",
|
|
2959
|
+
"time_valid",
|
|
2960
|
+
"exam_failf"
|
|
2961
|
+
],
|
|
2962
|
+
"code": "static inline void exam_time_check(const char *api, const char *argument, Time t) {\n if (!time_valid(t))\n exam_failf(\"algorithm\", api,\n \"invalid %s (hour=%d, minute=%d, second=%d; expected 0..23, 0..59, 0..59)\",\n argument, t.hour, t.minute, t.second);\n}\n"
|
|
2963
|
+
},
|
|
2964
|
+
{
|
|
2965
|
+
"id": "exam_time_seconds",
|
|
2966
|
+
"symbols": [],
|
|
2967
|
+
"requires": [
|
|
2968
|
+
"Time"
|
|
2969
|
+
],
|
|
2970
|
+
"code": "static inline int exam_time_seconds(Time t) {\n return 3600 * t.hour + 60 * t.minute + t.second;\n}\n"
|
|
2971
|
+
},
|
|
2972
|
+
{
|
|
2973
|
+
"id": "time_new",
|
|
2974
|
+
"symbols": [
|
|
2975
|
+
"time_new"
|
|
2976
|
+
],
|
|
2977
|
+
"requires": [
|
|
2978
|
+
"Time",
|
|
2979
|
+
"exam_time_check"
|
|
2980
|
+
],
|
|
2981
|
+
"code": "static inline Time time_new(int h, int m, int s) {\n Time t={h,m,s};\n exam_time_check(\"time_new\", \"time\", t);\n return t;\n}\n"
|
|
2982
|
+
},
|
|
2983
|
+
{
|
|
2984
|
+
"id": "time_read",
|
|
2985
|
+
"symbols": [
|
|
2986
|
+
"time_read"
|
|
2987
|
+
],
|
|
2988
|
+
"requires": [
|
|
2989
|
+
"Time",
|
|
2990
|
+
"exam_time_check",
|
|
2991
|
+
"exam_read_int_for"
|
|
2992
|
+
],
|
|
2993
|
+
"code": "static inline Time time_read(void) {\n Time t;\n t.hour = exam_read_int_for(\"time_read\", \"hour\");\n t.minute = exam_read_int_for(\"time_read\", \"minute\");\n t.second = exam_read_int_for(\"time_read\", \"second\");\n exam_time_check(\"time_read\", \"time\", t);\n return t;\n}\n"
|
|
2994
|
+
},
|
|
2995
|
+
{
|
|
2996
|
+
"id": "time_to_seconds",
|
|
2997
|
+
"symbols": [
|
|
2998
|
+
"time_to_seconds"
|
|
2999
|
+
],
|
|
3000
|
+
"requires": [
|
|
3001
|
+
"Time",
|
|
3002
|
+
"exam_time_check",
|
|
3003
|
+
"exam_time_seconds"
|
|
3004
|
+
],
|
|
3005
|
+
"code": "static inline int time_to_seconds(Time t) {\n exam_time_check(\"time_to_seconds\", \"time\", t);\n return exam_time_seconds(t);\n}\n"
|
|
3006
|
+
},
|
|
3007
|
+
{
|
|
3008
|
+
"id": "time_from_seconds",
|
|
3009
|
+
"symbols": [
|
|
3010
|
+
"time_from_seconds"
|
|
3011
|
+
],
|
|
3012
|
+
"requires": [
|
|
3013
|
+
"Time",
|
|
3014
|
+
"exam_failf"
|
|
3015
|
+
],
|
|
3016
|
+
"code": "static inline Time time_from_seconds(int seconds) {\n if (seconds < 0 || seconds >= 86400)\n exam_failf(\"algorithm\", \"time_from_seconds\", \"seconds=%d out of range (expected 0..86399)\", seconds);\n return (Time){seconds / 3600, (seconds / 60) % 60, seconds % 60};\n}\n"
|
|
3017
|
+
},
|
|
3018
|
+
{
|
|
3019
|
+
"id": "time_diff",
|
|
3020
|
+
"symbols": [
|
|
3021
|
+
"time_diff"
|
|
3022
|
+
],
|
|
3023
|
+
"requires": [
|
|
3024
|
+
"Time",
|
|
3025
|
+
"exam_time_check",
|
|
3026
|
+
"exam_time_seconds"
|
|
3027
|
+
],
|
|
3028
|
+
"code": "static inline int time_diff(Time a, Time b) {\n exam_time_check(\"time_diff\", \"a\", a);\n exam_time_check(\"time_diff\", \"b\", b);\n return exam_time_seconds(b) - exam_time_seconds(a);\n}\n"
|
|
3029
|
+
},
|
|
3030
|
+
{
|
|
3031
|
+
"id": "exam_shift_time",
|
|
3032
|
+
"symbols": [],
|
|
3033
|
+
"requires": [
|
|
3034
|
+
"Time",
|
|
3035
|
+
"exam_time_check",
|
|
3036
|
+
"exam_time_seconds",
|
|
3037
|
+
"time_from_seconds"
|
|
3038
|
+
],
|
|
3039
|
+
"code": "static inline Time exam_shift_time(const char *api, Time t, int delta) {\n exam_time_check(api, \"base time\", t);\n long long sum = (long long)exam_time_seconds(t) + delta;\n int seconds = (int)((sum % 86400 + 86400) % 86400);\n return time_from_seconds(seconds);\n}\n"
|
|
3040
|
+
},
|
|
3041
|
+
{
|
|
3042
|
+
"id": "time_add_seconds",
|
|
3043
|
+
"symbols": [
|
|
3044
|
+
"time_add_seconds"
|
|
3045
|
+
],
|
|
3046
|
+
"requires": [
|
|
3047
|
+
"Time",
|
|
3048
|
+
"exam_shift_time"
|
|
3049
|
+
],
|
|
3050
|
+
"code": "static inline Time time_add_seconds(Time t, int delta) {\n return exam_shift_time(\"time_add_seconds\", t, delta);\n}\n"
|
|
3051
|
+
},
|
|
3052
|
+
{
|
|
3053
|
+
"id": "time_next_second",
|
|
3054
|
+
"symbols": [
|
|
3055
|
+
"time_next_second"
|
|
3056
|
+
],
|
|
3057
|
+
"requires": [
|
|
3058
|
+
"Time",
|
|
3059
|
+
"exam_shift_time"
|
|
3060
|
+
],
|
|
3061
|
+
"code": "static inline Time time_next_second(Time t) {\n return exam_shift_time(\"time_next_second\", t, 1);\n}\n"
|
|
3062
|
+
},
|
|
3063
|
+
{
|
|
3064
|
+
"id": "time_next_minute",
|
|
3065
|
+
"symbols": [
|
|
3066
|
+
"time_next_minute"
|
|
3067
|
+
],
|
|
3068
|
+
"requires": [
|
|
3069
|
+
"Time",
|
|
3070
|
+
"exam_shift_time"
|
|
3071
|
+
],
|
|
3072
|
+
"code": "static inline Time time_next_minute(Time t) {\n return exam_shift_time(\"time_next_minute\", t, 60);\n}\n"
|
|
3073
|
+
},
|
|
3074
|
+
{
|
|
3075
|
+
"id": "time_print",
|
|
3076
|
+
"symbols": [
|
|
3077
|
+
"time_print"
|
|
3078
|
+
],
|
|
3079
|
+
"requires": [
|
|
3080
|
+
"Time",
|
|
3081
|
+
"exam_time_check",
|
|
3082
|
+
"exam_calendar_print"
|
|
3083
|
+
],
|
|
3084
|
+
"code": "static inline void time_print(Time t, const char *format, const char *end) {\n exam_time_check(\"time_print\", \"time\", t);\n const int values[] = {t.hour, t.minute, t.second};\n exam_calendar_print(\"time_print\", format, end, \"HMS\", values);\n}\n"
|
|
3085
|
+
},
|
|
3086
|
+
{
|
|
3087
|
+
"id": "try_read_int",
|
|
3088
|
+
"symbols": [
|
|
3089
|
+
"try_read_int"
|
|
3090
|
+
],
|
|
3091
|
+
"requires": [
|
|
3092
|
+
"exam_try_read_int"
|
|
3093
|
+
],
|
|
3094
|
+
"code": "static inline bool try_read_int(int *value) {\n return exam_try_read_int(\"try_read_int\", NULL, value);\n}\n"
|
|
3095
|
+
},
|
|
3096
|
+
{
|
|
3097
|
+
"id": "try_read_ll",
|
|
3098
|
+
"symbols": [
|
|
3099
|
+
"try_read_ll"
|
|
3100
|
+
],
|
|
3101
|
+
"requires": [
|
|
3102
|
+
"exam_try_read_ll"
|
|
3103
|
+
],
|
|
3104
|
+
"code": "static inline bool try_read_ll(long long *value) {\n return exam_try_read_ll(\"try_read_ll\", NULL, value);\n}\n"
|
|
3105
|
+
},
|
|
3106
|
+
{
|
|
3107
|
+
"id": "try_read_double",
|
|
3108
|
+
"symbols": [
|
|
3109
|
+
"try_read_double"
|
|
3110
|
+
],
|
|
3111
|
+
"requires": [
|
|
3112
|
+
"exam_try_read_double"
|
|
3113
|
+
],
|
|
3114
|
+
"code": "static inline bool try_read_double(double *value) {\n return exam_try_read_double(\"try_read_double\", NULL, value);\n}\n"
|
|
3115
|
+
}
|
|
3116
|
+
]
|
|
3117
|
+
}
|