[BZOJ-1002]轮状病毒

Description


如上图n轮状基由n个不同的基原子和圆心处的核原子构成。

n轮状病毒的产生就是n轮状基中删去若干条边,使得各原子间有唯一的信息通道。
求有多少个不同的n轮状病毒。

Solution

这是一道[FJOI2007]的题。
打表找规律,我们发现F[i] = 3 * F[i - 1] - F[i - 2] + 2;

Notice

我们发现可能会超出long long,所以我们要用到高精度。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define Rep(i, a, b) for (int i = (a); i < (b); i++)
#define travel(i, u) for (int i = head[u]; ~i; i = edge[i].next)

const ll INF = 1e9, Mo = 998244353;
const int N = 2000000;
const double eps = 1e-6;
namespace slow_IO
{
ll read()
{
ll x = 0; int zf = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * zf;
}
void write(ll y)
{
if (y < 0) putchar('-'), y = -y;
if (y > 9) write(y / 10);
putchar(y % 10 + '0');
}
}
using namespace slow_IO;

struct BigInteger
{
int len, negative;
int val[1005];
/* 转换 */
BigInteger(int x = 0)
{
I_TO_B(x);
}
BigInteger(char *st)
{
C_TO_B(st);
}
void C_TO_B(char *st)
{
negative = 0;
len = strlen(st);
memset(val, 0, sizeof(val));
rep(i, 0, len - 1) val[i] = st[len - i - 1] - '0';
if (st[0] == '-') val[--len] = 0, negative = 1;
while(len > 0 && val[len - 1] == 0) len--;
}
void I_TO_B(int x)
{
negative = 0;
if (x < 0) x = -x, negative = 1;
len = 0;
memset(val, 0, sizeof(val));
while(x)
{
val[len++] = x % 10;
x /= 10;
}
}
/*  输入输出 */
friend istream &operator >>(istream &in, BigInteger &t)
{
char st[1005];
in >> st;
t.C_TO_B(st);
return in;
}
friend ostream &operator <<(ostream &out, BigInteger &t)
{
if (t.negative) putchar('-');
per(i, t.len - 1, 0)
out << t.val[i];
return out;
}
void print()
{
if (negative) putchar('-');
per(i, len - 1, 0)
putchar(val[i] + '0');
putchar('\n');
}
/* 其他 */
friend void swap(BigInteger &x, BigInteger &y)
{
BigInteger t = x;
x = y;
y = t;
}
friend BigInteger operator -(BigInteger x)
{
BigInteger t = x;
t.negative = 1 - t.negative;
return t;
}
friend BigInteger abs(BigInteger x)
{
BigInteger t = x;
t.negative = 0;
return t;
}
/* 等于 or 不等于 */
friend bool operator ==(BigInteger x, BigInteger y)
{
if (x.negative != y.negative) return 0;
if (x.len != y.len) return 0;
per(i, x.len - 1, 0)
if (x.val[i] != y.val[i]) return 0;
return 1;
}
friend bool operator ==(BigInteger x, int t)
{
BigInteger y;
y.I_TO_B(t);
return x == y;
}
friend bool operator ==(int t, BigInteger y)
{
BigInteger x;
x.I_TO_B(t);
return x == y;
}
friend bool operator !=(BigInteger x, BigInteger y)
{
return !(x == y);
}
friend bool operator !=(BigInteger x, int t)
{
BigInteger y;
y.I_TO_B(t);
return !(x == y);
}
friend bool operator !=(const int t, const BigInteger y)
{
BigInteger x;
x.I_TO_B(t);
return !(x == y);
}
/* 小于 or 大于 */
friend bool operator <(BigInteger x, BigInteger y)
{
if (x.negative && !y.negative) return 1;
if (!x.negative && y.negative) return 0;
if (x.negative && y.negative) swap(x, y);
if (x.len < y.len) return 1;
if (x.len > y.len) return 0;
per(i, x.len - 1, 0)
if (x.val[i] < y.val[i]) return 1;
else if (x.val[i] > y.val[i]) return 0;
return 0;
}
friend bool operator <(BigInteger x, int t)
{
BigInteger y;
y.I_TO_B(t);
return x < y;
}
friend bool operator <(int t, BigInteger y)
{
BigInteger x;
x.I_TO_B(t);
return x < y;
}
friend bool operator >(BigInteger x, BigInteger y)
{
return y < x;
}
friend bool operator >(BigInteger x, int y)
{
return y < x;
}
friend bool operator >(int x, BigInteger y)
{
return y < x;
}
/* 小于等于 or 大于等于 */
friend bool operator <=(BigInteger x, BigInteger y)
{
return x < y || x == y;
}
friend bool operator <=(BigInteger x, int t)
{
BigInteger y;
y.I_TO_B(t);
return x < y || x == y;
}
friend bool operator <=(int t, BigInteger y)
{
BigInteger x;
x.I_TO_B(t);
return x < y || x == y;
}
friend bool operator >=(BigInteger x, BigInteger y)
{
return y <= x;
}
friend bool operator >=(BigInteger x, int y)
{
return y <= x;
}
friend bool operator >=(int x, BigInteger y)
{
return y <= x;
}
/* 加法 */
friend BigInteger operator +(BigInteger x, BigInteger y)
{
if (x.negative && y.negative) return -(abs(x) + abs(y));
if (x.negative && !y.negative) return y - abs(x);
if (!x.negative && y.negative) return x - abs(y);
int len = x.len > y.len ? x.len : y.len;
BigInteger ans;
rep(i, 0, len - 1)
{
ans.val[i] += x.val[i] + y.val[i];
ans.val[i + 1] += ans.val[i] / 10;
ans.val[i] %= 10;
}
if (ans.val[len] != 0) len++;
ans.len = len;
return ans;
}
friend BigInteger operator +(BigInteger x, int t)
{
BigInteger y;
y.I_TO_B(t);
return x + y;
}
friend void operator +=(BigInteger &x, BigInteger y)
{
x = x + y;
}
friend void operator +=(BigInteger &x, int t)
{
x = x + t;
}
friend void operator ++(BigInteger &x)
{
x += 1;
}
/* 减法 */
friend BigInteger operator -(BigInteger x, BigInteger y)
{
if (x.negative && y.negative) return abs(y) - abs(x);
if (x.negative && !y.negative) return -(abs(x) + y);
if (!x.negative && y.negative) return x + abs(y);
BigInteger ans;
if (x < y)
{
swap(x, y);
ans.negative = 1;
}
int len = max(x.len, y.len);
rep(i, 0, len - 1)
{
ans.val[i] += x.val[i] - y.val[i];
if (ans.val[i] < 0)
{
ans.val[i] += 10;
ans.val[i + 1]--;
}
}
while(len > 1 && ans.val[len - 1] == 0) len--;
ans.len = len;
return ans;
}
friend BigInteger operator -(int t, BigInteger y)
{
BigInteger x;
x.I_TO_B(t);
return x - y;
}
friend BigInteger operator -(BigInteger x, int t)
{
BigInteger y;
y.I_TO_B(t);
return x - y;
}
friend void operator -=(BigInteger &x, BigInteger y)
{
x = x - y;
}
friend void operator -=(BigInteger &x, int y)
{
x = x - y;
}
friend void operator --(BigInteger &x)
{
x -= 1;
}
/* 乘法 */
friend BigInteger operator *(BigInteger x, BigInteger y)
{
BigInteger ans;
ans.negative = (x.negative ^ y.negative);
int len = x.len + y.len;
rep(i, 0, x.len - 1)
rep(j, 0, y.len - 1)
ans.val[i + j] += x.val[i] * y.val[j];
rep(i, 0, len - 1)
{
ans.val[i + 1] += ans.val[i] / 10;
ans.val[i] %= 10;
}
while(len > 1 && ans.val[len - 1] == 0) len--;
ans.len = len;
return ans;
}
friend BigInteger operator *(BigInteger x, int t)
{
BigInteger y;
y.I_TO_B(t);
return x * y;
}
friend BigInteger operator *(int t, BigInteger y)
{
BigInteger x;
x.I_TO_B(t);
return x * y;
}
friend void operator *=(BigInteger &x, BigInteger y)
{
x = x * y;
}
friend void operator *=(BigInteger &x, int t)
{
x = x * t;
}
/* 除法 */
friend BigInteger operator /(BigInteger x, BigInteger y)
{
BigInteger R, ans;
if (y.len == 1 && y.val[0] == 0) return R;
per(i, x.len - 1, 0)
{
ans *= 10;
R = R * 10 + x.val[i];
while(R >= y)
{
++ans;
R -= y;
}
}
return ans;
}
friend BigInteger operator /(BigInteger x, int t)
{
BigInteger y;
y.I_TO_B(t);
return x / y;
}
friend BigInteger operator %(BigInteger x, BigInteger y)
{
return x - x / y;
}
friend BigInteger operator %(BigInteger x, int t)
{
return x - x / t;
}
friend void operator /=(BigInteger &x, BigInteger y)
{
x = x / y;
}
friend void operator /=(BigInteger &x, int t)
{
x = x / t;
}
friend void operator %=(BigInteger &x, BigInteger y)
{
x = x % y;
}
friend void operator %=(BigInteger &x, int t)
{
x = x % t;
}
}Now, Pre1, Pre2;

int sqz()
{
Pre1 = 1, Pre2 = 0;
int n = read();
if (n == 1) puts("1");
else
{
rep(i, 2, n)
{
Now = Pre1 * 3 - Pre2 + 2;
Pre2 = Pre1, Pre1 = Now;
}
Now.print();
}
return 0;
}
文章目录
  1. 1. Description
  2. 2. Solution
  3. 3. Notice
  4. 4. Code