链接
题解
按树的最大深度递增的顺序考虑,最大深度为2的只有 n = 4 n = 4 n=4和 n = 5 n = 5 n=5两种情况,再来考虑最大深度为3,那么左右子树的深度都必须是2(这里是因为如果一边的深度小了,把整棵树转一转就能调整深度),考虑奇偶性之后右子树的节点数必须是偶数,左子树的节点个数无所谓,这样就得到了此时只有 n = 9 n = 9 n=9和 n = 10 n = 10 n=10两种情况,数学归纳法即可找出所有情况。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline LL read()
{
LL x = 0;
bool t = false;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-')
ch = getchar();
if (ch == '-')
t = true, ch = getchar();
while (ch <= '9' && ch >= '0')
x = x * 10 + ch - 48, ch = getchar();
return t ? -x : x;
}
int n;
int main()
{
n = read();
int x = 1;
while (x <= n)
{
if (n == x || n == x + 1)
{
printf("1\n");
return 0;
}
if (x & 1)
x = 2 * x + 2;
else
x = 2 * x + 1;
}
printf("0\n");
return 0;
}