abc220 E 树上计数

题意:完全二叉树上求有多少对点距离为d。

思路:因为是树上最短路,所以是经过他们的lca在过去,那么我们枚举左右两边的长度然后计算一下即可。

/*keep on going and never give up*/
#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define int long long
typedef pair<int, int> pii;
#define lowbit(x) x&(-x)
#define endl '\n'
#define wk is zqx ta die
const int mod = 998244353;
int pow(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) {
			res = res * a % mod;
		}
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
signed main() {
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, d;
	cin >> n >> d;
	int ans = 0;
	for (int i = 0; i <= d; i++) {
		int x = n - max(i, d - i);
		if (x <= 0) {
			continue;
		}
		x = pow(2, x) - 1;
		int y = max((int)0, i - 1);
		int k = max((int)0, d - i - 1);
		y = pow(2, y);
		k = pow(2, k);
		ans = (ans + x * y % mod * k % mod) % mod;
	}
	ans *= 2;
	ans %= mod;
	cout << ans << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值