10.2模擬賽
阿新 • • 發佈:2021-10-02
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=2e5+11,M=4e5+11; 4 int n,ans,siz[N]; 5 int to[M],nxt[M],id[M],edge,head[N]; 6 7 inline int re_ad() { 8 char ch=getchar(); int x=0,f=1; 9 while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); } 10 while('0'<=ch && ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar(); 11 return x*f; 12 } 13 14 inline void addedge(int x,int y,int z) { 15 ++edge,to[edge]=y,nxt[edge]=head[x],id[edge]=z,head[x]=edge; 16 ++edge,to[edge]=x,nxt[edge]=head[y],id[edge]=z,head[y]=edge; 17 } 18 19 voiddfs(int d,int f,int ff) { 20 for(int i=head[d];i;i=nxt[i]) if(to[i]!=f) dfs(to[i],d,id[i]),siz[d]+=siz[to[i]]; 21 ++siz[d]; if(n-siz[d]==siz[d]) ans=ff; 22 } 23 24 int main() 25 { 26 n=re_ad(); 27 for(int i=1,x,y;i<n;++i) x=re_ad(),y=re_ad(),addedge(x,y,i); 28 ans=-1,dfs(1,0,0);29 printf("%d\n",ans); 30 return 0; 31 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1e7+11,mod=1e9+7; 4 int n,m,fac[N],inv[N]; 5 inline int QuickPow(int x,int y) { int res=1; for(;y;y>>=1,x=(1ll*x*x)%mod) if(y&1) res=(1ll*res*x)%mod; return res; } 6 int main() 7 { 8 scanf("%d",&n); 9 fac[0]=1; for(int i=1;i<=n;++i) fac[i]=(1ll*fac[i-1]*i)%mod; 10 inv[n]=QuickPow(fac[n],mod-2); for(int i=n-1;i>=0;--i) inv[i]=(1ll*inv[i+1]*(i+1))%mod; 11 for(m=1;(m<<1)<=n;) m<<=1; 12 int ans=fac[m]; 13 ans=(1ll*ans*inv[n-m])%mod; 14 ans=(1ll*ans*inv[2*m-n])%mod; 15 ans=(1ll*ans*fac[n])%mod; 16 printf("%d\n",ans); 17 return 0; 18 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=5e3+11,mod=1e9+7; 4 int n,k,dp[2][N][2]; 5 int main() 6 { 7 scanf("%d%d",&n,&k); 8 dp[0][0][1]=1; for(int j=1;j<=k;++j) dp[0][j][0]=1; 9 for(int i=1,t=1;i<=n;++i,t^=1) { 10 memset(dp[t],0,sizeof(dp[t])); 11 for(int j=0;j<=k;++j) for(int l=0;l<=1;++l) { 12 if(j>=1) (dp[t][j][l]+=dp[t^1][j-1][l])%=mod; 13 if(j>=2) (dp[t][j][l]+=dp[t^1][j-2][l])%=mod; 14 if(j<k) (dp[t][j][l]+=dp[t^1][j+1][l])%=mod; 15 } 16 (dp[t][0][1]+=dp[t][0][0])%=mod,dp[t][0][0]=0; 17 } 18 int ans=0; 19 for(int j=0;j<=k;++j) (ans+=dp[n&1][j][1])%=mod; 20 printf("%d\n",ans); 21 return 0; 22 }