/
FCTRL_solution.cpp
273 lines (216 loc) · 4.92 KB
/
FCTRL_solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* Input
There is a single positive integer T on the first line of input (equal to about 100000). It stands for the number of numbers to follow. Then there are T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.
Output
For every number N, output a single line containing the single non-negative integer Z(N).
Example
Sample Input:
6
3
60
100
1024
23456
8735373
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <inttypes.h>
typedef unsigned int uint_t;
const uint_t T_MAX = 100000, N_MAX = 1000000000;
static uint16_t a[78125];
void do_prep()
{
for (uint_t i = 0; i < 15625; i++) {
uint_t i5 = i * 5;
uint_t v = i + a[i];
assert (v < 65536);
for (uint_t j = 0; j < 5; j++)
a[i5 + j] = v;
}
}
static inline uint_t do_calc(uint_t n)
{
uint_t q = n / 78125, r = n % 78125;
return 19531 * q + a[q] + a[r];
}
class RawReader
{
static const uint_t D = 10;
static const uint_t RBUF_SIZE = 8192;
char rbuf[RBUF_SIZE];
char *rlim, *rpos;
int rrlen, reof;
char* read_more(char *rpos)
{
int l, s, t;
char *p;
if (reof)
return rpos;
l = rrlen - (rpos - rbuf);
if (rpos != rbuf)
memmove(rbuf, rpos, l);
rpos = rbuf;
p = rbuf + l;
t = sizeof(rbuf) - l;
s = read(0, p, t);
if (s < 0)
s = 0;
rrlen = l + s;
rlim = rpos + rrlen;
if (s <= 0) {
reof = 1;
}
return rpos;
}
public:
void init(void)
{
rrlen = 0;
reof = 0;
rlim = rbuf;
rpos = rbuf;
}
inline int rnextf() { return *rpos++; }
inline int rnext()
{
if (rlim - rpos <= 0)
rpos = read_more(rpos);
return (rlim - rpos > 0) ? rnextf() : -1;
}
inline uint32_t getu32()
{
if (rlim - rpos < 12)
rpos = read_more(rpos);
uint32_t v;
do {
v = rnextf() - '0';
} while (v >= D);
uint32_t res = v;
for (;;) {
v = rnextf() - '0';
if (v >= D)
break;
res = res * D + v;
}
return res;
}
};
class RawWriter
{
static const uint_t WBUF_SIZE = 8192;
char wbuf[WBUF_SIZE];
char *wlim, *wpos;
int werr;
static bool sinit_ok;
static const uint_t D = 10, K = 3, DPK = 1000;
static char dgroups[DPK][4];
static void sinit()
{
if (sinit_ok)
return;
char dg[K];
for (uint_t j = 0; j < K; j++)
dg[j] = '0';
for (uint_t i = 0; i < DPK; i++) {
char *dgroup = dgroups[i];
for (uint_t j = 0; j < K; j++)
dgroup[j] = dg[j];
for (uint_t j = 0; j < K; j++) {
dg[j]++;
if (dg[j] != '0' + D)
break;
dg[j] = '0';
}
}
sinit_ok = 1;
}
public:
void write_all()
{
ssize_t len = wpos - wbuf;
if (len <= 0)
return;
ssize_t r = write(1, wbuf, len);
if (r != len) {
fprintf(
stderr,
"RawWriter: cannot write buffer, r=%d, errno=%d \n",
(int) r, errno
);
abort();
}
wpos = wbuf;
}
void init()
{
sinit();
wpos = wbuf;
wlim = wbuf + WBUF_SIZE;
}
inline void wnextf(char c) { *wpos++ = c; }
inline void wnext(char c)
{
if (wlim - wpos <= 0)
write_all();
*wpos++ = c;
}
void putu32(uint32_t x)
{
char buf[12];
uint_t l = 0;
while (x >= DPK) {
uint32_t r = x % DPK;
x = x / DPK;
const char *dgroup = dgroups[r];
for (uint_t i = 0; i < K; i++) {
buf[l + i] = dgroup[i];
}
l += K;
}
{
uint32_t r = x;
const char *dgroup = dgroups[r];
for (uint_t i = 0; i < K; i++) {
buf[l + i] = dgroup[i];
}
l += K;
}
do {
l--;
} while (buf[l] == '0' && l > 0);
l++;
buf[l] = 0;
if (wlim - wpos <= l)
write_all();
while (l > 0) {
l--;
*wpos++ = buf[l];
}
*wpos++ = '\n';
}
};
bool RawWriter::sinit_ok;
char RawWriter::dgroups[DPK][4];
int main()
{
do_prep();
uint_t t;
RawWriter rw;
rw.init();
RawReader rr;
rr.init();
t = rr.getu32();
assert (t <= T_MAX);
for (uint_t i = 0; i < t; i++) {
uint_t n;
n = rr.getu32();
uint_t res = do_calc(n);
rw.putu32(res);
}
rw.write_all();
return 0;
}