A
ddDP
ddSTD
f
i
,
j
,
k
f_{i,j,k}
fi,j,k表示长度为
j
j
j 的序列放了
k
k
k 种长度不超过
k
k
k 的连续颜色段
然后我开的long long而不是long double成功败北
告辞
代码:
#include<bits/stdc++.h>
#define ri register int
#define fi first
#define se second
using namespace std;
const int rlen=1<<18|1;
char buf[rlen],*ib=buf,*ob=buf;
#define gc() (((ib==ob)&&(ob=(ib=buf)+fread(buf,1,rlen,stdin)),ib==ob)?-1:*ib++)
typedef pair<int,int> pii;
inline int read(){
int ans=0;
char ch=gc();
while(!isdigit(ch))ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return ans;
}
const int N=255;
int n,m;
long double f[N][N][N];
inline void init(int lm=250){
memset(f,0,sizeof(f));
for(ri len=1;len<=lm;++len){
f[len][0][0]=1;
for(ri i=1;i<=lm;++i){
for(ri j=1;j<=i;++j){
f[len][i][j]=f[len][i-1][j]+f[len][i-1][j-1];
(i-len-1>=0)&&(f[len][i][j]-=f[len][i-len-1][j-1]);
}
}
}
}
inline long double calc(int len){
long double A=0,B=0,C=n,D=1;
for(ri i=1;i<=m;++i){
A+=C*f[len][m][i];
B+=C*D;
C/=i+1,C*=n-i;
D/=i,D*=m-i;
}
return A/B;
}
int main(){
#ifdef ldxcaicai
freopen("lx.in","r",stdin);
#endif
init();
for(ri tt=read();tt;--tt){
m=read(),n=read();
long double ans=0,cur,lst=0;
for(ri i=1;i<=m;++i,lst=cur)ans+=i*((cur=calc(i))-lst);
printf("%.10lf\n",(double)ans);
}
return 0;
}
B
考虑对每条狗搞一个很妙的构造:
然后这样你每用一个人匹配掉一个点剩下的匹配数都是对的,就成了一般图最大匹配,带花树即可。
代码:
#include<bits/stdc++.h>
#define ri register int
#define pb push_back
using namespace std;
const int N=805,M=1e6+5,Lim=800;
int n,m;
const int rlen=1<<18|1;
inline char gc(){
static char buf[rlen],*ib,*ob;
(ib==ob)&&(ob=(ib=buf)+fread(buf,1,rlen,stdin));
return ib==ob?-1:*ib++;
}
inline int read(){
int ans=0;
char ch=gc();
while(!isdigit(ch))ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return ans;
}
namespace Blossom{
int q[N],hd,tl,len,tot=0,tim,vs[N],tg[N],mch[N],ft[N],pre[N];
vector<int>e[N];
inline void clear(){
for(ri i=1;i<=tot;++i)e[i].clear(),mch[i]=0;
tot=0;
}
inline void add(int u,int v){e[u].pb(v),e[v].pb(u),tot=max(tot,u),tot=max(tot,v);}
inline int find(int x){return x^ft[x]?ft[x]=find(ft[x]):x;}
inline int lca(int x,int y){
++tim;
while(1){
if(x){
if(vs[x=find(x)]==tim)return x;
vs[x]=tim,x=pre[mch[x]];
}
swap(x,y);
}
}
inline void shrink(int x,int y,int t){
while(find(x)^t){
pre[x]=y,y=mch[x];
if(tg[y]==2)tg[q[++len,tl=tl==tot?1:tl+1]=y]=1;
if(find(x)==x)ft[x]=t;
if(find(y)==y)ft[y]=t;
x=pre[y];
}
}
inline bool bfs(int x){
tim=0;
for(ri i=1;i<=tot;++i)tg[i]=q[i]=vs[i]=pre[i]=0,ft[i]=i;
tg[q[hd=tl=len=1]=x]=1;
while(len){
int x=q[hd];
--len,hd=hd==tot?1:hd+1;
for(ri t,i=0,v;i<e[x].size();++i){
if(find(x)==find(v=e[x][i])||tg[v]==2)continue;
if(!tg[v]){
pre[v]=x,tg[v]=2;
if(!mch[v]){
for(ri a=v,b,nxt;a;a=nxt){
b=pre[a];
nxt=mch[b];
mch[a]=b,mch[b]=a;
}
return 1;
}
tg[(++len,q[tl=tl==tot?1:tl+1]=mch[v])]=1;
}
else t=lca(x,v),shrink(x,v,t),shrink(v,x,t);
}
}
return 0;
}
inline int solve(){
int res=0;
for(ri i=1;i<=tot;++i)if(!mch[i]&&bfs(i))++res;
return res-n;
}
}
inline int idx(int a,int b){return n+(a-1)*7+b+1;}
int main(){
#ifdef ldxcaicai
freopen("lx.in","r",stdin);
#endif
for(ri tt=read();tt;--tt){
n=read(),m=read();
Blossom::clear();
for(ri i=1;i<=m;++i){
for(ri j=0;j<5;++j){
Blossom::add(idx(i,j),idx(i,(j+1)%5));
Blossom::add(idx(i,j),idx(i,5));
Blossom::add(idx(i,j),idx(i,6));
}
}
for(ri i=1;i<=n;++i){
for(ri ttt=read(),vl;ttt;--ttt){
vl=read();
for(ri j=0;j<5;++j)Blossom::add(idx(vl,j),i);
}
}
cout<<Blossom::solve()<<'\n';
}
return 0;
}
C
ddDFS序维护fib数
ddSTD居然写两个log
注:我代码里的HLD是拿来求lca的(捂脸
代码:
#include<bits/stdc++.h>
#define ri register int
#define fi first
#define se second
#define pb push_back
using namespace std;
const int rlen=1<<18|1;
char buf[rlen],*ib=buf,*ob=buf;
#define gc() (((ib==ob)&&(ob=(ib=buf)+fread(buf,1,rlen,stdin)),ib==ob)?-1:*ib++)
typedef pair<int,int> pii;
inline int read(){
int ans=0;
char ch=gc();
while(!isdigit(ch))ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return ans;
}
typedef long long ll;
inline ll readl(){
ll ans=0;
char ch=gc();
while(!isdigit(ch))ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return ans;
}
inline char gcr(){
char ch=gc();
while(!isalpha(ch))ch=gc();
return ch;
}
const int mod=1e9+7;
inline int add(int a,int b){return (a+=b)<mod?a:a-mod;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return (ll)a*b%mod;}
inline void Add(int&a,int b){(a+=b)<mod?a:(a-=mod);}
inline void Dec(int&a,int b){(a-=b)<0?(a+=mod):a;}
inline void Mul(int&a,int b){a=(ll)a*b%mod;}
inline int ksm(int a,int p){int ret=1;for(;p;p>>=1,Mul(a,a))if(p&1)Mul(ret,a);return ret;}
struct F{
int a,b;
F(int a=0,int b=0):a(a),b(b){}
inline F conj()const{return F(a,mod-b);}
friend inline F operator+(F a,F b){return F(add(a.a,b.a),add(a.b,b.b));}
friend inline F operator-(F a,F b){return F(dec(a.a,b.a),dec(a.b,b.b));}
friend inline F operator*(F a,F b){return F(add(mul(a.a,b.a),mul(5,mul(a.b,b.b))),add(mul(a.a,b.b),mul(a.b,b.a)));}
friend inline F operator*(F a,int b){return F(mul(a.a,b),mul(a.b,b));}
friend inline void operator+=(F&a,F b){a=a+b;}
friend inline void operator-=(F&a,F b){a=a-b;}
friend inline void operator*=(F&a,F b){a=a*b;}
friend inline void operator*=(F&a,int b){a=a*b;}
friend inline F operator^(F a,ll p){F ret=F(1,0);for(;p;p>>=1,a*=a)if(p&1)ret*=a;return ret;}
}t1,t2,t3;
const int N=1e5+5;
int n,m;
int fa[N],hson[N],dep[N],top[N],in[N],out[N],siz[N],tot=0;
vector<int>e[N];
void dfs1(int p){
siz[p]=1;
for(ri i=0,v;i<e[p].size();++i){
fa[v=e[p][i]]=p,dep[v]=dep[p]+1,dfs1(v),siz[p]+=siz[v];
if(siz[v]>siz[hson[p]])hson[p]=v;
}
}
void dfs2(int p,int tp){
top[p]=tp;
in[p]=++tot;
if(!hson[p]){out[p]=tot;return;}
dfs2(hson[p],tp);
for(ri i=0,v;i<e[p].size();++i)if((v=e[p][i])^hson[p])dfs2(v,v);
out[p]=tot;
}
inline int lca(int x,int y){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
struct BIT{
#define lowbit(x) ((x)&(-(x)))
F bit[N];
inline void update(int p,F v){for(;p<=n;p+=lowbit(p))bit[p]+=v;}
inline F query(int p){F ret;for(;p;p^=lowbit(p))ret+=bit[p];return ret;}
}T1,T2;
int main(){
int iv2=mod+1>>1;
t1=F(iv2,iv2);
t2=F(mod-iv2,iv2);
t3=F(mod-iv2,mod-iv2);
n=read(),m=read();
for(ri i=2;i<=n;++i)e[read()].pb(i);
dfs1(1),dfs2(1,1);
ll k;
F vl,tp;
for(ri x,y,t;m;--m){
char ch=gcr();
if(ch=='U'){
x=read(),k=readl();
vl=t1^k;
tp=t2^dep[x];
T1.update(in[x],vl*t3);
T1.update(out[x]+1,vl*t3*(mod-1));
T2.update(in[x],(vl*t3*tp)*(mod-1));
T2.update(out[x]+1,vl*t3*tp);
}
else{
x=read(),y=read(),t=lca(x,y);
tp=T1.query(in[x])+T1.query(in[y])-T1.query(in[t])-T1.query(in[fa[t]]);
tp+=T2.query(in[x])*(t1^(dep[x]+1))+T2.query(in[y])*(t1^(dep[y]+1))-T2.query(in[t])*(t1^(dep[t]+1))-T2.query(in[fa[t]])*(t1^(dep[fa[t]]+1));
cout<<mul(tp.b,2)<<'\n';
}
}
return 0;
}