题解:U541098 昌九城际上的奇怪生物
题目传送门
考虑用队列暴力模拟题意。把所有兔子的长度都存入一个队列中,每次从队头把队列里元素全都取出来,对每一个元素分裂,如果有变成
#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;
const int N = 1e4 + 10;
ull n, ans, cnt = 1, cnt2;
queue<ull> q;
int main(){
scanf("%llu", &n);
q.push(2);
for(ull i = 1; i <= n; i++){
ans *= 2;
cnt2 = 2 * cnt;
for(ull j = 1; j <= cnt; j++){
ull t = q.front();
q.pop();
if(t == 1) ans++, cnt2--;
else q.push(t - 1);
q.push(t + 1);
}
cnt = cnt2;
}
printf("%llu", ans);
return 0;
}
提交一看,才
事已至此,打个表先(
把前
0 1 0 2 0 5 14 0 42 0 132 0 429 0 1430 0 4862 0 16796
注意力惊人の朋友肯定已经看出来了:第奇数个午夜没有兔子消失,而第偶数个午夜消失的兔子数组成了
前一个规律比较好理解,长度为
我们尝试把兔子的分裂路径在坐标系中画出来。其中横坐标表示时间,纵坐标表示兔子的长度,那么问题就变成了:
从
考虑到
注意力惊人の朋友肯定又看出来了:这有点像
不过,我们可以套用类似的研究方法。因为除终点外路径严格高于
那么,这些路径就变成是从 (其实就是严谨证明太麻烦了,我不会)。将两式相减,就是我们的问题最终的答案:
现在,我们对其变形。原式等于
根据组合数递推式,我们有:
那么,原式就等于
而这正是第
这是很好证明的,直接把两边暴力展开即可。由
可以看到,
最后,程序的时间复杂度就为
#include<bits/stdc++.h>
using namespace std;
const int LEN = 3020;
int n, cnt;
int tmp[LEN], tmp2[LEN], emp[LEN], H[LEN], ans[LEN]; //emp是空数组,表示高精度的0,H是Catalan数
//高精度
int get_len(int a[]) {
int res = 0;
for (int i = 3015; i >= 1; i--) {
if (a[i]) {
res = i;
break;
}
}
return res;
}
void clear(int a[]) {
int len = get_len(a);
for (int i = len; i >= 1; i--) a[i] = 0;
}
int la, lb, lc; //高精度数的位数
void add(int a[], int b[], int c[]) {
clear(c);
for (int i = 1; i <= lc; i++) {
c[i] += a[i] + b[i];
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
if (c[lc + 1]) lc++;
}
void mul(int a[], int b, int c[]) {
clear(c);
for (int i = 1; i <= la; i++)
c[i] += a[i] * b;
for (int i = 1; i < lc; i++) {
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
while (c[lc] == 0 && lc > 1) lc--;
}
void div(int a[], int b, int c[]) {
clear(c);
long long t = 0;
for (int i = la; i >= 1; i--) {
t = t * 10 + a[i];
c[i] = t / b;
t %= b;
}
while (c[lc] == 0 && lc > 1) lc--;
}
void print(int a[]) {
int len = get_len(a);
for (int i = len; i >= 1; i--) putchar(a[i] + '0');
}
int main() {
scanf("%d", &n);
H[1] = 1;
if (n <= 1) putchar('0');
else {
for (int i = 2; i <= n; i++) {
la = lc;
mul(ans, 2, tmp);
if (!(i & 1)) {
cnt++;
la = get_len(H);
lc = la + (4 * cnt - 2) % 10 + 1;
mul(H, 4 * cnt - 2, tmp2);
la = lc;
div(tmp2, cnt + 1, H);
la = get_len(tmp);
lb = lc;
lc = max(la, lb);
add(tmp, H, ans);
} else {
la = lc;
add(tmp, emp, ans);
}
}
}
print(ans);
return 0;
}
但如果你用的是 Python,那就没什么高精度的事了,跑的也飞快。
c = 1
cnt = 0
ans = 0
n = int(input())
if n > 1:
for i in range(2, n + 1):
ans *= 2
if i % 2 == 0:
cnt += 1
c = c * (4 * cnt - 2) // (cnt + 1)
ans += c
print(str(ans))