#include<bits/stdc++.h> #define il inline #define ll long long constint N=5005,P=1e9+7; int n,m,s,ans,p[N],sz[N],f[N][N],to[N+N],nx[N+N],hd[N],sze; bool o[N]; il intksm(int a,int b){int res=1; for ( ; b; b>>=1,a=(ll)a*a%P) if (b&1) res=(ll)res*a%P; return res;} il voidadd(int u,int v){to[++sze]=v,nx[sze]=hd[u],hd[u]=sze;} il voiddfs(int u,int c) { o[u]=1; if (c<0) sz[-c]++; int i,v; for (i=hd[u]; i; i=nx[i]) if ((v=to[i])!=c) dfs(v,c); else s++; } intmain() { scanf("%d",&n); int i,j; for (i=1; i<=n; i++) scanf("%d",p+i),~p[i]&&(add(p[i],i),0); for (i=1; i<=n; i++) if (p[i]<0) m++,dfs(i,-m); for (i=1; i<=n; i++) if (!o[i]) dfs(i,i); ans=(ll)s*ksm(n-1,m)%P; for (i=0; i<=m; i++) for (f[i][0]=j=1; j<=i; j++) f[i][j]=(f[i-1][j]+(ll)f[i-1][j-1]*sz[i])%P; if (m) ans=(ans+(ll)(f[m][1]-m)*ksm(n-1,m-1))%P; for (j=1,i=2; i<=m; i++) j=(ll)j*(i-1)%P,ans=(ans+(ll)f[m][i]*j%P*ksm(n-1,m-i))%P; printf("%d\n",((ll)n*ksm(n-1,m)-ans+P)%P); return0; }
#include<bits/stdc++.h> #define il inline #define ull unsigned long long std::mt19937 rd(233); constint N=6e5+5,M=20*N;
int n,q,tot,a[N],rt[N],ls[M],rs[M]; ull b[N],t[M]; int dep[N],top[N],fa[N],sz[N],son[N],to[N],nx[N],hd[N],sze;
namespace IO { constint S=1<<20|500; char iuf[S+5],*s,*t,ouf[S+5],*o; int p[40],P; il chargc(){if (t==s){t=(s=iuf)+fread(iuf,1,1<<20,stdin); if (t==s) return EOF;} return *s++;} template<classT=int>il T read() { T x; char c; while ((c=gc())<48||c>57); x=c^48; while ((c=gc())>47&&c<58) x=(x<<3)+(x<<1)+(c^48); return x; } il voidfs(){if (o-ouf) fwrite(ouf,1,o-ouf,stdout); o=ouf;} il voidpc(char c){*o++=c; if (o-ouf==S) fs();} template<classT=int>il voidprint(T x){T y; if (x<0) pc('-'),x=-x; do y=x/10,p[P++]=x-10*y;while (x=y); while (P) pc(p[--P]^48);} structfsr{fsr(){o=ouf;} ~fsr(){fs();}}FF; } using IO::read; using IO::print; using IO::pc;
il voidadd(int u,int v){to[++sze]=v,nx[sze]=hd[u],hd[u]=sze;}
il intLCA(int x,int y) { for ( ; top[x]!=top[y]; x=fa[top[x]]) if (dep[top[x]]<dep[top[y]]) std::swap(x,y); return dep[x]<dep[y]?x:y; }
il voidupt(int &p,int q,int l,int r,int x,ull v) { t[p=++tot]=t[q]^v; if (l==r) return; int mid=l+r>>1; if (x<=mid) rs[p]=rs[q],upt(ls[p],ls[q],l,mid,x,v); else ls[p]=ls[q],upt(rs[p],rs[q],mid+1,r,x,v); }
il intfind(int a,int b,int c,int d,int l,int r,int x,int y) { if (!(t[a]^t[b]^t[c]^t[d])) return-1; if (l==r) return l; int mid=l+r>>1,res=-1; if (x<=mid){if ((res=find(ls[a],ls[b],ls[c],ls[d],l,mid,x,y))!=-1) return res;} if (y>mid){if ((res=find(rs[a],rs[b],rs[c],rs[d],mid+1,r,x,y))!=-1) return res;} return-1; }
il voiddfs1(int u,int p) { fa[u]=p,dep[u]=dep[p]+1,sz[u]=1, upt(rt[u],rt[p],1,n,a[u],b[a[u]]); int i,v; for (i=hd[u]; i; i=nx[i]) if ((v=to[i])!=p) { dfs1(v,u),sz[u]+=sz[v]; if (sz[v]>sz[son[u]]) son[u]=v; } }
il voiddfs2(int u,int t) { top[u]=t; int i,v; if (son[u]) dfs2(son[u],t); for (i=hd[u]; i; i=nx[i]) if (!top[v=to[i]]) dfs2(v,v); }
intmain() { n=read(),q=read(); int i,j,u,v,w; for (i=1; i<=n; i++) a[i]=read(),b[i]=rd(); for (i=1; i<n; i++) u=read(),v=read(),add(u,v),add(v,u); dfs1(1,0),dfs2(1,1); while (q--) u=read(),v=read(),i=read(),j=read(),w=LCA(u,v), print(find(rt[u],rt[v],rt[w],rt[fa[w]],1,n,i,j)),pc('\n'); return0; }
#include<bits/stdc++.h> #define il inline #define ll long long constint N=1e5+5,M=512,P=998244353;
int n,m,sg[N],f[M][M+1],g[M],p[M],to[N+N],nx[N+N],hd[N],sze; bool o[N],O[M];
il intksm(int a,int b){int res=1; for ( ; b; b>>=1,a=(ll)a*a%P) if (b&1) res=(ll)res*a%P; return res;}
il voidadd(int u,int v){to[++sze]=v,nx[sze]=hd[u],hd[u]=sze;}
il voiddfs(int u) { if (o[u]) return; int i; for (i=hd[u]; i; i=nx[i]) dfs(to[i]); for (memset(O,0,M),i=hd[u]; i; i=nx[i]) O[sg[to[i]]]=1; while (O[sg[u]]) sg[u]++; o[u]=1,g[sg[u]]++; }
il voidgauss() { std::fill(f[0],f[0]+M+1,1); int i,j,k,l; for (i=0; i<M; i++) for (j=i+1; j<M; j++) if (f[j][i]) for (l=(ll)f[j][i]*ksm(f[i][i],P-2)%P,k=i; k<=M; k++) f[j][k]=(f[j][k]-(ll)l*f[i][k]%P+P)%P; for (i=M-1; ~i; i--) { for (j=i+1; j<M; j++) f[i][M]=(f[i][M]-(ll)p[j]*f[i][j]%P+P)%P; p[i]=(ll)f[i][M]*ksm(f[i][i],P-2)%P; } }
intmain() { scanf("%d%d",&n,&m); int i,j,k; while (m--) scanf("%d%d",&i,&j),add(i,j); for (i=1; i<=n; i++) dfs(i); for (k=ksm(n+1,P-2),i=1; i<M; i++) for (f[i][i]=1,j=0; j<M; j++) f[i][j]=(f[i][j]-(ll)k*g[i^j]%P+P)%P; gauss(),printf("%d\n",(1-p[0]+P)%P); return0; }
#include<bits/stdc++.h> #define il inline #define ll long long constint N=2005,P=1e9+7;
int n,a[N],I[N],f[N][N],A[N],B[N],C[N][N];
il voidupt(int *o,int x,int v){for ( ; x<n+2; x+=x&-x) o[x]=(o[x]+v)%P;}
il intsum(int *o,int x){int res=0; for ( ; x; x-=x&-x) res=(res+o[x])%P; return res;}
il intquery(int *o,int x,int y){return (sum(o,y)-sum(o,x)+P)%P;}
intmain() { scanf("%d",&n),a[n+1]=n+1; int i,j,k; for (i=1; i<=n; i++) scanf("%d",a+i); for (I[1]=1,i=2; i<=n; i++) I[i]=(ll)(P-P/i)*I[P%i]%P; for (j=1; j<n+2; j++) for (memset(A,0,4*n+8),memset(B,0,4*n+8),i=j-1; ~i; i--) { if (a[i]<a[j]) { f[i][j]=((ll)f[i][j]+query(B,a[i],a[j])+query(C[i],a[i],a[j]))%P; if (k=query(A,a[i],a[j])) f[i][j]=((ll)f[i][j]*I[k]+1)%P; } upt(C[i],a[j],f[i][j]); if (i) upt(A,a[i],1),upt(B,a[i],f[i][j]); } printf("%d\n",f[0][n+1]); return0; }
#include<bits/stdc++.h> #define int long long constint N=55,K=18; int n,s,A,B,ans,m,a[N],b[N],f[N][N],C[N][N]; bool o[N],O[K]; signedmain() { scanf("%lld%lld%ld%lld",&n,&s,&A,&B); int i,j,k,S,T,p,q; for (i=1; i<=n; i++) scanf("%lld",a+i); for (i=0; i<=n; i++) for (C[i][0]=j=1; j<=i; j++) C[i][j]=C[i-1][j]+C[i-1][j-1]; for (k=0; k<K; k++) { if (A>>k&1){for (i=1; i<=n; i++) if (a[i]>>k&1^1) o[i]=1; O[k]=1;} if (B>>k&1^1){for (i=1; i<=n; i++) if (a[i]>>k&1) o[i]=1; O[k]=1;} } for (i=1; i<=n; i++) if (!o[i]) b[++m]=a[i]; if (!m) returnputs("0"),0; for (i=1; i<=m; i++) for (j=i+1; j<=m; f[i][j]=T,j++) for (T=k=0; k<K; k++) if ((b[i]>>k&1)==(b[j]>>k&1)) T|=1<<k; for (i=1; i<=m; i++) for (S=0; S<1<<K; S++) { for (k=q=p=0; !q&&k<K; k++) q=(S>>k&1)&&O[k]; if (q) continue; for (j=i+1; j<=m; j++) if ((f[i][j]&S)==S) p++; q=__builtin_popcount(S)&1?-1:1; for (j=0; j<s; j++) ans+=q*C[p][j]; } printf("%lld\n",ans); return0; }
#include<bits/stdc++.h> #define il inline constint N=2e5+5,M=1<<16;
int n,m,s,a[N],b[16],f[M];
il intdfs(int S) { if (~f[S]) return f[S]; if (S<1) return f[S]=0; int i,j,k; for (f[S]=1e9,i=1; i<16; i++) if (S>>i&1) for (j=1; j<16; j++) if (j!=i&&S>>j&1) k=i^j,f[S]=std::min(f[S],dfs(S^1<<i^1<<j^1<<k)+1+(S>>k&1)); return f[S]; }
intmain() { scanf("%d",&n),memset(f,-1,sizeof f); int i,u,v,w; for (i=1; i<n; i++) scanf("%d%d%d",&u,&v,&w),a[u+1]^=w,a[v+1]^=w; for (i=1; i<=n; i++) b[a[i]]++; for (i=1; i<16; i++) s+=b[i]/2,b[i]&1&&(m+=1<<i); printf("%d\n",s+dfs(m)); return0; }
#include<bits/stdc++.h> #define il inline constint N=6005;
int n,m,s,tp,p[N],st[N]; bool o[N],c[N]; int to[N],nx[N],hd[N],it[N],du[N],d[N],sze=1;
il voidadd(int u,int v){to[++sze]=v,nx[sze]=hd[u],hd[u]=sze,du[v]++;}
il voiddfs(int u){for (int i=it[u]; i; i=it[u]) if (it[u]=nx[i],!o[i>>1]&&!c[i>>1]) o[i>>1]=1,dfs(to[i]); st[++tp]=u;}
il boolck(int u) { int i,j,x=0; for (i=1; i<=n; i++) x+=d[i]&1; if (x>2) return0; memset(o,0,m+1),tp=0,memcpy(it,hd,4*n+4),tp=0; if (!x) { dfs(u); for (i=1; i<=m; i++) if (!o[i]&&!c[i]) return0; return1; } if (d[u]&1^1) return0; for (i=1; i<=n; i++) if (d[i]%2&&i!=u) { dfs(i); for (j=1; j<=m; j++) if (!o[j]&&!c[j]) return0; return1; } return0; }
il voidprint(int x,int y) { int i; for (printf("%d\n",tp+(s?(y?s+s-1:s+s+1):0)),i=tp; i; i--) printf("%d ",st[i]); if (s){for (printf("-1 "),i=s; i; i--) if (p[i]!=y) printf("%d %d ",p[i],x);} exit(0); }
intmain() { scanf("%d%d",&n,&m); int i,u,v; for (i=1; i<=m; i++) scanf("%d%d",&u,&v),add(u,v),add(v,u); for (u=1; u<=n; u++) { s=0,memcpy(d,du,4*n+4),memset(c,0,m+1); if (ck(u)) print(u,0); for (i=hd[u]; i; i=nx[i]) if (d[v=to[i]]&1) c[i>>1]=1,d[u]--,d[v]--,p[++s]=v; if (ck(u)) print(u,0); for (i=hd[u]; i; i=nx[i]) if (c[i>>1]) { c[i>>1]=0,d[u]++,d[v=to[i]]++; if (ck(u)) print(u,v); c[i>>1]=1,d[u]--,d[v]--; } } puts("0"); return0; }
#include<bits/stdc++.h> #define il inline #define ll long long constint N=2e5+5,B=316;
int n,q,b[N],c[N]; ll a[N],lz[N];
il intjump(int x){return x?std::max(a[x]-lz[x/B],0ll):-1;}
il voidrebuild(int x,int v) { if (c[x]+v>B) return ; c[x]+=v; int i; for (i=x*B; i<x*B+B; i++) b[i]=jump(i)<x*B?i:b[jump(i)]; }
il voidadd(int x,int y,int v) { int i; for (i=x/B*B; i<x; i++) a[i]+=v; for (i=y+1; i<y/B*B+B; i++) a[i]+=v; for (i=x/B; i<=y/B; i++) lz[i]+=v,rebuild(i,i!=x/B&&i!=y/B); }
il intLCA(int x,int y) { for ( ; b[x]!=b[y]; x=jump(b[x])) if (jump(b[x])<jump(b[y])) std::swap(x,y); for ( ; x!=y; x=jump(x)) if (x<y) std::swap(x,y); return x; }
intmain() { scanf("%d%d",&n,&q),a[0]=-1; int i,x,y,k; for (i=1; i<n; i++) scanf("%d",a+i),a[i]--; for (add(0,n-1,0); q; q--) if (scanf("%d%d%d",&i,&x,&y),i>1) printf("%d\n",LCA(x-1,y-1)+1); elsescanf("%d",&k),add(x-1,y-1,k); return0; }