Comet OJ Contest #2
A:暴力,显然每两次至少翻一倍。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } ll n,s; signed main() { cin>>n; s=1; for (int i=2;;i++) { if (i==2) s++; else s+=s/2; if (s>n) {cout<<i;return 0;} } return 0; //NOTICE LONG LONG!!!!! }
B:列出柿子发现是二次函数。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int l,r,L,R; double ans,a,b,u; double calc(double x){return (-x*x+b*x-l*(L+R)*0.5)/(r-l);} signed main() { l=read(),r=read(),L=read(),R=read(); a=-1,b=(L+R)*0.5+l,u=-b/(2*a); double ans=0; if (l<=u&&u<=r) ans=calc(u); else if (u>r) ans=calc(r); printf("%.4f",max(0.0,ans)); return 0; //NOTICE LONG LONG!!!!! }
C:考虑大小为i的点集有多大的概率是独立集,则要求其内部C(i,2)条边均被破坏,概率显然为(x/y)C(i,2),累加各大小点集贡献即可。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define P 998244353 #define N 100010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,x,y,ans,fac[N],inv[N]; int ksm(int a,ll k) { int s=1; for (;k;k>>=1,a=1ll*a*a%P) if (k&1) s=1ll*s*a%P; return s; } int Inv(int a){return ksm(a,P-2);} int calc(int k) { return 1ll*ksm(x,1ll*k*(k-1)/2)*Inv(ksm(y,1ll*k*(k-1)/2))%P; } int C(int n,int m){return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;} signed main() { cin>>n>>x>>y; fac[0]=1;for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%P; inv[0]=inv[1]=1;for (int i=2;i<=n;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P; for (int i=2;i<=n;i++) inv[i]=1ll*inv[i]*inv[i-1]%P; for (int i=0;i<=n;i++) ans=(ans+1ll*C(n,i)*calc(i))%P; cout<<ans; return 0; //NOTICE LONG LONG!!!!! }
D:对于一个连通块的所有直径,其中点一定相同。考虑枚举中点,这个中点可以是某个点也可以是某条边,以其为树根。然后若要统计直径为i的点集数量,只要保证所选点深度均<=i/2,并且跨过树根有至少两个深度为i/2的点。随便怎么算。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define P 998244353 #define N 2010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,p[N],deep[N],cnt[N],tot[N],qwq[N],ctrb[N],pw[N],ans[N],t; struct data{int to,nxt; }edge[N<<1]; void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;} void dfs(int k,int from) { cnt[deep[k]=deep[from]+1]++; for (int i=p[k];i;i=edge[i].nxt) if (edge[i].to!=from) dfs(edge[i].to,k); } void inc(int &x,int y){x+=y;if (x>=P) x-=P;} int calc(int k,int *cnt){return 1ll*pw[cnt[k-1]]*(pw[cnt[k]-cnt[k-1]]-1)%P;} signed main() { n=read(); for (int i=1;i<n;i++) { int x=read(),y=read(); addedge(x,y),addedge(y,x); } pw[0]=1;for (int i=1;i<=n;i++) pw[i]=2ll*pw[i-1]%P; for (int i=1;i<=n;i++) { deep[i]=0; memset(tot,0,sizeof(tot)); memset(ctrb,0,sizeof(ctrb)); memset(qwq,0,sizeof(qwq)); for (int j=p[i];j;j=edge[j].nxt) { memset(cnt,0,sizeof(cnt)); dfs(edge[j].to,i); for (int k=1;k<=n/2;k++) cnt[k]+=cnt[k-1]; for (int k=1;k<=n/2;k++) ctrb[k]=1ll*ctrb[k]*pw[cnt[k]]%P; for (int k=1;k<=n/2;k++) inc(ctrb[k],1ll*qwq[k]*calc(k,cnt)%P); for (int k=1;k<=n/2;k++) qwq[k]=(1ll*qwq[k]*pw[cnt[k-1]]+1ll*pw[tot[k-1]]*calc(k,cnt))%P; for (int k=1;k<=n/2;k++) tot[k]+=cnt[k]; } for (int j=1;j<=n/2;j++) inc(ans[j*2],2ll*ctrb[j]%P); } for (int i=1;i<=n;i++) for (int j=p[i];j;j=edge[j].nxt) if (j&1) { int x=i,y=edge[j].to; deep[y]=-1;memset(cnt,0,sizeof(cnt)); dfs(x,y); for (int k=1;k<=n/2;k++) cnt[k]+=cnt[k-1]; for (int k=0;k<=n/2;k++) tot[k]=cnt[k]; deep[x]=-1;memset(cnt,0,sizeof(cnt)); dfs(y,x); for (int k=1;k<=n/2;k++) cnt[k]+=cnt[k-1]; for (int k=0;k<=n/2;k++) inc(ans[k*2+1],1ll*calc(k,cnt)*calc(k,tot)%P); } for (int i=1;i<n;i++) cout<<ans[i]<<endl; return 0; //NOTICE LONG LONG!!!!! }
E:先考虑树,显然有设f[i]为i最终醒来的概率,则初始f[i]=pi,依次合并子树有f[i]=f[i]+(1-f[i])*f[son]*sson。dp完后即转化为若干个环的问题。环上容易想到类似的做法,求出f[i]为i被自身或子树内点唤醒的概率后,有f[i]=f[i]+(1-f[i])*g[from[i]]*sfrom[i],即为答案。注意这里的g[i]指to[i]不被自身或子树内点唤醒的前提下i醒来的概率,而不能直接用f[i]代替做高斯消元。不考虑复杂度的话,这个g[i]可以通过枚举是谁唤醒他的求出。观察一下式子容易发现加加减减乘乘除除就可以递推出所有g[i]了。
#include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 #define P 998244353 #define N 100010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,a[N],b[N],p[N],to[N],from[N],f[N],g[N],ans[N],degree[N],q[N],t,cnt; bool flag[N]; struct data{int to,nxt; }edge[N]; int ksm(int a,int k) { assert(k>=0); int s=1; for (;k;k>>=1,a=1ll*a*a%P) if (k&1) s=1ll*s*a%P; return s; } int inv(int a){return ksm(a,P-2);} void dfs(int k) { f[k]=a[k]; for (int i=p[k];i;i=edge[i].nxt) if (!flag[edge[i].to]) { dfs(edge[i].to); f[k]=(f[k]+1ll*(P+1-f[k])*f[edge[i].to]%P*b[edge[i].to])%P; } ans[k]=f[k]; } void topsort() { int head=0,tail=0; for (int i=1;i<=n;i++) degree[to[i]]++; for (int i=1;i<=n;i++) if (!degree[i]) q[++tail]=i; while (head<tail) { int x=q[++head]; for (int i=p[x];i;i=edge[i].nxt) { degree[edge[i].to]--; if (!degree[edge[i].to]) q[++tail]=edge[i].to; } } for (int i=1;i<=n;i++) if (degree[i]) flag[i]=1; } void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;} signed main() { n=read(); for (int i=1;i<=n;i++) { int x=read(),y=read(); a[i]=1ll*x*inv(y)%P; } for (int i=1;i<=n;i++) { to[i]=read(); addedge(i,to[i]); } for (int i=1;i<=n;i++) { int x=read(),y=read(); b[i]=1ll*x*inv(y)%P; } topsort(); memset(p,0,sizeof(p));t=0; for (int i=1;i<=n;i++) addedge(to[i],i); for (int i=1;i<=n;i++) if (flag[i]) from[to[i]]=i; for (int i=1;i<=n;i++) if (flag[i]) dfs(i); for (int i=1;i<=n;i++) if (flag[i]) { int x=f[i],s=f[i]; for (int j=from[i];j!=to[i];j=from[j]) { x=1ll*x*b[j]%P*(P+1-f[to[j]])%P*f[j]%P*inv(f[to[j]])%P; s=(s+x)%P; } g[i]=s; for (int j=to[i];j!=i;j=to[j]) { s=(s-x+P)%P; s=1ll*s*b[from[j]]%P*(P+1-f[j])%P; x=1ll*x*b[from[j]]%P*(P+1-f[j])%P; x=1ll*x*inv(b[to[j]])%P*inv(P+1-f[to[to[j]]])%P; x=1ll*x*inv(f[to[j]])%P; x=1ll*x*(f[to[to[j]]])%P; s=(s+f[j])%P; g[j]=s; } for (int j=i;flag[j];j=to[j]) { flag[j]=0; ans[j]=(f[j]+1ll*(P+1-f[j])*g[from[j]]%P*b[from[j]])%P; } } for (int i=1;i<=n;i++) printf("%d ",ans[i]); return 0; //NOTICE LONG LONG!!!!! }