[20190113]四校联考

T1

数位DP,太菜了打挂只有10分……


Code 

//2019.1.14 12:25~12:47 PaperCloud
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
#define mod 1000000007
#define int ll
ll B,n,m,a[100005],L[100005],R[100005],P[100005][2],S[100005][2],M[100005];
ll O,Ans=0;
int dp(int o)
{
	S[o+1][0]=S[o+1][1]=P[o+1][0]=P[o+1][1]=0;
	for(register int i=o;i;--i)
	{
		P[i][1]=(P[i+1][1]+1)*a[i]%mod;
		P[i][0]=(P[i+1][0]+M[i+1])%mod*O%mod+(a[i]*(a[i]-1)/2%mod)*(P[i+1][1]+1)%mod;P[i][0]%=mod;
		S[i][1]=(S[i+1][1]+P[i][1])%mod;
		S[i][0]=(S[i+1][1]*a[i]%mod+S[i+1][0]*B%mod+P[i][0])%mod;
	}
	return (S[1][0]+S[1][1])%mod;
}
main()
{
	freopen("number.in","r",stdin);
	freopen("number.out","w",stdout);
	register int i,j;
	B=read();O=B*(B-1)/2;
	n=read();for(i=1;i<=n;++i) L[n-i+1]=read();
	m=read();for(j=1;j<=m;++j) R[m-j+1]=read();
	for(j=1;!L[j];++j) L[j]=B-1;L[j]--;
	if(L[j]==0&&j==n) n--;
	
	for(M[n+1]=0,M[n]=a[n]=L[n],i=n-1;i;--i) a[i]=L[i],M[i]=M[i+1]*B%mod+L[i]%mod,M[i]%=mod;Ans-=dp(n);Ans+=mod;
	for(M[m+1]=0,M[m]=a[m]=R[m],i=m-1;i;--i) a[i]=R[i],M[i]=M[i+1]*B%mod+R[i]%mod,M[i]%=mod;Ans+=dp(m);Ans%=mod;
	return 0*printf("%lld
",Ans);
}

T2

求树上任选(k)个点点形成的所有虚树大小之和。答案对(998244353)取模

首先,如果选(k)个点,考虑一个点(x)会在几个虚树内,应该是:(C_{n}^{k}-C_{son[i]}^{k}),其中,(son[i])表示以(x)为根时,一个(x)的子树的大小。

然后可以直接处理成类似(ans_k=sum a_iC_{i}^{k})的形式

进一步转化为(ans_k=(k!)^{-1}sum (i!a_i)(i-k)!^{-1}),这可以(NTT)优化


Code 

//2019.1.14 12:52~16:49 PaperCloud
#include<bits/stdc++.h>
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
#define MN 100005
#define mod 998244353
#define g 3
#define invg 332748118
struct edge{int to,nex;}e[MN<<1];
int N,en,hr[MN];
inline void ins(int f,int t)
{
	e[++en]=(edge){t,hr[f]};hr[f]=en;
	e[++en]=(edge){f,hr[t]};hr[t]=en;
}
int fac[MN],inv[MN],A[524288],B[524288],siz[MN];
inline int fpow(int x,int m){int r=1;for(;m;m>>=1,x=1ll*x*x%mod) if(m&1) r=1ll*r*x%mod;return r;}
inline int C(int m,int n){return 1ll*fac[m]*inv[n]%mod*1ll*inv[m-n]%mod;}
inline void init()
{
	register int i;
	for(i=fac[0]=1;i<=N;++i) fac[i]=1ll*fac[i-1]*i%mod;
	for(inv[N]=fpow(fac[N],mod-2),i=N-1;~i;--i) inv[i]=1ll*inv[i+1]*(i+1)%mod;
}
int cnt=0;
inline void dfs(int x=1,int f=0)
{
	register int i;siz[x]=1;
	for(i=hr[x];i;i=e[i].nex)if(e[i].to^f) dfs(e[i].to,x),siz[x]+=siz[e[i].to],A[siz[e[i].to]]--;
	A[N-siz[x]]--;
}
int M,di,pos[524288],invM;

inline void NTT(int *a,int type)
{
    register int i,j,p,k;
    for(i=0;i<M;++i)if(i<pos[i]) std::swap(a[i],a[pos[i]]);
    for(i=1;i<M;i<<=1)
    {
        ll wn=fpow(type>0?g:invg,(mod-1)/(i<<1));
        for(p=i<<1,j=0;j<M;j+=p) 
        {
            ll w=1;
            for(k=0;k<i;++k,w=w*wn%mod)
            {
                ll X=a[j+k],Y=w*a[j+i+k]%mod;
                a[j+k]=(X+Y)%mod;a[j+i+k]=(X-Y+mod)%mod;
            }
        }
    }
    if(type==-1) for(i=0;i<M;++i) a[i]=1ll*a[i]*invM%mod;
}
int main()
{
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	N=read();
	register int i,x;
	for(i=1;i<N;++i) x=read(),ins(x,read());
	init();dfs();
	for(i=1;i< N;++i) A[i]=(A[i]+mod)%mod;A[N]=N;
	for(i=1;i<=N;++i) A[i]=1ll*A[i]*fac[i]%mod;
	for(i=0;i<=N;++i) B[i]=inv[N-i];A[0]=0;
	for(M=1,di=0;M<=N<<1;M<<=1,di++);invM=fpow(M,mod-2);
	for(i=0;i<M;++i) pos[i]=(pos[i>>1]>>1)|((i&1)<<(di-1));
	NTT(A,1);NTT(B,1);
	for(i=0;i<M;++i) A[i]=1ll*A[i]*B[i]%mod;
	NTT(A,-1);
	
	for(i=1;i<=N;++i)
	printf("%d
",(1ll*A[N+i]*inv[i])%mod);
	return 0;
}


Blog来自PaperCloud,未经允许,请勿转载,TKS!

原文地址:https://www.cnblogs.com/PaperCloud/p/10268705.html