T1
数位DP,太菜了打挂只有10分……
Code
//2019.1.14 12:25~12:47 PaperCloud#include#define ll long longusing 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 llll 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\n",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#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 332748118struct 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 0?g:invg,(mod-1)/(i<<1)); for(p=i<<1,j=0;j >1]>>1)|((i&1)<<(di-1)); NTT(A,1);NTT(B,1); for(i=0;i
Blog来自PaperCloud,未经允许,请勿转载,TKS!