- 一路向北
警示后人
- @ 2026-8-29 16:15:51
#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)
const int NR=5005,M=1e9+7;
int n,q,u[NR],v[NR],f[NR][NR],sz[NR],g[NR],x,y;
int C[NR][NR],f2[NR][NR];
int pre[NR<<1],to[NR<<1],h[NR],ecnt=0;
void newedge(int x,int y) {
to[++ecnt]=y;
pre[ecnt]=h[x];
h[x]=ecnt;
}
void Dfs(int u=1,int fa=1,int son=0) {
for(int i=h[u];i;i=pre[i]) {
int v=to[i];
if(v!=fa) {
++son;
Dfs(v,u);
if(son==1) {
rep(j,0,sz[v]-1) {
f[u][j]=f[v][j];
}
} else {
rep(i,0,sz[u]+sz[v]-1)g[i]=0;
rep(j,0,sz[v]-1) {
rep(k,0,sz[u]-1) {
g[j+k]=(g[j+k]+1ll*f[u][k]*f[v][j]%M*C[sz[u]-k+sz[v]-j][sz[u]-k]%M)%M;
}
}
rep(i,0,sz[u]+sz[v]-1) f[u][i]=g[i];
};
sz[u]+=sz[v];
}
}
sz[u]++;
if(son) {
g[0]=1ll*f[u][0]*(sz[u]-1)%M;
rep(i,1,sz[u]-1) {
g[i]=(1ll*f[u][i]*(sz[u]-1-(i-1))%M+1ll*f[u][i-1]*(sz[u]-1-(i-1))%M)%M;
}
rep(i,0,sz[u]-1)
f[u][i]=g[i];
} else {
f[u][0]=1;
}
}
int qp(int a,int b=M-2,int res=1){
while(b){
if(b&1)res=(1ll*res*a)%M;
a=(1ll*a*a)%M;
b>>=1;
}
return res;
}
int main() {
freopen("north.in","r",stdin);
freopen("north.out","w",stdout);
cin>>n>>q;
C[0][0]=1;
rep(i,1,n) {
C[i][0]=1;
rep(j,1,i) {
C[i][j]=(C[i-1][j-1]+C[i-1][j])%M;
}
}
rep(i,1,n-1) {
cin>>u[i]>>v[i];
newedge(u[i],v[i]);
newedge(v[i],u[i]);
}
Dfs();
while(q--) {
cin>>x>>y;
int ans=0;
rep(j,y,n){
if((j-y)&1)ans=(ans-1ll*C[j][y]*f[x][j]%M+M)%M;
else ans=(ans+1ll*C[j][y]*f[x][j]%M)%M;
}
cout<<ans<<'\n';
}
return 0;
}
考场代码,猜猜错哪了。
1 条评论
-
C2024LiRH LV 3 @ 2026-8-29 16:26:05注意到 csyzoj 显然 << 洛谷 老师,证明如下:
- 使用人数中,csyzoj << luogu
- 题目总数中,csyzoj << luogu
- 评测速度中,csyzoj << luogu
etc.
不难得到推论:
推论 1:csyzoj 的功能,luogu 均有;
不难得到进一步推论:
推论 2:luogu 没有的功能,csyzoj 一定没有。
证明:命题与其逆否命题同真假。
注意到:
定理:luogu 无法使用 freopen
得到结论
你使用了 freopen。
🌿 1
- 1
信息
- ID
- 22
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 48
- 已通过
- 10
- 上传者