其他分享
首页 > 其他分享> > CF 1528E - Mashtali and Hagh Trees

CF 1528E - Mashtali and Hagh Trees

作者:互联网

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10, mod = 998244353;

int fpow_(int a, int b, int res = 1) {
	for (; b; b >>= 1, a = 1ll*a*a%mod)
		if (b&1)
			res = 1ll*res*a%mod;
	
	return res;
}

int n, i2 = fpow_(2, mod - 2), i6 = fpow_(6, mod - 2), ans, f[N], g[N], h[N];

int cal_(int x, int k) {
	if (k == 2) 
		return 1ll*x*(x + 1)%mod*i2%mod;
	
	return 1ll*x*(1ll*x*(x + 3)%mod + 2)%mod*i6%mod;
}

int main() {
	scanf("%lld", &n), n++;
	
	f[0] = g[0] = 1;
	
	for (int i = 1; i <= n; i++) {
		f[i] = (cal_(g[i - 1], 2) - cal_(g[i - 1] - f[i - 1], 2))%mod;
		g[i] = (g[i - 1] + f[i])%mod, h[i] = (1ll*h[i - 1] + f[i] - f[i - 1])%mod;
	}
	
	ans = 2ll*(cal_(g[n - 1], 3) - cal_(g[n - 1] - f[n - 1], 3))%mod - 1;
	
	for (int i = 2; i <= n - 2; i++)
		ans = (ans + 1ll*(f[i] - f[i - 1])*h[n - i])%mod;
	
	printf("%d\n", (ans + mod)%mod);
}



标签:Mashtali,外向,1528E,res,1ll,Hagh,int,内向,mod
来源: https://www.cnblogs.com/iqx37f/p/14837531.html