题解:AT_abc290_f [ABC290F] Maximum Diameter
upd on 2025.12.21:修改一处错误
好题。严肃学习范德蒙德卷积。
首先度的总数是
这篇题解中不使用“叶节点”,取而代之的是度数为
然后我们考虑当有
一种最优的构造方案就是将剩下的
我们考虑去计数这种情况有多少种。
首先,从
我们总共有
由插板法得知将
那么一个
由于无解为
其中,当
引出两个式子:
- 吸纳恒等式:
k \cdot C_n^k=n \cdot C_{n-1}^{k-1} 。 - 范德蒙德卷积:
\sum_{i=0}^k C_n^i \cdot C_m^{k-i}=C_{n+m}^k 。
那么我们就可以推导出如下的式子:
那么这个东西就可以预处理组合数然后
code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 2e6+1,mod = 998244353;
namespace fastIO{char *p1,*p2,buf[100000];inline void read(int&n){int x=0,f=1;char ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++);while(ch<48||ch>57){if(ch=='-'){f=-1;}ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++);}while(ch>=48&&ch<=57){x=(x<<3)+(x<<1)+(ch^48),ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++);}n=x*f;}inline void read(string&s){s="";char ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++);while(ch==' '||ch=='\n'){ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++);}while(ch!=' '&&ch!='\n'){s+=ch,ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++);}}inline void read(char&ch){ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++);while(ch==' '||ch=='\n'){ch=(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++);}}inline void write(int x){if(x<0){putchar('-'),x=-x;}if(x>9){write(x/10);}putchar(x%10+'0');return;}inline void write(const string&s){for(int i=0;i<(int)s.size();++i){putchar(s[i]);}}inline void write(const char&c){putchar(c);}}using namespace fastIO;
int T,n,fact[N],inv[N];
inline int qpow(int base,int e){
int res = 1;
while(e){
if(e&1){
res = res*base%mod;
}
base = base*base%mod;
e>>=1;
}
return res;
}
void init(){
fact[0] = 1;
for(int i = 1;i<N;++i){
fact[i] = fact[i-1]*i%mod;
}
inv[N-1] = qpow(fact[N-1],mod-2);
for(int i = N-2;i>=0;--i){
inv[i] = inv[i+1]*(i+1)%mod;
}
}
inline int C(const int &n,const int &m){
return fact[n]*inv[m]%mod*inv[n-m]%mod;
}
signed main(){
init();
int T;
read(T);
while(T--){
read(n);
write(((n-3)*C(2*n-4,n-2)%mod+2*C(2*n-3,n-1)%mod)%mod),write('\n');
}
return 0;
}